Upload upstream chromium 76.0.3809.146
[platform/framework/web/chromium-efl.git] / pdf / pdf_ppapi.cc
1 // Copyright 2018 The Chromium 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 #include "pdf/pdf_ppapi.h"
6
7 #include <memory>
8
9 #include "pdf/out_of_process_instance.h"
10 #include "ppapi/c/ppp.h"
11 #include "ppapi/cpp/private/internal_module.h"
12 #include "ppapi/cpp/private/pdf.h"
13 #include "v8/include/v8.h"
14
15 namespace chrome_pdf {
16
17 namespace {
18
19 bool g_sdk_initialized_via_pepper = false;
20
21 class PDFModule : public pp::Module {
22  public:
23   PDFModule();
24   ~PDFModule() override;
25
26   // pp::Module implementation.
27   bool Init() override;
28   pp::Instance* CreateInstance(PP_Instance instance) override;
29 };
30
31 PDFModule::PDFModule() = default;
32
33 PDFModule::~PDFModule() {
34   if (g_sdk_initialized_via_pepper) {
35     ShutdownSDK();
36     g_sdk_initialized_via_pepper = false;
37   }
38 }
39
40 bool PDFModule::Init() {
41   return true;
42 }
43
44 pp::Instance* PDFModule::CreateInstance(PP_Instance instance) {
45   if (!g_sdk_initialized_via_pepper) {
46     v8::StartupData natives;
47     v8::StartupData snapshot;
48     pp::PDF::GetV8ExternalSnapshotData(pp::InstanceHandle(instance),
49                                        &natives.data, &natives.raw_size,
50                                        &snapshot.data, &snapshot.raw_size);
51     if (natives.data) {
52       v8::V8::SetNativesDataBlob(&natives);
53       v8::V8::SetSnapshotDataBlob(&snapshot);
54     }
55     if (!InitializeSDK())
56       return nullptr;
57     g_sdk_initialized_via_pepper = true;
58   }
59
60   return new OutOfProcessInstance(instance);
61 }
62
63 }  // namespace
64
65 int32_t PPP_InitializeModule(PP_Module module_id,
66                              PPB_GetInterface get_browser_interface) {
67   auto module = std::make_unique<PDFModule>();
68   if (!module->InternalInit(module_id, get_browser_interface))
69     return PP_ERROR_FAILED;
70
71   pp::InternalSetModuleSingleton(module.release());
72   return PP_OK;
73 }
74
75 void PPP_ShutdownModule() {
76   delete pp::Module::Get();
77   pp::InternalSetModuleSingleton(nullptr);
78 }
79
80 const void* PPP_GetInterface(const char* interface_name) {
81   auto* module = pp::Module::Get();
82   return module ? module->GetPluginInterface(interface_name) : nullptr;
83 }
84
85 bool IsSDKInitializedViaPepper() {
86   return g_sdk_initialized_via_pepper;
87 }
88
89 }  // namespace chrome_pdf