Use smart pointers for class owned pointers in xfa/fxfa

Use smart pointers instead of raw pointer to make memory management
easier for classes mainly under xfa/fxfa.

Also change the return type of IFGAS_FontMgr::Create() to smart
pointer type.

BUG=pdfium:518

Review-Url: https://codereview.chromium.org/2227883002
diff --git a/fpdfsdk/fsdk_baseform.cpp b/fpdfsdk/fsdk_baseform.cpp
index 1bd60b1..c5c6187 100644
--- a/fpdfsdk/fsdk_baseform.cpp
+++ b/fpdfsdk/fsdk_baseform.cpp
@@ -115,7 +115,7 @@
         }
 
         if (!sName.IsEmpty())
-          m_hMixXFAWidget = pDocView->GetWidgetByName(sName);
+          m_hMixXFAWidget = pDocView->GetWidgetByName(sName, nullptr);
       }
     }
     return m_hMixXFAWidget;
@@ -131,7 +131,7 @@
     if (CXFA_FFDocView* pDocView = pDoc->GetXFADocView()) {
       CFX_WideString sName = GetName();
       if (!sName.IsEmpty())
-        return pDocView->GetWidgetByName(sName);
+        return pDocView->GetWidgetByName(sName, nullptr);
     }
   }
 
diff --git a/xfa/fgas/font/fgas_font.h b/xfa/fgas/font/fgas_font.h
index 7fcfbd7..f91c015 100644
--- a/xfa/fgas/font/fgas_font.h
+++ b/xfa/fgas/font/fgas_font.h
@@ -80,11 +80,13 @@
     FX_LPFONTMATCHPARAMS pParams,
     const CFX_FontDescriptors& fonts);
 FX_LPMatchFont FX_GetDefFontMatchor();
