Use observed ptrs from CFGAS_GEFont back to its font mgrs.

CFGAS_GEFonts are ref counted, and its a good thing since they
are managed by at least three different font managers: 
CFGAS_FontMgr, CXFA_PDFFontMgr, and CXFA_FontMgr.  None of these
have a clear claim to ownership of the CFGAS_GEFont.

CFGAS_GEFont has back-pointers to two of these, CFGAS_FontMgr, and
CXFA_PDFFontMgr, and they could each outlive the other.  Thus the
font needs to watch for destruction of either of its managers, so
as to stop using it after its gone.

Bug: 724640
Change-Id: I907ec35e300e11e532e13545d51fb200ac86b4f9
Reviewed-on: https://pdfium-review.googlesource.com/5735
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/xfa/fgas/font/cfgas_fontmgr.h b/xfa/fgas/font/cfgas_fontmgr.h
index 1ef78a7..ca3cf1c 100644
--- a/xfa/fgas/font/cfgas_fontmgr.h
+++ b/xfa/fgas/font/cfgas_fontmgr.h
@@ -14,12 +14,14 @@
 #include <vector>
 
 #include "core/fxcrt/cfx_crtfileaccess.h"
+#include "core/fxcrt/cfx_observable.h"
 #include "core/fxcrt/cfx_retain_ptr.h"
 #include "core/fxcrt/cfx_seekablestreamproxy.h"
 #include "core/fxcrt/fx_extension.h"
 #include "core/fxge/cfx_fontmapper.h"
 #include "core/fxge/fx_freetype.h"
 #include "core/fxge/ifx_systemfontinfo.h"
+#include "xfa/fxfa/cxfa_pdffontmgr.h"
 
 #define FX_FONTSTYLE_Normal 0x00
 #define FX_FONTSTYLE_FixedPitch 0x01
@@ -32,8 +34,6 @@
 #define FX_FONTSTYLE_ExactMatch 0x80000000
 
 class CFX_FontSourceEnum_File;
-class CXFA_PDFFontMgr;
-class CFGAS_FontMgr;
 class CFGAS_GEFont;
 
 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
@@ -81,7 +81,7 @@
 
 FX_LPEnumAllFonts FX_GetDefFontEnumerator();
 
