[ServiceWorker] Implement WebServiceWorkerContextClient::openWindow().
[chromium-blink-merge.git] / content / renderer / pepper / message_channel.h
blob4889777ed76de51152bb7b6a3030fe9660ac993e
1 // Copyright (c) 2012 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.
5 #ifndef CONTENT_RENDERER_PEPPER_MESSAGE_CHANNEL_H_
6 #define CONTENT_RENDERER_PEPPER_MESSAGE_CHANNEL_H_
8 #include <deque>
9 #include <list>
10 #include <map>
12 #include "base/basictypes.h"
13 #include "base/memory/weak_ptr.h"
14 #include "content/renderer/pepper/v8_var_converter.h"
15 #include "gin/handle.h"
16 #include "gin/interceptor.h"
17 #include "gin/wrappable.h"
18 #include "ppapi/proxy/host_dispatcher.h"
19 #include "ppapi/shared_impl/resource.h"
20 #include "third_party/WebKit/public/web/WebSerializedScriptValue.h"
21 #include "v8/include/v8.h"
23 struct PP_Var;
25 namespace gin {
26 class Arguments;
27 } // namespace gin
29 namespace ppapi {
30 class ScopedPPVar;
31 } // namespace ppapi
33 namespace content {
35 class PepperPluginInstanceImpl;
36 class PluginObject;
38 // MessageChannel implements bidirectional postMessage functionality, allowing
39 // calls from JavaScript to plugins and vice-versa. See
40 // PPB_Messaging::PostMessage and PPP_Messaging::HandleMessage for more
41 // information.
43 // Currently, only 1 MessageChannel can exist, to implement postMessage
44 // functionality for the instance interfaces. In the future, when we create a
45 // MessagePort type in PPAPI, those may be implemented here as well with some
46 // refactoring.
47 // - Separate message ports won't require the passthrough object.
48 // - The message target won't be limited to instance, and should support
49 // either plugin-provided or JS objects.
50 // TODO(dmichael): Add support for separate MessagePorts.
51 class MessageChannel :
52 public gin::Wrappable<MessageChannel>,
53 public gin::NamedPropertyInterceptor,
54 public ppapi::proxy::HostDispatcher::SyncMessageStatusObserver {
55 public:
56 static gin::WrapperInfo kWrapperInfo;
58 // Creates a MessageChannel, returning a pointer to it and sets |result| to
59 // the v8 object which is backed by the message channel. The returned pointer
60 // is only valid as long as the object in |result| is alive.
61 static MessageChannel* Create(PepperPluginInstanceImpl* instance,
62 v8::Persistent<v8::Object>* result);
64 ~MessageChannel() override;
66 // Called when the instance is deleted. The MessageChannel might outlive the
67 // plugin instance because it is garbage collected.
68 void InstanceDeleted();
70 // Post a message to the onmessage handler for this channel's instance
71 // asynchronously.
72 void PostMessageToJavaScript(PP_Var message_data);
74 // Messages are queued initially. After the PepperPluginInstanceImpl is ready
75 // to send and handle messages, users of MessageChannel should call
76 // Start().
77 void Start();
79 // Set the V8Object to which we should forward any calls which aren't
80 // related to postMessage. Note that this can be empty; it only gets set if
81 // there is a scriptable 'InstanceObject' associated with this channel's
82 // instance.
83 void SetPassthroughObject(v8::Handle<v8::Object> passthrough);
85 PepperPluginInstanceImpl* instance() { return instance_; }
87 void SetReadOnlyProperty(PP_Var key, PP_Var value);
89 private:
90 // Struct for storing the result of a v8 object being converted to a PP_Var.
91 struct VarConversionResult;
93 explicit MessageChannel(PepperPluginInstanceImpl* instance);
95 // gin::NamedPropertyInterceptor
96 v8::Local<v8::Value> GetNamedProperty(v8::Isolate* isolate,
97 const std::string& property) override;
98 bool SetNamedProperty(v8::Isolate* isolate,
99 const std::string& property,
100 v8::Local<v8::Value> value) override;
101 std::vector<std::string> EnumerateNamedProperties(
102 v8::Isolate* isolate) override;
104 // gin::Wrappable
105 gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
106 v8::Isolate* isolate) override;
108 // ppapi::proxy::HostDispatcher::SyncMessageStatusObserver
109 void BeginBlockOnSyncMessage() override;
110 void EndBlockOnSyncMessage() override;
112 // Post a message to the plugin's HandleMessage function for this channel's
113 // instance.
114 void PostMessageToNative(gin::Arguments* args);
115 // Post a message to the plugin's HandleBlocking Message function for this
116 // channel's instance synchronously, and return a result.
117 void PostBlockingMessageToNative(gin::Arguments* args);
119 // Post a message to the onmessage handler for this channel's instance
120 // synchronously. This is used by PostMessageToJavaScript.
121 void PostMessageToJavaScriptImpl(
122 const blink::WebSerializedScriptValue& message_data);
124 PluginObject* GetPluginObject(v8::Isolate* isolate);
126 void EnqueuePluginMessage(v8::Handle<v8::Value> v8_value);
128 void FromV8ValueComplete(VarConversionResult* result_holder,
129 const ppapi::ScopedPPVar& result_var,
130 bool success);
132 // Drain the queue of messages that are going to the plugin. All "completed"
133 // messages at the head of the queue will be sent; any messages awaiting
134 // conversion as well as messages after that in the queue will not be sent.
135 void DrainCompletedPluginMessages();
136 // Drain the queue of messages that are going to JavaScript.
137 void DrainJSMessageQueue();
138 // PostTask to call DrainJSMessageQueue() soon. Use this when you want to
139 // send the messages, but can't immediately (e.g., because the instance is
140 // not ready or JavaScript is on the stack).
141 void DrainJSMessageQueueSoon();
143 void UnregisterSyncMessageStatusObserver();
145 PepperPluginInstanceImpl* instance_;
147 // We pass all non-postMessage calls through to the passthrough_object_.
148 // This way, a plugin can use PPB_Class or PPP_Class_Deprecated and also
149 // postMessage. This is necessary to support backwards-compatibility, and
150 // also trusted plugins for which we will continue to support synchronous
151 // scripting.
152 v8::Persistent<v8::Object> passthrough_object_;
154 enum MessageQueueState {
155 WAITING_TO_START, // Waiting for Start() to be called. Queue messages.
156 QUEUE_MESSAGES, // Queue messages temporarily.
157 SEND_DIRECTLY, // Post messages directly.
160 // This queue stores values being posted to JavaScript.
161 std::deque<blink::WebSerializedScriptValue> js_message_queue_;
162 MessageQueueState js_message_queue_state_;
163 // When the renderer is sending a blocking message to the plugin, we will
164 // queue Plugin->JS messages temporarily to avoid re-entering JavaScript. This
165 // counts how many blocking renderer->plugin messages are on the stack so that
166 // we only begin sending messages to JavaScript again when the depth reaches
167 // zero.
168 int blocking_message_depth_;
170 // This queue stores vars that are being sent to the plugin. Because
171 // conversion can happen asynchronously for object types, the queue stores
172 // the var until all previous vars have been converted and sent. This
173 // preserves the order in which JS->plugin messages are processed.
175 // Note we rely on raw VarConversionResult* pointers remaining valid after
176 // calls to push_back or pop_front; hence why we're using list. (deque would
177 // probably also work, but is less clearly specified).
178 std::list<VarConversionResult> plugin_message_queue_;
179 MessageQueueState plugin_message_queue_state_;
181 std::map<std::string, ppapi::ScopedPPVar> internal_named_properties_;
183 V8VarConverter var_converter_;
185 // A callback to invoke at shutdown to ensure we unregister ourselves as
186 // Observers for sync messages.
187 base::Closure unregister_observer_callback_;
189 // This is used to ensure pending tasks will not fire after this object is
190 // destroyed.
191 base::WeakPtrFactory<MessageChannel> weak_ptr_factory_;
193 DISALLOW_COPY_AND_ASSIGN(MessageChannel);
196 } // namespace content
198 #endif // CONTENT_RENDERER_PEPPER_MESSAGE_CHANNEL_H_