+
 class IFGAS_FontMgr {
  public:
-  static IFGAS_FontMgr* Create(FX_LPEnumAllFonts pEnumerator);
   virtual ~IFGAS_FontMgr() {}
-  virtual void Release() = 0;
+
+  static std::unique_ptr<IFGAS_FontMgr> Create(FX_LPEnumAllFonts pEnumerator);
+
   virtual CFGAS_GEFont* GetDefFontByCodePage(
       uint16_t wCodePage,
       uint32_t dwFontStyles,
@@ -121,46 +123,40 @@
 
 class IFGAS_FontMgr {
  public:
-  static IFGAS_FontMgr* Create(CFX_FontSourceEnum_File* pFontEnum);
   virtual ~IFGAS_FontMgr() {}
-  virtual void Release() = 0;
-  virtual CFGAS_GEFont* GetDefFontByCodePage(
-      uint16_t wCodePage,
-      uint32_t dwFontStyles,
-      const FX_WCHAR* pszFontFamily = nullptr) = 0;
-  virtual CFGAS_GEFont* GetDefFontByCharset(
-      uint8_t nCharset,
-      uint32_t dwFontStyles,
-      const FX_WCHAR* pszFontFamily = nullptr) = 0;
-  virtual CFGAS_GEFont* GetDefFontByUnicode(
-      FX_WCHAR wUnicode,
-      uint32_t dwFontStyles,
-      const FX_WCHAR* pszFontFamily = nullptr) = 0;
-  virtual CFGAS_GEFont* GetDefFontByLanguage(
-      uint16_t wLanguage,
-      uint32_t dwFontStyles,
-      const FX_WCHAR* pszFontFamily = nullptr) = 0;
-  virtual CFGAS_GEFont* GetFontByCodePage(
-      uint16_t wCodePage,
-      uint32_t dwFontStyles,
-      const FX_WCHAR* pszFontFamily = nullptr) = 0;
+
+  static std::unique_ptr<IFGAS_FontMgr> Create(
+      CFX_FontSourceEnum_File* pFontEnum);
+
+  virtual CFGAS_GEFont* GetDefFontByCodePage(uint16_t wCodePage,
+                                             uint32_t dwFontStyles,
+                                             const FX_WCHAR* pszFontFamily) = 0;
+  virtual CFGAS_GEFont* GetDefFontByCharset(uint8_t nCharset,
+                                            uint32_t dwFontStyles,
+                                            const FX_WCHAR* pszFontFamily) = 0;
+  virtual CFGAS_GEFont* GetDefFontByUnicode(FX_WCHAR wUnicode,
+                                            uint32_t dwFontStyles,
+                                            const FX_WCHAR* pszFontFamily) = 0;
+  virtual CFGAS_GEFont* GetDefFontByLanguage(uint16_t wLanguage,
+                                             uint32_t dwFontStyles,
+                                             const FX_WCHAR* pszFontFamily) = 0;
+  virtual CFGAS_GEFont* GetFontByCodePage(uint16_t wCodePage,
+                                          uint32_t dwFontStyles,
+                                          const FX_WCHAR* pszFontFamily) = 0;
   inline CFGAS_GEFont* LoadFont(const FX_WCHAR* pszFontFamily,
                                 uint32_t dwFontStyles,
                                 uint16_t wCodePage) {
     return GetFontByCodePage(wCodePage, dwFontStyles, pszFontFamily);
   }
-  virtual CFGAS_GEFont* GetFontByCharset(
-      uint8_t nCharset,
-      uint32_t dwFontStyles,
-      const FX_WCHAR* pszFontFamily = nullptr) = 0;
-  virtual CFGAS_GEFont* GetFontByUnicode(
-      FX_WCHAR wUnicode,
-      uint32_t dwFontStyles,
-      const FX_WCHAR* pszFontFamily = nullptr) = 0;
-  virtual CFGAS_GEFont* GetFontByLanguage(
-      uint16_t wLanguage,
-      uint32_t dwFontStyles,
-      const FX_WCHAR* pszFontFamily = nullptr) = 0;
+  virtual CFGAS_GEFont* GetFontByCharset(uint8_t nCharset,
+                                         uint32_t dwFontStyles,
+                                         const FX_WCHAR* pszFontFamily) = 0;
+  virtual CFGAS_GEFont* GetFontByUnicode(FX_WCHAR wUnicode,
+                                         uint32_t dwFontStyles,
+                                         const FX_WCHAR* pszFontFamily) = 0;
+  virtual CFGAS_GEFont* GetFontByLanguage(uint16_t wLanguage,
+                                          uint32_t dwFontStyles,
+                                          const FX_WCHAR* pszFontFamily) = 0;
   virtual void ClearFontCache() = 0;
   virtual void RemoveFont(CFGAS_GEFont* pFont) = 0;
 };
diff --git a/xfa/fgas/font/fgas_stdfontmgr.cpp b/xfa/fgas/font/fgas_stdfontmgr.cpp
index 20a5204..ab7852e 100644
--- a/xfa/fgas/font/fgas_stdfontmgr.cpp
+++ b/xfa/fgas/font/fgas_stdfontmgr.cpp
@@ -17,8 +17,9 @@
 
 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
 
-IFGAS_FontMgr* IFGAS_FontMgr::Create(FX_LPEnumAllFonts pEnumerator) {
-  return new CFGAS_StdFontMgrImp(pEnumerator);
+std::unique_ptr<IFGAS_FontMgr> IFGAS_FontMgr::Create(
+    FX_LPEnumAllFonts pEnumerator) {
+  return std::unique_ptr<IFGAS_FontMgr>(new CFGAS_StdFontMgrImp(pEnumerator));
 }
 
 CFGAS_StdFontMgrImp::CFGAS_StdFontMgrImp(FX_LPEnumAllFonts pEnumerator)
@@ -47,10 +48,6 @@
     m_Fonts[i]->Release();
 }
 
-void CFGAS_StdFontMgrImp::Release() {
-  delete this;
-}
-
 CFGAS_GEFont* CFGAS_StdFontMgrImp::GetDefFontByCodePage(
     uint16_t wCodePage,
     uint32_t dwFontStyles,
@@ -564,20 +561,47 @@
   return pAccess;
 }
 
-IFGAS_FontMgr* IFGAS_FontMgr::Create(CFX_FontSourceEnum_File* pFontEnum) {
+std::unique_ptr<IFGAS_FontMgr> IFGAS_FontMgr::Create(
+    CFX_FontSourceEnum_File* pFontEnum) {
   if (!pFontEnum)
     return nullptr;
 
   std::unique_ptr<CFGAS_FontMgrImp> pFontMgr(new CFGAS_FontMgrImp(pFontEnum));
   if (!pFontMgr->EnumFonts())
     return nullptr;
-  return pFontMgr.release();
+  return std::move(pFontMgr);
 }
 
 CFGAS_FontMgrImp::CFGAS_FontMgrImp(CFX_FontSourceEnum_File* pFontEnum)
     : m_pFontSource(pFontEnum) {}
 
-CFGAS_FontMgrImp::~CFGAS_FontMgrImp() {}
+CFGAS_FontMgrImp::~CFGAS_FontMgrImp() {
+  for (int32_t i = 0; i < m_InstalledFonts.GetSize(); i++) {
+    delete m_InstalledFonts[i];
+  }
+  FX_POSITION pos = m_Hash2CandidateList.GetStartPosition();
+  while (pos) {
+    uint32_t dwHash;
+    CFX_FontDescriptorInfos* pDescs;
+    m_Hash2CandidateList.GetNextAssoc(pos, dwHash, pDescs);
+    delete pDescs;
+  }
+  pos = m_Hash2Fonts.GetStartPosition();
+  while (pos) {
+    uint32_t dwHash;
+    CFX_ArrayTemplate<CFGAS_GEFont*>* pFonts;
+    m_Hash2Fonts.GetNextAssoc(pos, dwHash, pFonts);
+    delete pFonts;
+  }
+  m_Hash2Fonts.RemoveAll();
+  pos = m_IFXFont2FileRead.GetStartPosition();
+  while (pos) {
+    CFGAS_GEFont* pFont;
+    IFX_FileRead* pFileRead;
+    m_IFXFont2FileRead.GetNextAssoc(pos, pFont, pFileRead);
+    pFileRead->Release();
+  }
+}
 
 FX_BOOL CFGAS_FontMgrImp::EnumFontsFromFontMapper() {
   CFX_FontMapper* pFontMapper =
@@ -634,35 +658,6 @@
   return EnumFontsFromFiles();
 }
 
-void CFGAS_FontMgrImp::Release() {
-  for (int32_t i = 0; i < m_InstalledFonts.GetSize(); i++) {
-    delete m_InstalledFonts[i];
-  }
-  FX_POSITION pos = m_Hash2CandidateList.GetStartPosition();
-  while (pos) {
-    uint32_t dwHash;
-    CFX_FontDescriptorInfos* pDescs;
-    m_Hash2CandidateList.GetNextAssoc(pos, dwHash, pDescs);
-    delete pDescs;
-  }
-  pos = m_Hash2Fonts.GetStartPosition();
-  while (pos) {
-    uint32_t dwHash;
-    CFX_ArrayTemplate<CFGAS_GEFont*>* pFonts;
-    m_Hash2Fonts.GetNextAssoc(pos, dwHash, pFonts);
-    delete pFonts;
-  }
-  m_Hash2Fonts.RemoveAll();
-  pos = m_IFXFont2FileRead.GetStartPosition();
-  while (pos) {
-    CFGAS_GEFont* pFont;
-    IFX_FileRead* pFileRead;
-    m_IFXFont2FileRead.GetNextAssoc(pos, pFont, pFileRead);
-    pFileRead->Release();
-  }
-  delete this;
-}
-
 CFGAS_GEFont* CFGAS_FontMgrImp::GetDefFontByCodePage(
     uint16_t wCodePage,
     uint32_t dwFontStyles,
diff --git a/xfa/fgas/font/fgas_stdfontmgr.h b/xfa/fgas/font/fgas_stdfontmgr.h
index 4de6971..65f260c 100644
--- a/xfa/fgas/font/fgas_stdfontmgr.h
+++ b/xfa/fgas/font/fgas_stdfontmgr.h
@@ -28,7 +28,6 @@
   ~CFGAS_StdFontMgrImp() override;
 
   // IFGAS_FontMgr:
-  void Release() override;
   CFGAS_GEFont* GetDefFontByCodePage(
       uint16_t wCodePage,
       uint32_t dwFontStyles,
@@ -132,7 +131,6 @@
   CFX_FontSourceEnum_File();
   ~CFX_FontSourceEnum_File();
 
-  void Release() { delete this; }
   FX_POSITION GetStartPosition();
   IFX_FileAccess* GetNext(FX_POSITION& pos);
 
@@ -150,39 +148,30 @@
   ~CFGAS_FontMgrImp() override;
 
   // IFGAS_FontMgr:
-  void Release() override;
-  CFGAS_GEFont* GetDefFontByCodePage(
-      uint16_t wCodePage,
-      uint32_t dwFontStyles,
-      const FX_WCHAR* pszFontFamily = nullptr) override;
-  CFGAS_GEFont* GetDefFontByCharset(
-      uint8_t nCharset,
-      uint32_t dwFontStyles,
-      const FX_WCHAR* pszFontFamily = nullptr) override;
-  CFGAS_GEFont* GetDefFontByUnicode(
-      FX_WCHAR wUnicode,
-      uint32_t dwFontStyles,
-      const FX_WCHAR* pszFontFamily = nullptr) override;
-  CFGAS_GEFont* GetDefFontByLanguage(
-      uint16_t wLanguage,
-      uint32_t dwFontStyles,
-      const FX_WCHAR* pszFontFamily = nullptr) override;
-  CFGAS_GEFont* GetFontByCodePage(
-      uint16_t wCodePage,
-      uint32_t dwFontStyles,
-      const FX_WCHAR* pszFontFamily = nullptr) override;
-  CFGAS_GEFont* GetFontByCharset(
-      uint8_t nCharset,
-      uint32_t dwFontStyles,
-      const FX_WCHAR* pszFontFamily = nullptr) override;
-  CFGAS_GEFont* GetFontByUnicode(
-      FX_WCHAR wUnicode,
-      uint32_t dwFontStyles,
-      const FX_WCHAR* pszFontFamily = nullptr) override;
-  CFGAS_GEFont* GetFontByLanguage(
-      uint16_t wLanguage,
-      uint32_t dwFontStyles,
-      const FX_WCHAR* pszFontFamily = nullptr) override;
+  CFGAS_GEFont* GetDefFontByCodePage(uint16_t wCodePage,
+                                     uint32_t dwFontStyles,
+                                     const FX_WCHAR* pszFontFamily) override;
+  CFGAS_GEFont* GetDefFontByCharset(uint8_t nCharset,
+                                    uint32_t dwFontStyles,
+                                    const FX_WCHAR* pszFontFamily) override;
+  CFGAS_GEFont* GetDefFontByUnicode(FX_WCHAR wUnicode,
+                                    uint32_t dwFontStyles,
+                                    const FX_WCHAR* pszFontFamily) override;
+  CFGAS_GEFont* GetDefFontByLanguage(uint16_t wLanguage,
+                                     uint32_t dwFontStyles,
+                                     const FX_WCHAR* pszFontFamily) override;
+  CFGAS_GEFont* GetFontByCodePage(uint16_t wCodePage,
+                                  uint32_t dwFontStyles,
+                                  const FX_WCHAR* pszFontFamily) override;
+  CFGAS_GEFont* GetFontByCharset(uint8_t nCharset,
+                                 uint32_t dwFontStyles,
+                                 const FX_WCHAR* pszFontFamily) override;
+  CFGAS_GEFont* GetFontByUnicode(FX_WCHAR wUnicode,
+                                 uint32_t dwFontStyles,
+                                 const FX_WCHAR* pszFontFamily) override;
+  CFGAS_GEFont* GetFontByLanguage(uint16_t wLanguage,
+                                  uint32_t dwFontStyles,
+                                  const FX_WCHAR* pszFontFamily) override;
   void ClearFontCache() override;
   void RemoveFont(CFGAS_GEFont* pFont) override;
 
diff --git a/xfa/fwl/theme/cfwl_widgettp.cpp b/xfa/fwl/theme/cfwl_widgettp.cpp
index 9960257..b5658cf 100644
--- a/xfa/fwl/theme/cfwl_widgettp.cpp
+++ b/xfa/fwl/theme/cfwl_widgettp.cpp
@@ -697,31 +697,9 @@
   SetColorData(0);
 }
 
-CFWL_FontData::CFWL_FontData()
-    : m_dwStyles(0),
-      m_dwCodePage(0),
-      m_pFont(0),
-      m_pFontMgr(nullptr)
-#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
-      ,
-      m_pFontSource(nullptr)
-#endif
-{
-}
+CFWL_FontData::CFWL_FontData() : m_dwStyles(0), m_dwCodePage(0) {}
 
-CFWL_FontData::~CFWL_FontData() {
-  if (m_pFont) {
-    m_pFont->Release();
-  }
-  if (m_pFontMgr) {
-    m_pFontMgr->Release();
-  }
-#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
-  if (m_pFontSource) {
-    m_pFontSource->Release();
-  }
-#endif
-}
+CFWL_FontData::~CFWL_FontData() {}
 
 FX_BOOL CFWL_FontData::Equal(const CFX_WideStringC& wsFontFamily,
                              uint32_t dwFontStyles,
@@ -740,12 +718,12 @@
 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
     m_pFontMgr = IFGAS_FontMgr::Create(FX_GetDefFontEnumerator());
 #else
-    m_pFontSource = new CFX_FontSourceEnum_File;
-    m_pFontMgr = IFGAS_FontMgr::Create(m_pFontSource);
+    m_pFontSource.reset(new CFX_FontSourceEnum_File);
+    m_pFontMgr = IFGAS_FontMgr::Create(m_pFontSource.get());
 #endif
   }
-  m_pFont = CFGAS_GEFont::LoadFont(wsFontFamily.c_str(), dwFontStyles,
-                                   dwCodePage, m_pFontMgr);
+  m_pFont.reset(CFGAS_GEFont::LoadFont(wsFontFamily.c_str(), dwFontStyles,
+                                       dwCodePage, m_pFontMgr.get()));
   return !!m_pFont;
 }
 
diff --git a/xfa/fwl/theme/cfwl_widgettp.h b/xfa/fwl/theme/cfwl_widgettp.h
index 8a1e0c1..e6d0932 100644
--- a/xfa/fwl/theme/cfwl_widgettp.h
+++ b/xfa/fwl/theme/cfwl_widgettp.h
@@ -236,23 +236,24 @@
  public:
   CFWL_FontData();
   virtual ~CFWL_FontData();
+
   FX_BOOL Equal(const CFX_WideStringC& wsFontFamily,
                 uint32_t dwFontStyles,
                 uint16_t wCodePage);
   FX_BOOL LoadFont(const CFX_WideStringC& wsFontFamily,
                    uint32_t dwFontStyles,
                    uint16_t wCodePage);
-  CFGAS_GEFont* GetFont() const { return m_pFont; }
+  CFGAS_GEFont* GetFont() const { return m_pFont.get(); }
 
  protected:
   CFX_WideString m_wsFamily;
   uint32_t m_dwStyles;
   uint32_t m_dwCodePage;
-  CFGAS_GEFont* m_pFont;
-  IFGAS_FontMgr* m_pFontMgr;
 #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
-  CFX_FontSourceEnum_File* m_pFontSource;
+  std::unique_ptr<CFX_FontSourceEnum_File> m_pFontSource;
 #endif
+  std::unique_ptr<IFGAS_FontMgr> m_pFontMgr;
+  std::unique_ptr<CFGAS_GEFont> m_pFont;
 };
 
 class CFWL_FontManager {
diff --git a/xfa/fxfa/app/xfa_ffapp.cpp b/xfa/fxfa/app/xfa_ffapp.cpp
index 22989b8..18e2d76 100644
--- a/xfa/fxfa/app/xfa_ffapp.cpp
+++ b/xfa/fxfa/app/xfa_ffapp.cpp
@@ -37,6 +37,7 @@
   }
   return dwSize;
 }
+
 FX_BOOL CXFA_FileRead::ReadBlock(void* buffer,
                                  FX_FILESIZE offset,
                                  size_t size) {
@@ -72,47 +73,29 @@
 }
 
 CXFA_FFApp::CXFA_FFApp(IXFA_AppProvider* pProvider)
-    : m_pDocHandler(nullptr),
-      m_pFWLTheme(nullptr),
-      m_pProvider(pProvider),
-      m_pFontMgr(nullptr),
-#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
-      m_pFontSource(nullptr),
-#endif
-      m_pAdapterWidgetMgr(nullptr),
+    : m_pProvider(pProvider),
       m_pWidgetMgrDelegate(nullptr),
-      m_pFDEFontMgr(nullptr) {
-  m_pFWLApp = IFWL_App::Create(this);
-  FWL_SetApp(m_pFWLApp);
+      m_pFWLApp(IFWL_App::Create(this)) {
+  FWL_SetApp(m_pFWLApp.get());
   m_pFWLApp->Initialize();
   CXFA_TimeZoneProvider::Create();
 }
 
 CXFA_FFApp::~CXFA_FFApp() {
-  delete m_pDocHandler;
   if (m_pFWLApp) {
     m_pFWLApp->Finalize();
     m_pFWLApp->Release();
-    delete m_pFWLApp;
   }
-  delete m_pFWLTheme;
-  delete m_pAdapterWidgetMgr;
 
   CXFA_TimeZoneProvider::Destroy();
-  delete m_pFontMgr;
-#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
-  if (m_pFontSource)
-    m_pFontSource->Release();
-#endif
-  if (m_pFDEFontMgr)
-    m_pFDEFontMgr->Release();
 }
 
 CXFA_FFDocHandler* CXFA_FFApp::GetDocHandler() {
   if (!m_pDocHandler)
-    m_pDocHandler = new CXFA_FFDocHandler;
-  return m_pDocHandler;
+    m_pDocHandler.reset(new CXFA_FFDocHandler);
+  return m_pDocHandler.get();
 }
+
 CXFA_FFDoc* CXFA_FFApp::CreateDoc(IXFA_DocProvider* pProvider,
                                   IFX_FileRead* pStream,
                                   FX_BOOL bTakeOverFile) {
@@ -133,12 +116,12 @@
 
 void CXFA_FFApp::SetDefaultFontMgr(std::unique_ptr<CXFA_DefFontMgr> pFontMgr) {
   if (!m_pFontMgr)
-    m_pFontMgr = new CXFA_FontMgr();
+    m_pFontMgr.reset(new CXFA_FontMgr());
   m_pFontMgr->SetDefFontMgr(std::move(pFontMgr));
 }
 
-CXFA_FontMgr* CXFA_FFApp::GetXFAFontMgr() {
-  return m_pFontMgr;
+CXFA_FontMgr* CXFA_FFApp::GetXFAFontMgr() const {
+  return m_pFontMgr.get();
 }
 
 IFGAS_FontMgr* CXFA_FFApp::GetFDEFontMgr() {
@@ -146,28 +129,30 @@
 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
     m_pFDEFontMgr = IFGAS_FontMgr::Create(FX_GetDefFontEnumerator());
 #else
-    m_pFontSource = new CFX_FontSourceEnum_File;
-    m_pFDEFontMgr = IFGAS_FontMgr::Create(m_pFontSource);
+    m_pFontSource.reset(new CFX_FontSourceEnum_File);
+    m_pFDEFontMgr = IFGAS_FontMgr::Create(m_pFontSource.get());
 #endif
   }
-  return m_pFDEFontMgr;
+  return m_pFDEFontMgr.get();
 }
+
 CXFA_FWLTheme* CXFA_FFApp::GetFWLTheme() {
-  if (!m_pFWLTheme) {
-    m_pFWLTheme = new CXFA_FWLTheme(this);
-  }
-  return m_pFWLTheme;
+  if (!m_pFWLTheme)
+    m_pFWLTheme.reset(new CXFA_FWLTheme(this));
+  return m_pFWLTheme.get();
 }
+
 CXFA_FWLAdapterWidgetMgr* CXFA_FFApp::GetWidgetMgr(
     CFWL_WidgetMgrDelegate* pDelegate) {
   if (!m_pAdapterWidgetMgr) {
-    m_pAdapterWidgetMgr = new CXFA_FWLAdapterWidgetMgr;
+    m_pAdapterWidgetMgr.reset(new CXFA_FWLAdapterWidgetMgr);
     pDelegate->OnSetCapability(FWL_WGTMGR_DisableThread |
                                FWL_WGTMGR_DisableForm);
     m_pWidgetMgrDelegate = pDelegate;
   }
-  return m_pAdapterWidgetMgr;
+  return m_pAdapterWidgetMgr.get();
 }
-IFWL_AdapterTimerMgr* CXFA_FFApp::GetTimerMgr() {
+
+IFWL_AdapterTimerMgr* CXFA_FFApp::GetTimerMgr() const {
   return m_pProvider->GetTimerMgr();
 }
diff --git a/xfa/fxfa/app/xfa_ffdocview.cpp b/xfa/fxfa/app/xfa_ffdocview.cpp
index c0b4241..3609e55 100644
--- a/xfa/fxfa/app/xfa_ffdocview.cpp
+++ b/xfa/fxfa/app/xfa_ffdocview.cpp
@@ -56,7 +56,6 @@
       m_pListFocusWidget(nullptr),
       m_bInLayoutStatus(FALSE),
       m_pDoc(pDoc),
-      m_pWidgetHandler(nullptr),
       m_pXFADocLayout(nullptr),
       m_pFocusAcc(nullptr),
       m_pFocusWidget(nullptr),
@@ -66,13 +65,14 @@
 
 CXFA_FFDocView::~CXFA_FFDocView() {
   DestroyDocView();
-  delete m_pWidgetHandler;
 }
 
 void CXFA_FFDocView::InitLayout(CXFA_Node* pNode) {
   RunBindItems();
-  ExecEventActivityByDeepFirst(pNode, XFA_EVENT_Initialize);
-  ExecEventActivityByDeepFirst(pNode, XFA_EVENT_IndexChange);
+  ExecEventActivityByDeepFirst(pNode, XFA_EVENT_Initialize, FALSE, TRUE,
+                               nullptr);
+  ExecEventActivityByDeepFirst(pNode, XFA_EVENT_IndexChange, FALSE, TRUE,
+                               nullptr);
 }
 int32_t CXFA_FFDocView::StartLayout(int32_t iStartPage) {
   m_iStatus = XFA_DOCVIEW_LAYOUTSTATUS_Start;
@@ -91,7 +91,7 @@
   InitLayout(pRootItem);
   InitCalculate(pRootItem);
   InitValidate(pRootItem);
-  ExecEventActivityByDeepFirst(pRootItem, XFA_EVENT_Ready, TRUE);
+  ExecEventActivityByDeepFirst(pRootItem, XFA_EVENT_Ready, TRUE, TRUE, nullptr);
   m_iStatus = XFA_DOCVIEW_LAYOUTSTATUS_Start;
   return iStatus;
 }
@@ -124,13 +124,17 @@
   InitLayout(pPageSetNode);
   InitCalculate(pPageSetNode);
   InitValidate(pPageSetNode);
-  ExecEventActivityByDeepFirst(pPageSetNode, XFA_EVENT_Ready, TRUE);
-  ExecEventActivityByDeepFirst(pRootItem, XFA_EVENT_Ready);
-  ExecEventActivityByDeepFirst(pRootItem, XFA_EVENT_DocReady);
+  ExecEventActivityByDeepFirst(pPageSetNode, XFA_EVENT_Ready, TRUE, TRUE,
+                               nullptr);
+  ExecEventActivityByDeepFirst(pRootItem, XFA_EVENT_Ready, FALSE, TRUE,
+                               nullptr);
+  ExecEventActivityByDeepFirst(pRootItem, XFA_EVENT_DocReady, FALSE, TRUE,
+                               nullptr);
   RunCalculateWidgets();
   RunValidate();
   if (RunLayout()) {
-    ExecEventActivityByDeepFirst(pRootItem, XFA_EVENT_Ready);
+    ExecEventActivityByDeepFirst(pRootItem, XFA_EVENT_Ready, FALSE, TRUE,
+                                 nullptr);
   }
   m_CalculateAccs.RemoveAll();
   if (m_pFocusAcc && !m_pFocusWidget) {
@@ -177,7 +181,7 @@
     CXFA_Node* pNode = m_NewAddedNodes[i];
     InitCalculate(pNode);
     InitValidate(pNode);
-    ExecEventActivityByDeepFirst(pNode, XFA_EVENT_Ready, TRUE);
+    ExecEventActivityByDeepFirst(pNode, XFA_EVENT_Ready, TRUE, TRUE, nullptr);
   }
   m_NewAddedNodes.RemoveAll();
   RunSubformIndexChange();
@@ -303,14 +307,15 @@
     }
     pNode = pRootItem->GetChild(0, XFA_Element::Subform);
   }
-  ExecEventActivityByDeepFirst(pNode, pParam->m_eType, pParam->m_bIsFormReady);
+  ExecEventActivityByDeepFirst(pNode, pParam->m_eType, pParam->m_bIsFormReady,
+                               TRUE, nullptr);
   return XFA_EVENTERROR_Success;
 }
 CXFA_FFWidgetHandler* CXFA_FFDocView::GetWidgetHandler() {
   if (!m_pWidgetHandler) {
-    m_pWidgetHandler = new CXFA_FFWidgetHandler(this);
+    m_pWidgetHandler.reset(new CXFA_FFWidgetHandler(this));
   }
-  return m_pWidgetHandler;
+  return m_pWidgetHandler.get();
 }
 
 CXFA_WidgetAccIterator* CXFA_FFDocView::CreateWidgetAccIterator(
@@ -607,7 +612,8 @@
   if (!pRootItem) {
     return;
   }
-  ExecEventActivityByDeepFirst(pRootItem, XFA_EVENT_DocClose);
+  ExecEventActivityByDeepFirst(pRootItem, XFA_EVENT_DocClose, FALSE, TRUE,
+                               nullptr);
 }
 void CXFA_FFDocView::DestroyDocView() {
   ClearInvalidateList();
@@ -685,14 +691,15 @@
     m_ValidateAccs.Add(pWidget);
 }
 FX_BOOL CXFA_FFDocView::InitCalculate(CXFA_Node* pNode) {
-  ExecEventActivityByDeepFirst(pNode, XFA_EVENT_InitCalculate);
+  ExecEventActivityByDeepFirst(pNode, XFA_EVENT_InitCalculate, FALSE, TRUE,
+                               nullptr);
   return TRUE;
 }
 FX_BOOL CXFA_FFDocView::InitValidate(CXFA_Node* pNode) {
   if (!m_pDoc->GetDocProvider()->IsValidationsEnabled(m_pDoc)) {
     return FALSE;
   }
-  ExecEventActivityByDeepFirst(pNode, XFA_EVENT_Validate);
+  ExecEventActivityByDeepFirst(pNode, XFA_EVENT_Validate, FALSE, TRUE, nullptr);
   m_ValidateAccs.RemoveAll();
   return TRUE;
 }
@@ -717,7 +724,8 @@
   if (!pRootItem) {
     return FALSE;
   }
-  ExecEventActivityByDeepFirst(pRootItem, XFA_EVENT_Ready);
+  ExecEventActivityByDeepFirst(pRootItem, XFA_EVENT_Ready, FALSE, TRUE,
+                               nullptr);
   RunLayout();
   return TRUE;
 }
