OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_RENDERER_PEPPER_MESSAGE_CHANNEL_H_ | 5 #ifndef CONTENT_RENDERER_PEPPER_MESSAGE_CHANNEL_H_ |
6 #define CONTENT_RENDERER_PEPPER_MESSAGE_CHANNEL_H_ | 6 #define CONTENT_RENDERER_PEPPER_MESSAGE_CHANNEL_H_ |
7 | 7 |
8 #include <deque> | 8 #include <deque> |
9 #include <list> | 9 #include <list> |
10 #include <map> | 10 #include <map> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
14 #include "gin/handle.h" | 14 #include "gin/handle.h" |
15 #include "gin/interceptor.h" | 15 #include "gin/interceptor.h" |
16 #include "gin/wrappable.h" | 16 #include "gin/wrappable.h" |
| 17 #include "ppapi/proxy/host_dispatcher.h" |
17 #include "ppapi/shared_impl/resource.h" | 18 #include "ppapi/shared_impl/resource.h" |
18 #include "third_party/WebKit/public/web/WebSerializedScriptValue.h" | 19 #include "third_party/WebKit/public/web/WebSerializedScriptValue.h" |
19 #include "v8/include/v8.h" | 20 #include "v8/include/v8.h" |
20 | 21 |
21 struct PP_Var; | 22 struct PP_Var; |
22 | 23 |
23 namespace gin { | 24 namespace gin { |
24 class Arguments; | 25 class Arguments; |
25 } // namespace gin | 26 } // namespace gin |
26 | 27 |
(...skipping 12 matching lines...) Expand all Loading... |
39 // information. | 40 // information. |
40 // | 41 // |
41 // Currently, only 1 MessageChannel can exist, to implement postMessage | 42 // Currently, only 1 MessageChannel can exist, to implement postMessage |
42 // functionality for the instance interfaces. In the future, when we create a | 43 // functionality for the instance interfaces. In the future, when we create a |
43 // MessagePort type in PPAPI, those may be implemented here as well with some | 44 // MessagePort type in PPAPI, those may be implemented here as well with some |
44 // refactoring. | 45 // refactoring. |
45 // - Separate message ports won't require the passthrough object. | 46 // - Separate message ports won't require the passthrough object. |
46 // - The message target won't be limited to instance, and should support | 47 // - The message target won't be limited to instance, and should support |
47 // either plugin-provided or JS objects. | 48 // either plugin-provided or JS objects. |
48 // TODO(dmichael): Add support for separate MessagePorts. | 49 // TODO(dmichael): Add support for separate MessagePorts. |
49 class MessageChannel : public gin::Wrappable<MessageChannel>, | 50 class MessageChannel : |
50 public gin::NamedPropertyInterceptor { | 51 public gin::Wrappable<MessageChannel>, |
| 52 public gin::NamedPropertyInterceptor, |
| 53 public ppapi::proxy::HostDispatcher::SyncMessageStatusObserver { |
51 public: | 54 public: |
52 static gin::WrapperInfo kWrapperInfo; | 55 static gin::WrapperInfo kWrapperInfo; |
53 | 56 |
54 // Creates a MessageChannel, returning a pointer to it and sets |result| to | 57 // Creates a MessageChannel, returning a pointer to it and sets |result| to |
55 // the v8 object which is backed by the message channel. The returned pointer | 58 // the v8 object which is backed by the message channel. The returned pointer |
56 // is only valid as long as the object in |result| is alive. | 59 // is only valid as long as the object in |result| is alive. |
57 static MessageChannel* Create(PepperPluginInstanceImpl* instance, | 60 static MessageChannel* Create(PepperPluginInstanceImpl* instance, |
58 v8::Persistent<v8::Object>* result); | 61 v8::Persistent<v8::Object>* result); |
59 | 62 |
60 virtual ~MessageChannel(); | 63 virtual ~MessageChannel(); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 virtual bool SetNamedProperty(v8::Isolate* isolate, | 98 virtual bool SetNamedProperty(v8::Isolate* isolate, |
96 const std::string& property, | 99 const std::string& property, |
97 v8::Local<v8::Value> value) OVERRIDE; | 100 v8::Local<v8::Value> value) OVERRIDE; |
98 virtual std::vector<std::string> EnumerateNamedProperties( | 101 virtual std::vector<std::string> EnumerateNamedProperties( |
99 v8::Isolate* isolate) OVERRIDE; | 102 v8::Isolate* isolate) OVERRIDE; |
100 | 103 |
101 // gin::Wrappable | 104 // gin::Wrappable |
102 virtual gin::ObjectTemplateBuilder GetObjectTemplateBuilder( | 105 virtual gin::ObjectTemplateBuilder GetObjectTemplateBuilder( |
103 v8::Isolate* isolate) OVERRIDE; | 106 v8::Isolate* isolate) OVERRIDE; |
104 | 107 |
| 108 // ppapi::proxy::HostDispatcher::SyncMessageStatusObserver |
| 109 virtual void BeginBlockOnSyncMessage() OVERRIDE; |
| 110 virtual void EndBlockOnSyncMessage() OVERRIDE; |
| 111 |
105 // Post a message to the plugin's HandleMessage function for this channel's | 112 // Post a message to the plugin's HandleMessage function for this channel's |
106 // instance. | 113 // instance. |
107 void PostMessageToNative(gin::Arguments* args); | 114 void PostMessageToNative(gin::Arguments* args); |
108 // Post a message to the plugin's HandleBlocking Message function for this | 115 // Post a message to the plugin's HandleBlocking Message function for this |
109 // channel's instance synchronously, and return a result. | 116 // channel's instance synchronously, and return a result. |
110 void PostBlockingMessageToNative(gin::Arguments* args); | 117 void PostBlockingMessageToNative(gin::Arguments* args); |
111 | 118 |
112 // Post a message to the onmessage handler for this channel's instance | 119 // Post a message to the onmessage handler for this channel's instance |
113 // synchronously. This is used by PostMessageToJavaScript. | 120 // synchronously. This is used by PostMessageToJavaScript. |
114 void PostMessageToJavaScriptImpl( | 121 void PostMessageToJavaScriptImpl( |
115 const blink::WebSerializedScriptValue& message_data); | 122 const blink::WebSerializedScriptValue& message_data); |
116 | 123 |
117 PluginObject* GetPluginObject(v8::Isolate* isolate); | 124 PluginObject* GetPluginObject(v8::Isolate* isolate); |
118 | 125 |
119 void EnqueuePluginMessage(v8::Handle<v8::Value> v8_value); | 126 void EnqueuePluginMessage(v8::Handle<v8::Value> v8_value); |
120 | 127 |
121 void FromV8ValueComplete(VarConversionResult* result_holder, | 128 void FromV8ValueComplete(VarConversionResult* result_holder, |
122 const ppapi::ScopedPPVar& result_var, | 129 const ppapi::ScopedPPVar& result_var, |
123 bool success); | 130 bool success); |
124 | 131 |
| 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. |
125 void DrainCompletedPluginMessages(); | 135 void DrainCompletedPluginMessages(); |
126 void DrainEarlyMessageQueue(); | 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(); |
| 142 |
| 143 void UnregisterSyncMessageStatusObserver(); |
127 | 144 |
128 PepperPluginInstanceImpl* instance_; | 145 PepperPluginInstanceImpl* instance_; |
129 | 146 |
130 // We pass all non-postMessage calls through to the passthrough_object_. | 147 // We pass all non-postMessage calls through to the passthrough_object_. |
131 // This way, a plugin can use PPB_Class or PPP_Class_Deprecated and also | 148 // This way, a plugin can use PPB_Class or PPP_Class_Deprecated and also |
132 // postMessage. This is necessary to support backwards-compatibility, and | 149 // postMessage. This is necessary to support backwards-compatibility, and |
133 // also trusted plugins for which we will continue to support synchronous | 150 // also trusted plugins for which we will continue to support synchronous |
134 // scripting. | 151 // scripting. |
135 v8::Persistent<v8::Object> passthrough_object_; | 152 v8::Persistent<v8::Object> passthrough_object_; |
136 | 153 |
137 std::deque<blink::WebSerializedScriptValue> early_message_queue_; | 154 enum MessageQueueState { |
138 enum EarlyMessageQueueState { | 155 WAITING_TO_START, // Waiting for Start() to be called. Queue messages. |
139 QUEUE_MESSAGES, // Queue JS messages. | 156 QUEUE_MESSAGES, // Queue messages temporarily. |
140 SEND_DIRECTLY, // Post JS messages directly. | 157 SEND_DIRECTLY, // Post messages directly. |
141 }; | 158 }; |
142 EarlyMessageQueueState early_message_queue_state_; | 159 |
| 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_; |
143 | 169 |
144 // This queue stores vars that are being sent to the plugin. Because | 170 // This queue stores vars that are being sent to the plugin. Because |
145 // conversion can happen asynchronously for object types, the queue stores | 171 // conversion can happen asynchronously for object types, the queue stores |
146 // the var until all previous vars have been converted and sent. This | 172 // the var until all previous vars have been converted and sent. This |
147 // preserves the order in which JS->plugin messages are processed. | 173 // preserves the order in which JS->plugin messages are processed. |
148 // | 174 // |
149 // Note we rely on raw VarConversionResult* pointers remaining valid after | 175 // Note we rely on raw VarConversionResult* pointers remaining valid after |
150 // calls to push_back or pop_front; hence why we're using list. (deque would | 176 // calls to push_back or pop_front; hence why we're using list. (deque would |
151 // probably also work, but is less clearly specified). | 177 // probably also work, but is less clearly specified). |
152 std::list<VarConversionResult> plugin_message_queue_; | 178 std::list<VarConversionResult> plugin_message_queue_; |
| 179 MessageQueueState plugin_message_queue_state_; |
153 | 180 |
154 std::map<std::string, ppapi::ScopedPPVar> internal_named_properties_; | 181 std::map<std::string, ppapi::ScopedPPVar> internal_named_properties_; |
155 | 182 |
| 183 // A callback to invoke at shutdown to ensure we unregister ourselves as |
| 184 // Observers for sync messages. |
| 185 base::Closure unregister_observer_callback_; |
| 186 |
156 // This is used to ensure pending tasks will not fire after this object is | 187 // This is used to ensure pending tasks will not fire after this object is |
157 // destroyed. | 188 // destroyed. |
158 base::WeakPtrFactory<MessageChannel> weak_ptr_factory_; | 189 base::WeakPtrFactory<MessageChannel> weak_ptr_factory_; |
159 | 190 |
160 DISALLOW_COPY_AND_ASSIGN(MessageChannel); | 191 DISALLOW_COPY_AND_ASSIGN(MessageChannel); |
161 }; | 192 }; |
162 | 193 |
163 } // namespace content | 194 } // namespace content |
164 | 195 |
165 #endif // CONTENT_RENDERER_PEPPER_MESSAGE_CHANNEL_H_ | 196 #endif // CONTENT_RENDERER_PEPPER_MESSAGE_CHANNEL_H_ |
OLD | NEW |