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_
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-util.h"
22 #include "v8/include/v8.h"
36 class PepperPluginInstanceImpl
;
39 // MessageChannel implements bidirectional postMessage functionality, allowing
40 // calls from JavaScript to plugins and vice-versa. See
41 // PPB_Messaging::PostMessage and PPP_Messaging::HandleMessage for more
44 // Currently, only 1 MessageChannel can exist, to implement postMessage
45 // functionality for the instance interfaces. In the future, when we create a
46 // MessagePort type in PPAPI, those may be implemented here as well with some
48 // - Separate message ports won't require the passthrough object.
49 // - The message target won't be limited to instance, and should support
50 // either plugin-provided or JS objects.
51 // TODO(dmichael): Add support for separate MessagePorts.
52 class MessageChannel
:
53 public gin::Wrappable
<MessageChannel
>,
54 public gin::NamedPropertyInterceptor
,
55 public ppapi::proxy::HostDispatcher::SyncMessageStatusObserver
{
57 static gin::WrapperInfo kWrapperInfo
;
59 // Creates a MessageChannel, returning a pointer to it and sets |result| to
60 // the v8 object which is backed by the message channel. The returned pointer
61 // is only valid as long as the object in |result| is alive.
62 static MessageChannel
* Create(PepperPluginInstanceImpl
* instance
,
63 v8::Persistent
<v8::Object
>* result
);
65 ~MessageChannel() override
;
67 // Called when the instance is deleted. The MessageChannel might outlive the
68 // plugin instance because it is garbage collected.
69 void InstanceDeleted();
71 // Post a message to the onmessage handler for this channel's instance
73 void PostMessageToJavaScript(PP_Var message_data
);
75 // Messages are queued initially. After the PepperPluginInstanceImpl is ready
76 // to send and handle messages, users of MessageChannel should call
80 // Set the V8Object to which we should forward any calls which aren't
81 // related to postMessage. Note that this can be empty; it only gets set if
82 // there is a scriptable 'InstanceObject' associated with this channel's
84 void SetPassthroughObject(v8::Handle
<v8::Object
> passthrough
);
86 PepperPluginInstanceImpl
* instance() { return instance_
; }
88 void SetReadOnlyProperty(PP_Var key
, PP_Var value
);
91 // Struct for storing the result of a v8 object being converted to a PP_Var.
92 struct VarConversionResult
;
94 explicit MessageChannel(PepperPluginInstanceImpl
* instance
);
96 // gin::NamedPropertyInterceptor
97 v8::Local
<v8::Value
> GetNamedProperty(v8::Isolate
* isolate
,
98 const std::string
& property
) override
;
99 bool SetNamedProperty(v8::Isolate
* isolate
,
100 const std::string
& property
,
101 v8::Local
<v8::Value
> value
) override
;
102 std::vector
<std::string
> EnumerateNamedProperties(
103 v8::Isolate
* isolate
) override
;
106 gin::ObjectTemplateBuilder
GetObjectTemplateBuilder(
107 v8::Isolate
* isolate
) override
;
109 // ppapi::proxy::HostDispatcher::SyncMessageStatusObserver
110 void BeginBlockOnSyncMessage() override
;
111 void EndBlockOnSyncMessage() override
;
113 // Post a message to the plugin's HandleMessage function for this channel's
115 void PostMessageToNative(gin::Arguments
* args
);
116 // Post a message to the plugin's HandleBlocking Message function for this
117 // channel's instance synchronously, and return a result.
118 void PostBlockingMessageToNative(gin::Arguments
* args
);
120 // Post a message to the onmessage handler for this channel's instance
121 // synchronously. This is used by PostMessageToJavaScript.
122 void PostMessageToJavaScriptImpl(
123 const blink::WebSerializedScriptValue
& message_data
);
125 PluginObject
* GetPluginObject(v8::Isolate
* isolate
);
127 void EnqueuePluginMessage(v8::Handle
<v8::Value
> v8_value
);
129 void FromV8ValueComplete(VarConversionResult
* result_holder
,
130 const ppapi::ScopedPPVar
& result_var
,
133 // Drain the queue of messages that are going to the plugin. All "completed"
134 // messages at the head of the queue will be sent; any messages awaiting
135 // conversion as well as messages after that in the queue will not be sent.
136 void DrainCompletedPluginMessages();
137 // Drain the queue of messages that are going to JavaScript.
138 void DrainJSMessageQueue();
139 // PostTask to call DrainJSMessageQueue() soon. Use this when you want to
140 // send the messages, but can't immediately (e.g., because the instance is
141 // not ready or JavaScript is on the stack).
142 void DrainJSMessageQueueSoon();
144 void UnregisterSyncMessageStatusObserver();
146 v8::Local
<v8::FunctionTemplate
> GetFunctionTemplate(
147 v8::Isolate
* isolate
,
148 const std::string
& name
,
149 void (MessageChannel::*memberFuncPtr
)(gin::Arguments
* args
));
151 PepperPluginInstanceImpl
* instance_
;
153 // We pass all non-postMessage calls through to the passthrough_object_.
154 // This way, a plugin can use PPB_Class or PPP_Class_Deprecated and also
155 // postMessage. This is necessary to support backwards-compatibility, and
156 // also trusted plugins for which we will continue to support synchronous
158 v8::Persistent
<v8::Object
> passthrough_object_
;
160 enum MessageQueueState
{
161 WAITING_TO_START
, // Waiting for Start() to be called. Queue messages.
162 QUEUE_MESSAGES
, // Queue messages temporarily.
163 SEND_DIRECTLY
, // Post messages directly.
166 // This queue stores values being posted to JavaScript.
167 std::deque
<blink::WebSerializedScriptValue
> js_message_queue_
;
168 MessageQueueState js_message_queue_state_
;
169 // When the renderer is sending a blocking message to the plugin, we will
170 // queue Plugin->JS messages temporarily to avoid re-entering JavaScript. This
171 // counts how many blocking renderer->plugin messages are on the stack so that
172 // we only begin sending messages to JavaScript again when the depth reaches
174 int blocking_message_depth_
;
176 // This queue stores vars that are being sent to the plugin. Because
177 // conversion can happen asynchronously for object types, the queue stores
178 // the var until all previous vars have been converted and sent. This
179 // preserves the order in which JS->plugin messages are processed.
181 // Note we rely on raw VarConversionResult* pointers remaining valid after
182 // calls to push_back or pop_front; hence why we're using list. (deque would
183 // probably also work, but is less clearly specified).
184 std::list
<VarConversionResult
> plugin_message_queue_
;
185 MessageQueueState plugin_message_queue_state_
;
187 std::map
<std::string
, ppapi::ScopedPPVar
> internal_named_properties_
;
189 V8VarConverter var_converter_
;
191 // A callback to invoke at shutdown to ensure we unregister ourselves as
192 // Observers for sync messages.
193 base::Closure unregister_observer_callback_
;
195 v8::StdPersistentValueMap
<std::string
, v8::FunctionTemplate
> template_cache_
;
197 // This is used to ensure pending tasks will not fire after this object is
199 base::WeakPtrFactory
<MessageChannel
> weak_ptr_factory_
;
201 DISALLOW_COPY_AND_ASSIGN(MessageChannel
);
204 } // namespace content
206 #endif // CONTENT_RENDERER_PEPPER_MESSAGE_CHANNEL_H_