@@ -803,21 +811,24 @@
 
 CXFA_WidgetAccIterator::CXFA_WidgetAccIterator(CXFA_FFDocView* pDocView,
                                                CXFA_Node* pTravelRoot)
-    : m_ContentIterator(pTravelRoot) {
-  m_pDocView = pDocView;
-  m_pCurWidgetAcc = nullptr;
-}
+    : m_ContentIterator(pTravelRoot),
+      m_pDocView(pDocView),
+      m_pCurWidgetAcc(nullptr) {}
+
 CXFA_WidgetAccIterator::~CXFA_WidgetAccIterator() {}
 void CXFA_WidgetAccIterator::Reset() {
   m_pCurWidgetAcc = nullptr;
   m_ContentIterator.Reset();
 }
+
 CXFA_WidgetAcc* CXFA_WidgetAccIterator::MoveToFirst() {
   return nullptr;
 }
+
 CXFA_WidgetAcc* CXFA_WidgetAccIterator::MoveToLast() {
   return nullptr;
 }
+
 CXFA_WidgetAcc* CXFA_WidgetAccIterator::MoveToNext() {
   CXFA_Node* pItem = m_pCurWidgetAcc ? m_ContentIterator.MoveToNext()
                                      : m_ContentIterator.GetCurrent();
@@ -829,15 +840,19 @@
   }
   return nullptr;
 }
