Home | History | Annotate | Download | only in app
      1 // Copyright 2014 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 
      7 #include "xfa/fxfa/xfa_ffapp.h"
      8 
      9 #include <algorithm>
     10 #include <memory>
     11 #include <utility>
     12 #include <vector>
     13 
     14 #include "third_party/base/ptr_util.h"
     15 #include "third_party/base/stl_util.h"
     16 #include "xfa/fgas/font/cfgas_fontmgr.h"
     17 #include "xfa/fwl/cfwl_notedriver.h"
     18 #include "xfa/fwl/cfwl_widgetmgr.h"
     19 #include "xfa/fxfa/app/xfa_fwladapter.h"
     20 #include "xfa/fxfa/app/xfa_fwltheme.h"
     21 #include "xfa/fxfa/xfa_ffdoc.h"
     22 #include "xfa/fxfa/xfa_ffdochandler.h"
     23 #include "xfa/fxfa/xfa_ffwidgethandler.h"
     24 #include "xfa/fxfa/xfa_fontmgr.h"
     25 
     26 namespace {
     27 
     28 class CXFA_FileRead : public IFX_SeekableReadStream {
     29  public:
     30   explicit CXFA_FileRead(const std::vector<CPDF_Stream*>& streams);
     31   ~CXFA_FileRead() override;
     32 
     33   // IFX_SeekableReadStream
     34   FX_FILESIZE GetSize() override;
     35   bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override;
     36 
     37  private:
     38   std::vector<std::unique_ptr<CPDF_StreamAcc>> m_Data;
     39 };
     40 
     41 CXFA_FileRead::CXFA_FileRead(const std::vector<CPDF_Stream*>& streams) {
     42   for (CPDF_Stream* pStream : streams) {
     43     m_Data.push_back(pdfium::MakeUnique<CPDF_StreamAcc>());
     44     m_Data.back()->LoadAllData(pStream);
     45   }
     46 }
     47 
     48 CXFA_FileRead::~CXFA_FileRead() {}
     49 
     50 FX_FILESIZE CXFA_FileRead::GetSize() {
     51   uint32_t dwSize = 0;
     52   for (const auto& acc : m_Data)
     53     dwSize += acc->GetSize();
     54   return dwSize;
     55 }
     56 
     57 bool CXFA_FileRead::ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) {
     58   int32_t iCount = pdfium::CollectionSize<int32_t>(m_Data);
     59   int32_t index = 0;
     60   while (index < iCount) {
     61     const auto& acc = m_Data[index];
     62     FX_FILESIZE dwSize = acc->GetSize();
     63     if (offset < dwSize)
     64       break;
     65 
     66     offset -= dwSize;
     67     index++;
     68   }
     69   while (index < iCount) {
     70     const auto& acc = m_Data[index];
     71     uint32_t dwSize = acc->GetSize();
     72     size_t dwRead = std::min(size, static_cast<size_t>(dwSize - offset));
     73     FXSYS_memcpy(buffer, acc->GetData() + offset, dwRead);
     74     size -= dwRead;
     75     if (size == 0)
     76       return true;
     77 
     78     buffer = (uint8_t*)buffer + dwRead;
     79     offset = 0;
     80     index++;
     81   }
     82   return false;
     83 }
     84 
     85 }  // namespace
     86 
     87 CFX_RetainPtr<IFX_SeekableReadStream> MakeSeekableReadStream(
     88     const std::vector<CPDF_Stream*>& streams) {
     89   return CFX_RetainPtr<IFX_SeekableReadStream>(new CXFA_FileRead(streams));
     90 }
     91 
     92 CXFA_FFApp::CXFA_FFApp(IXFA_AppProvider* pProvider)
     93     : m_pProvider(pProvider),
     94       m_pWidgetMgrDelegate(nullptr),
     95       m_pFWLApp(new CFWL_App(this)) {}
     96 
     97 CXFA_FFApp::~CXFA_FFApp() {}
     98 
     99 CXFA_FFDocHandler* CXFA_FFApp::GetDocHandler() {
    100   if (!m_pDocHandler)
    101     m_pDocHandler = pdfium::MakeUnique<CXFA_FFDocHandler>();
    102   return m_pDocHandler.get();
    103 }
    104 
    105 std::unique_ptr<CXFA_FFDoc> CXFA_FFApp::CreateDoc(
    106     IXFA_DocEnvironment* pDocEnvironment,
    107     CPDF_Document* pPDFDoc) {
    108   if (!pPDFDoc)
    109     return nullptr;
    110 
    111   auto pDoc = pdfium::MakeUnique<CXFA_FFDoc>(this, pDocEnvironment);
    112   if (!pDoc->OpenDoc(pPDFDoc))
    113     return nullptr;
    114 
    115   return pDoc;
    116 }
    117 
    118 void CXFA_FFApp::SetDefaultFontMgr(std::unique_ptr<CXFA_DefFontMgr> pFontMgr) {
    119   if (!m_pFontMgr)
    120     m_pFontMgr = pdfium::MakeUnique<CXFA_FontMgr>();
    121   m_pFontMgr->SetDefFontMgr(std::move(pFontMgr));
    122 }
    123 
    124 CXFA_FontMgr* CXFA_FFApp::GetXFAFontMgr() const {
    125   return m_pFontMgr.get();
    126 }
    127 
    128 CFGAS_FontMgr* CXFA_FFApp::GetFDEFontMgr() {
    129   if (!m_pFDEFontMgr) {
    130 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
    131     m_pFDEFontMgr = CFGAS_FontMgr::Create(FX_GetDefFontEnumerator());
    132 #else
    133     m_pFontSource = pdfium::MakeUnique<CFX_FontSourceEnum_File>();
    134     m_pFDEFontMgr = CFGAS_FontMgr::Create(m_pFontSource.get());
    135 #endif
    136   }
    137   return m_pFDEFontMgr.get();
    138 }
    139 
    140 CXFA_FWLTheme* CXFA_FFApp::GetFWLTheme() {
    141   if (!m_pFWLTheme)
    142     m_pFWLTheme = pdfium::MakeUnique<CXFA_FWLTheme>(this);
    143   return m_pFWLTheme.get();
    144 }
    145 
    146 CXFA_FWLAdapterWidgetMgr* CXFA_FFApp::GetWidgetMgr(
    147     CFWL_WidgetMgrDelegate* pDelegate) {
    148   if (!m_pAdapterWidgetMgr) {
    149     m_pAdapterWidgetMgr = pdfium::MakeUnique<CXFA_FWLAdapterWidgetMgr>();
    150     pDelegate->OnSetCapability(FWL_WGTMGR_DisableForm);
    151     m_pWidgetMgrDelegate = pDelegate;
    152   }
    153   return m_pAdapterWidgetMgr.get();
    154 }
    155 
    156 IFWL_AdapterTimerMgr* CXFA_FFApp::GetTimerMgr() const {
    157   return m_pProvider->GetTimerMgr();
    158 }
    159 
    160 void CXFA_FFApp::ClearEventTargets() {
    161   m_pFWLApp->GetNoteDriver()->ClearEventTargets(false);
    162 }
    163