dsinclair | 02e6f59 | 2016-08-02 11:25:48 -0700 | [diff] [blame] | 1 | // Copyright 2016 PDFium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
dsinclair | 1727aee | 2016-09-29 13:12:56 -0700 | [diff] [blame] | 7 | #include "core/fpdfdoc/cpdf_action.h" |
dsinclair | 02e6f59 | 2016-08-02 11:25:48 -0700 | [diff] [blame] | 8 | |
Lei Zhang | 2617056 | 2018-04-17 17:01:52 +0000 | [diff] [blame^] | 9 | #include "constants/stream_dict_common.h" |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 10 | #include "core/fpdfapi/parser/cpdf_array.h" |
| 11 | #include "core/fpdfapi/parser/cpdf_document.h" |
dsinclair | 1727aee | 2016-09-29 13:12:56 -0700 | [diff] [blame] | 12 | #include "core/fpdfdoc/cpdf_filespec.h" |
| 13 | #include "core/fpdfdoc/cpdf_nametree.h" |
dsinclair | 02e6f59 | 2016-08-02 11:25:48 -0700 | [diff] [blame] | 14 | |
| 15 | namespace { |
| 16 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 17 | const char* const g_sATypes[] = { |
dsinclair | 02e6f59 | 2016-08-02 11:25:48 -0700 | [diff] [blame] | 18 | "Unknown", "GoTo", "GoToR", "GoToE", "Launch", |
| 19 | "Thread", "URI", "Sound", "Movie", "Hide", |
| 20 | "Named", "SubmitForm", "ResetForm", "ImportData", "JavaScript", |
| 21 | "SetOCGState", "Rendition", "Trans", "GoTo3DView", nullptr}; |
| 22 | |
| 23 | } // namespace |
| 24 | |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 25 | CPDF_Action::CPDF_Action(CPDF_Dictionary* pDict) : m_pDict(pDict) {} |
| 26 | |
| 27 | CPDF_Action::CPDF_Action(const CPDF_Action& that) = default; |
| 28 | |
| 29 | CPDF_Action::~CPDF_Action() {} |
| 30 | |
dsinclair | 02e6f59 | 2016-08-02 11:25:48 -0700 | [diff] [blame] | 31 | CPDF_Dest CPDF_Action::GetDest(CPDF_Document* pDoc) const { |
| 32 | if (!m_pDict) |
| 33 | return CPDF_Dest(); |
| 34 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 35 | ByteString type = m_pDict->GetStringFor("S"); |
dsinclair | 02e6f59 | 2016-08-02 11:25:48 -0700 | [diff] [blame] | 36 | if (type != "GoTo" && type != "GoToR") |
| 37 | return CPDF_Dest(); |
| 38 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 39 | CPDF_Object* pDest = m_pDict->GetDirectObjectFor("D"); |
dsinclair | 02e6f59 | 2016-08-02 11:25:48 -0700 | [diff] [blame] | 40 | if (!pDest) |
| 41 | return CPDF_Dest(); |
| 42 | if (pDest->IsString() || pDest->IsName()) { |
| 43 | CPDF_NameTree name_tree(pDoc, "Dests"); |
Jane Liu | 67ccef7 | 2017-07-19 13:10:50 -0400 | [diff] [blame] | 44 | return CPDF_Dest(name_tree.LookupNamedDest(pDoc, pDest->GetUnicodeText())); |
dsinclair | 02e6f59 | 2016-08-02 11:25:48 -0700 | [diff] [blame] | 45 | } |
| 46 | if (CPDF_Array* pArray = pDest->AsArray()) |
| 47 | return CPDF_Dest(pArray); |
| 48 | |
| 49 | return CPDF_Dest(); |
| 50 | } |
| 51 | |
| 52 | CPDF_Action::ActionType CPDF_Action::GetType() const { |
| 53 | if (!m_pDict) |
| 54 | return Unknown; |
| 55 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 56 | ByteString csType = m_pDict->GetStringFor("S"); |
dsinclair | 02e6f59 | 2016-08-02 11:25:48 -0700 | [diff] [blame] | 57 | if (csType.IsEmpty()) |
| 58 | return Unknown; |
| 59 | |
| 60 | for (int i = 0; g_sATypes[i]; ++i) { |
| 61 | if (csType == g_sATypes[i]) |
| 62 | return static_cast<ActionType>(i); |
| 63 | } |
| 64 | return Unknown; |
| 65 | } |
| 66 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 67 | WideString CPDF_Action::GetFilePath() const { |
| 68 | ByteString type = m_pDict->GetStringFor("S"); |
dsinclair | 02e6f59 | 2016-08-02 11:25:48 -0700 | [diff] [blame] | 69 | if (type != "GoToR" && type != "Launch" && type != "SubmitForm" && |
| 70 | type != "ImportData") { |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 71 | return WideString(); |
dsinclair | 02e6f59 | 2016-08-02 11:25:48 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Lei Zhang | 2617056 | 2018-04-17 17:01:52 +0000 | [diff] [blame^] | 74 | CPDF_Object* pFile = m_pDict->GetDirectObjectFor(pdfium::stream::kF); |
Jane Liu | c8a17e5 | 2017-07-13 10:37:59 -0400 | [diff] [blame] | 75 | if (pFile) |
| 76 | return CPDF_FileSpec(pFile).GetFileName(); |
dsinclair | 02e6f59 | 2016-08-02 11:25:48 -0700 | [diff] [blame] | 77 | |
Jane Liu | c8a17e5 | 2017-07-13 10:37:59 -0400 | [diff] [blame] | 78 | if (type == "Launch") { |
| 79 | CPDF_Dictionary* pWinDict = m_pDict->GetDictFor("Win"); |
| 80 | if (pWinDict) { |
Lei Zhang | 2617056 | 2018-04-17 17:01:52 +0000 | [diff] [blame^] | 81 | return WideString::FromLocal( |
| 82 | pWinDict->GetStringFor(pdfium::stream::kF).AsStringView()); |
Jane Liu | c8a17e5 | 2017-07-13 10:37:59 -0400 | [diff] [blame] | 83 | } |
| 84 | } |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 85 | return WideString(); |
dsinclair | 02e6f59 | 2016-08-02 11:25:48 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 88 | ByteString CPDF_Action::GetURI(const CPDF_Document* pDoc) const { |
| 89 | ByteString csURI; |
dsinclair | 02e6f59 | 2016-08-02 11:25:48 -0700 | [diff] [blame] | 90 | if (!m_pDict) |
| 91 | return csURI; |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 92 | if (m_pDict->GetStringFor("S") != "URI") |
dsinclair | 02e6f59 | 2016-08-02 11:25:48 -0700 | [diff] [blame] | 93 | return csURI; |
| 94 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 95 | csURI = m_pDict->GetStringFor("URI"); |
Lei Zhang | 0158106 | 2017-08-30 14:19:26 -0700 | [diff] [blame] | 96 | const CPDF_Dictionary* pRoot = pDoc->GetRoot(); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 97 | CPDF_Dictionary* pURI = pRoot->GetDictFor("URI"); |
dsinclair | 02e6f59 | 2016-08-02 11:25:48 -0700 | [diff] [blame] | 98 | if (pURI) { |
Ryan Harrison | 12db751 | 2017-08-23 10:39:35 -0400 | [diff] [blame] | 99 | auto result = csURI.Find(":"); |
| 100 | if (!result.has_value() || result.value() == 0) |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 101 | csURI = pURI->GetStringFor("Base") + csURI; |
dsinclair | 02e6f59 | 2016-08-02 11:25:48 -0700 | [diff] [blame] | 102 | } |
| 103 | return csURI; |
| 104 | } |
| 105 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 106 | WideString CPDF_Action::GetJavaScript() const { |
| 107 | WideString csJS; |
dsinclair | 02e6f59 | 2016-08-02 11:25:48 -0700 | [diff] [blame] | 108 | if (!m_pDict) |
| 109 | return csJS; |
| 110 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 111 | CPDF_Object* pJS = m_pDict->GetDirectObjectFor("JS"); |
dsinclair | 02e6f59 | 2016-08-02 11:25:48 -0700 | [diff] [blame] | 112 | return pJS ? pJS->GetUnicodeText() : csJS; |
| 113 | } |
| 114 | |
| 115 | size_t CPDF_Action::GetSubActionsCount() const { |
| 116 | if (!m_pDict || !m_pDict->KeyExist("Next")) |
| 117 | return 0; |
| 118 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 119 | CPDF_Object* pNext = m_pDict->GetDirectObjectFor("Next"); |
dsinclair | 02e6f59 | 2016-08-02 11:25:48 -0700 | [diff] [blame] | 120 | if (!pNext) |
| 121 | return 0; |
| 122 | if (pNext->IsDictionary()) |
| 123 | return 1; |
| 124 | if (CPDF_Array* pArray = pNext->AsArray()) |
| 125 | return pArray->GetCount(); |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | CPDF_Action CPDF_Action::GetSubAction(size_t iIndex) const { |
| 130 | if (!m_pDict || !m_pDict->KeyExist("Next")) |
Lei Zhang | 4ecf9a8 | 2017-12-11 14:34:58 +0000 | [diff] [blame] | 131 | return CPDF_Action(nullptr); |
dsinclair | 02e6f59 | 2016-08-02 11:25:48 -0700 | [diff] [blame] | 132 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 133 | CPDF_Object* pNext = m_pDict->GetDirectObjectFor("Next"); |
Lei Zhang | 4ecf9a8 | 2017-12-11 14:34:58 +0000 | [diff] [blame] | 134 | if (CPDF_Array* pArray = ToArray(pNext)) |
| 135 | return CPDF_Action(pArray->GetDictAt(iIndex)); |
dsinclair | 02e6f59 | 2016-08-02 11:25:48 -0700 | [diff] [blame] | 136 | if (CPDF_Dictionary* pDict = ToDictionary(pNext)) { |
| 137 | if (iIndex == 0) |
| 138 | return CPDF_Action(pDict); |
dsinclair | 02e6f59 | 2016-08-02 11:25:48 -0700 | [diff] [blame] | 139 | } |
Lei Zhang | 4ecf9a8 | 2017-12-11 14:34:58 +0000 | [diff] [blame] | 140 | return CPDF_Action(nullptr); |
dsinclair | 02e6f59 | 2016-08-02 11:25:48 -0700 | [diff] [blame] | 141 | } |