+
 CXFA_WidgetAcc* CXFA_WidgetAccIterator::MoveToPrevious() {
   return nullptr;
 }
+
 CXFA_WidgetAcc* CXFA_WidgetAccIterator::GetCurrentWidgetAcc() {
   return nullptr;
 }
+
 FX_BOOL CXFA_WidgetAccIterator::SetCurrentWidgetAcc(CXFA_WidgetAcc* hWidget) {
   return FALSE;
 }
+
 void CXFA_WidgetAccIterator::SkipTree() {
   m_ContentIterator.SkipChildrenAndMoveToNext();
   m_pCurWidgetAcc = nullptr;
diff --git a/xfa/fxfa/app/xfa_ffwidget.cpp b/xfa/fxfa/app/xfa_ffwidget.cpp
index ed42aa4..446fcd6 100644
--- a/xfa/fxfa/app/xfa_ffwidget.cpp
+++ b/xfa/fxfa/app/xfa_ffwidget.cpp
@@ -564,19 +564,6 @@
   FX_BOOL Continue(IFX_Pause* pPause);
 
  protected:
-  CFX_RenderDevice* m_pDevice;
-  int m_Status;
-  CFX_Matrix m_ImageMatrix;
-  CFX_DIBSource* m_pDIBSource;
-  CFX_DIBitmap* m_pCloneConvert;
-  int m_BitmapAlpha;
-  FX_ARGB m_FillArgb;
-  uint32_t m_Flags;
-  std::unique_ptr<CFX_ImageTransformer> m_pTransformer;
-  void* m_DeviceHandle;
-  int32_t m_BlendType;
-  FX_BOOL m_Result;
-  FX_BOOL m_bPrint;
   FX_BOOL StartDIBSource();
   void CompositeDIBitmap(CFX_DIBitmap* pDIBitmap,
                          int left,
@@ -585,23 +572,35 @@
                          int bitmap_alpha,
                          int blend_mode,
                          int Transparency);
+
+  CFX_RenderDevice* m_pDevice;
+  int m_Status;
+  CFX_Matrix m_ImageMatrix;
+  CFX_DIBSource* m_pDIBSource;
+  std::unique_ptr<CFX_DIBitmap> m_pCloneConvert;
+  int m_BitmapAlpha;
+  FX_ARGB m_FillArgb;
+  uint32_t m_Flags;
+  std::unique_ptr<CFX_ImageTransformer> m_pTransformer;
+  void* m_DeviceHandle;
+  int32_t m_BlendType;
+  FX_BOOL m_Result;
+  FX_BOOL m_bPrint;
 };
