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/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "gin/handle.h" |
| 15 #include "gin/interceptor.h" |
| 16 #include "gin/wrappable.h" |
13 #include "ppapi/shared_impl/resource.h" | 17 #include "ppapi/shared_impl/resource.h" |
14 #include "third_party/WebKit/public/web/WebSerializedScriptValue.h" | 18 #include "third_party/WebKit/public/web/WebSerializedScriptValue.h" |
15 #include "third_party/npapi/bindings/npruntime.h" | 19 #include "v8/include/v8.h" |
16 | 20 |
17 struct PP_Var; | 21 struct PP_Var; |
18 | 22 |
| 23 namespace gin { |
| 24 class Arguments; |
| 25 } // namespace gin |
| 26 |
19 namespace ppapi { | 27 namespace ppapi { |
20 class ScopedPPVar; | 28 class ScopedPPVar; |
21 } | 29 } // namespace ppapi |
22 | 30 |
23 namespace content { | 31 namespace content { |
24 | 32 |
25 class PepperPluginInstanceImpl; | 33 class PepperPluginInstanceImpl; |
| 34 class PluginObject; |
26 | 35 |
27 // MessageChannel implements bidirectional postMessage functionality, allowing | 36 // MessageChannel implements bidirectional postMessage functionality, allowing |
28 // calls from JavaScript to plugins and vice-versa. See | 37 // calls from JavaScript to plugins and vice-versa. See |
29 // PPB_Messaging::PostMessage and PPP_Messaging::HandleMessage for more | 38 // PPB_Messaging::PostMessage and PPP_Messaging::HandleMessage for more |
30 // information. | 39 // information. |
31 // | 40 // |
32 // Currently, only 1 MessageChannel can exist, to implement postMessage | 41 // Currently, only 1 MessageChannel can exist, to implement postMessage |
33 // 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 |
34 // 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 |
35 // refactoring. | 44 // refactoring. |
36 // - Separate message ports won't require the passthrough object. | 45 // - Separate message ports won't require the passthrough object. |
37 // - 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 |
38 // either plugin-provided or JS objects. | 47 // either plugin-provided or JS objects. |
39 // TODO(dmichael): Add support for separate MessagePorts. | 48 // TODO(dmichael): Add support for separate MessagePorts. |
40 class MessageChannel { | 49 class MessageChannel : public gin::Wrappable<MessageChannel>, |
| 50 public gin::NamedPropertyInterceptor, |
| 51 public gin::IndexedPropertyInterceptor { |
41 public: | 52 public: |
42 // MessageChannelNPObject is a simple struct that adds a pointer back to a | 53 static gin::WrapperInfo kWrapperInfo; |
43 // MessageChannel instance. This way, we can use an NPObject to allow | |
44 // JavaScript interactions without forcing MessageChannel to inherit from | |
45 // NPObject. | |
46 struct MessageChannelNPObject : public NPObject { | |
47 MessageChannelNPObject(); | |
48 ~MessageChannelNPObject(); | |
49 | 54 |
50 base::WeakPtr<MessageChannel> message_channel; | 55 static void Create(PepperPluginInstanceImpl* instance, |
51 }; | 56 v8::Persistent<v8::Object>* result); |
52 | 57 |
53 explicit MessageChannel(PepperPluginInstanceImpl* instance); | 58 virtual ~MessageChannel(); |
54 ~MessageChannel(); | 59 |
| 60 // gin::NamedPropertyInterceptor |
| 61 virtual v8::Local<v8::Value> GetNamedProperty( |
| 62 v8::Isolate* isolate, |
| 63 const std::string& property) OVERRIDE; |
| 64 virtual void SetNamedProperty(v8::Isolate* isolate, |
| 65 const std::string& property, |
| 66 v8::Local<v8::Value> value) OVERRIDE; |
| 67 virtual std::vector<std::string> EnumerateNamedProperties( |
| 68 v8::Isolate* isolate) OVERRIDE; |
| 69 |
| 70 // gin::IndexedPropertyInterceptor |
| 71 virtual v8::Local<v8::Value> GetIndexedProperty(v8::Isolate* isolate, |
| 72 uint32_t index) OVERRIDE; |
| 73 virtual void SetIndexedProperty(v8::Isolate* isolate, |
| 74 uint32_t index, |
| 75 v8::Local<v8::Value> value) OVERRIDE; |
| 76 virtual std::vector<uint32_t> EnumerateIndexedProperties( |
| 77 v8::Isolate* isolate) OVERRIDE; |
55 | 78 |
56 // Post a message to the onmessage handler for this channel's instance | 79 // Post a message to the onmessage handler for this channel's instance |
57 // asynchronously. | 80 // asynchronously. |
58 void PostMessageToJavaScript(PP_Var message_data); | 81 void PostMessageToJavaScript(PP_Var message_data); |
59 | 82 |
60 // Post a message to the plugin's HandleMessage function for this channel's | |
61 // instance. | |
62 void PostMessageToNative(const NPVariant* message_data); | |
63 // Post a message to the plugin's HandleBlocking Message function for this | |
64 // channel's instance synchronously, and return a result. | |
65 void PostBlockingMessageToNative(const NPVariant* message_data, | |
66 NPVariant* np_result); | |
67 | |
68 // Return the NPObject* to which we should forward any calls which aren't | |
69 // related to postMessage. Note that this can be NULL; it only gets set if | |
70 // there is a scriptable 'InstanceObject' associated with this channel's | |
71 // instance. | |
72 NPObject* passthrough_object() { return passthrough_object_; } | |
73 void SetPassthroughObject(NPObject* passthrough); | |
74 | |
75 NPObject* np_object() { return np_object_; } | |
76 | |
77 PepperPluginInstanceImpl* instance() { return instance_; } | |
78 | |
79 // Messages are queued initially. After the PepperPluginInstanceImpl is ready | 83 // Messages are queued initially. After the PepperPluginInstanceImpl is ready |
80 // to send and handle messages, users of MessageChannel should call | 84 // to send and handle messages, users of MessageChannel should call |
81 // Start(). | 85 // Start(). |
82 void Start(); | 86 void Start(); |
83 | 87 |
84 bool GetReadOnlyProperty(NPIdentifier key, NPVariant* value) const; | 88 // Set the V8Object to which we should forward any calls which aren't |
| 89 // related to postMessage. Note that this can be empty; it only gets set if |
| 90 // there is a scriptable 'InstanceObject' associated with this channel's |
| 91 // instance. |
| 92 void SetPassthroughObject(v8::Handle<v8::Object> passthrough); |
| 93 |
| 94 PepperPluginInstanceImpl* instance() { return instance_; } |
| 95 |
85 void SetReadOnlyProperty(PP_Var key, PP_Var value); | 96 void SetReadOnlyProperty(PP_Var key, PP_Var value); |
86 | 97 |
87 private: | 98 private: |
88 // Struct for storing the result of a NPVariant being converted to a PP_Var. | 99 // Struct for storing the result of a v8 object being converted to a PP_Var. |
89 struct VarConversionResult; | 100 struct VarConversionResult; |
90 | 101 |
91 void EnqueuePluginMessage(const NPVariant* variant); | 102 explicit MessageChannel(PepperPluginInstanceImpl* instance); |
| 103 |
| 104 // gin::Wrappable |
| 105 virtual gin::ObjectTemplateBuilder GetObjectTemplateBuilder( |
| 106 v8::Isolate* isolate) OVERRIDE; |
| 107 |
| 108 // Post a message to the plugin's HandleMessage function for this channel's |
| 109 // instance. |
| 110 void PostMessageToNative(gin::Arguments* args); |
| 111 // Post a message to the plugin's HandleBlocking Message function for this |
| 112 // channel's instance synchronously, and return a result. |
| 113 void PostBlockingMessageToNative(gin::Arguments* args); |
| 114 |
| 115 PluginObject* GetPluginObject(v8::Isolate* isolate); |
| 116 |
| 117 void EnqueuePluginMessage(v8::Handle<v8::Value> v8_value); |
92 | 118 |
93 void FromV8ValueComplete(VarConversionResult* result_holder, | 119 void FromV8ValueComplete(VarConversionResult* result_holder, |
94 const ppapi::ScopedPPVar& result_var, | 120 const ppapi::ScopedPPVar& result_var, |
95 bool success); | 121 bool success); |
96 void DrainCompletedPluginMessages(); | 122 void DrainCompletedPluginMessages(); |
| 123 void DrainEarlyMessageQueue(); |
| 124 |
| 125 // Post a message to the onmessage handler for this channel's instance |
| 126 // synchronously. This is used by PostMessageToJavaScript. |
| 127 void PostMessageToJavaScriptImpl( |
| 128 const blink::WebSerializedScriptValue& message_data); |
97 | 129 |
98 PepperPluginInstanceImpl* instance_; | 130 PepperPluginInstanceImpl* instance_; |
99 | 131 |
100 // We pass all non-postMessage calls through to the passthrough_object_. | 132 // We pass all non-postMessage calls through to the passthrough_object_. |
101 // This way, a plugin can use PPB_Class or PPP_Class_Deprecated and also | 133 // This way, a plugin can use PPB_Class or PPP_Class_Deprecated and also |
102 // postMessage. This is necessary to support backwards-compatibility, and | 134 // postMessage. This is necessary to support backwards-compatibility, and |
103 // also trusted plugins for which we will continue to support synchronous | 135 // also trusted plugins for which we will continue to support synchronous |
104 // scripting. | 136 // scripting. |
105 NPObject* passthrough_object_; | 137 v8::Persistent<v8::Object> passthrough_object_; |
106 | |
107 // The NPObject we use to expose postMessage to JavaScript. | |
108 MessageChannelNPObject* np_object_; | |
109 | |
110 // Post a message to the onmessage handler for this channel's instance | |
111 // synchronously. This is used by PostMessageToJavaScript. | |
112 void PostMessageToJavaScriptImpl( | |
113 const blink::WebSerializedScriptValue& message_data); | |
114 // Post a message to the PPP_Instance HandleMessage function for this | |
115 // channel's instance. This is used by PostMessageToNative. | |
116 void PostMessageToNativeImpl(PP_Var message_data); | |
117 | |
118 void DrainEarlyMessageQueue(); | |
119 | 138 |
120 std::deque<blink::WebSerializedScriptValue> early_message_queue_; | 139 std::deque<blink::WebSerializedScriptValue> early_message_queue_; |
121 enum EarlyMessageQueueState { | 140 enum EarlyMessageQueueState { |
122 QUEUE_MESSAGES, // Queue JS messages. | 141 QUEUE_MESSAGES, // Queue JS messages. |
123 SEND_DIRECTLY, // Post JS messages directly. | 142 SEND_DIRECTLY, // Post JS messages directly. |
124 }; | 143 }; |
125 EarlyMessageQueueState early_message_queue_state_; | 144 EarlyMessageQueueState early_message_queue_state_; |
126 | 145 |
127 // This queue stores vars that are being sent to the plugin. Because | 146 // This queue stores vars that are being sent to the plugin. Because |
128 // conversion can happen asynchronously for object types, the queue stores | 147 // conversion can happen asynchronously for object types, the queue stores |
129 // the var until all previous vars have been converted and sent. This | 148 // the var until all previous vars have been converted and sent. This |
130 // preserves the order in which JS->plugin messages are processed. | 149 // preserves the order in which JS->plugin messages are processed. |
131 // | 150 // |
132 // Note we rely on raw VarConversionResult* pointers remaining valid after | 151 // Note we rely on raw VarConversionResult* pointers remaining valid after |
133 // calls to push_back or pop_front; hence why we're using list. (deque would | 152 // calls to push_back or pop_front; hence why we're using list. (deque would |
134 // probably also work, but is less clearly specified). | 153 // probably also work, but is less clearly specified). |
135 std::list<VarConversionResult> plugin_message_queue_; | 154 std::list<VarConversionResult> plugin_message_queue_; |
136 | 155 |
137 std::map<NPIdentifier, ppapi::ScopedPPVar> internal_properties_; | 156 std::map<std::string, ppapi::ScopedPPVar> internal_named_properties_; |
| 157 std::map<uint32_t, ppapi::ScopedPPVar> internal_indexed_properties_; |
138 | 158 |
139 // This is used to ensure pending tasks will not fire after this object is | 159 // This is used to ensure pending tasks will not fire after this object is |
140 // destroyed. | 160 // destroyed. |
141 base::WeakPtrFactory<MessageChannel> weak_ptr_factory_; | 161 base::WeakPtrFactory<MessageChannel> weak_ptr_factory_; |
142 | 162 |
143 DISALLOW_COPY_AND_ASSIGN(MessageChannel); | 163 DISALLOW_COPY_AND_ASSIGN(MessageChannel); |
144 }; | 164 }; |
145 | 165 |
146 } // namespace content | 166 } // namespace content |
147 | 167 |
148 #endif // CONTENT_RENDERER_PEPPER_MESSAGE_CHANNEL_H_ | 168 #endif // CONTENT_RENDERER_PEPPER_MESSAGE_CHANNEL_H_ |
| 169 |
OLD | NEW |