summaryrefslogtreecommitdiff
path: root/chromium/pdf/pdf_ppapi.cc
blob: 1ac23b1f53d58236de9d98e02c06036deff67550 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "pdf/pdf_ppapi.h"

#include <memory>

#include "pdf/out_of_process_instance.h"
#include "pdf/pdf.h"
#include "pdf/pdf_init.h"
#include "ppapi/c/ppp.h"
#include "ppapi/cpp/private/internal_module.h"
#include "ppapi/cpp/private/pdf.h"
#include "v8/include/v8.h"

namespace chrome_pdf {

namespace {

class PDFModule : public pp::Module {
 public:
  PDFModule();
  ~PDFModule() override;

  // pp::Module implementation.
  bool Init() override;
  pp::Instance* CreateInstance(PP_Instance instance) override;
};

PDFModule::PDFModule() = default;

PDFModule::~PDFModule() {
  if (IsSDKInitializedViaPlugin()) {
    ShutdownSDK();
    SetIsSDKInitializedViaPlugin(false);
  }
}

bool PDFModule::Init() {
  return true;
}

pp::Instance* PDFModule::CreateInstance(PP_Instance instance) {
  if (!IsSDKInitializedViaPlugin()) {
    v8::StartupData snapshot;
    pp::PDF::GetV8ExternalSnapshotData(pp::InstanceHandle(instance),
                                       &snapshot.data, &snapshot.raw_size);
    if (snapshot.data) {
      v8::V8::SetSnapshotDataBlob(&snapshot);
    }

    InitializeSDK(/*enable_v8=*/true);
    SetIsSDKInitializedViaPlugin(true);
  }

  return new OutOfProcessInstance(instance);
}

}  // namespace

int32_t PPP_InitializeModule(PP_Module module_id,
                             PPB_GetInterface get_browser_interface) {
  auto module = std::make_unique<PDFModule>();
  if (!module->InternalInit(module_id, get_browser_interface))
    return PP_ERROR_FAILED;

  pp::InternalSetModuleSingleton(module.release());
  return PP_OK;
}

void PPP_ShutdownModule() {
  delete pp::Module::Get();
  pp::InternalSetModuleSingleton(nullptr);
}

const void* PPP_GetInterface(const char* interface_name) {
  auto* module = pp::Module::Get();
  return module ? module->GetPluginInterface(interface_name) : nullptr;
}

}  // namespace chrome_pdf