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 |
125 void DrainCompletedPluginMessages(); | 132 void DrainCompletedPluginMessages(); |
126 void DrainEarlyMessageQueue(); | 133 void DrainJSMessageQueue(); |
134 void DrainJSMessageQueueSoon(); | |
raymes
2014/09/23 02:57:09
nit: a comment here might be good.
dmichael (off chromium)
2014/09/23 17:38:42
Done.
| |
127 | 135 |
128 PepperPluginInstanceImpl* instance_; | 136 PepperPluginInstanceImpl* instance_; |
129 | 137 |
130 // We pass all non-postMessage calls through to the passthrough_object_. | 138 // 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 | 139 // This way, a plugin can use PPB_Class or PPP_Class_Deprecated and also |
132 // postMessage. This is necessary to support backwards-compatibility, and | 140 // postMessage. This is necessary to support backwards-compatibility, and |
133 // also trusted plugins for which we will continue to support synchronous | 141 // also trusted plugins for which we will continue to support synchronous |
134 // scripting. | 142 // scripting. |
135 v8::Persistent<v8::Object> passthrough_object_; | 143 v8::Persistent<v8::Object> passthrough_object_; |
136 | 144 |
137 std::deque<blink::WebSerializedScriptValue> early_message_queue_; | 145 enum MessageQueueState { |
138 enum EarlyMessageQueueState { | 146 WAITING_TO_START, // Waiting for Start() to be called. Queue messages. |
139 QUEUE_MESSAGES, // Queue JS messages. | 147 QUEUE_MESSAGES, // Queue messages temporarily. |
140 SEND_DIRECTLY, // Post JS messages directly. | 148 SEND_DIRECTLY, // Post messages directly. |
141 }; | 149 }; |
142 EarlyMessageQueueState early_message_queue_state_; | 150 |
151 std::deque<blink::WebSerializedScriptValue> js_message_queue_; | |
raymes
2014/09/23 02:57:09
nit: Maybe just add a comment // This queue stores
dmichael (off chromium)
2014/09/23 17:38:42
Done.
| |
152 MessageQueueState js_message_queue_state_; | |
153 // When the renderer is sending a blocking message to the plugin, we will | |
154 // queue Plugin->JS messages temporarily to avoid re-entering JavaScript. This | |
155 // counts how many blocking renderer->plugin messages are on the stack so that | |
156 // we only begin sending messages to JavaScript again when the depth reaches | |
157 // zero. | |
158 int blocking_message_depth_; | |
143 | 159 |
144 // This queue stores vars that are being sent to the plugin. Because | 160 // This queue stores vars that are being sent to the plugin. Because |
145 // conversion can happen asynchronously for object types, the queue stores | 161 // conversion can happen asynchronously for object types, the queue stores |
146 // the var until all previous vars have been converted and sent. This | 162 // the var until all previous vars have been converted and sent. This |
147 // preserves the order in which JS->plugin messages are processed. | 163 // preserves the order in which JS->plugin messages are processed. |
148 // | 164 // |
149 // Note we rely on raw VarConversionResult* pointers remaining valid after | 165 // 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 | 166 // calls to push_back or pop_front; hence why we're using list. (deque would |
151 // probably also work, but is less clearly specified). | 167 // probably also work, but is less clearly specified). |
152 std::list<VarConversionResult> plugin_message_queue_; | 168 std::list<VarConversionResult> plugin_message_queue_; |
169 MessageQueueState plugin_message_queue_state_; | |
153 | 170 |
154 std::map<std::string, ppapi::ScopedPPVar> internal_named_properties_; | 171 std::map<std::string, ppapi::ScopedPPVar> internal_named_properties_; |
155 | 172 |
156 // This is used to ensure pending tasks will not fire after this object is | 173 // This is used to ensure pending tasks will not fire after this object is |
157 // destroyed. | 174 // destroyed. |
158 base::WeakPtrFactory<MessageChannel> weak_ptr_factory_; | 175 base::WeakPtrFactory<MessageChannel> weak_ptr_factory_; |
159 | 176 |
160 DISALLOW_COPY_AND_ASSIGN(MessageChannel); | 177 DISALLOW_COPY_AND_ASSIGN(MessageChannel); |
161 }; | 178 }; |
162 | 179 |
163 } // namespace content | 180 } // namespace content |
164 | 181 |
165 #endif // CONTENT_RENDERER_PEPPER_MESSAGE_CHANNEL_H_ | 182 #endif // CONTENT_RENDERER_PEPPER_MESSAGE_CHANNEL_H_ |
OLD | NEW |