diff options
author | weili <weili@chromium.org> | 2016-07-20 10:35:31 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-07-20 10:35:31 -0700 |
commit | 47228aceb86744f858ab8bfa98f3f8b62054dfae (patch) | |
tree | d5d8abbc5eaeb23b291cf604bf33c77cf648b0bb | |
parent | 31f8740fe51ceca8e973a2efe40d4d440d7a5cb7 (diff) | |
download | pdfium-47228aceb86744f858ab8bfa98f3f8b62054dfae.tar.xz |
Clean up singleton implementation
Move the singleton instances into their namespaces, and use
get()/getInstance() for uniform accesses.
Review-Url: https://codereview.chromium.org/2154843002
-rw-r--r-- | BUILD.gn | 4 | ||||
-rw-r--r-- | core/fpdfapi/cpdf_modulemgr.cpp | 16 | ||||
-rw-r--r-- | core/fxge/ge/fx_ge.cpp | 34 | ||||
-rw-r--r-- | core/fxge/include/fx_ge.h | 7 | ||||
-rw-r--r-- | fpdfsdk/fpdfdoc_unittest.cpp | 1 | ||||
-rw-r--r-- | fpdfsdk/fpdfview.cpp | 4 | ||||
-rw-r--r-- | fpdfsdk/fpdfxfa/fpdfxfa_app.cpp | 6 | ||||
-rw-r--r-- | fpdfsdk/fpdfxfa/include/fpdfxfa_app.h | 2 | ||||
-rw-r--r-- | fpdfsdk/javascript/JS_GlobalData.cpp | 145 | ||||
-rw-r--r-- | fpdfsdk/javascript/JS_GlobalData.h | 41 | ||||
-rw-r--r-- | fpdfsdk/javascript/JS_KeyValue.cpp | 70 | ||||
-rw-r--r-- | fpdfsdk/javascript/JS_KeyValue.h | 46 | ||||
-rw-r--r-- | fpdfsdk/javascript/global.cpp | 108 | ||||
-rw-r--r-- | fpdfsdk/javascript/global.h | 5 | ||||
-rw-r--r-- | pdfium.gyp | 2 | ||||
-rw-r--r-- | xfa.gyp | 2 | ||||
-rw-r--r-- | xfa/fwl/theme/cfwl_arrowdata.cpp | 73 | ||||
-rw-r--r-- | xfa/fwl/theme/cfwl_arrowdata.h | 36 | ||||
-rw-r--r-- | xfa/fwl/theme/cfwl_widgettp.cpp | 112 | ||||
-rw-r--r-- | xfa/fwl/theme/cfwl_widgettp.h | 22 |
20 files changed, 412 insertions, 324 deletions
@@ -800,6 +800,8 @@ static_library("javascript") { "fpdfsdk/javascript/JS_EventHandler.h", "fpdfsdk/javascript/JS_GlobalData.cpp", "fpdfsdk/javascript/JS_GlobalData.h", + "fpdfsdk/javascript/JS_KeyValue.cpp", + "fpdfsdk/javascript/JS_KeyValue.h", "fpdfsdk/javascript/JS_Object.cpp", "fpdfsdk/javascript/JS_Object.h", "fpdfsdk/javascript/JS_Value.cpp", @@ -1095,6 +1097,8 @@ if (pdf_enable_xfa) { "xfa/fwl/lightwidget/cfwl_widget.h", "xfa/fwl/lightwidget/cfwl_widgetproperties.cpp", "xfa/fwl/lightwidget/cfwl_widgetproperties.h", + "xfa/fwl/theme/cfwl_arrowdata.cpp", + "xfa/fwl/theme/cfwl_arrowdata.h", "xfa/fwl/theme/cfwl_barcodetp.cpp", "xfa/fwl/theme/cfwl_barcodetp.h", "xfa/fwl/theme/cfwl_carettp.cpp", diff --git a/core/fpdfapi/cpdf_modulemgr.cpp b/core/fpdfapi/cpdf_modulemgr.cpp index fe5368d3f7..454ffcd465 100644 --- a/core/fpdfapi/cpdf_modulemgr.cpp +++ b/core/fpdfapi/cpdf_modulemgr.cpp @@ -11,25 +11,21 @@ namespace { -CPDF_ModuleMgr* g_FPDFAPI_pDefaultMgr = nullptr; +CPDF_ModuleMgr* g_pDefaultMgr = nullptr; } // namespace // static CPDF_ModuleMgr* CPDF_ModuleMgr::Get() { - return g_FPDFAPI_pDefaultMgr; -} - -// static -void CPDF_ModuleMgr::Create() { - ASSERT(!g_FPDFAPI_pDefaultMgr); - g_FPDFAPI_pDefaultMgr = new CPDF_ModuleMgr; + if (!g_pDefaultMgr) + g_pDefaultMgr = new CPDF_ModuleMgr; + return g_pDefaultMgr; } // static void CPDF_ModuleMgr::Destroy() { - delete g_FPDFAPI_pDefaultMgr; - g_FPDFAPI_pDefaultMgr = nullptr; + delete g_pDefaultMgr; + g_pDefaultMgr = nullptr; } CPDF_ModuleMgr::CPDF_ModuleMgr() : m_pCodecModule(nullptr) {} diff --git a/core/fxge/ge/fx_ge.cpp b/core/fxge/ge/fx_ge.cpp index 4ebf812f12..fe0cc2c3eb 100644 --- a/core/fxge/ge/fx_ge.cpp +++ b/core/fxge/ge/fx_ge.cpp @@ -8,19 +8,19 @@ #include "core/fxge/ge/fx_text_int.h" -static CFX_GEModule* g_pGEModule = nullptr; +namespace { -CFX_GEModule::CFX_GEModule(const char** pUserFontPaths, - CCodec_ModuleMgr* pCodecModule) +CFX_GEModule* g_pGEModule = nullptr; + +} // namespace + +CFX_GEModule::CFX_GEModule() : m_FTLibrary(nullptr), m_pFontCache(nullptr), m_pFontMgr(new CFX_FontMgr), - m_pCodecModule(pCodecModule), + m_pCodecModule(nullptr), m_pPlatformData(nullptr), - m_pUserFontPaths(pUserFontPaths) { - InitPlatform(); - SetTextGamma(2.2f); -} + m_pUserFontPaths(nullptr) {} CFX_GEModule::~CFX_GEModule() { delete m_pFontCache; @@ -28,14 +28,9 @@ CFX_GEModule::~CFX_GEModule() { } // static -void CFX_GEModule::Create(const char** userFontPaths, - CCodec_ModuleMgr* pCodecModule) { - ASSERT(!g_pGEModule); - g_pGEModule = new CFX_GEModule(userFontPaths, pCodecModule); -} - -// static CFX_GEModule* CFX_GEModule::Get() { + if (!g_pGEModule) + g_pGEModule = new CFX_GEModule(); return g_pGEModule; } @@ -46,6 +41,15 @@ void CFX_GEModule::Destroy() { g_pGEModule = nullptr; } +void CFX_GEModule::Init(const char** userFontPaths, + CCodec_ModuleMgr* pCodecModule) { + ASSERT(g_pGEModule); + m_pCodecModule = pCodecModule; + m_pUserFontPaths = userFontPaths; + InitPlatform(); + SetTextGamma(2.2f); +} + CFX_FontCache* CFX_GEModule::GetFontCache() { if (!m_pFontCache) m_pFontCache = new CFX_FontCache(); diff --git a/core/fxge/include/fx_ge.h b/core/fxge/include/fx_ge.h index ea2adec1dc..dbc4fd0a43 100644 --- a/core/fxge/include/fx_ge.h +++ b/core/fxge/include/fx_ge.h @@ -23,11 +23,10 @@ class SkPictureRecorder; class CFX_GEModule { public: - static void Create(const char** pUserFontPaths, - CCodec_ModuleMgr* pCodecModule); static CFX_GEModule* Get(); static void Destroy(); + void Init(const char** pUserFontPaths, CCodec_ModuleMgr* pCodecModule); CFX_FontCache* GetFontCache(); CFX_FontMgr* GetFontMgr() { return m_pFontMgr.get(); } void SetTextGamma(FX_FLOAT gammaValue); @@ -39,7 +38,7 @@ class CFX_GEModule { FXFT_Library m_FTLibrary; private: - CFX_GEModule(const char** pUserFontPaths, CCodec_ModuleMgr* pCodecModule); + CFX_GEModule(); ~CFX_GEModule(); void InitPlatform(); @@ -48,7 +47,7 @@ class CFX_GEModule { uint8_t m_GammaValue[256]; CFX_FontCache* m_pFontCache; std::unique_ptr<CFX_FontMgr> m_pFontMgr; - CCodec_ModuleMgr* const m_pCodecModule; + CCodec_ModuleMgr* m_pCodecModule; void* m_pPlatformData; const char** m_pUserFontPaths; }; diff --git a/fpdfsdk/fpdfdoc_unittest.cpp b/fpdfsdk/fpdfdoc_unittest.cpp index e315fd853e..2bd2e586aa 100644 --- a/fpdfsdk/fpdfdoc_unittest.cpp +++ b/fpdfsdk/fpdfdoc_unittest.cpp @@ -57,7 +57,6 @@ class PDFDocTest : public testing::Test { void SetUp() override { // We don't need page module or render module, but // initialize them to keep the code sane. - CPDF_ModuleMgr::Create(); CPDF_ModuleMgr* module_mgr = CPDF_ModuleMgr::Get(); module_mgr->InitPageModule(); diff --git a/fpdfsdk/fpdfview.cpp b/fpdfsdk/fpdfview.cpp index 34e7d23619..f47de1518d 100644 --- a/fpdfsdk/fpdfview.cpp +++ b/fpdfsdk/fpdfview.cpp @@ -262,8 +262,8 @@ DLLEXPORT void STDCALL FPDF_InitLibraryWithConfig(const FPDF_LIBRARY_CONFIG* cfg) { g_pCodecModule = new CCodec_ModuleMgr(); - CFX_GEModule::Create(cfg ? cfg->m_pUserFontPaths : nullptr, g_pCodecModule); - CPDF_ModuleMgr::Create(); + CFX_GEModule* pModule = CFX_GEModule::Get(); + pModule->Init(cfg ? cfg->m_pUserFontPaths : nullptr, g_pCodecModule); CPDF_ModuleMgr* pModuleMgr = CPDF_ModuleMgr::Get(); pModuleMgr->SetCodecModule(g_pCodecModule); pModuleMgr->InitPageModule(); diff --git a/fpdfsdk/fpdfxfa/fpdfxfa_app.cpp b/fpdfsdk/fpdfxfa/fpdfxfa_app.cpp index 5774a54785..22d84f1f9f 100644 --- a/fpdfsdk/fpdfxfa/fpdfxfa_app.cpp +++ b/fpdfsdk/fpdfxfa/fpdfxfa_app.cpp @@ -13,7 +13,11 @@ #include "xfa/fxfa/include/xfa_ffapp.h" #include "xfa/fxfa/include/xfa_fontmgr.h" -CPDFXFA_App* CPDFXFA_App::g_pApp = nullptr; +namespace { + +CPDFXFA_App* g_pApp = nullptr; + +} // namespace CPDFXFA_App* CPDFXFA_App::GetInstance() { if (!g_pApp) { diff --git a/fpdfsdk/fpdfxfa/include/fpdfxfa_app.h b/fpdfsdk/fpdfxfa/include/fpdfxfa_app.h index 7b96e4378f..5ebb2c2623 100644 --- a/fpdfsdk/fpdfxfa/include/fpdfxfa_app.h +++ b/fpdfsdk/fpdfxfa/include/fpdfxfa_app.h @@ -73,8 +73,6 @@ class CPDFXFA_App : public IXFA_AppProvider { CFX_ArrayTemplate<CPDFDoc_Environment*> m_pEnvList; protected: - static CPDFXFA_App* g_pApp; - FX_BOOL m_bJavaScriptInitialized; CXFA_FFApp* m_pXFAApp; v8::Isolate* m_pIsolate; diff --git a/fpdfsdk/javascript/JS_GlobalData.cpp b/fpdfsdk/javascript/JS_GlobalData.cpp index 14c36530d5..d1268b042f 100644 --- a/fpdfsdk/javascript/JS_GlobalData.cpp +++ b/fpdfsdk/javascript/JS_GlobalData.cpp @@ -11,78 +11,13 @@ #define JS_MAXGLOBALDATA (1024 * 4 - 8) -CJS_GlobalVariableArray::CJS_GlobalVariableArray() {} - -CJS_GlobalVariableArray::~CJS_GlobalVariableArray() { - Empty(); -} - -void CJS_GlobalVariableArray::Copy(const CJS_GlobalVariableArray& array) { - Empty(); - for (int i = 0, sz = array.Count(); i < sz; i++) { - CJS_KeyValue* pOldObjData = array.GetAt(i); - switch (pOldObjData->nType) { - case JS_GLOBALDATA_TYPE_NUMBER: { - CJS_KeyValue* pNewObjData = new CJS_KeyValue; - pNewObjData->sKey = pOldObjData->sKey; - pNewObjData->nType = pOldObjData->nType; - pNewObjData->dData = pOldObjData->dData; - Add(pNewObjData); - } break; - case JS_GLOBALDATA_TYPE_BOOLEAN: { - CJS_KeyValue* pNewObjData = new CJS_KeyValue; - pNewObjData->sKey = pOldObjData->sKey; - pNewObjData->nType = pOldObjData->nType; - pNewObjData->bData = pOldObjData->bData; - Add(pNewObjData); - } break; - case JS_GLOBALDATA_TYPE_STRING: { - CJS_KeyValue* pNewObjData = new CJS_KeyValue; - pNewObjData->sKey = pOldObjData->sKey; - pNewObjData->nType = pOldObjData->nType; - pNewObjData->sData = pOldObjData->sData; - Add(pNewObjData); - } break; - case JS_GLOBALDATA_TYPE_OBJECT: { - CJS_KeyValue* pNewObjData = new CJS_KeyValue; - pNewObjData->sKey = pOldObjData->sKey; - pNewObjData->nType = pOldObjData->nType; - pNewObjData->objData.Copy(pOldObjData->objData); - Add(pNewObjData); - } break; - case JS_GLOBALDATA_TYPE_NULL: { - CJS_KeyValue* pNewObjData = new CJS_KeyValue; - pNewObjData->sKey = pOldObjData->sKey; - pNewObjData->nType = pOldObjData->nType; - Add(pNewObjData); - } break; - } - } -} - -void CJS_GlobalVariableArray::Add(CJS_KeyValue* p) { - m_Array.Add(p); -} - -int CJS_GlobalVariableArray::Count() const { - return m_Array.GetSize(); -} - -CJS_KeyValue* CJS_GlobalVariableArray::GetAt(int index) const { - return m_Array.GetAt(index); -} - -void CJS_GlobalVariableArray::Empty() { - for (int i = 0, sz = m_Array.GetSize(); i < sz; i++) - delete m_Array.GetAt(i); - m_Array.RemoveAll(); -} - #define READER_JS_GLOBALDATA_FILENAME L"Reader_JsGlobal.Data" #define PHANTOM_JS_GLOBALDATA_FILENAME L"Phantom_JsGlobal.Data" #define SDK_JS_GLOBALDATA_FILENAME L"SDK_JsGlobal.Data" -static const uint8_t JS_RC4KEY[] = { +namespace { + +const uint8_t JS_RC4KEY[] = { 0x19, 0xa8, 0xe8, 0x01, 0xf6, 0xa8, 0xb6, 0x4d, 0x82, 0x04, 0x45, 0x6d, 0xb4, 0xcf, 0xd7, 0x77, 0x67, 0xf9, 0x75, 0x9f, 0xf0, 0xe0, 0x1e, 0x51, 0xee, 0x46, 0xfd, 0x0b, 0xc9, 0x93, 0x25, 0x55, 0x4a, 0xee, 0xe0, 0x16, @@ -94,27 +29,29 @@ static const uint8_t JS_RC4KEY[] = { 0xf8, 0x77, 0xd5, 0xa3}; // Returns true if non-empty, setting sPropName -static bool TrimPropName(CFX_ByteString* sPropName) { +bool TrimPropName(CFX_ByteString* sPropName) { sPropName->TrimLeft(); sPropName->TrimRight(); return sPropName->GetLength() != 0; } -CJS_GlobalData* CJS_GlobalData::g_Instance = nullptr; +CJS_GlobalData* g_pInstance = nullptr; + +} // namespace // static CJS_GlobalData* CJS_GlobalData::GetRetainedInstance(CPDFDoc_Environment* pApp) { - if (!g_Instance) { - g_Instance = new CJS_GlobalData(); + if (!g_pInstance) { + g_pInstance = new CJS_GlobalData(); } - ++g_Instance->m_RefCount; - return g_Instance; + ++g_pInstance->m_RefCount; + return g_pInstance; } void CJS_GlobalData::Release() { if (!--m_RefCount) { - delete g_Instance; - g_Instance = nullptr; + delete g_pInstance; + g_pInstance = nullptr; } } @@ -160,13 +97,13 @@ void CJS_GlobalData::SetGlobalVariableNumber(const CFX_ByteString& propname, return; if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { - pData->data.nType = JS_GLOBALDATA_TYPE_NUMBER; + pData->data.nType = JS_GlobalDataType::NUMBER; pData->data.dData = dData; return; } std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element); pNewData->data.sKey = sPropName; - pNewData->data.nType = JS_GLOBALDATA_TYPE_NUMBER; + pNewData->data.nType = JS_GlobalDataType::NUMBER; pNewData->data.dData = dData; m_arrayGlobalData.push_back(std::move(pNewData)); } @@ -178,13 +115,13 @@ void CJS_GlobalData::SetGlobalVariableBoolean(const CFX_ByteString& propname, return; if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { - pData->data.nType = JS_GLOBALDATA_TYPE_BOOLEAN; + pData->data.nType = JS_GlobalDataType::BOOLEAN; pData->data.bData = bData; return; } std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element); pNewData->data.sKey = sPropName; - pNewData->data.nType = JS_GLOBALDATA_TYPE_BOOLEAN; + pNewData->data.nType = JS_GlobalDataType::BOOLEAN; pNewData->data.bData = bData; m_arrayGlobalData.push_back(std::move(pNewData)); } @@ -196,13 +133,13 @@ void CJS_GlobalData::SetGlobalVariableString(const CFX_ByteString& propname, return; if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { - pData->data.nType = JS_GLOBALDATA_TYPE_STRING; + pData->data.nType = JS_GlobalDataType::STRING; pData->data.sData = sData; return; } std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element); pNewData->data.sKey = sPropName; - pNewData->data.nType = JS_GLOBALDATA_TYPE_STRING; + pNewData->data.nType = JS_GlobalDataType::STRING; pNewData->data.sData = sData; m_arrayGlobalData.push_back(std::move(pNewData)); } @@ -215,13 +152,13 @@ void CJS_GlobalData::SetGlobalVariableObject( return; if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { - pData->data.nType = JS_GLOBALDATA_TYPE_OBJECT; + pData->data.nType = JS_GlobalDataType::OBJECT; pData->data.objData.Copy(array); return; } std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element); pNewData->data.sKey = sPropName; - pNewData->data.nType = JS_GLOBALDATA_TYPE_OBJECT; + pNewData->data.nType = JS_GlobalDataType::OBJECT; pNewData->data.objData.Copy(array); m_arrayGlobalData.push_back(std::move(pNewData)); } @@ -232,12 +169,12 @@ void CJS_GlobalData::SetGlobalVariableNull(const CFX_ByteString& propname) { return; if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { - pData->data.nType = JS_GLOBALDATA_TYPE_NULL; + pData->data.nType = JS_GlobalDataType::NULLOBJ; return; } std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element); pNewData->data.sKey = sPropName; - pNewData->data.nType = JS_GLOBALDATA_TYPE_NULL; + pNewData->data.nType = JS_GlobalDataType::NULLOBJ; m_arrayGlobalData.push_back(std::move(pNewData)); } @@ -317,11 +254,12 @@ void CJS_GlobalData::LoadGlobalPersistentVariables() { CFX_ByteString sEntry = CFX_ByteString(p, dwNameLen); p += sizeof(char) * dwNameLen; - uint16_t wDataType = *((uint16_t*)p); + JS_GlobalDataType wDataType = + static_cast<JS_GlobalDataType>(*((uint16_t*)p)); p += sizeof(uint16_t); switch (wDataType) { - case JS_GLOBALDATA_TYPE_NUMBER: { + case JS_GlobalDataType::NUMBER: { double dData = 0; switch (wVersion) { case 1: { @@ -337,13 +275,13 @@ void CJS_GlobalData::LoadGlobalPersistentVariables() { SetGlobalVariableNumber(sEntry, dData); SetGlobalVariablePersistent(sEntry, TRUE); } break; - case JS_GLOBALDATA_TYPE_BOOLEAN: { + case JS_GlobalDataType::BOOLEAN: { uint16_t wData = *((uint16_t*)p); p += sizeof(uint16_t); SetGlobalVariableBoolean(sEntry, (bool)(wData == 1)); SetGlobalVariablePersistent(sEntry, TRUE); } break; - case JS_GLOBALDATA_TYPE_STRING: { + case JS_GlobalDataType::STRING: { uint32_t dwLength = *((uint32_t*)p); p += sizeof(uint32_t); @@ -354,10 +292,12 @@ void CJS_GlobalData::LoadGlobalPersistentVariables() { SetGlobalVariablePersistent(sEntry, TRUE); p += sizeof(char) * dwLength; } break; - case JS_GLOBALDATA_TYPE_NULL: { + case JS_GlobalDataType::NULLOBJ: { SetGlobalVariableNull(sEntry); SetGlobalVariablePersistent(sEntry, TRUE); } + case JS_GlobalDataType::OBJECT: + break; } } } @@ -413,47 +353,42 @@ void CJS_GlobalData::WriteFileBuffer(const FX_WCHAR* sFilePath, void CJS_GlobalData::MakeByteString(const CFX_ByteString& name, CJS_KeyValue* pData, CFX_BinaryBuf& sData) { - uint16_t wType = (uint16_t)pData->nType; - switch (wType) { - case JS_GLOBALDATA_TYPE_NUMBER: { + switch (pData->nType) { + case JS_GlobalDataType::NUMBER: { uint32_t dwNameLen = (uint32_t)name.GetLength(); sData.AppendBlock(&dwNameLen, sizeof(uint32_t)); sData.AppendString(name); - sData.AppendBlock(&wType, sizeof(uint16_t)); + sData.AppendBlock(&pData->nType, sizeof(uint16_t)); double dData = pData->dData; sData.AppendBlock(&dData, sizeof(double)); } break; - case JS_GLOBALDATA_TYPE_BOOLEAN: { + case JS_GlobalDataType::BOOLEAN: { uint32_t dwNameLen = (uint32_t)name.GetLength(); sData.AppendBlock(&dwNameLen, sizeof(uint32_t)); sData.AppendString(name); - sData.AppendBlock(&wType, sizeof(uint16_t)); + sData.AppendBlock(&pData->nType, sizeof(uint16_t)); uint16_t wData = (uint16_t)pData->bData; sData.AppendBlock(&wData, sizeof(uint16_t)); } break; - case JS_GLOBALDATA_TYPE_STRING: { + case JS_GlobalDataType::STRING: { uint32_t dwNameLen = (uint32_t)name.GetLength(); sData.AppendBlock(&dwNameLen, sizeof(uint32_t)); sData.AppendString(name); - sData.AppendBlock(&wType, sizeof(uint16_t)); + sData.AppendBlock(&pData->nType, sizeof(uint16_t)); uint32_t dwDataLen = (uint32_t)pData->sData.GetLength(); sData.AppendBlock(&dwDataLen, sizeof(uint32_t)); sData.AppendString(pData->sData); } break; - case JS_GLOBALDATA_TYPE_NULL: { + case JS_GlobalDataType::NULLOBJ: { uint32_t dwNameLen = (uint32_t)name.GetLength(); sData.AppendBlock(&dwNameLen, sizeof(uint32_t)); sData.AppendString(name); - sData.AppendBlock(&wType, sizeof(uint32_t)); + sData.AppendBlock(&pData->nType, sizeof(uint32_t)); } break; default: break; } } - -CJS_KeyValue::CJS_KeyValue() {} - -CJS_KeyValue::~CJS_KeyValue() {} diff --git a/fpdfsdk/javascript/JS_GlobalData.h b/fpdfsdk/javascript/JS_GlobalData.h index 8273c0838c..d901ec58b3 100644 --- a/fpdfsdk/javascript/JS_GlobalData.h +++ b/fpdfsdk/javascript/JS_GlobalData.h @@ -11,49 +11,14 @@ #include <vector> #include "core/fxcrt/include/fx_basic.h" +#include "fpdfsdk/javascript/JS_KeyValue.h" -#define JS_GLOBALDATA_TYPE_NUMBER 0 -#define JS_GLOBALDATA_TYPE_BOOLEAN 1 -#define JS_GLOBALDATA_TYPE_STRING 2 -#define JS_GLOBALDATA_TYPE_OBJECT 3 -#define JS_GLOBALDATA_TYPE_NULL 4 - -class CJS_KeyValue; class CPDFDoc_Environment; -class CJS_GlobalVariableArray { - public: - CJS_GlobalVariableArray(); - virtual ~CJS_GlobalVariableArray(); - - void Add(CJS_KeyValue* p); - int Count() const; - CJS_KeyValue* GetAt(int index) const; - void Copy(const CJS_GlobalVariableArray& array); - - void Empty(); - - private: - CFX_ArrayTemplate<CJS_KeyValue*> m_Array; -}; - -class CJS_KeyValue { - public: - CJS_KeyValue(); - virtual ~CJS_KeyValue(); - - CFX_ByteString sKey; - int nType; // 0:int 1:bool 2:string 3:obj - double dData; - bool bData; - CFX_ByteString sData; - CJS_GlobalVariableArray objData; -}; - class CJS_GlobalData_Element { public: CJS_GlobalData_Element() {} - virtual ~CJS_GlobalData_Element() {} + ~CJS_GlobalData_Element() {} CJS_KeyValue data; FX_BOOL bPersistent; @@ -84,8 +49,6 @@ class CJS_GlobalData { using const_iterator = std::vector<std::unique_ptr<CJS_GlobalData_Element>>::const_iterator; - static CJS_GlobalData* g_Instance; - CJS_GlobalData(); ~CJS_GlobalData(); diff --git a/fpdfsdk/javascript/JS_KeyValue.cpp b/fpdfsdk/javascript/JS_KeyValue.cpp new file mode 100644 index 0000000000..7d1e575377 --- /dev/null +++ b/fpdfsdk/javascript/JS_KeyValue.cpp @@ -0,0 +1,70 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fpdfsdk/javascript/JS_KeyValue.h" + +CJS_GlobalVariableArray::CJS_GlobalVariableArray() {} + +CJS_GlobalVariableArray::~CJS_GlobalVariableArray() {} + +void CJS_GlobalVariableArray::Copy(const CJS_GlobalVariableArray& array) { + m_Array.clear(); + for (int i = 0, sz = array.Count(); i < sz; i++) { + CJS_KeyValue* pOldObjData = array.GetAt(i); + switch (pOldObjData->nType) { + case JS_GlobalDataType::NUMBER: { + CJS_KeyValue* pNewObjData = new CJS_KeyValue; + pNewObjData->sKey = pOldObjData->sKey; + pNewObjData->nType = pOldObjData->nType; + pNewObjData->dData = pOldObjData->dData; + Add(pNewObjData); + } break; + case JS_GlobalDataType::BOOLEAN: { + CJS_KeyValue* pNewObjData = new CJS_KeyValue; + pNewObjData->sKey = pOldObjData->sKey; + pNewObjData->nType = pOldObjData->nType; + pNewObjData->bData = pOldObjData->bData; + Add(pNewObjData); + } break; + case JS_GlobalDataType::STRING: { + CJS_KeyValue* pNewObjData = new CJS_KeyValue; + pNewObjData->sKey = pOldObjData->sKey; + pNewObjData->nType = pOldObjData->nType; + pNewObjData->sData = pOldObjData->sData; + Add(pNewObjData); + } break; + case JS_GlobalDataType::OBJECT: { + CJS_KeyValue* pNewObjData = new CJS_KeyValue; + pNewObjData->sKey = pOldObjData->sKey; + pNewObjData->nType = pOldObjData->nType; + pNewObjData->objData.Copy(pOldObjData->objData); + Add(pNewObjData); + } break; + case JS_GlobalDataType::NULLOBJ: { + CJS_KeyValue* pNewObjData = new CJS_KeyValue; + pNewObjData->sKey = pOldObjData->sKey; + pNewObjData->nType = pOldObjData->nType; + Add(pNewObjData); + } break; + } + } +} + +void CJS_GlobalVariableArray::Add(CJS_KeyValue* p) { + m_Array.push_back(std::unique_ptr<CJS_KeyValue>(p)); +} + +int CJS_GlobalVariableArray::Count() const { + return m_Array.size(); +} + +CJS_KeyValue* CJS_GlobalVariableArray::GetAt(int index) const { + return m_Array.at(index).get(); +} + +CJS_KeyValue::CJS_KeyValue() {} + +CJS_KeyValue::~CJS_KeyValue() {} diff --git a/fpdfsdk/javascript/JS_KeyValue.h b/fpdfsdk/javascript/JS_KeyValue.h new file mode 100644 index 0000000000..43f6687b8d --- /dev/null +++ b/fpdfsdk/javascript/JS_KeyValue.h @@ -0,0 +1,46 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FPDFSDK_JAVASCRIPT_JS_KEYVALUE_H_ +#define FPDFSDK_JAVASCRIPT_JS_KEYVALUE_H_ + +#include <memory> +#include <vector> + +#include "core/fxcrt/include/fx_basic.h" + +enum class JS_GlobalDataType { NUMBER = 0, BOOLEAN, STRING, OBJECT, NULLOBJ }; + +class CJS_KeyValue; + +class CJS_GlobalVariableArray { + public: + CJS_GlobalVariableArray(); + ~CJS_GlobalVariableArray(); + + void Add(CJS_KeyValue* p); + int Count() const; + CJS_KeyValue* GetAt(int index) const; + void Copy(const CJS_GlobalVariableArray& array); + + private: + std::vector<std::unique_ptr<CJS_KeyValue>> m_Array; +}; + +class CJS_KeyValue { + public: + CJS_KeyValue(); + ~CJS_KeyValue(); + + CFX_ByteString sKey; + JS_GlobalDataType nType; + double dData; + bool bData; + CFX_ByteString sData; + CJS_GlobalVariableArray objData; +}; + +#endif // FPDFSDK_JAVASCRIPT_JS_KEYVALUE_H_ diff --git a/fpdfsdk/javascript/global.cpp b/fpdfsdk/javascript/global.cpp index 45e2bd0a90..b305c4104b 100644 --- a/fpdfsdk/javascript/global.cpp +++ b/fpdfsdk/javascript/global.cpp @@ -37,7 +37,7 @@ void CJS_Global::InitInstance(IJS_Runtime* pIRuntime) { } JSGlobalData::JSGlobalData() - : nType(0), + : nType(JS_GlobalDataType::NUMBER), dData(0), bData(FALSE), sData(""), @@ -87,30 +87,30 @@ FX_BOOL JSGlobalAlternate::DoProperty(IJS_Context* cc, case CJS_Value::VT_number: { double dData; vp >> dData; - return SetGlobalVariables(sPropName, JS_GLOBALDATA_TYPE_NUMBER, dData, + return SetGlobalVariables(sPropName, JS_GlobalDataType::NUMBER, dData, false, "", v8::Local<v8::Object>(), FALSE); } case CJS_Value::VT_boolean: { bool bData; vp >> bData; - return SetGlobalVariables(sPropName, JS_GLOBALDATA_TYPE_BOOLEAN, 0, + return SetGlobalVariables(sPropName, JS_GlobalDataType::BOOLEAN, 0, bData, "", v8::Local<v8::Object>(), FALSE); } case CJS_Value::VT_string: { CFX_ByteString sData; vp >> sData; - return SetGlobalVariables(sPropName, JS_GLOBALDATA_TYPE_STRING, 0, + return SetGlobalVariables(sPropName, JS_GlobalDataType::STRING, 0, false, sData, v8::Local<v8::Object>(), FALSE); } case CJS_Value::VT_object: { v8::Local<v8::Object> pData; vp >> pData; - return SetGlobalVariables(sPropName, JS_GLOBALDATA_TYPE_OBJECT, 0, + return SetGlobalVariables(sPropName, JS_GlobalDataType::OBJECT, 0, false, "", pData, FALSE); } case CJS_Value::VT_null: { - return SetGlobalVariables(sPropName, JS_GLOBALDATA_TYPE_NULL, 0, false, - "", v8::Local<v8::Object>(), FALSE); + return SetGlobalVariables(sPropName, JS_GlobalDataType::NULLOBJ, 0, + false, "", v8::Local<v8::Object>(), FALSE); } case CJS_Value::VT_undefined: { DelProperty(cc, propname, sError); @@ -131,22 +131,22 @@ FX_BOOL JSGlobalAlternate::DoProperty(IJS_Context* cc, return TRUE; } switch (pData->nType) { - case JS_GLOBALDATA_TYPE_NUMBER: + case JS_GlobalDataType::NUMBER: vp << pData->dData; return TRUE; - case JS_GLOBALDATA_TYPE_BOOLEAN: + case JS_GlobalDataType::BOOLEAN: vp << pData->bData; return TRUE; - case JS_GLOBALDATA_TYPE_STRING: + case JS_GlobalDataType::STRING: vp << pData->sData; return TRUE; - case JS_GLOBALDATA_TYPE_OBJECT: { + case JS_GlobalDataType::OBJECT: { v8::Local<v8::Object> obj = v8::Local<v8::Object>::New( vp.GetJSRuntime()->GetIsolate(), pData->pData); vp << obj; return TRUE; } - case JS_GLOBALDATA_TYPE_NULL: + case JS_GlobalDataType::NULLOBJ: vp.SetNull(); return TRUE; default: @@ -183,44 +183,44 @@ void JSGlobalAlternate::UpdateGlobalPersistentVariables() { for (int i = 0, sz = m_pGlobalData->GetSize(); i < sz; i++) { CJS_GlobalData_Element* pData = m_pGlobalData->GetAt(i); switch (pData->data.nType) { - case JS_GLOBALDATA_TYPE_NUMBER: - SetGlobalVariables(pData->data.sKey, JS_GLOBALDATA_TYPE_NUMBER, + case JS_GlobalDataType::NUMBER: + SetGlobalVariables(pData->data.sKey, JS_GlobalDataType::NUMBER, pData->data.dData, false, "", v8::Local<v8::Object>(), pData->bPersistent == 1); FXJS_PutObjectNumber(nullptr, m_pJSObject->ToV8Object(), pData->data.sKey.UTF8Decode(), pData->data.dData); break; - case JS_GLOBALDATA_TYPE_BOOLEAN: - SetGlobalVariables(pData->data.sKey, JS_GLOBALDATA_TYPE_BOOLEAN, 0, + case JS_GlobalDataType::BOOLEAN: + SetGlobalVariables(pData->data.sKey, JS_GlobalDataType::BOOLEAN, 0, (bool)(pData->data.bData == 1), "", v8::Local<v8::Object>(), pData->bPersistent == 1); FXJS_PutObjectBoolean(nullptr, m_pJSObject->ToV8Object(), pData->data.sKey.UTF8Decode(), (bool)(pData->data.bData == 1)); break; - case JS_GLOBALDATA_TYPE_STRING: - SetGlobalVariables(pData->data.sKey, JS_GLOBALDATA_TYPE_STRING, 0, + case JS_GlobalDataType::STRING: + SetGlobalVariables(pData->data.sKey, JS_GlobalDataType::STRING, 0, false, pData->data.sData, v8::Local<v8::Object>(), pData->bPersistent == 1); FXJS_PutObjectString(nullptr, m_pJSObject->ToV8Object(), pData->data.sKey.UTF8Decode(), pData->data.sData.UTF8Decode()); break; - case JS_GLOBALDATA_TYPE_OBJECT: { + case JS_GlobalDataType::OBJECT: { v8::Isolate* pRuntime = m_pJSObject->ToV8Object()->GetIsolate(); v8::Local<v8::Object> pObj = FXJS_NewFxDynamicObj(pRuntime, nullptr, -1); PutObjectProperty(pObj, &pData->data); - SetGlobalVariables(pData->data.sKey, JS_GLOBALDATA_TYPE_OBJECT, 0, + SetGlobalVariables(pData->data.sKey, JS_GlobalDataType::OBJECT, 0, false, "", pObj, pData->bPersistent == 1); FXJS_PutObjectObject(nullptr, m_pJSObject->ToV8Object(), pData->data.sKey.UTF8Decode(), pObj); } break; - case JS_GLOBALDATA_TYPE_NULL: - SetGlobalVariables(pData->data.sKey, JS_GLOBALDATA_TYPE_NULL, 0, false, - "", v8::Local<v8::Object>(), + case JS_GlobalDataType::NULLOBJ: + SetGlobalVariables(pData->data.sKey, JS_GlobalDataType::NULLOBJ, 0, + false, "", v8::Local<v8::Object>(), pData->bPersistent == 1); FXJS_PutObjectNull(nullptr, m_pJSObject->ToV8Object(), pData->data.sKey.UTF8Decode()); @@ -237,19 +237,19 @@ void JSGlobalAlternate::CommitGlobalPersisitentVariables(IJS_Context* cc) { m_pGlobalData->DeleteGlobalVariable(name); } else { switch (pData->nType) { - case JS_GLOBALDATA_TYPE_NUMBER: + case JS_GlobalDataType::NUMBER: m_pGlobalData->SetGlobalVariableNumber(name, pData->dData); m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent); break; - case JS_GLOBALDATA_TYPE_BOOLEAN: + case JS_GlobalDataType::BOOLEAN: m_pGlobalData->SetGlobalVariableBoolean(name, pData->bData); m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent); break; - case JS_GLOBALDATA_TYPE_STRING: + case JS_GlobalDataType::STRING: m_pGlobalData->SetGlobalVariableString(name, pData->sData); m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent); break; - case JS_GLOBALDATA_TYPE_OBJECT: { + case JS_GlobalDataType::OBJECT: { CJS_GlobalVariableArray array; v8::Local<v8::Object> obj = v8::Local<v8::Object>::New( GetJSObject()->GetIsolate(), pData->pData); @@ -257,7 +257,7 @@ void JSGlobalAlternate::CommitGlobalPersisitentVariables(IJS_Context* cc) { m_pGlobalData->SetGlobalVariableObject(name, array); m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent); } break; - case JS_GLOBALDATA_TYPE_NULL: + case JS_GlobalDataType::NULLOBJ: m_pGlobalData->SetGlobalVariableNull(name); m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent); break; @@ -282,14 +282,14 @@ void JSGlobalAlternate::ObjectToArray(IJS_Context* cc, switch (CJS_Value::GetValueType(v)) { case CJS_Value::VT_number: { CJS_KeyValue* pObjElement = new CJS_KeyValue; - pObjElement->nType = JS_GLOBALDATA_TYPE_NUMBER; + pObjElement->nType = JS_GlobalDataType::NUMBER; pObjElement->sKey = sKey; pObjElement->dData = FXJS_ToNumber(isolate, v); array.Add(pObjElement); } break; case CJS_Value::VT_boolean: { CJS_KeyValue* pObjElement = new CJS_KeyValue; - pObjElement->nType = JS_GLOBALDATA_TYPE_BOOLEAN; + pObjElement->nType = JS_GlobalDataType::BOOLEAN; pObjElement->sKey = sKey; pObjElement->dData = FXJS_ToBoolean(isolate, v); array.Add(pObjElement); @@ -297,21 +297,21 @@ void JSGlobalAlternate::ObjectToArray(IJS_Context* cc, case CJS_Value::VT_string: { CFX_ByteString sValue = CJS_Value(pRuntime, v).ToCFXByteString(); CJS_KeyValue* pObjElement = new CJS_KeyValue; - pObjElement->nType = JS_GLOBALDATA_TYPE_STRING; + pObjElement->nType = JS_GlobalDataType::STRING; pObjElement->sKey = sKey; pObjElement->sData = sValue; array.Add(pObjElement); } break; case CJS_Value::VT_object: { CJS_KeyValue* pObjElement = new CJS_KeyValue; - pObjElement->nType = JS_GLOBALDATA_TYPE_OBJECT; + pObjElement->nType = JS_GlobalDataType::OBJECT; pObjElement->sKey = sKey; ObjectToArray(cc, FXJS_ToObject(isolate, v), pObjElement->objData); array.Add(pObjElement); } break; case CJS_Value::VT_null: { CJS_KeyValue* pObjElement = new CJS_KeyValue; - pObjElement->nType = JS_GLOBALDATA_TYPE_NULL; + pObjElement->nType = JS_GlobalDataType::NULLOBJ; pObjElement->sKey = sKey; array.Add(pObjElement); } break; @@ -326,19 +326,19 @@ void JSGlobalAlternate::PutObjectProperty(v8::Local<v8::Object> pObj, for (int i = 0, sz = pData->objData.Count(); i < sz; i++) { CJS_KeyValue* pObjData = pData->objData.GetAt(i); switch (pObjData->nType) { - case JS_GLOBALDATA_TYPE_NUMBER: + case JS_GlobalDataType::NUMBER: FXJS_PutObjectNumber(nullptr, pObj, pObjData->sKey.UTF8Decode(), pObjData->dData); break; - case JS_GLOBALDATA_TYPE_BOOLEAN: + case JS_GlobalDataType::BOOLEAN: FXJS_PutObjectBoolean(nullptr, pObj, pObjData->sKey.UTF8Decode(), pObjData->bData == 1); break; - case JS_GLOBALDATA_TYPE_STRING: + case JS_GlobalDataType::STRING: FXJS_PutObjectString(nullptr, pObj, pObjData->sKey.UTF8Decode(), pObjData->sData.UTF8Decode()); break; - case JS_GLOBALDATA_TYPE_OBJECT: { + case JS_GlobalDataType::OBJECT: { v8::Isolate* pRuntime = m_pJSObject->ToV8Object()->GetIsolate(); v8::Local<v8::Object> pNewObj = FXJS_NewFxDynamicObj(pRuntime, nullptr, -1); @@ -346,7 +346,7 @@ void JSGlobalAlternate::PutObjectProperty(v8::Local<v8::Object> pObj, FXJS_PutObjectObject(nullptr, pObj, pObjData->sKey.UTF8Decode(), pNewObj); } break; - case JS_GLOBALDATA_TYPE_NULL: + case JS_GlobalDataType::NULLOBJ: FXJS_PutObjectNull(nullptr, pObj, pObjData->sKey.UTF8Decode()); break; } @@ -361,7 +361,7 @@ void JSGlobalAlternate::DestroyGlobalPersisitentVariables() { } FX_BOOL JSGlobalAlternate::SetGlobalVariables(const CFX_ByteString& propname, - int nType, + JS_GlobalDataType nType, double dData, bool bData, const CFX_ByteString& sData, @@ -382,19 +382,19 @@ FX_BOOL JSGlobalAlternate::SetGlobalVariables(const CFX_ByteString& propname, pTemp->bDeleted = FALSE; switch (nType) { - case JS_GLOBALDATA_TYPE_NUMBER: { + case JS_GlobalDataType::NUMBER: { pTemp->dData = dData; } break; - case JS_GLOBALDATA_TYPE_BOOLEAN: { + case JS_GlobalDataType::BOOLEAN: { pTemp->bData = bData; } break; - case JS_GLOBALDATA_TYPE_STRING: { + case JS_GlobalDataType::STRING: { pTemp->sData = sData; } break; - case JS_GLOBALDATA_TYPE_OBJECT: { + case JS_GlobalDataType::OBJECT: { pTemp->pData.Reset(pData->GetIsolate(), pData); } break; - case JS_GLOBALDATA_TYPE_NULL: + case JS_GlobalDataType::NULLOBJ: break; default: return FALSE; @@ -405,33 +405,33 @@ FX_BOOL JSGlobalAlternate::SetGlobalVariables(const CFX_ByteString& propname, JSGlobalData* pNewData = nullptr; switch (nType) { - case JS_GLOBALDATA_TYPE_NUMBER: { + case JS_GlobalDataType::NUMBER: { pNewData = new JSGlobalData; - pNewData->nType = JS_GLOBALDATA_TYPE_NUMBER; + pNewData->nType = JS_GlobalDataType::NUMBER; pNewData->dData = dData; pNewData->bPersistent = bDefaultPersistent; } break; - case JS_GLOBALDATA_TYPE_BOOLEAN: { + case JS_GlobalDataType::BOOLEAN: { pNewData = new JSGlobalData; - pNewData->nType = JS_GLOBALDATA_TYPE_BOOLEAN; + pNewData->nType = JS_GlobalDataType::BOOLEAN; pNewData->bData = bData; pNewData->bPersistent = bDefaultPersistent; } break; - case JS_GLOBALDATA_TYPE_STRING: { + case JS_GlobalDataType::STRING: { pNewData = new JSGlobalData; - pNewData->nType = JS_GLOBALDATA_TYPE_STRING; + pNewData->nType = JS_GlobalDataType::STRING; pNewData->sData = sData; pNewData->bPersistent = bDefaultPersistent; } break; - case JS_GLOBALDATA_TYPE_OBJECT: { + case JS_GlobalDataType::OBJECT: { pNewData = new JSGlobalData; - pNewData->nType = JS_GLOBALDATA_TYPE_OBJECT; + pNewData->nType = JS_GlobalDataType::OBJECT; pNewData->pData.Reset(pData->GetIsolate(), pData); pNewData->bPersistent = bDefaultPersistent; } break; - case JS_GLOBALDATA_TYPE_NULL: { + case JS_GlobalDataType::NULLOBJ: { pNewData = new JSGlobalData; - pNewData->nType = JS_GLOBALDATA_TYPE_NULL; + pNewData->nType = JS_GlobalDataType::NULLOBJ; pNewData->bPersistent = bDefaultPersistent; } break; default: diff --git a/fpdfsdk/javascript/global.h b/fpdfsdk/javascript/global.h index eca9aa12d1..6604d9bad8 100644 --- a/fpdfsdk/javascript/global.h +++ b/fpdfsdk/javascript/global.h @@ -11,6 +11,7 @@ #include <vector> #include "fpdfsdk/javascript/JS_Define.h" +#include "fpdfsdk/javascript/JS_KeyValue.h" class CJS_GlobalData; class CJS_GlobalVariableArray; @@ -20,7 +21,7 @@ struct JSGlobalData { JSGlobalData(); ~JSGlobalData(); - int nType; // 0:int 1:bool 2:string 3:obj + JS_GlobalDataType nType; double dData; bool bData; CFX_ByteString sData; @@ -53,7 +54,7 @@ class JSGlobalAlternate : public CJS_EmbedObj { void CommitGlobalPersisitentVariables(IJS_Context* cc); void DestroyGlobalPersisitentVariables(); FX_BOOL SetGlobalVariables(const CFX_ByteString& propname, - int nType, + JS_GlobalDataType nType, double dData, bool bData, const CFX_ByteString& sData, diff --git a/pdfium.gyp b/pdfium.gyp index 04bc0060aa..0faf6fdf44 100644 --- a/pdfium.gyp +++ b/pdfium.gyp @@ -797,6 +797,8 @@ 'fpdfsdk/javascript/JS_EventHandler.h', 'fpdfsdk/javascript/JS_GlobalData.cpp', 'fpdfsdk/javascript/JS_GlobalData.h', + 'fpdfsdk/javascript/JS_KeyValue.cpp', + 'fpdfsdk/javascript/JS_KeyValue.h', 'fpdfsdk/javascript/JS_Object.cpp', 'fpdfsdk/javascript/JS_Object.h', 'fpdfsdk/javascript/JS_Value.cpp', @@ -205,6 +205,8 @@ "xfa/fwl/lightwidget/cfwl_widget.h", "xfa/fwl/lightwidget/cfwl_widgetproperties.cpp", "xfa/fwl/lightwidget/cfwl_widgetproperties.h", + "xfa/fwl/theme/cfwl_arrowdata.cpp", + "xfa/fwl/theme/cfwl_arrowdata.h", "xfa/fwl/theme/cfwl_barcodetp.cpp", "xfa/fwl/theme/cfwl_barcodetp.h", "xfa/fwl/theme/cfwl_carettp.cpp", diff --git a/xfa/fwl/theme/cfwl_arrowdata.cpp b/xfa/fwl/theme/cfwl_arrowdata.cpp new file mode 100644 index 0000000000..16580f7f9c --- /dev/null +++ b/xfa/fwl/theme/cfwl_arrowdata.cpp @@ -0,0 +1,73 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "xfa/fwl/theme/cfwl_arrowdata.h" + +#include <algorithm> + +namespace { + +CFWL_ArrowData* g_pInstance = nullptr; + +} // namespace + +CFWL_ArrowData* CFWL_ArrowData::GetInstance() { + if (!g_pInstance) + g_pInstance = new CFWL_ArrowData; + return g_pInstance; +} + +FX_BOOL CFWL_ArrowData::HasInstance() { + return !!g_pInstance; +} + +void CFWL_ArrowData::DestroyInstance() { + delete g_pInstance; + g_pInstance = nullptr; +} + +CFWL_ArrowData::~CFWL_ArrowData() {} + +void CFWL_ArrowData::SetColorData(uint32_t dwID) { + if (!m_pColorData) + m_pColorData.reset(new CColorData); + + if (dwID) { + m_pColorData->clrBorder[0] = ArgbEncode(255, 142, 153, 125); + m_pColorData->clrBorder[1] = ArgbEncode(255, 157, 171, 119); + m_pColorData->clrBorder[2] = ArgbEncode(255, 118, 131, 97); + m_pColorData->clrBorder[3] = ArgbEncode(255, 172, 168, 153); + m_pColorData->clrStart[0] = ArgbEncode(255, 203, 215, 186); + m_pColorData->clrStart[1] = ArgbEncode(255, 218, 232, 185); + m_pColorData->clrStart[2] = ArgbEncode(255, 203, 215, 186); + m_pColorData->clrStart[3] = ArgbEncode(255, 254, 254, 251); + m_pColorData->clrEnd[0] = ArgbEncode(255, 149, 167, 117); + m_pColorData->clrEnd[1] = ArgbEncode(255, 198, 211, 155); + m_pColorData->clrEnd[2] = ArgbEncode(255, 149, 167, 117); + m_pColorData->clrEnd[3] = ArgbEncode(255, 243, 241, 236); + m_pColorData->clrSign[0] = ArgbEncode(255, 255, 255, 255); + m_pColorData->clrSign[1] = ArgbEncode(255, 255, 255, 255); + m_pColorData->clrSign[2] = ArgbEncode(255, 255, 255, 255); + m_pColorData->clrSign[3] = ArgbEncode(255, 128, 128, 128); + } else { + m_pColorData->clrBorder[0] = ArgbEncode(255, 202, 216, 249); + m_pColorData->clrBorder[1] = ArgbEncode(255, 171, 190, 233); + m_pColorData->clrBorder[2] = ArgbEncode(255, 135, 147, 219); + m_pColorData->clrBorder[3] = ArgbEncode(255, 172, 168, 153); + m_pColorData->clrStart[0] = ArgbEncode(255, 225, 234, 254); + m_pColorData->clrStart[1] = ArgbEncode(255, 253, 255, 255); + m_pColorData->clrStart[2] = ArgbEncode(255, 110, 142, 241); + m_pColorData->clrStart[3] = ArgbEncode(255, 254, 254, 251); + m_pColorData->clrEnd[0] = ArgbEncode(255, 175, 204, 251); + m_pColorData->clrEnd[1] = ArgbEncode(255, 185, 218, 251); + m_pColorData->clrEnd[2] = ArgbEncode(255, 210, 222, 235); + m_pColorData->clrEnd[3] = ArgbEncode(255, 243, 241, 236); + m_pColorData->clrSign[0] = ArgbEncode(255, 77, 97, 133); + m_pColorData->clrSign[1] = ArgbEncode(255, 77, 97, 133); + m_pColorData->clrSign[2] = ArgbEncode(255, 77, 97, 133); + m_pColorData->clrSign[3] = ArgbEncode(255, 128, 128, 128); + } +} diff --git a/xfa/fwl/theme/cfwl_arrowdata.h b/xfa/fwl/theme/cfwl_arrowdata.h new file mode 100644 index 0000000000..88982ab12d --- /dev/null +++ b/xfa/fwl/theme/cfwl_arrowdata.h @@ -0,0 +1,36 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FWL_THEME_CFWL_ARROWDATA_H_ +#define XFA_FWL_THEME_CFWL_ARROWDATA_H_ + +#include <memory> + +#include "core/fxcrt/include/fx_system.h" +#include "core/fxge/include/fx_dib.h" + +class CFWL_ArrowData { + public: + struct CColorData { + FX_ARGB clrBorder[4]; + FX_ARGB clrStart[4]; + FX_ARGB clrEnd[4]; + FX_ARGB clrSign[4]; + }; + + static CFWL_ArrowData* GetInstance(); + static FX_BOOL HasInstance(); + static void DestroyInstance(); + void SetColorData(uint32_t dwID); + + std::unique_ptr<CColorData> m_pColorData; + + private: + CFWL_ArrowData(); + ~CFWL_ArrowData(); +}; + +#endif // XFA_FWL_THEME_CFWL_ARROWDATA_H_ diff --git a/xfa/fwl/theme/cfwl_widgettp.cpp b/xfa/fwl/theme/cfwl_widgettp.cpp index 3ef9b0ba2d..9960257730 100644 --- a/xfa/fwl/theme/cfwl_widgettp.cpp +++ b/xfa/fwl/theme/cfwl_widgettp.cpp @@ -17,6 +17,7 @@ #include "xfa/fwl/core/cfwl_widgetmgr.h" #include "xfa/fwl/core/ifwl_themeprovider.h" #include "xfa/fwl/core/ifwl_widget.h" +#include "xfa/fwl/theme/cfwl_arrowdata.h" #include "xfa/fxgraphics/cfx_color.h" #include "xfa/fxgraphics/cfx_path.h" #include "xfa/fxgraphics/cfx_shading.h" @@ -61,7 +62,7 @@ uint32_t CFWL_WidgetTP::SetThemeID(IFWL_Widget* pWidget, FX_BOOL bChildren) { uint32_t dwOld = m_dwThemeID; m_dwThemeID = dwThemeID; - if (CFWL_ArrowData::IsInstance()) { + if (CFWL_ArrowData::HasInstance()) { CFWL_ArrowData::GetInstance()->SetColorData(FWL_GetThemeColor(dwThemeID)); } if (bChildren) { @@ -69,19 +70,23 @@ uint32_t CFWL_WidgetTP::SetThemeID(IFWL_Widget* pWidget, } return dwOld; } + FWL_Error CFWL_WidgetTP::GetThemeMatrix(IFWL_Widget* pWidget, CFX_Matrix& matrix) { matrix.Set(_ctm.a, _ctm.b, _ctm.c, _ctm.d, _ctm.e, _ctm.f); return FWL_Error::Succeeded; } + FWL_Error CFWL_WidgetTP::SetThemeMatrix(IFWL_Widget* pWidget, const CFX_Matrix& matrix) { _ctm.Set(matrix.a, matrix.b, matrix.c, matrix.d, matrix.e, matrix.f); return FWL_Error::Succeeded; } + FX_BOOL CFWL_WidgetTP::DrawBackground(CFWL_ThemeBackground* pParams) { return TRUE; } + FX_BOOL CFWL_WidgetTP::DrawText(CFWL_ThemeText* pParams) { if (!m_pTextOut) InitTTO(); @@ -99,6 +104,7 @@ FX_BOOL CFWL_WidgetTP::DrawText(CFWL_ThemeText* pParams) { m_pTextOut->DrawLogicText(pParams->m_wsText.c_str(), iLen, pParams->m_rtPart); return TRUE; } + void* CFWL_WidgetTP::GetCapacity(CFWL_ThemePart* pThemePart, CFWL_WidgetCapacity dwCapacity) { switch (dwCapacity) { @@ -154,18 +160,22 @@ void* CFWL_WidgetTP::GetCapacity(CFWL_ThemePart* pThemePart, } return &m_fValue; } + FX_BOOL CFWL_WidgetTP::IsCustomizedLayout(IFWL_Widget* pWidget) { return FWL_GetThemeLayout(m_dwThemeID); } + FWL_Error CFWL_WidgetTP::GetPartRect(CFWL_ThemePart* pThemePart, CFX_RectF& rect) { return FWL_Error::Succeeded; } + FX_BOOL CFWL_WidgetTP::IsInPart(CFWL_ThemePart* pThemePart, FX_FLOAT fx, FX_FLOAT fy) { return TRUE; } + FX_BOOL CFWL_WidgetTP::CalcTextRect(CFWL_ThemeText* pParams, CFX_RectF& rect) { if (!pParams) return FALSE; @@ -177,17 +187,21 @@ FX_BOOL CFWL_WidgetTP::CalcTextRect(CFWL_ThemeText* pParams, CFX_RectF& rect) { pParams->m_wsText.GetLength(), rect); return TRUE; } + FWL_Error CFWL_WidgetTP::Initialize() { m_dwThemeID = 0; _ctm.SetIdentity(); return FWL_Error::Succeeded; } + FWL_Error CFWL_WidgetTP::Finalize() { if (!m_pTextOut) FinalizeTTO(); return FWL_Error::Succeeded; } + CFWL_WidgetTP::~CFWL_WidgetTP() {} + FWL_Error CFWL_WidgetTP::SetFont(IFWL_Widget* pWidget, const FX_WCHAR* strFont, FX_FLOAT fFontSize, @@ -201,6 +215,7 @@ FWL_Error CFWL_WidgetTP::SetFont(IFWL_Widget* pWidget, m_pTextOut->SetTextColor(rgbFont); return FWL_Error::Succeeded; } + FWL_Error CFWL_WidgetTP::SetFont(IFWL_Widget* pWidget, CFGAS_GEFont* pFont, FX_FLOAT fFontSize, @@ -213,6 +228,7 @@ FWL_Error CFWL_WidgetTP::SetFont(IFWL_Widget* pWidget, m_pTextOut->SetTextColor(rgbFont); return FWL_Error::Succeeded; } + CFGAS_GEFont* CFWL_WidgetTP::GetFont(IFWL_Widget* pWidget) { return m_pFDEFont; } @@ -352,6 +368,7 @@ void CFWL_WidgetTP::Draw3DRect(CFX_Graphics* pGraphics, } pGraphics->RestoreGraphState(); } + void CFWL_WidgetTP::Draw3DCircle(CFX_Graphics* pGraphics, FWLTHEME_EDGE eType, FX_FLOAT fWidth, @@ -394,6 +411,7 @@ void CFWL_WidgetTP::Draw3DCircle(CFX_Graphics* pGraphics, pGraphics->StrokePath(&path, pMatrix); pGraphics->RestoreGraphState(); } + void CFWL_WidgetTP::DrawBorder(CFX_Graphics* pGraphics, const CFX_RectF* pRect, CFX_Matrix* pMatrix) { @@ -412,11 +430,13 @@ void CFWL_WidgetTP::DrawBorder(CFX_Graphics* pGraphics, pGraphics->FillPath(&path, FXFILL_ALTERNATE, pMatrix); pGraphics->RestoreGraphState(); } + void CFWL_WidgetTP::FillBackground(CFX_Graphics* pGraphics, const CFX_RectF* pRect, CFX_Matrix* pMatrix) { FillSoildRect(pGraphics, FWLTHEME_COLOR_Background, pRect, pMatrix); } + void CFWL_WidgetTP::FillSoildRect(CFX_Graphics* pGraphics, FX_ARGB fillColor, const CFX_RectF* pRect, @@ -434,6 +454,7 @@ void CFWL_WidgetTP::FillSoildRect(CFX_Graphics* pGraphics, pGraphics->FillPath(&path, FXFILL_WINDING, pMatrix); pGraphics->RestoreGraphState(); } + void CFWL_WidgetTP::DrawAxialShading(CFX_Graphics* pGraphics, FX_FLOAT fx1, FX_FLOAT fy1, @@ -456,6 +477,7 @@ void CFWL_WidgetTP::DrawAxialShading(CFX_Graphics* pGraphics, pGraphics->FillPath(path, fillMode, pMatrix); pGraphics->RestoreGraphState(); } + void CFWL_WidgetTP::DrawAnnulusRect(CFX_Graphics* pGraphics, FX_ARGB fillColor, const CFX_RectF* pRect, @@ -477,6 +499,7 @@ void CFWL_WidgetTP::DrawAnnulusRect(CFX_Graphics* pGraphics, pGraphics->FillPath(&path, FXFILL_ALTERNATE, pMatrix); pGraphics->RestoreGraphState(); } + void CFWL_WidgetTP::DrawAnnulusCircle(CFX_Graphics* pGraphics, FX_ARGB fillColor, const CFX_RectF* pRect, @@ -501,6 +524,7 @@ void CFWL_WidgetTP::DrawAnnulusCircle(CFX_Graphics* pGraphics, pGraphics->FillPath(&path, FXFILL_ALTERNATE, pMatrix); pGraphics->RestoreGraphState(); } + void CFWL_WidgetTP::DrawFocus(CFX_Graphics* pGraphics, const CFX_RectF* pRect, CFX_Matrix* pMatrix) { @@ -574,6 +598,7 @@ void CFWL_WidgetTP::DrawArrow(CFX_Graphics* pGraphics, pGraphics->FillPath(&path, FXFILL_WINDING, pMatrix); pGraphics->RestoreGraphState(); } + void CFWL_WidgetTP::DrawArrow(CFX_Graphics* pGraphics, const CFX_RectF* pRect, FWLTHEME_DIRECTION eDict, @@ -629,17 +654,18 @@ void CFWL_WidgetTP::DrawArrow(CFX_Graphics* pGraphics, pGraphics->SetFillColor(&cr); pGraphics->FillPath(&path, FXFILL_WINDING, pMatrix); } + void CFWL_WidgetTP::DrawBtn(CFX_Graphics* pGraphics, const CFX_RectF* pRect, FWLTHEME_STATE eState, CFX_Matrix* pMatrix) { CFX_Path path; path.Create(); - if (!CFWL_ArrowData::IsInstance()) { + if (!CFWL_ArrowData::HasInstance()) { CFWL_ArrowData::GetInstance()->SetColorData(FWL_GetThemeColor(m_dwThemeID)); } CFWL_ArrowData::CColorData* pColorData = - CFWL_ArrowData::GetInstance()->m_pColorData; + CFWL_ArrowData::GetInstance()->m_pColorData.get(); FX_FLOAT fRight = pRect->right(); FX_FLOAT fBottom = pRect->bottom(); path.AddRectangle(pRect->left, pRect->top, pRect->width, pRect->height); @@ -652,22 +678,25 @@ void CFWL_WidgetTP::DrawBtn(CFX_Graphics* pGraphics, pGraphics->SetStrokeColor(&rcStroke); pGraphics->StrokePath(&path, pMatrix); } + void CFWL_WidgetTP::DrawArrowBtn(CFX_Graphics* pGraphics, const CFX_RectF* pRect, FWLTHEME_DIRECTION eDict, FWLTHEME_STATE eState, CFX_Matrix* pMatrix) { DrawBtn(pGraphics, pRect, eState, pMatrix); - if (!CFWL_ArrowData::IsInstance()) { + if (!CFWL_ArrowData::HasInstance()) { CFWL_ArrowData::GetInstance()->SetColorData(FWL_GetThemeColor(m_dwThemeID)); } CFWL_ArrowData::CColorData* pColorData = - CFWL_ArrowData::GetInstance()->m_pColorData; + CFWL_ArrowData::GetInstance()->m_pColorData.get(); DrawArrow(pGraphics, pRect, eDict, pColorData->clrSign[eState - 1], pMatrix); } + CFWL_ArrowData::CFWL_ArrowData() : m_pColorData(nullptr) { SetColorData(0); } + CFWL_FontData::CFWL_FontData() : m_dwStyles(0), m_dwCodePage(0), @@ -679,6 +708,7 @@ CFWL_FontData::CFWL_FontData() #endif { } + CFWL_FontData::~CFWL_FontData() { if (m_pFont) { m_pFont->Release(); @@ -692,12 +722,14 @@ CFWL_FontData::~CFWL_FontData() { } #endif } + FX_BOOL CFWL_FontData::Equal(const CFX_WideStringC& wsFontFamily, uint32_t dwFontStyles, uint16_t wCodePage) { return m_wsFamily == wsFontFamily && m_dwStyles == dwFontStyles && m_dwCodePage == wCodePage; } + FX_BOOL CFWL_FontData::LoadFont(const CFX_WideStringC& wsFontFamily, uint32_t dwFontStyles, uint16_t dwCodePage) { @@ -723,12 +755,16 @@ CFWL_FontManager* CFWL_FontManager::GetInstance() { s_FontManager = new CFWL_FontManager; return s_FontManager; } + void CFWL_FontManager::DestroyInstance() { delete s_FontManager; s_FontManager = nullptr; } + CFWL_FontManager::CFWL_FontManager() {} + CFWL_FontManager::~CFWL_FontManager() {} + CFGAS_GEFont* CFWL_FontManager::FindFont(const CFX_WideStringC& wsFontFamily, uint32_t dwFontStyles, uint16_t wCodePage) { @@ -742,78 +778,20 @@ CFGAS_GEFont* CFWL_FontManager::FindFont(const CFX_WideStringC& wsFontFamily, m_FontsArray.push_back(std::move(pFontData)); return m_FontsArray.back()->GetFont(); } + FX_BOOL FWLTHEME_Init() { return TRUE; } + void FWLTHEME_Release() { CFWL_ArrowData::DestroyInstance(); CFWL_FontManager::DestroyInstance(); } + uint32_t FWL_GetThemeLayout(uint32_t dwThemeID) { return 0xffff0000 & dwThemeID; } + uint32_t FWL_GetThemeColor(uint32_t dwThemeID) { return 0x0000ffff & dwThemeID; } - -CFWL_ArrowData* CFWL_ArrowData::m_pInstance = nullptr; - -CFWL_ArrowData* CFWL_ArrowData::GetInstance() { - if (!m_pInstance) - m_pInstance = new CFWL_ArrowData; - return m_pInstance; -} - -FX_BOOL CFWL_ArrowData::IsInstance() { - return !!m_pInstance; -} - -void CFWL_ArrowData::DestroyInstance() { - delete m_pInstance; - m_pInstance = nullptr; -} - -CFWL_ArrowData::~CFWL_ArrowData() { - delete m_pColorData; -} - -void CFWL_ArrowData::SetColorData(uint32_t dwID) { - if (!m_pColorData) { - m_pColorData = new CColorData; - } - if (dwID) { - m_pColorData->clrBorder[0] = ArgbEncode(255, 142, 153, 125); - m_pColorData->clrBorder[1] = ArgbEncode(255, 157, 171, 119); - m_pColorData->clrBorder[2] = ArgbEncode(255, 118, 131, 97); - m_pColorData->clrBorder[3] = ArgbEncode(255, 172, 168, 153); - m_pColorData->clrStart[0] = ArgbEncode(255, 203, 215, 186); - m_pColorData->clrStart[1] = ArgbEncode(255, 218, 232, 185); - m_pColorData->clrStart[2] = ArgbEncode(255, 203, 215, 186); - m_pColorData->clrStart[3] = ArgbEncode(255, 254, 254, 251); - m_pColorData->clrEnd[0] = ArgbEncode(255, 149, 167, 117); - m_pColorData->clrEnd[1] = ArgbEncode(255, 198, 211, 155); - m_pColorData->clrEnd[2] = ArgbEncode(255, 149, 167, 117); - m_pColorData->clrEnd[3] = ArgbEncode(255, 243, 241, 236); - m_pColorData->clrSign[0] = ArgbEncode(255, 255, 255, 255); - m_pColorData->clrSign[1] = ArgbEncode(255, 255, 255, 255); - m_pColorData->clrSign[2] = ArgbEncode(255, 255, 255, 255); - m_pColorData->clrSign[3] = ArgbEncode(255, 128, 128, 128); - } else { - m_pColorData->clrBorder[0] = ArgbEncode(255, 202, 216, 249); - m_pColorData->clrBorder[1] = ArgbEncode(255, 171, 190, 233); - m_pColorData->clrBorder[2] = ArgbEncode(255, 135, 147, 219); - m_pColorData->clrBorder[3] = ArgbEncode(255, 172, 168, 153); - m_pColorData->clrStart[0] = ArgbEncode(255, 225, 234, 254); - m_pColorData->clrStart[1] = ArgbEncode(255, 253, 255, 255); - m_pColorData->clrStart[2] = ArgbEncode(255, 110, 142, 241); - m_pColorData->clrStart[3] = ArgbEncode(255, 254, 254, 251); - m_pColorData->clrEnd[0] = ArgbEncode(255, 175, 204, 251); - m_pColorData->clrEnd[1] = ArgbEncode(255, 185, 218, 251); - m_pColorData->clrEnd[2] = ArgbEncode(255, 210, 222, 235); - m_pColorData->clrEnd[3] = ArgbEncode(255, 243, 241, 236); - m_pColorData->clrSign[0] = ArgbEncode(255, 77, 97, 133); - m_pColorData->clrSign[1] = ArgbEncode(255, 77, 97, 133); - m_pColorData->clrSign[2] = ArgbEncode(255, 77, 97, 133); - m_pColorData->clrSign[3] = ArgbEncode(255, 128, 128, 128); - } -} diff --git a/xfa/fwl/theme/cfwl_widgettp.h b/xfa/fwl/theme/cfwl_widgettp.h index d8015e19db..8a1e0c152a 100644 --- a/xfa/fwl/theme/cfwl_widgettp.h +++ b/xfa/fwl/theme/cfwl_widgettp.h @@ -93,7 +93,6 @@ enum class CFWL_WidgetCapacity { class CFDE_TextOut; class CFGAS_GEFont; -class CFWL_ArrowData; class CFWL_ThemeBackground; class CFWL_ThemePart; class CFWL_ThemeText; @@ -233,27 +232,6 @@ void FWLTHEME_Release(); uint32_t FWL_GetThemeLayout(uint32_t dwThemeID); uint32_t FWL_GetThemeColor(uint32_t dwThemeID); -class CFWL_ArrowData { - public: - static CFWL_ArrowData* GetInstance(); - static FX_BOOL IsInstance(); - static void DestroyInstance(); - virtual ~CFWL_ArrowData(); - void SetColorData(uint32_t dwID); - - class CColorData { - public: - FX_ARGB clrBorder[4]; - FX_ARGB clrStart[4]; - FX_ARGB clrEnd[4]; - FX_ARGB clrSign[4]; - } * m_pColorData; - - protected: - CFWL_ArrowData(); - static CFWL_ArrowData* m_pInstance; -}; - class CFWL_FontData { public: CFWL_FontData(); |