fixup! [M120 Migration][NaCl][PPFWK] Upgradable pepper plugin requirement
[platform/framework/web/chromium-efl.git] / pdf / post_message_receiver.h
1 // Copyright 2021 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef PDF_POST_MESSAGE_RECEIVER_H_
6 #define PDF_POST_MESSAGE_RECEIVER_H_
7
8 #include <memory>
9
10 #include "base/memory/raw_ptr.h"
11 #include "base/memory/scoped_refptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/values.h"
14 #include "gin/interceptor.h"
15 #include "gin/public/wrapper_info.h"
16 #include "gin/wrappable.h"
17 #include "v8/include/v8.h"
18
19 namespace base {
20 class SequencedTaskRunner;
21 }  // namespace base
22
23 namespace gin {
24 class ObjectTemplateBuilder;
25 }  // namespace gin
26
27 namespace chrome_pdf {
28
29 class V8ValueConverter;
30
31 // Implements the `postMessage()` API exposed to the plugin embedder. The
32 // received messages are converted and forwarded to the `Client`.
33 // `PostMessageReceiver`'s lifetime is managed by the V8 garbage collector,
34 // meaning it can outlive the `Client`. Messages are dropped if the `Client` is
35 // destroyed.
36 class PostMessageReceiver final : public gin::Wrappable<PostMessageReceiver>,
37                                   public gin::NamedPropertyInterceptor {
38  public:
39   // The interface for a plugin client that handles messages from its embedder.
40   class Client {
41    public:
42     // Handles converted messages from the embedder.
43     virtual void OnMessage(const base::Value::Dict& message) = 0;
44
45    protected:
46     Client() = default;
47     ~Client() = default;
48   };
49
50   static gin::WrapperInfo kWrapperInfo;
51
52   // Creates a scriptable object with an implemented `postMessage()` method.
53   // Messages are posted asynchronously to `client` using `client_task_runner`.
54   static v8::Local<v8::Object> Create(
55       v8::Isolate* isolate,
56       base::WeakPtr<V8ValueConverter> v8_value_converter,
57       base::WeakPtr<Client> client,
58       scoped_refptr<base::SequencedTaskRunner> client_task_runner);
59
60   PostMessageReceiver(const PostMessageReceiver&) = delete;
61   PostMessageReceiver& operator=(const PostMessageReceiver&) = delete;
62
63  protected:
64   ~PostMessageReceiver() override;
65
66  private:
67   PostMessageReceiver(
68       v8::Isolate* isolate,
69       base::WeakPtr<V8ValueConverter> v8_value_converter,
70       base::WeakPtr<Client> client,
71       scoped_refptr<base::SequencedTaskRunner> client_task_runner);
72
73   // gin::Wrappable:
74   gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
75       v8::Isolate* isolate) override;
76   const char* GetTypeName() override;
77
78   // gin::NamedPropertyInterceptor:
79   v8::Local<v8::Value> GetNamedProperty(v8::Isolate* isolate,
80                                         const std::string& property) override;
81   std::vector<std::string> EnumerateNamedProperties(
82       v8::Isolate* isolate) override;
83
84   // Lazily creates and retrieves `function_template_`.
85   v8::Local<v8::FunctionTemplate> GetFunctionTemplate();
86
87   // Implements the `postMessage()` method called by the embedder.
88   void PostMessage(v8::Local<v8::Value> message);
89
90   base::WeakPtr<V8ValueConverter> v8_value_converter_;
91
92   v8::Persistent<v8::FunctionTemplate> function_template_;
93
94   raw_ptr<v8::Isolate> isolate_;
95
96   base::WeakPtr<Client> client_;
97
98   scoped_refptr<base::SequencedTaskRunner> client_task_runner_;
99
100   base::WeakPtrFactory<PostMessageReceiver> weak_factory_{this};
101 };
102
103 }  // namespace chrome_pdf
104
105 #endif  // PDF_POST_MESSAGE_RECEIVER_H_