-CXFA_ImageRenderer::CXFA_ImageRenderer() {
-  m_pDevice = nullptr;
-  m_Status = 0;
-  m_pDIBSource = nullptr;
-  m_pCloneConvert = nullptr;
-  m_BitmapAlpha = 255;
-  m_FillArgb = 0;
-  m_Flags = 0;
-  m_DeviceHandle = nullptr;
-  m_BlendType = FXDIB_BLEND_NORMAL;
-  m_Result = TRUE;
-  m_bPrint = FALSE;
-}
+
+CXFA_ImageRenderer::CXFA_ImageRenderer()
+    : m_pDevice(nullptr),
+      m_Status(0),
+      m_pDIBSource(nullptr),
+      m_BitmapAlpha(255),
+      m_FillArgb(0),
+      m_Flags(0),
+      m_DeviceHandle(nullptr),
+      m_BlendType(FXDIB_BLEND_NORMAL),
+      m_Result(TRUE),
+      m_bPrint(FALSE) {}
 
 CXFA_ImageRenderer::~CXFA_ImageRenderer() {
-  delete m_pCloneConvert;
   if (m_DeviceHandle)
     m_pDevice->CancelDIBits(m_DeviceHandle);
 }
@@ -647,12 +646,12 @@
     if (m_pDIBSource->HasAlpha() &&
         !(m_pDevice->GetRenderCaps() & FXRC_ALPHA_IMAGE) &&
         !(m_pDevice->GetRenderCaps() & FXRC_GET_BITS)) {
-      m_pCloneConvert = m_pDIBSource->CloneConvert(FXDIB_Rgb);
+      m_pCloneConvert.reset(m_pDIBSource->CloneConvert(FXDIB_Rgb));
       if (!m_pCloneConvert) {
         m_Result = FALSE;
         return FALSE;
       }
-      pDib = m_pCloneConvert;
+      pDib = m_pCloneConvert.get();
     }
     FX_RECT clip_box = m_pDevice->GetClipBox();
     clip_box.Intersect(image_rect);
diff --git a/xfa/fxfa/app/xfa_fwltheme.cpp b/xfa/fxfa/app/xfa_fwltheme.cpp
index 41d8fec..8e1b23c 100644
--- a/xfa/fxfa/app/xfa_fwltheme.cpp
+++ b/xfa/fxfa/app/xfa_fwltheme.cpp
@@ -43,38 +43,30 @@
                 : nullptr;
 }
 
