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.
5 #ifndef CONTENT_RENDERER_PEPPER_MESSAGE_CHANNEL_H_
6 #define CONTENT_RENDERER_PEPPER_MESSAGE_CHANNEL_H_
12 #include "base/memory/weak_ptr.h"
13 #include "ppapi/shared_impl/resource.h"
14 #include "third_party/WebKit/public/web/WebSerializedScriptValue.h"
15 #include "third_party/npapi/bindings/npruntime.h"
25 class PepperPluginInstanceImpl;
27 // MessageChannel implements bidirectional postMessage functionality, allowing
28 // calls from JavaScript to plugins and vice-versa. See
29 // PPB_Messaging::PostMessage and PPP_Messaging::HandleMessage for more
32 // Currently, only 1 MessageChannel can exist, to implement postMessage
33 // 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
36 // - Separate message ports won't require the passthrough object.
37 // - The message target won't be limited to instance, and should support
38 // either plugin-provided or JS objects.
39 // TODO(dmichael): Add support for separate MessagePorts.
40 class MessageChannel {
42 // MessageChannelNPObject is a simple struct that adds a pointer back to a
43 // MessageChannel instance. This way, we can use an NPObject to allow
44 // JavaScript interactions without forcing MessageChannel to inherit from
46 struct MessageChannelNPObject : public NPObject {
47 MessageChannelNPObject();
48 ~MessageChannelNPObject();
50 base::WeakPtr<MessageChannel> message_channel;
53 explicit MessageChannel(PepperPluginInstanceImpl* instance);
56 // Converts an NPVariant to a PP_Var. This occurs asynchronously and
57 // NPVariantToPPVarComplete will be called upon completion.
58 void NPVariantToPPVar(const NPVariant* variant);
60 // Post a message to the onmessage handler for this channel's instance
62 void PostMessageToJavaScript(PP_Var message_data);
63 // Post a message to the PPP_Instance HandleMessage function for this
64 // channel's instance.
65 void PostMessageToNative(PP_Var message_data);
67 // Return the NPObject* to which we should forward any calls which aren't
68 // related to postMessage. Note that this can be NULL; it only gets set if
69 // there is a scriptable 'InstanceObject' associated with this channel's
71 NPObject* passthrough_object() {
72 return passthrough_object_;
74 void SetPassthroughObject(NPObject* passthrough);
76 NPObject* np_object() { return np_object_; }
78 PepperPluginInstanceImpl* instance() {
82 // Messages sent to JavaScript are queued by default. After the DOM is
83 // set up for the plugin, users of MessageChannel should call
84 // StopQueueingJavaScriptMessages to start dispatching messages to JavaScript.
85 void QueueJavaScriptMessages();
86 void StopQueueingJavaScriptMessages();
88 bool GetReadOnlyProperty(NPIdentifier key, NPVariant* value) const;
89 void SetReadOnlyProperty(PP_Var key, PP_Var value);
92 // Struct for storing the result of a NPVariant being converted to a PP_Var.
93 struct VarConversionResult;
95 // This is called when an NPVariant is finished being converted.
96 // |result_iteartor| is an iterator into |converted_var_queue_| where the
97 // result should be stored.
98 void NPVariantToPPVarComplete(
99 const std::list<VarConversionResult>::iterator& result_iterator,
100 const ppapi::ScopedPPVar& result,
103 PepperPluginInstanceImpl* instance_;
105 // We pass all non-postMessage calls through to the passthrough_object_.
106 // This way, a plugin can use PPB_Class or PPP_Class_Deprecated and also
107 // postMessage. This is necessary to support backwards-compatibility, and
108 // also trusted plugins for which we will continue to support synchronous
110 NPObject* passthrough_object_;
112 // The NPObject we use to expose postMessage to JavaScript.
113 MessageChannelNPObject* np_object_;
115 // Post a message to the onmessage handler for this channel's instance
116 // synchronously. This is used by PostMessageToJavaScript.
117 void PostMessageToJavaScriptImpl(
118 const blink::WebSerializedScriptValue& message_data);
119 // Post a message to the PPP_Instance HandleMessage function for this
120 // channel's instance. This is used by PostMessageToNative.
121 void PostMessageToNativeImpl(PP_Var message_data);
123 void DrainEarlyMessageQueue();
125 // TODO(teravest): Remove all the tricky DRAIN_CANCELLED logic once
126 // PluginInstance::ResetAsProxied() is gone.
127 std::deque<blink::WebSerializedScriptValue> early_message_queue_;
128 enum EarlyMessageQueueState {
129 QUEUE_MESSAGES, // Queue JS messages.
130 SEND_DIRECTLY, // Post JS messages directly.
131 DRAIN_PENDING, // Drain queue, then transition to DIRECT.
132 DRAIN_CANCELLED // Preempt drain, go back to QUEUE.
134 EarlyMessageQueueState early_message_queue_state_;
136 // This queue stores vars that have been converted from NPVariants. Because
137 // conversion can happen asynchronously, the queue stores the var until all
138 // previous vars have been converted before calling PostMessage to ensure that
139 // the order in which messages are processed is preserved.
140 std::list<VarConversionResult> converted_var_queue_;
142 std::map<NPIdentifier, ppapi::ScopedPPVar> internal_properties_;
144 // This is used to ensure pending tasks will not fire after this object is
146 base::WeakPtrFactory<MessageChannel> weak_ptr_factory_;
148 DISALLOW_COPY_AND_ASSIGN(MessageChannel);
151 } // namespace content
153 #endif // CONTENT_RENDERER_PEPPER_MESSAGE_CHANNEL_H_