-class CFGAS_FontMgr {
+class CFGAS_FontMgr : public CFX_Observable<CFGAS_FontMgr> {
  public:
   static std::unique_ptr<CFGAS_FontMgr> Create(FX_LPEnumAllFonts pEnumerator);
 
@@ -180,7 +180,7 @@
   std::vector<CFX_ByteString> m_FolderPaths;
 };
 
-class CFGAS_FontMgr {
+class CFGAS_FontMgr : public CFX_Observable<CFGAS_FontMgr> {
  public:
   static std::unique_ptr<CFGAS_FontMgr> Create(
       CFX_FontSourceEnum_File* pFontEnum);
diff --git a/xfa/fgas/font/cfgas_gefont.cpp b/xfa/fgas/font/cfgas_gefont.cpp
index b1bd769..151aec7 100644
--- a/xfa/fgas/font/cfgas_gefont.cpp
+++ b/xfa/fgas/font/cfgas_gefont.cpp
@@ -61,9 +61,8 @@
       m_dwLogFontStyle(0),
 #endif
       m_pFont(nullptr),
-      m_pFontMgr(pFontMgr),
       m_bExternalFont(false),
-      m_pProvider(nullptr) {
+      m_pFontMgr(pFontMgr) {
 }
 
 CFGAS_GEFont::CFGAS_GEFont(const CFX_RetainPtr<CFGAS_GEFont>& src,
@@ -74,10 +73,9 @@
       m_dwLogFontStyle(0),
 #endif
       m_pFont(nullptr),
-      m_pSrcFont(src),
-      m_pFontMgr(src->m_pFontMgr),
       m_bExternalFont(false),
-      m_pProvider(nullptr) {
+      m_pSrcFont(src),
+      m_pFontMgr(src->m_pFontMgr) {
   ASSERT(m_pSrcFont->m_pFont);
   m_pFont = new CFX_Font;
   m_pFont->LoadClone(m_pSrcFont->m_pFont);
diff --git a/xfa/fgas/font/cfgas_gefont.h b/xfa/fgas/font/cfgas_gefont.h
index 5fe73bb..cc9e56a 100644
--- a/xfa/fgas/font/cfgas_gefont.h
+++ b/xfa/fgas/font/cfgas_gefont.h
@@ -15,12 +15,12 @@
 #include "core/fxcrt/cfx_unowned_ptr.h"
 #include "core/fxcrt/fx_memory.h"
 #include "xfa/fgas/font/cfgas_fontmgr.h"
+#include "xfa/fxfa/cxfa_pdffontmgr.h"
 
 #define FXFONT_SUBST_ITALIC 0x02
 
 class CFGAS_FontMgr;
 class CFX_UnicodeEncoding;
-class CXFA_PDFFontMgr;
 
 class CFGAS_GEFont : public CFX_Retainable {
  public:
@@ -50,7 +50,9 @@
   bool GetBBox(CFX_Rect* bbox);
   CFX_RetainPtr<CFGAS_GEFont> GetSubstFont(int32_t iGlyphIndex);
   CFX_Font* GetDevFont() const { return m_pFont; }
-  void SetFontProvider(CXFA_PDFFontMgr* pProvider) { m_pProvider = pProvider; }
+  void SetFontProvider(CXFA_PDFFontMgr* pProvider) {
+    m_pProvider.Reset(pProvider);
+  }
 #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
   void SetLogicalFontStyle(uint32_t dwLogFontStyle) {
     m_bUseLogFontStyle = true;
@@ -94,15 +96,15 @@
   uint32_t m_dwLogFontStyle;
 #endif
   CFX_Font* m_pFont;
-  CFX_RetainPtr<CFGAS_GEFont> m_pSrcFont;  // Only set by ctor, so no cycles.
-  CFGAS_FontMgr* const m_pFontMgr;
   bool m_bExternalFont;
+  CFX_RetainPtr<CFGAS_GEFont> m_pSrcFont;  // Only set by ctor, so no cycles.
+  CFGAS_FontMgr::ObservedPtr m_pFontMgr;
+  CXFA_PDFFontMgr::ObservedPtr m_pProvider;
   CFX_RetainPtr<CFX_SeekableStreamProxy> m_pStream;
   CFX_RetainPtr<IFX_SeekableReadStream> m_pFileRead;
   std::unique_ptr<CFX_UnicodeEncoding> m_pFontEncoding;
   std::map<wchar_t, int32_t> m_CharWidthMap;
   std::map<wchar_t, CFX_Rect> m_BBoxMap;
-  CFX_UnownedPtr<CXFA_PDFFontMgr> m_pProvider;
   std::vector<CFX_RetainPtr<CFGAS_GEFont>> m_SubstFonts;
   std::map<wchar_t, CFX_RetainPtr<CFGAS_GEFont>> m_FontMapper;
 };
diff --git a/xfa/fxfa/cxfa_pdffontmgr.cpp b/xfa/fxfa/cxfa_pdffontmgr.cpp
index 9e2cf68..77c4433 100644
--- a/xfa/fxfa/cxfa_pdffontmgr.cpp
+++ b/xfa/fxfa/cxfa_pdffontmgr.cpp
@@ -9,6 +9,7 @@
 #include <algorithm>
 
 #include "core/fpdfapi/font/cpdf_font.h"
+#include "xfa/fgas/font/cfgas_gefont.h"
 #include "xfa/fxfa/cxfa_ffapp.h"
 
 namespace {
diff --git a/xfa/fxfa/cxfa_pdffontmgr.h b/xfa/fxfa/cxfa_pdffontmgr.h
index 1188ce1..d0a8240 100644
--- a/xfa/fxfa/cxfa_pdffontmgr.h
+++ b/xfa/fxfa/cxfa_pdffontmgr.h
@@ -11,15 +11,16 @@
 
 #include "core/fpdfapi/parser/cpdf_dictionary.h"
 #include "core/fpdfapi/parser/cpdf_document.h"
+#include "core/fxcrt/cfx_observable.h"
 #include "core/fxcrt/cfx_retain_ptr.h"
 #include "core/fxcrt/fx_string.h"
-#include "xfa/fgas/font/cfgas_gefont.h"
 #include "xfa/fxfa/cxfa_ffdoc.h"
 
+class CFGAS_GEFont;
 class CPDF_Font;
 class CXFA_FFDoc;
 
-class CXFA_PDFFontMgr {
+class CXFA_PDFFontMgr : public CFX_Observable<CXFA_PDFFontMgr> {
  public:
   explicit CXFA_PDFFontMgr(CXFA_FFDoc* pDoc);
   ~CXFA_PDFFontMgr();