-CXFA_FWLTheme::CXFA_FWLTheme(CXFA_FFApp* pApp) : m_pApp(pApp) {
-  m_dwCapacity = 0;
-  m_fCapacity = 0;
-  m_pCalendarFont = nullptr;
-  m_Rect.Set(0, 0, 0, 0);
-  m_pCheckBoxTP = new CXFA_FWLCheckBoxTP;
-  m_pListBoxTP = new CFWL_ListBoxTP;
-  m_pPictureBoxTP = new CFWL_PictureBoxTP;
-  m_pSrollBarTP = new CFWL_ScrollBarTP;
-  m_pEditTP = new CXFA_FWLEditTP;
-  m_pComboBoxTP = new CFWL_ComboBoxTP;
-  m_pMonthCalendarTP = new CFWL_MonthCalendarTP;
-  m_pDateTimePickerTP = new CFWL_DateTimePickerTP;
-  m_pPushButtonTP = new CFWL_PushButtonTP;
-  m_pCaretTP = new CFWL_CaretTP;
-  m_pBarcodeTP = new CFWL_BarcodeTP;
+CXFA_FWLTheme::CXFA_FWLTheme(CXFA_FFApp* pApp)
+    : m_pCheckBoxTP(new CXFA_FWLCheckBoxTP),
+      m_pListBoxTP(new CFWL_ListBoxTP),
+      m_pPictureBoxTP(new CFWL_PictureBoxTP),
+      m_pSrollBarTP(new CFWL_ScrollBarTP),
+      m_pEditTP(new CXFA_FWLEditTP),
+      m_pComboBoxTP(new CFWL_ComboBoxTP),
+      m_pMonthCalendarTP(new CFWL_MonthCalendarTP),
+      m_pDateTimePickerTP(new CFWL_DateTimePickerTP),
+      m_pPushButtonTP(new CFWL_PushButtonTP),
+      m_pCaretTP(new CFWL_CaretTP),
+      m_pBarcodeTP(new CFWL_BarcodeTP),
+      m_fCapacity(0.0f),
+      m_dwCapacity(0),
+      m_pCalendarFont(nullptr),
+      m_pApp(pApp) {
+  m_Rect.Reset();
   Initialize();
 }
+
 CXFA_FWLTheme::~CXFA_FWLTheme() {
   Finalize();
-  delete m_pCheckBoxTP;
-  delete m_pListBoxTP;
-  delete m_pPictureBoxTP;
-  delete m_pSrollBarTP;
-  delete m_pEditTP;
-  delete m_pComboBoxTP;
-  delete m_pMonthCalendarTP;
-  delete m_pDateTimePickerTP;
-  delete m_pPushButtonTP;
-  delete m_pCaretTP;
-  delete m_pBarcodeTP;
 }
+
 FWL_Error CXFA_FWLTheme::Initialize() {
   m_pTextOut.reset(new CFDE_TextOut);
   for (size_t i = 0; !m_pCalendarFont && i < FX_ArraySize(g_FWLTheme_CalFonts);
@@ -96,6 +88,7 @@
   FWLTHEME_Init();
   return FWL_Error::Succeeded;
 }
+
 FWL_Error CXFA_FWLTheme::Finalize() {
   m_pTextOut.reset();
   if (m_pCalendarFont) {
@@ -406,27 +399,27 @@
 CFWL_WidgetTP* CXFA_FWLTheme::GetTheme(IFWL_Widget* pWidget) {
   switch (pWidget->GetClassID()) {
     case FWL_Type::CheckBox:
-      return m_pCheckBoxTP;
+      return m_pCheckBoxTP.get();
     case FWL_Type::ListBox:
-      return m_pListBoxTP;
+      return m_pListBoxTP.get();
     case FWL_Type::PictureBox:
-      return m_pPictureBoxTP;
+      return m_pPictureBoxTP.get();
     case FWL_Type::ScrollBar:
-      return m_pSrollBarTP;
+      return m_pSrollBarTP.get();
     case FWL_Type::Edit:
-      return m_pEditTP;
+      return m_pEditTP.get();
     case FWL_Type::ComboBox:
-      return m_pComboBoxTP;
+      return m_pComboBoxTP.get();
     case FWL_Type::MonthCalendar:
-      return m_pMonthCalendarTP;
+      return m_pMonthCalendarTP.get();
     case FWL_Type::DateTimePicker:
-      return m_pDateTimePickerTP;
+      return m_pDateTimePickerTP.get();
     case FWL_Type::PushButton:
-      return m_pPushButtonTP;
+      return m_pPushButtonTP.get();
     case FWL_Type::Caret:
-      return m_pCaretTP;
+      return m_pCaretTP.get();
     case FWL_Type::Barcode:
-      return m_pBarcodeTP;
+      return m_pBarcodeTP.get();
     default:
       return nullptr;
   }
diff --git a/xfa/fxfa/app/xfa_fwltheme.h b/xfa/fxfa/app/xfa_fwltheme.h
index 63a4247..81f33b2 100644
--- a/xfa/fxfa/app/xfa_fwltheme.h
+++ b/xfa/fxfa/app/xfa_fwltheme.h
@@ -29,13 +29,6 @@
   CXFA_FWLTheme(CXFA_FFApp* pApp);
   ~CXFA_FWLTheme() override;
 
-  FWL_Error GetClassName(CFX_WideString& wsClass) const {
-    return FWL_Error::Succeeded;
-  }
-  uint32_t GetHashCode() const { return 0; }
-  FWL_Error Initialize();
-  FWL_Error Finalize();
-
   // IFWL_ThemeProvider:
   bool IsValidWidget(IFWL_Widget* pWidget) override;
   uint32_t GetThemeID(IFWL_Widget* pWidget) override;
@@ -56,30 +49,37 @@
                    FX_FLOAT fy) override;
   FX_BOOL CalcTextRect(CFWL_ThemeText* pParams, CFX_RectF& rect) override;
 
+  FWL_Error GetClassName(CFX_WideString& wsClass) const {
+    return FWL_Error::Succeeded;
+  }
+  uint32_t GetHashCode() const { return 0; }
+  FWL_Error Initialize();
+  FWL_Error Finalize();
   FWL_Error GetPartRect(CFWL_ThemePart* pThemePart);
 
  protected:
   CFWL_WidgetTP* GetTheme(IFWL_Widget* pWidget);
-  CFWL_CheckBoxTP* m_pCheckBoxTP;
-  CFWL_ListBoxTP* m_pListBoxTP;
-  CFWL_PictureBoxTP* m_pPictureBoxTP;
-  CFWL_ScrollBarTP* m_pSrollBarTP;
-  CFWL_EditTP* m_pEditTP;
-  CFWL_ComboBoxTP* m_pComboBoxTP;
-  CFWL_MonthCalendarTP* m_pMonthCalendarTP;
-  CFWL_DateTimePickerTP* m_pDateTimePickerTP;
-  CFWL_PushButtonTP* m_pPushButtonTP;
-  CFWL_CaretTP* m_pCaretTP;
-  CFWL_BarcodeTP* m_pBarcodeTP;
+  std::unique_ptr<CFWL_CheckBoxTP> m_pCheckBoxTP;
+  std::unique_ptr<CFWL_ListBoxTP> m_pListBoxTP;
+  std::unique_ptr<CFWL_PictureBoxTP> m_pPictureBoxTP;
+  std::unique_ptr<CFWL_ScrollBarTP> m_pSrollBarTP;
+  std::unique_ptr<CFWL_EditTP> m_pEditTP;
+  std::unique_ptr<CFWL_ComboBoxTP> m_pComboBoxTP;
+  std::unique_ptr<CFWL_MonthCalendarTP> m_pMonthCalendarTP;
+  std::unique_ptr<CFWL_DateTimePickerTP> m_pDateTimePickerTP;
+  std::unique_ptr<CFWL_PushButtonTP> m_pPushButtonTP;
+  std::unique_ptr<CFWL_CaretTP> m_pCaretTP;
+  std::unique_ptr<CFWL_BarcodeTP> m_pBarcodeTP;
   std::unique_ptr<CFDE_TextOut> m_pTextOut;
   FX_FLOAT m_fCapacity;
   uint32_t m_dwCapacity;
   CFGAS_GEFont* m_pCalendarFont;
   CFX_WideString m_wsResource;
-  CXFA_FFApp* m_pApp;
+  CXFA_FFApp* const m_pApp;
   CFX_RectF m_Rect;
   CFX_SizeF m_SizeAboveBelow;
 };
+
 class CXFA_FWLCheckBoxTP : public CFWL_CheckBoxTP {
  public:
   CXFA_FWLCheckBoxTP();
@@ -94,6 +94,7 @@
                      int32_t iState,
                      CFX_Matrix* pMatrix);
 };
+
 class CXFA_FWLEditTP : public CFWL_EditTP {
  public:
   CXFA_FWLEditTP();
diff --git a/xfa/fxfa/include/xfa_ffapp.h b/xfa/fxfa/include/xfa_ffapp.h
index 5780859..bc0d6df 100644
--- a/xfa/fxfa/include/xfa_ffapp.h
+++ b/xfa/fxfa/include/xfa_ffapp.h
@@ -7,6 +7,8 @@
 #ifndef XFA_FXFA_INCLUDE_XFA_FFAPP_H_
 #define XFA_FXFA_INCLUDE_XFA_FFAPP_H_
 
+#include <memory>
+
 #include "core/fpdfapi/fpdf_parser/include/cpdf_stream.h"
 #include "core/fpdfapi/fpdf_parser/include/cpdf_stream_acc.h"
 #include "xfa/fgas/font/fgas_font.h"
@@ -40,36 +42,38 @@
   explicit CXFA_FFApp(IXFA_AppProvider* pProvider);
   ~CXFA_FFApp();
 
-  CXFA_FFDocHandler* GetDocHandler();
   CXFA_FFDoc* CreateDoc(IXFA_DocProvider* pProvider,
                         IFX_FileRead* pStream,
                         FX_BOOL bTakeOverFile);
   CXFA_FFDoc* CreateDoc(IXFA_DocProvider* pProvider, CPDF_Document* pPDFDoc);
-  IXFA_AppProvider* GetAppProvider() { return m_pProvider; }
   void SetDefaultFontMgr(std::unique_ptr<CXFA_DefFontMgr> pFontMgr);
 
+  CXFA_FFDocHandler* GetDocHandler();
   CXFA_FWLAdapterWidgetMgr* GetWidgetMgr(CFWL_WidgetMgrDelegate* pDelegate);
-  IFWL_AdapterTimerMgr* GetTimerMgr();
-
-  CXFA_FontMgr* GetXFAFontMgr();
   IFGAS_FontMgr* GetFDEFontMgr();
   CXFA_FWLTheme* GetFWLTheme();
-  CFWL_WidgetMgrDelegate* GetWidgetMgrDelegate() {
+
+  IXFA_AppProvider* GetAppProvider() const { return m_pProvider; }
+  IFWL_AdapterTimerMgr* GetTimerMgr() const;
+  CXFA_FontMgr* GetXFAFontMgr() const;
+  CFWL_WidgetMgrDelegate* GetWidgetMgrDelegate() const {
     return m_pWidgetMgrDelegate;
   }
 
  protected:
-  CXFA_FFDocHandler* m_pDocHandler;
-  IFWL_App* m_pFWLApp;
-  CXFA_FWLTheme* m_pFWLTheme;
-  IXFA_AppProvider* m_pProvider;
-  CXFA_FontMgr* m_pFontMgr;
+  std::unique_ptr<CXFA_FFDocHandler> m_pDocHandler;
+  IXFA_AppProvider* const m_pProvider;
+  std::unique_ptr<CXFA_FontMgr> m_pFontMgr;
 #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
-  CFX_FontSourceEnum_File* m_pFontSource;
+  std::unique_ptr<CFX_FontSourceEnum_File> m_pFontSource;
 #endif
-  CXFA_FWLAdapterWidgetMgr* m_pAdapterWidgetMgr;
-  CFWL_WidgetMgrDelegate* m_pWidgetMgrDelegate;
-  IFGAS_FontMgr* m_pFDEFontMgr;
+  std::unique_ptr<CXFA_FWLAdapterWidgetMgr> m_pAdapterWidgetMgr;
+  CFWL_WidgetMgrDelegate* m_pWidgetMgrDelegate;  // not owned.
+  std::unique_ptr<IFGAS_FontMgr> m_pFDEFontMgr;
+  // |m_pFWLApp| has to be released first, then |m_pFWLTheme| since the former
+  // may refers to theme manager and the latter refers to font manager.
+  std::unique_ptr<CXFA_FWLTheme> m_pFWLTheme;
+  std::unique_ptr<IFWL_App> m_pFWLApp;
 };
 
 #endif  // XFA_FXFA_INCLUDE_XFA_FFAPP_H_
diff --git a/xfa/fxfa/include/xfa_ffdocview.h b/xfa/fxfa/include/xfa_ffdocview.h
index bc2373d..8372981 100644
--- a/xfa/fxfa/include/xfa_ffdocview.h
+++ b/xfa/fxfa/include/xfa_ffdocview.h
@@ -42,16 +42,16 @@
 
   CXFA_FFDoc* GetDoc() { return m_pDoc; }
   int32_t StartLayout(int32_t iStartPage = 0);
-  int32_t DoLayout(IFX_Pause* pPause = nullptr);
+  int32_t DoLayout(IFX_Pause* pPause);
   void StopLayout();
   int32_t GetLayoutStatus();
   void UpdateDocView();
   int32_t CountPageViews();
   CXFA_FFPageView* GetPageView(int32_t nIndex);
 
-  void ResetWidgetData(CXFA_WidgetAcc* pWidgetAcc = nullptr);
+  void ResetWidgetData(CXFA_WidgetAcc* pWidgetAcc);
   int32_t ProcessWidgetEvent(CXFA_EventParam* pParam,
-                             CXFA_WidgetAcc* pWidgetAcc = nullptr);
+                             CXFA_WidgetAcc* pWidgetAcc);
   CXFA_FFWidgetHandler* GetWidgetHandler();
   CXFA_WidgetAccIterator* CreateWidgetAccIterator(
       XFA_WIDGETORDER eOrder = XFA_WIDGETORDER_PreOrder);
@@ -59,9 +59,9 @@
   void KillFocus();
   FX_BOOL SetFocus(CXFA_FFWidget* hWidget);
   CXFA_FFWidget* GetWidgetByName(const CFX_WideString& wsName,
-                                 CXFA_FFWidget* pRefWidget = nullptr);
+                                 CXFA_FFWidget* pRefWidget);
   CXFA_WidgetAcc* GetWidgetAccByName(const CFX_WideString& wsName,
-                                     CXFA_WidgetAcc* pRefWidgetAcc = nullptr);
+                                     CXFA_WidgetAcc* pRefWidgetAcc);
   CXFA_LayoutProcessor* GetXFALayout() const;
   void OnPageEvent(CXFA_ContainerLayoutItem* pSender, uint32_t dwEvent);
   void LockUpdate();
@@ -94,9 +94,9 @@
   void DeleteLayoutItem(CXFA_FFWidget* pWidget);
   int32_t ExecEventActivityByDeepFirst(CXFA_Node* pFormNode,
                                        XFA_EVENTTYPE eEventType,
-                                       FX_BOOL bIsFormReady = FALSE,
-                                       FX_BOOL bRecursive = TRUE,
-                                       CXFA_Node* pExclude = nullptr);
+                                       FX_BOOL bIsFormReady,
+                                       FX_BOOL bRecursive,
+                                       CXFA_Node* pExclude);
   FX_BOOL m_bLayoutEvent;
   CFX_WideStringArray m_arrNullTestMsg;
   CXFA_FFWidget* m_pListFocusWidget;
