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