Remove const from public fpdfview types. They contain (or are aliases for) parser objects, which are now refcounted, and order to manipulate the internal refcounts, non-const objects must be used. This avoids a number of const casts throughout the codebase. Regrettably, there is some loss of const as a result. This CL must happen first before we can use RetainPtr in a number of these classes. Change-Id: I0e894b56373ad35c68daf4499255be582699378d Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/57853 Reviewed-by: Lei Zhang <thestig@chromium.org> Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfdoc/cpdf_aaction.cpp b/core/fpdfdoc/cpdf_aaction.cpp index 8284913..905be7c 100644 --- a/core/fpdfdoc/cpdf_aaction.cpp +++ b/core/fpdfdoc/cpdf_aaction.cpp
@@ -41,7 +41,7 @@ } // namespace -CPDF_AAction::CPDF_AAction(const CPDF_Dictionary* pDict) : m_pDict(pDict) {} +CPDF_AAction::CPDF_AAction(CPDF_Dictionary* pDict) : m_pDict(pDict) {} CPDF_AAction::CPDF_AAction(const CPDF_AAction& that) = default;
diff --git a/core/fpdfdoc/cpdf_aaction.h b/core/fpdfdoc/cpdf_aaction.h index edd3539..699d913 100644 --- a/core/fpdfdoc/cpdf_aaction.h +++ b/core/fpdfdoc/cpdf_aaction.h
@@ -39,7 +39,7 @@ kNumberOfActions // Must be last. }; - explicit CPDF_AAction(const CPDF_Dictionary* pDict); + explicit CPDF_AAction(CPDF_Dictionary* pDict); CPDF_AAction(const CPDF_AAction& that); ~CPDF_AAction(); @@ -50,7 +50,7 @@ static bool IsUserClick(AActionType eType); private: - UnownedPtr<const CPDF_Dictionary> const m_pDict; + UnownedPtr<CPDF_Dictionary> const m_pDict; }; #endif // CORE_FPDFDOC_CPDF_AACTION_H_
diff --git a/core/fpdfdoc/cpdf_action.cpp b/core/fpdfdoc/cpdf_action.cpp index 18e4927..dc4f485 100644 --- a/core/fpdfdoc/cpdf_action.cpp +++ b/core/fpdfdoc/cpdf_action.cpp
@@ -23,7 +23,7 @@ } // namespace -CPDF_Action::CPDF_Action(const CPDF_Dictionary* pDict) : m_pDict(pDict) {} +CPDF_Action::CPDF_Action(CPDF_Dictionary* pDict) : m_pDict(pDict) {} CPDF_Action::CPDF_Action(const CPDF_Action& that) = default; @@ -49,14 +49,14 @@ if (type != GoTo && type != GoToR) return CPDF_Dest(); - const CPDF_Object* pDest = m_pDict->GetDirectObjectFor("D"); + CPDF_Object* pDest = m_pDict->GetDirectObjectFor("D"); if (!pDest) return CPDF_Dest(); if (pDest->IsString() || pDest->IsName()) { CPDF_NameTree name_tree(pDoc, "Dests"); return CPDF_Dest(name_tree.LookupNamedDest(pDoc, pDest->GetUnicodeText())); } - if (const CPDF_Array* pArray = pDest->AsArray()) + if (CPDF_Array* pArray = pDest->AsArray()) return CPDF_Dest(pArray); return CPDF_Dest(); @@ -141,10 +141,10 @@ if (!m_pDict || !m_pDict->KeyExist("Next")) return CPDF_Action(nullptr); - const CPDF_Object* pNext = m_pDict->GetDirectObjectFor("Next"); - if (const CPDF_Array* pArray = ToArray(pNext)) + CPDF_Object* pNext = m_pDict->GetDirectObjectFor("Next"); + if (CPDF_Array* pArray = ToArray(pNext)) return CPDF_Action(pArray->GetDictAt(iIndex)); - if (const CPDF_Dictionary* pDict = ToDictionary(pNext)) { + if (CPDF_Dictionary* pDict = ToDictionary(pNext)) { if (iIndex == 0) return CPDF_Action(pDict); }
diff --git a/core/fpdfdoc/cpdf_action.h b/core/fpdfdoc/cpdf_action.h index f0f8679..6ea1c63 100644 --- a/core/fpdfdoc/cpdf_action.h +++ b/core/fpdfdoc/cpdf_action.h
@@ -37,11 +37,13 @@ GoTo3DView }; - explicit CPDF_Action(const CPDF_Dictionary* pDict); + explicit CPDF_Action(CPDF_Dictionary* pDict); CPDF_Action(const CPDF_Action& that); ~CPDF_Action(); + CPDF_Dictionary* GetDict() { return m_pDict.Get(); } const CPDF_Dictionary* GetDict() const { return m_pDict.Get(); } + ActionType GetType() const; CPDF_Dest GetDest(CPDF_Document* pDoc) const; WideString GetFilePath() const; @@ -54,7 +56,7 @@ CPDF_Action GetSubAction(size_t iIndex) const; private: - UnownedPtr<const CPDF_Dictionary> const m_pDict; + UnownedPtr<CPDF_Dictionary> const m_pDict; }; #endif // CORE_FPDFDOC_CPDF_ACTION_H_
diff --git a/core/fpdfdoc/cpdf_bookmark.cpp b/core/fpdfdoc/cpdf_bookmark.cpp index 7f7958b..3a414ff 100644 --- a/core/fpdfdoc/cpdf_bookmark.cpp +++ b/core/fpdfdoc/cpdf_bookmark.cpp
@@ -19,7 +19,7 @@ CPDF_Bookmark::CPDF_Bookmark(const CPDF_Bookmark& that) = default; -CPDF_Bookmark::CPDF_Bookmark(const CPDF_Dictionary* pDict) : m_pDict(pDict) {} +CPDF_Bookmark::CPDF_Bookmark(CPDF_Dictionary* pDict) : m_pDict(pDict) {} CPDF_Bookmark::~CPDF_Bookmark() = default; @@ -52,7 +52,7 @@ if (!m_pDict) return CPDF_Dest(); - const CPDF_Object* pDest = m_pDict->GetDirectObjectFor("Dest"); + CPDF_Object* pDest = m_pDict->GetDirectObjectFor("Dest"); if (!pDest) return CPDF_Dest(); if (pDest->IsString() || pDest->IsName()) { @@ -60,7 +60,7 @@ return CPDF_Dest( name_tree.LookupNamedDest(pDocument, pDest->GetUnicodeText())); } - if (const CPDF_Array* pArray = pDest->AsArray()) + if (CPDF_Array* pArray = pDest->AsArray()) return CPDF_Dest(pArray); return CPDF_Dest(); }
diff --git a/core/fpdfdoc/cpdf_bookmark.h b/core/fpdfdoc/cpdf_bookmark.h index 47fd071..1081867 100644 --- a/core/fpdfdoc/cpdf_bookmark.h +++ b/core/fpdfdoc/cpdf_bookmark.h
@@ -19,17 +19,19 @@ public: CPDF_Bookmark(); CPDF_Bookmark(const CPDF_Bookmark& that); - explicit CPDF_Bookmark(const CPDF_Dictionary* pDict); + explicit CPDF_Bookmark(CPDF_Dictionary* pDict); ~CPDF_Bookmark(); + CPDF_Dictionary* GetDict() { return m_pDict.Get(); } const CPDF_Dictionary* GetDict() const { return m_pDict.Get(); } + uint32_t GetFontStyle() const; WideString GetTitle() const; CPDF_Dest GetDest(CPDF_Document* pDocument) const; CPDF_Action GetAction() const; private: - UnownedPtr<const CPDF_Dictionary> m_pDict; + UnownedPtr<CPDF_Dictionary> m_pDict; }; #endif // CORE_FPDFDOC_CPDF_BOOKMARK_H_
diff --git a/core/fpdfdoc/cpdf_bookmarktree.cpp b/core/fpdfdoc/cpdf_bookmarktree.cpp index fc72db9..ce7cfef 100644 --- a/core/fpdfdoc/cpdf_bookmarktree.cpp +++ b/core/fpdfdoc/cpdf_bookmarktree.cpp
@@ -13,27 +13,25 @@ CPDF_BookmarkTree::~CPDF_BookmarkTree() = default; -CPDF_Bookmark CPDF_BookmarkTree::GetFirstChild( - const CPDF_Bookmark& parent) const { - const CPDF_Dictionary* pParentDict = parent.GetDict(); +CPDF_Bookmark CPDF_BookmarkTree::GetFirstChild(CPDF_Bookmark* parent) const { + CPDF_Dictionary* pParentDict = parent->GetDict(); if (pParentDict) return CPDF_Bookmark(pParentDict->GetDictFor("First")); - const CPDF_Dictionary* pRoot = m_pDocument->GetRoot(); + CPDF_Dictionary* pRoot = m_pDocument->GetRoot(); if (!pRoot) return CPDF_Bookmark(); - const CPDF_Dictionary* pOutlines = pRoot->GetDictFor("Outlines"); + CPDF_Dictionary* pOutlines = pRoot->GetDictFor("Outlines"); return pOutlines ? CPDF_Bookmark(pOutlines->GetDictFor("First")) : CPDF_Bookmark(); } -CPDF_Bookmark CPDF_BookmarkTree::GetNextSibling( - const CPDF_Bookmark& bookmark) const { - const CPDF_Dictionary* pDict = bookmark.GetDict(); +CPDF_Bookmark CPDF_BookmarkTree::GetNextSibling(CPDF_Bookmark* bookmark) const { + CPDF_Dictionary* pDict = bookmark->GetDict(); if (!pDict) return CPDF_Bookmark(); - const CPDF_Dictionary* pNext = pDict->GetDictFor("Next"); + CPDF_Dictionary* pNext = pDict->GetDictFor("Next"); return pNext == pDict ? CPDF_Bookmark() : CPDF_Bookmark(pNext); }
diff --git a/core/fpdfdoc/cpdf_bookmarktree.h b/core/fpdfdoc/cpdf_bookmarktree.h index 3729dfc..b374bfa 100644 --- a/core/fpdfdoc/cpdf_bookmarktree.h +++ b/core/fpdfdoc/cpdf_bookmarktree.h
@@ -17,8 +17,8 @@ explicit CPDF_BookmarkTree(CPDF_Document* pDoc); ~CPDF_BookmarkTree(); - CPDF_Bookmark GetFirstChild(const CPDF_Bookmark& parent) const; - CPDF_Bookmark GetNextSibling(const CPDF_Bookmark& bookmark) const; + CPDF_Bookmark GetFirstChild(CPDF_Bookmark* parent) const; + CPDF_Bookmark GetNextSibling(CPDF_Bookmark* bookmark) const; CPDF_Document* GetDocument() const { return m_pDocument.Get(); } private:
diff --git a/core/fpdfdoc/cpdf_dest.cpp b/core/fpdfdoc/cpdf_dest.cpp index 1a373b6..511081f 100644 --- a/core/fpdfdoc/cpdf_dest.cpp +++ b/core/fpdfdoc/cpdf_dest.cpp
@@ -32,9 +32,9 @@ CPDF_Dest::CPDF_Dest() {} -CPDF_Dest::CPDF_Dest(const CPDF_Dest& that) = default; +CPDF_Dest::CPDF_Dest(CPDF_Array* pArray) : m_pArray(pArray) {} -CPDF_Dest::CPDF_Dest(const CPDF_Array* pArray) : m_pArray(pArray) {} +CPDF_Dest::CPDF_Dest(const CPDF_Dest& that) = default; CPDF_Dest::~CPDF_Dest() {}
diff --git a/core/fpdfdoc/cpdf_dest.h b/core/fpdfdoc/cpdf_dest.h index e6e673e..4d9bfb8 100644 --- a/core/fpdfdoc/cpdf_dest.h +++ b/core/fpdfdoc/cpdf_dest.h
@@ -17,11 +17,13 @@ class CPDF_Dest { public: CPDF_Dest(); + explicit CPDF_Dest(CPDF_Array* pArray); CPDF_Dest(const CPDF_Dest& that); - explicit CPDF_Dest(const CPDF_Array* pArray); ~CPDF_Dest(); + CPDF_Array* GetArray() { return m_pArray.Get(); } const CPDF_Array* GetArray() const { return m_pArray.Get(); } + ByteString GetRemoteName() const; int GetDestPageIndex(CPDF_Document* pDoc) const; @@ -39,7 +41,7 @@ float* pZoom) const; private: - UnownedPtr<const CPDF_Array> const m_pArray; + UnownedPtr<CPDF_Array> const m_pArray; }; #endif // CORE_FPDFDOC_CPDF_DEST_H_
diff --git a/core/fpdfdoc/cpdf_formfield.cpp b/core/fpdfdoc/cpdf_formfield.cpp index 0322faf..b8d01f8 100644 --- a/core/fpdfdoc/cpdf_formfield.cpp +++ b/core/fpdfdoc/cpdf_formfield.cpp
@@ -298,7 +298,7 @@ } CPDF_AAction CPDF_FormField::GetAdditionalAction() const { - const CPDF_Object* pObj = + CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict.Get(), pdfium::form_fields::kAA); return CPDF_AAction(pObj ? pObj->GetDict() : nullptr); }
diff --git a/core/fpdfdoc/cpdf_viewerpreferences.cpp b/core/fpdfdoc/cpdf_viewerpreferences.cpp index 0481d5c..9e63d37 100644 --- a/core/fpdfdoc/cpdf_viewerpreferences.cpp +++ b/core/fpdfdoc/cpdf_viewerpreferences.cpp
@@ -30,8 +30,8 @@ return pDict ? pDict->GetIntegerFor("NumCopies") : 1; } -const CPDF_Array* CPDF_ViewerPreferences::PrintPageRange() const { - const CPDF_Dictionary* pDict = GetViewerPreferences(); +CPDF_Array* CPDF_ViewerPreferences::PrintPageRange() const { + CPDF_Dictionary* pDict = GetViewerPreferences(); return pDict ? pDict->GetArrayFor("PrintPageRange") : nullptr; } @@ -53,7 +53,7 @@ return pName->GetString(); } -const CPDF_Dictionary* CPDF_ViewerPreferences::GetViewerPreferences() const { - const CPDF_Dictionary* pDict = m_pDoc->GetRoot(); +CPDF_Dictionary* CPDF_ViewerPreferences::GetViewerPreferences() const { + CPDF_Dictionary* pDict = m_pDoc->GetRoot(); return pDict ? pDict->GetDictFor("ViewerPreferences") : nullptr; }
diff --git a/core/fpdfdoc/cpdf_viewerpreferences.h b/core/fpdfdoc/cpdf_viewerpreferences.h index 45e4390..ff2b1c8 100644 --- a/core/fpdfdoc/cpdf_viewerpreferences.h +++ b/core/fpdfdoc/cpdf_viewerpreferences.h
@@ -24,14 +24,14 @@ bool IsDirectionR2L() const; bool PrintScaling() const; int32_t NumCopies() const; - const CPDF_Array* PrintPageRange() const; + CPDF_Array* PrintPageRange() const; ByteString Duplex() const; // Gets the entry for |bsKey|. Optional<ByteString> GenericName(const ByteString& bsKey) const; private: - const CPDF_Dictionary* GetViewerPreferences() const; + CPDF_Dictionary* GetViewerPreferences() const; UnownedPtr<const CPDF_Document> const m_pDoc; };
diff --git a/fpdfsdk/cpdfsdk_formfillenvironment.cpp b/fpdfsdk/cpdfsdk_formfillenvironment.cpp index 94bd808..9a5e726 100644 --- a/fpdfsdk/cpdfsdk_formfillenvironment.cpp +++ b/fpdfsdk/cpdfsdk_formfillenvironment.cpp
@@ -576,11 +576,11 @@ if (!m_pCPDFDoc) return false; - const CPDF_Dictionary* pRoot = m_pCPDFDoc->GetRoot(); + CPDF_Dictionary* pRoot = m_pCPDFDoc->GetRoot(); if (!pRoot) return false; - const CPDF_Object* pOpenAction = pRoot->GetDictFor("OpenAction"); + CPDF_Object* pOpenAction = pRoot->GetDictFor("OpenAction"); if (!pOpenAction) pOpenAction = pRoot->GetArrayFor("OpenAction"); if (!pOpenAction) @@ -589,7 +589,7 @@ if (pOpenAction->IsArray()) return true; - const CPDF_Dictionary* pDict = pOpenAction->AsDictionary(); + CPDF_Dictionary* pDict = pOpenAction->AsDictionary(); if (!pDict) return false;
diff --git a/fpdfsdk/cpdfsdk_helpers.h b/fpdfsdk/cpdfsdk_helpers.h index 97cebfd..5d9522a 100644 --- a/fpdfsdk/cpdfsdk_helpers.h +++ b/fpdfsdk/cpdfsdk_helpers.h
@@ -32,7 +32,6 @@ class CPDF_LinkExtract; class CPDF_PageObject; class CPDF_PageRenderContext; -class CPDF_PathObject; class CPDF_Stream; class CPDF_StructElement; class CPDF_StructTree; @@ -57,11 +56,11 @@ CPDF_Document* CPDFDocumentFromFPDFDocument(FPDF_DOCUMENT doc); // Conversions to/from incomplete FPDF_ API types. -inline FPDF_ACTION FPDFActionFromCPDFDictionary(const CPDF_Dictionary* action) { +inline FPDF_ACTION FPDFActionFromCPDFDictionary(CPDF_Dictionary* action) { return reinterpret_cast<FPDF_ACTION>(action); } -inline const CPDF_Dictionary* CPDFDictionaryFromFPDFAction(FPDF_ACTION action) { - return reinterpret_cast<const CPDF_Dictionary*>(action); +inline CPDF_Dictionary* CPDFDictionaryFromFPDFAction(FPDF_ACTION action) { + return reinterpret_cast<CPDF_Dictionary*>(action); } inline FPDF_ANNOTATION FPDFAnnotationFromCPDFAnnotContext( @@ -87,13 +86,11 @@ return reinterpret_cast<CFX_DIBitmap*>(bitmap); } -inline FPDF_BOOKMARK FPDFBookmarkFromCPDFDictionary( - const CPDF_Dictionary* bookmark) { +inline FPDF_BOOKMARK FPDFBookmarkFromCPDFDictionary(CPDF_Dictionary* bookmark) { return reinterpret_cast<FPDF_BOOKMARK>(bookmark); } -inline const CPDF_Dictionary* CPDFDictionaryFromFPDFBookmark( - FPDF_BOOKMARK bookmark) { - return reinterpret_cast<const CPDF_Dictionary*>(bookmark); +inline CPDF_Dictionary* CPDFDictionaryFromFPDFBookmark(FPDF_BOOKMARK bookmark) { + return reinterpret_cast<CPDF_Dictionary*>(bookmark); } inline FPDF_CLIPPATH FPDFClipPathFromCPDFClipPath(CPDF_ClipPath* path) { @@ -103,11 +100,11 @@ return reinterpret_cast<CPDF_ClipPath*>(path); } -inline FPDF_DEST FPDFDestFromCPDFArray(const CPDF_Array* dest) { +inline FPDF_DEST FPDFDestFromCPDFArray(CPDF_Array* dest) { return reinterpret_cast<FPDF_DEST>(dest); } -inline const CPDF_Array* CPDFArrayFromFPDFDest(FPDF_DEST dest) { - return reinterpret_cast<const CPDF_Array*>(dest); +inline CPDF_Array* CPDFArrayFromFPDFDest(FPDF_DEST dest) { + return reinterpret_cast<CPDF_Array*>(dest); } inline FPDF_FONT FPDFFontFromCPDFFont(CPDF_Font* font) { @@ -149,11 +146,11 @@ return reinterpret_cast<CPDF_ContentMarkItem*>(mark); } -inline FPDF_PAGERANGE FPDFPageRangeFromCPDFArray(const CPDF_Array* range) { +inline FPDF_PAGERANGE FPDFPageRangeFromCPDFArray(CPDF_Array* range) { return reinterpret_cast<FPDF_PAGERANGE>(range); } -inline const CPDF_Array* CPDFArrayFromFPDFPageRange(FPDF_PAGERANGE range) { - return reinterpret_cast<const CPDF_Array*>(range); +inline CPDF_Array* CPDFArrayFromFPDFPageRange(FPDF_PAGERANGE range) { + return reinterpret_cast<CPDF_Array*>(range); } inline FPDF_PATHSEGMENT FPDFPathSegmentFromFXPathPoint(
diff --git a/fpdfsdk/fpdf_doc.cpp b/fpdfsdk/fpdf_doc.cpp index 6552b8a..b9bf93d 100644 --- a/fpdfsdk/fpdf_doc.cpp +++ b/fpdfsdk/fpdf_doc.cpp
@@ -42,13 +42,13 @@ } // Go into children items. - CPDF_Bookmark child = tree.GetFirstChild(bookmark); + CPDF_Bookmark child = tree.GetFirstChild(&bookmark); while (child.GetDict() && !pdfium::ContainsKey(*visited, child.GetDict())) { // Check this item and its children. CPDF_Bookmark found = FindBookmark(tree, child, title, visited); if (found.GetDict()) return found; - child = tree.GetNextSibling(child); + child = tree.GetNextSibling(&child); } return CPDF_Bookmark(); } @@ -75,7 +75,7 @@ CPDF_BookmarkTree tree(pDoc); CPDF_Bookmark cBookmark(CPDFDictionaryFromFPDFBookmark(bookmark)); return FPDFBookmarkFromCPDFDictionary( - tree.GetFirstChild(cBookmark).GetDict()); + tree.GetFirstChild(&cBookmark).GetDict()); } FPDF_EXPORT FPDF_BOOKMARK FPDF_CALLCONV @@ -90,7 +90,7 @@ CPDF_BookmarkTree tree(pDoc); CPDF_Bookmark cBookmark(CPDFDictionaryFromFPDFBookmark(bookmark)); return FPDFBookmarkFromCPDFDictionary( - tree.GetNextSibling(cBookmark).GetDict()); + tree.GetNextSibling(&cBookmark).GetDict()); } FPDF_EXPORT unsigned long FPDF_CALLCONV
diff --git a/fpdfsdk/fpdf_formfill.cpp b/fpdfsdk/fpdf_formfill.cpp index 3a53dcd..5293f05 100644 --- a/fpdfsdk/fpdf_formfill.cpp +++ b/fpdfsdk/fpdf_formfill.cpp
@@ -683,7 +683,7 @@ return; CPDF_Document* pDoc = pFormFillEnv->GetPDFDocument(); - const CPDF_Dictionary* pDict = pDoc->GetRoot(); + CPDF_Dictionary* pDict = pDoc->GetRoot(); if (!pDict) return;
diff --git a/public/fpdfview.h b/public/fpdfview.h index f521259..6bbc2bf 100644 --- a/public/fpdfview.h +++ b/public/fpdfview.h
@@ -34,14 +34,14 @@ #define FPDF_OBJECT_NULLOBJ 8 #define FPDF_OBJECT_REFERENCE 9 -// PDF types - use incomplete types for type safety. -typedef const struct fpdf_action_t__* FPDF_ACTION; +// PDF types - use incomplete types (never completed) just for API type safety. +typedef struct fpdf_action_t__* FPDF_ACTION; typedef struct fpdf_annotation_t__* FPDF_ANNOTATION; typedef struct fpdf_attachment_t__* FPDF_ATTACHMENT; typedef struct fpdf_bitmap_t__* FPDF_BITMAP; -typedef const struct fpdf_bookmark_t__* FPDF_BOOKMARK; +typedef struct fpdf_bookmark_t__* FPDF_BOOKMARK; typedef struct fpdf_clippath_t__* FPDF_CLIPPATH; -typedef const struct fpdf_dest_t__* FPDF_DEST; +typedef struct fpdf_dest_t__* FPDF_DEST; typedef struct fpdf_document_t__* FPDF_DOCUMENT; typedef struct fpdf_font_t__* FPDF_FONT; typedef struct fpdf_form_handle_t__* FPDF_FORMHANDLE; @@ -50,7 +50,7 @@ typedef struct fpdf_pagelink_t__* FPDF_PAGELINK; typedef struct fpdf_pageobject_t__* FPDF_PAGEOBJECT; // (text, path, etc.) typedef struct fpdf_pageobjectmark_t__* FPDF_PAGEOBJECTMARK; -typedef const struct fpdf_pagerange_t__* FPDF_PAGERANGE; +typedef struct fpdf_pagerange_t__* FPDF_PAGERANGE; typedef const struct fpdf_pathsegment_t* FPDF_PATHSEGMENT; typedef void* FPDF_RECORDER; // Passed into skia. typedef struct fpdf_schhandle_t__* FPDF_SCHHANDLE;