@@ -112,12 +112,12 @@
   FX_BOOL ResetSingleWidgetAccData(CXFA_WidgetAcc* pWidgetAcc);
   CXFA_Node* GetRootSubform();
 
-  CXFA_FFDoc* m_pDoc;
-  CXFA_FFWidgetHandler* m_pWidgetHandler;
-  CXFA_LayoutProcessor* m_pXFADocLayout;
-  CXFA_WidgetAcc* m_pFocusAcc;
-  CXFA_FFWidget* m_pFocusWidget;
-  CXFA_FFWidget* m_pOldFocusWidget;
+  CXFA_FFDoc* const m_pDoc;
+  std::unique_ptr<CXFA_FFWidgetHandler> m_pWidgetHandler;
+  CXFA_LayoutProcessor* m_pXFADocLayout;  // not owned.
+  CXFA_WidgetAcc* m_pFocusAcc;            // not owned.
+  CXFA_FFWidget* m_pFocusWidget;          // not owned.
+  CXFA_FFWidget* m_pOldFocusWidget;       // not owned.
   std::map<CXFA_FFPageView*, std::unique_ptr<CFX_RectF>> m_mapPageInvalidate;
   CFX_ArrayTemplate<CXFA_WidgetAcc*> m_ValidateAccs;
   CFX_ArrayTemplate<CXFA_WidgetAcc*> m_CalculateAccs;
@@ -145,8 +145,8 @@
 
  protected:
   CXFA_ContainerIterator m_ContentIterator;
-  CXFA_FFDocView* m_pDocView;
-  CXFA_WidgetAcc* m_pCurWidgetAcc;
+  CXFA_FFDocView* const m_pDocView;
+  CXFA_WidgetAcc* m_pCurWidgetAcc;  // not owned.
 };
 
 #endif  // XFA_FXFA_INCLUDE_XFA_FFDOCVIEW_H_
diff --git a/xfa/fxfa/parser/cxfa_resolveprocessor.cpp b/xfa/fxfa/parser/cxfa_resolveprocessor.cpp
index 2a281f9..60f3671 100644
--- a/xfa/fxfa/parser/cxfa_resolveprocessor.cpp
+++ b/xfa/fxfa/parser/cxfa_resolveprocessor.cpp
@@ -18,9 +18,7 @@
 CXFA_ResolveProcessor::CXFA_ResolveProcessor()
     : m_iCurStart(0), m_pNodeHelper(new CXFA_NodeHelper) {}
 
-CXFA_ResolveProcessor::~CXFA_ResolveProcessor() {
-  delete m_pNodeHelper;
-}
+CXFA_ResolveProcessor::~CXFA_ResolveProcessor() {}
 
 int32_t CXFA_ResolveProcessor::Resolve(CXFA_ResolveNodesData& rnd) {
   if (!rnd.m_CurNode) {
diff --git a/xfa/fxfa/parser/cxfa_resolveprocessor.h b/xfa/fxfa/parser/cxfa_resolveprocessor.h
index 4db0ea2..630a54e 100644
--- a/xfa/fxfa/parser/cxfa_resolveprocessor.h
+++ b/xfa/fxfa/parser/cxfa_resolveprocessor.h
@@ -7,6 +7,8 @@
 #ifndef XFA_FXFA_PARSER_CXFA_RESOLVEPROCESSOR_H_
 #define XFA_FXFA_PARSER_CXFA_RESOLVEPROCESSOR_H_
 
+#include <memory>
+
 #include "xfa/fxfa/parser/xfa_object.h"
 #include "xfa/fxfa/parser/xfa_resolvenode_rs.h"
 
@@ -46,7 +48,7 @@
                         int32_t iCount);
   void SetCurStart(int32_t start) { m_iCurStart = start; }
 
-  CXFA_NodeHelper* GetNodeHelper() { return m_pNodeHelper; }
+  CXFA_NodeHelper* GetNodeHelper() const { return m_pNodeHelper.get(); }
 
  private:
   int32_t ResolveForAttributeRs(CXFA_Object* curNode,
@@ -72,7 +74,7 @@
   void FilterCondition(CXFA_ResolveNodesData& rnd, CFX_WideString wsCondition);
 
   int32_t m_iCurStart;
-  CXFA_NodeHelper* m_pNodeHelper;
+  std::unique_ptr<CXFA_NodeHelper> m_pNodeHelper;
 };
 
 #endif  // XFA_FXFA_PARSER_CXFA_RESOLVEPROCESSOR_H_
diff --git a/xfa/fxfa/parser/xfa_locale.cpp b/xfa/fxfa/parser/xfa_locale.cpp
index 67a9760..723fdbc 100644
--- a/xfa/fxfa/parser/xfa_locale.cpp
+++ b/xfa/fxfa/parser/xfa_locale.cpp
@@ -6,6 +6,8 @@
 
 #include "xfa/fxfa/parser/xfa_locale.h"
 
+#include <utility>
+
 #include "core/fxcrt/include/fx_xml.h"
 #include "xfa/fxfa/parser/cxfa_document.h"
 #include "xfa/fxfa/parser/xfa_localemgr.h"
@@ -17,19 +19,19 @@
 static const FX_WCHAR g_FX_Decimal[] = L"z,zzz,zzz,zzz,zzz,zz9.zzz";
 static const FX_WCHAR g_FX_Integer[] = L"z,zzz,zzz,zzz,zzz,zzz";
 
-CXFA_XMLLocale::CXFA_XMLLocale(CXML_Element* pLocaleData)
-    : m_pLocaleData(pLocaleData) {}
+CXFA_XMLLocale::CXFA_XMLLocale(std::unique_ptr<CXML_Element> pLocaleData)
+    : m_pLocaleData(std::move(pLocaleData)) {}
 
-CXFA_XMLLocale::~CXFA_XMLLocale() {
-  delete m_pLocaleData;
-}
+CXFA_XMLLocale::~CXFA_XMLLocale() {}
 
 void CXFA_XMLLocale::Release() {
   delete this;
 }
+
 CFX_WideString CXFA_XMLLocale::GetName() {
   return m_pLocaleData ? m_pLocaleData->GetAttrValue("name") : CFX_WideString();
 }
+
 void CXFA_XMLLocale::GetNumbericSymbol(FX_LOCALENUMSYMBOL eType,
                                        CFX_WideString& wsNumSymbol) const {
   CFX_ByteString bsSymbols;
@@ -74,6 +76,7 @@
              CFX_ByteStringC(bsSymbols.c_str(), bsSymbols.GetLength() - 1),
              wsName.AsStringC(), wsNumSymbol);
 }
+
 void CXFA_XMLLocale::GetDateTimeSymbols(CFX_WideString& wsDtSymbol) const {
   if (!m_pLocaleData) {
     return;
@@ -86,26 +89,32 @@
   }
   wsDtSymbol = pNumberSymbols->GetContent(0);
 }
+
 void CXFA_XMLLocale::GetMonthName(int32_t nMonth,
                                   CFX_WideString& wsMonthName,
                                   FX_BOOL bAbbr) const {
   wsMonthName = GetCalendarSymbol("month", nMonth, bAbbr);
 }
+
 void CXFA_XMLLocale::GetDayName(int32_t nWeek,
                                 CFX_WideString& wsDayName,
                                 FX_BOOL bAbbr) const {
   wsDayName = GetCalendarSymbol("day", nWeek, bAbbr);
 }
+
 void CXFA_XMLLocale::GetMeridiemName(CFX_WideString& wsMeridiemName,
                                      FX_BOOL bAM) const {
   wsMeridiemName = GetCalendarSymbol("meridiem", bAM ? 0 : 1, FALSE);
 }
+
 void CXFA_XMLLocale::GetTimeZone(FX_TIMEZONE& tz) const {
   CXFA_TimeZoneProvider::Get()->GetTimeZone(tz);
 }
+
 void CXFA_XMLLocale::GetEraName(CFX_WideString& wsEraName, FX_BOOL bAD) const {
   wsEraName = GetCalendarSymbol("era", bAD ? 1 : 0, FALSE);
 }
+
 CFX_WideString CXFA_XMLLocale::GetCalendarSymbol(const CFX_ByteStringC& symbol,
                                                  int index,
                                                  FX_BOOL bAbbr) const {
@@ -132,6 +141,7 @@
   }
   return wsSymbolName;
 }
+
 void CXFA_XMLLocale::GetDatePattern(FX_LOCALEDATETIMESUBCATEGORY eType,
                                     CFX_WideString& wsPattern) const {
   CXML_Element* pElement = m_pLocaleData->GetElement("", "datePatterns");
@@ -156,6 +166,7 @@
   }
   GetPattern(pElement, "datePattern", wsName.AsStringC(), wsPattern);
 }
