OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef WEBKIT_PLUGINS_PPAPI_MESSAGE_CHANNEL_H_ | |
6 #define WEBKIT_PLUGINS_PPAPI_MESSAGE_CHANNEL_H_ | |
7 | |
8 #include <deque> | |
9 | |
10 #include "base/memory/weak_ptr.h" | |
11 #include "ppapi/shared_impl/resource.h" | |
12 #include "third_party/WebKit/public/web/WebSerializedScriptValue.h" | |
13 #include "third_party/npapi/bindings/npruntime.h" | |
14 | |
15 struct PP_Var; | |
16 | |
17 namespace webkit { | |
18 namespace ppapi { | |
19 | |
20 class PluginInstanceImpl; | |
21 | |
22 // MessageChannel implements bidirectional postMessage functionality, allowing | |
23 // calls from JavaScript to plugins and vice-versa. See | |
24 // PPB_Messaging::PostMessage and PPP_Messaging::HandleMessage for more | |
25 // information. | |
26 // | |
27 // Currently, only 1 MessageChannel can exist, to implement postMessage | |
28 // functionality for the instance interfaces. In the future, when we create a | |
29 // MessagePort type in PPAPI, those may be implemented here as well with some | |
30 // refactoring. | |
31 // - Separate message ports won't require the passthrough object. | |
32 // - The message target won't be limited to instance, and should support | |
33 // either plugin-provided or JS objects. | |
34 // TODO(dmichael): Add support for separate MessagePorts. | |
35 class MessageChannel { | |
36 public: | |
37 // MessageChannelNPObject is a simple struct that adds a pointer back to a | |
38 // MessageChannel instance. This way, we can use an NPObject to allow | |
39 // JavaScript interactions without forcing MessageChannel to inherit from | |
40 // NPObject. | |
41 struct MessageChannelNPObject : public NPObject { | |
42 MessageChannelNPObject(); | |
43 ~MessageChannelNPObject(); | |
44 | |
45 base::WeakPtr<MessageChannel> message_channel; | |
46 }; | |
47 | |
48 explicit MessageChannel(PluginInstanceImpl* instance); | |
49 ~MessageChannel(); | |
50 | |
51 // Post a message to the onmessage handler for this channel's instance | |
52 // asynchronously. | |
53 void PostMessageToJavaScript(PP_Var message_data); | |
54 // Post a message to the PPP_Instance HandleMessage function for this | |
55 // channel's instance. | |
56 void PostMessageToNative(PP_Var message_data); | |
57 | |
58 // Return the NPObject* to which we should forward any calls which aren't | |
59 // related to postMessage. Note that this can be NULL; it only gets set if | |
60 // there is a scriptable 'InstanceObject' associated with this channel's | |
61 // instance. | |
62 NPObject* passthrough_object() { | |
63 return passthrough_object_; | |
64 } | |
65 void SetPassthroughObject(NPObject* passthrough); | |
66 | |
67 NPObject* np_object() { return np_object_; } | |
68 | |
69 PluginInstanceImpl* instance() { | |
70 return instance_; | |
71 } | |
72 | |
73 // Messages sent to JavaScript are queued by default. After the DOM is | |
74 // set up for the plugin, users of MessageChannel should call | |
75 // StopQueueingJavaScriptMessages to start dispatching messages to JavaScript. | |
76 void QueueJavaScriptMessages(); | |
77 void StopQueueingJavaScriptMessages(); | |
78 | |
79 private: | |
80 PluginInstanceImpl* instance_; | |
81 | |
82 // We pass all non-postMessage calls through to the passthrough_object_. | |
83 // This way, a plugin can use PPB_Class or PPP_Class_Deprecated and also | |
84 // postMessage. This is necessary to support backwards-compatibility, and | |
85 // also trusted plugins for which we will continue to support synchronous | |
86 // scripting. | |
87 NPObject* passthrough_object_; | |
88 | |
89 // The NPObject we use to expose postMessage to JavaScript. | |
90 MessageChannelNPObject* np_object_; | |
91 | |
92 // Post a message to the onmessage handler for this channel's instance | |
93 // synchronously. This is used by PostMessageToJavaScript. | |
94 void PostMessageToJavaScriptImpl( | |
95 const WebKit::WebSerializedScriptValue& message_data); | |
96 // Post a message to the PPP_Instance HandleMessage function for this | |
97 // channel's instance. This is used by PostMessageToNative. | |
98 void PostMessageToNativeImpl(PP_Var message_data); | |
99 | |
100 void DrainEarlyMessageQueue(); | |
101 | |
102 // This is used to ensure pending tasks will not fire after this object is | |
103 // destroyed. | |
104 base::WeakPtrFactory<MessageChannel> weak_ptr_factory_; | |
105 | |
106 // TODO(teravest): Remove all the tricky DRAIN_CANCELLED logic once | |
107 // webkit::ppapi::PluginInstance::ResetAsProxied() is gone. | |
108 std::deque<WebKit::WebSerializedScriptValue> early_message_queue_; | |
109 enum EarlyMessageQueueState { | |
110 QUEUE_MESSAGES, // Queue JS messages. | |
111 SEND_DIRECTLY, // Post JS messages directly. | |
112 DRAIN_PENDING, // Drain queue, then transition to DIRECT. | |
113 DRAIN_CANCELLED // Preempt drain, go back to QUEUE. | |
114 }; | |
115 EarlyMessageQueueState early_message_queue_state_; | |
116 | |
117 DISALLOW_COPY_AND_ASSIGN(MessageChannel); | |
118 }; | |
119 | |
120 } // namespace ppapi | |
121 } // namespace webkit | |
122 | |
123 #endif // WEBKIT_PLUGINS_PPAPI_MESSAGE_CHANNEL_H_ | |
OLD | NEW |