+
 void CXFA_XMLLocale::GetTimePattern(FX_LOCALEDATETIMESUBCATEGORY eType,
                                     CFX_WideString& wsPattern) const {
   CXML_Element* pElement = m_pLocaleData->GetElement("", "timePatterns");
@@ -180,6 +191,7 @@
   }
   GetPattern(pElement, "timePattern", wsName.AsStringC(), wsPattern);
 }
+
 void CXFA_XMLLocale::GetNumPattern(FX_LOCALENUMSUBCATEGORY eType,
                                    CFX_WideString& wsPattern) const {
   CXML_Element* pElement = m_pLocaleData->GetElement("", "numberPatterns");
@@ -201,6 +213,7 @@
       break;
   }
 }
+
 void CXFA_XMLLocale::GetPattern(CXML_Element* pElement,
                                 const CFX_ByteStringC& bsTag,
                                 const CFX_WideStringC& wsName,
@@ -214,17 +227,20 @@
     }
   }
 }
-CXFA_NodeLocale::CXFA_NodeLocale(CXFA_Node* pLocale) {
-  m_pLocale = pLocale;
-}
+
+CXFA_NodeLocale::CXFA_NodeLocale(CXFA_Node* pLocale) : m_pLocale(pLocale) {}
+
 CXFA_NodeLocale::~CXFA_NodeLocale() {}
+
 void CXFA_NodeLocale::Release() {
   delete this;
 }
+
 CFX_WideString CXFA_NodeLocale::GetName() {
   return CFX_WideString(m_pLocale ? m_pLocale->GetCData(XFA_ATTRIBUTE_Name)
                                   : nullptr);
 }
+
 void CXFA_NodeLocale::GetNumbericSymbol(FX_LOCALENUMSYMBOL eType,
                                         CFX_WideString& wsNumSymbol) const {
   switch (eType) {
@@ -254,33 +270,40 @@
       break;
   }
 }
+
 void CXFA_NodeLocale::GetDateTimeSymbols(CFX_WideString& wsDtSymbol) const {
   CXFA_Node* pSymbols =
       m_pLocale ? m_pLocale->GetChild(0, XFA_Element::DateTimeSymbols)
                 : nullptr;
   wsDtSymbol = pSymbols ? pSymbols->GetContent() : CFX_WideString();
 }
+
 void CXFA_NodeLocale::GetMonthName(int32_t nMonth,
                                    CFX_WideString& wsMonthName,
                                    FX_BOOL bAbbr) const {
   wsMonthName = GetCalendarSymbol(XFA_Element::MonthNames, nMonth, bAbbr);
 }
+
 void CXFA_NodeLocale::GetDayName(int32_t nWeek,
                                  CFX_WideString& wsDayName,
                                  FX_BOOL bAbbr) const {
   wsDayName = GetCalendarSymbol(XFA_Element::DayNames, nWeek, bAbbr);
 }
+
 void CXFA_NodeLocale::GetMeridiemName(CFX_WideString& wsMeridiemName,
                                       FX_BOOL bAM) const {
   wsMeridiemName =
       GetCalendarSymbol(XFA_Element::MeridiemNames, bAM ? 0 : 1, FALSE);
 }
+
 void CXFA_NodeLocale::GetTimeZone(FX_TIMEZONE& tz) const {
   CXFA_TimeZoneProvider::Get()->GetTimeZone(tz);
 }
+
 void CXFA_NodeLocale::GetEraName(CFX_WideString& wsEraName, FX_BOOL bAD) const {
   wsEraName = GetCalendarSymbol(XFA_Element::EraNames, bAD ? 1 : 0, FALSE);
 }
+
 void CXFA_NodeLocale::GetDatePattern(FX_LOCALEDATETIMESUBCATEGORY eType,
                                      CFX_WideString& wsPattern) const {
   switch (eType) {
@@ -299,6 +322,7 @@
       break;
   }
 }
+
 void CXFA_NodeLocale::GetTimePattern(FX_LOCALEDATETIMESUBCATEGORY eType,
                                      CFX_WideString& wsPattern) const {
   switch (eType) {
@@ -317,6 +341,7 @@
       break;
   }
 }
+
 void CXFA_NodeLocale::GetNumPattern(FX_LOCALENUMSUBCATEGORY eType,
                                     CFX_WideString& wsPattern) const {
   switch (eType) {
@@ -334,6 +359,7 @@
       break;
   }
 }
+
 CXFA_Node* CXFA_NodeLocale::GetNodeByName(CXFA_Node* pParent,
                                           const CFX_WideStringC& wsName) const {
   CXFA_Node* pChild =
@@ -349,6 +375,7 @@
   }
   return nullptr;
 }
+
 CFX_WideString CXFA_NodeLocale::GetSymbol(
     XFA_Element eElement,
     const CFX_WideStringC& symbol_type) const {
@@ -356,6 +383,7 @@
   CXFA_Node* pSymbol = GetNodeByName(pSymbols, symbol_type);
   return pSymbol ? pSymbol->GetContent() : CFX_WideString();
 }
+
 CFX_WideString CXFA_NodeLocale::GetCalendarSymbol(XFA_Element eElement,
                                                   int index,
                                                   FX_BOOL bAbbr) const {
diff --git a/xfa/fxfa/parser/xfa_locale.h b/xfa/fxfa/parser/xfa_locale.h
index 0c72381..0463a9f 100644
--- a/xfa/fxfa/parser/xfa_locale.h
+++ b/xfa/fxfa/parser/xfa_locale.h
@@ -7,12 +7,14 @@
 #ifndef XFA_FXFA_PARSER_XFA_LOCALE_H_
 #define XFA_FXFA_PARSER_XFA_LOCALE_H_
 
+#include <memory>
+
 #include "xfa/fgas/localization/fgas_locale.h"
 #include "xfa/fxfa/parser/xfa_object.h"
 
 class CXFA_XMLLocale : public IFX_Locale {
  public:
-  CXFA_XMLLocale(CXML_Element* pLocaleData);
+  explicit CXFA_XMLLocale(std::unique_ptr<CXML_Element> pLocaleData);
 
   // IFX_Locale
   void Release() override;
@@ -51,7 +53,7 @@
                                    FX_BOOL bAbbr) const;
 
  private:
-  CXML_Element* m_pLocaleData;
+  std::unique_ptr<CXML_Element> m_pLocaleData;
 };
 
 class CXFA_NodeLocale : public IFX_Locale {
@@ -94,7 +96,7 @@
                                    int index,
                                    FX_BOOL bAbbr) const;
 
-  CXFA_Node* m_pLocale;
+  CXFA_Node* const m_pLocale;
 };
 
 #endif  // XFA_FXFA_PARSER_XFA_LOCALE_H_
diff --git a/xfa/fxfa/parser/xfa_localemgr.cpp b/xfa/fxfa/parser/xfa_localemgr.cpp
index 2d10074..efc18af 100644
--- a/xfa/fxfa/parser/xfa_localemgr.cpp
+++ b/xfa/fxfa/parser/xfa_localemgr.cpp
@@ -6,6 +6,9 @@
 
 #include "xfa/fxfa/parser/xfa_localemgr.h"
 
+#include <memory>
+#include <utility>
+
 #include "core/fxcodec/include/fx_codec.h"
 #include "core/fxcrt/include/fx_xml.h"
 #include "core/fxge/include/cfx_gemodule.h"
@@ -1029,6 +1032,8 @@
     0x89, 0x26, 0xB5, 0x2C, 0xA3, 0xB6, 0x4E, 0x5C, 0xA6, 0x17, 0xA4, 0x7B,
     0xB3, 0x85, 0xFA, 0x59, 0x2A, 0x7A, 0xFF, 0x3D, 0xC4, 0x3F, 0xDE, 0xCB,
     0x8B, 0xC4};
+
+// TODO(weili): Change this function to return std::unique_ptr type.
 static IFX_Locale* XFA_GetLocaleFromBuffer(const uint8_t* pBuf, int nBufLen) {
   if (!pBuf || nBufLen <= 0) {
     return nullptr;
@@ -1041,20 +1046,18 @@
   if (!pCodecMgr) {
     return nullptr;
   }
-  CXML_Element* pLocale = nullptr;
+  std::unique_ptr<CXML_Element> pLocale;
   uint8_t* pOut = nullptr;
   uint32_t dwSize;
   pCodecMgr->GetFlateModule()->FlateOrLZWDecode(FALSE, pBuf, nBufLen, TRUE, 0,
                                                 0, 0, 0, 0, pOut, dwSize);
   if (pOut) {
-    pLocale = CXML_Element::Parse(pOut, dwSize);
+    pLocale.reset(CXML_Element::Parse(pOut, dwSize));
     FX_Free(pOut);
   }
-  if (pLocale) {
-    return new CXFA_XMLLocale(pLocale);
-  }
-  return nullptr;
+  return pLocale ? new CXFA_XMLLocale(std::move(pLocale)) : nullptr;
 }
+
 static uint16_t XFA_GetLanguage(CFX_WideString wsLanguage) {
   uint16_t dwLangueID = XFA_LANGID_en_US;
   if (wsLanguage.GetLength() < 2) {