Refactor management of overview window copy lifetime into a separate class.
[chromium-blink-merge.git] / content / renderer / pepper / message_channel.h
blobf577224ff7ab0e6c35086acaefe6337b8668ecd9
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_
8 #include <deque>
9 #include <list>
11 #include "base/memory/weak_ptr.h"
12 #include "ppapi/shared_impl/resource.h"
13 #include "third_party/WebKit/public/web/WebSerializedScriptValue.h"
14 #include "third_party/npapi/bindings/npruntime.h"
16 struct PP_Var;
18 namespace ppapi {
19 class ScopedPPVar;
22 namespace content {
24 class PepperPluginInstanceImpl;
26 // MessageChannel implements bidirectional postMessage functionality, allowing
27 // calls from JavaScript to plugins and vice-versa. See
28 // PPB_Messaging::PostMessage and PPP_Messaging::HandleMessage for more
29 // information.
31 // Currently, only 1 MessageChannel can exist, to implement postMessage
32 // functionality for the instance interfaces. In the future, when we create a
33 // MessagePort type in PPAPI, those may be implemented here as well with some
34 // refactoring.
35 // - Separate message ports won't require the passthrough object.
36 // - The message target won't be limited to instance, and should support
37 // either plugin-provided or JS objects.
38 // TODO(dmichael): Add support for separate MessagePorts.
39 class MessageChannel {
40 public:
41 // MessageChannelNPObject is a simple struct that adds a pointer back to a
42 // MessageChannel instance. This way, we can use an NPObject to allow
43 // JavaScript interactions without forcing MessageChannel to inherit from
44 // NPObject.
45 struct MessageChannelNPObject : public NPObject {
46 MessageChannelNPObject();
47 ~MessageChannelNPObject();
49 base::WeakPtr<MessageChannel> message_channel;
52 explicit MessageChannel(PepperPluginInstanceImpl* instance);
53 ~MessageChannel();
55 // Converts an NPVariant to a PP_Var. This occurs asynchronously and
56 // NPVariantToPPVarComplete will be called upon completion.
57 void NPVariantToPPVar(const NPVariant* variant);
59 // Post a message to the onmessage handler for this channel's instance
60 // asynchronously.
61 void PostMessageToJavaScript(PP_Var message_data);
62 // Post a message to the PPP_Instance HandleMessage function for this
63 // channel's instance.
64 void PostMessageToNative(PP_Var message_data);
66 // Return the NPObject* to which we should forward any calls which aren't
67 // related to postMessage. Note that this can be NULL; it only gets set if
68 // there is a scriptable 'InstanceObject' associated with this channel's
69 // instance.
70 NPObject* passthrough_object() {
71 return passthrough_object_;
73 void SetPassthroughObject(NPObject* passthrough);
75 NPObject* np_object() { return np_object_; }
77 PepperPluginInstanceImpl* instance() {
78 return instance_;
81 // Messages sent to JavaScript are queued by default. After the DOM is
82 // set up for the plugin, users of MessageChannel should call
83 // StopQueueingJavaScriptMessages to start dispatching messages to JavaScript.
84 void QueueJavaScriptMessages();
85 void StopQueueingJavaScriptMessages();
87 private:
88 // Struct for storing the result of a NPVariant being converted to a PP_Var.
89 struct VarConversionResult;
91 // This is called when an NPVariant is finished being converted.
92 // |result_iteartor| is an iterator into |converted_var_queue_| where the
93 // result should be stored.
94 void NPVariantToPPVarComplete(
95 const std::list<VarConversionResult>::iterator& result_iterator,
96 const ppapi::ScopedPPVar& result,
97 bool success);
99 PepperPluginInstanceImpl* instance_;
101 // We pass all non-postMessage calls through to the passthrough_object_.
102 // This way, a plugin can use PPB_Class or PPP_Class_Deprecated and also
103 // postMessage. This is necessary to support backwards-compatibility, and
104 // also trusted plugins for which we will continue to support synchronous
105 // scripting.
106 NPObject* passthrough_object_;
108 // The NPObject we use to expose postMessage to JavaScript.
109 MessageChannelNPObject* np_object_;
111 // Post a message to the onmessage handler for this channel's instance
112 // synchronously. This is used by PostMessageToJavaScript.
113 void PostMessageToJavaScriptImpl(
114 const WebKit::WebSerializedScriptValue& message_data);
115 // Post a message to the PPP_Instance HandleMessage function for this
116 // channel's instance. This is used by PostMessageToNative.
117 void PostMessageToNativeImpl(PP_Var message_data);
119 void DrainEarlyMessageQueue();
121 // TODO(teravest): Remove all the tricky DRAIN_CANCELLED logic once
122 // PluginInstance::ResetAsProxied() is gone.
123 std::deque<WebKit::WebSerializedScriptValue> early_message_queue_;
124 enum EarlyMessageQueueState {
125 QUEUE_MESSAGES, // Queue JS messages.
126 SEND_DIRECTLY, // Post JS messages directly.
127 DRAIN_PENDING, // Drain queue, then transition to DIRECT.
128 DRAIN_CANCELLED // Preempt drain, go back to QUEUE.
130 EarlyMessageQueueState early_message_queue_state_;
132 // This queue stores vars that have been converted from NPVariants. Because
133 // conversion can happen asynchronously, the queue stores the var until all
134 // previous vars have been converted before calling PostMessage to ensure that
135 // the order in which messages are processed is preserved.
136 std::list<VarConversionResult> converted_var_queue_;
138 // This is used to ensure pending tasks will not fire after this object is
139 // destroyed.
140 base::WeakPtrFactory<MessageChannel> weak_ptr_factory_;
142 DISALLOW_COPY_AND_ASSIGN(MessageChannel);
145 } // namespace content
147 #endif // CONTENT_RENDERER_PEPPER_MESSAGE_CHANNEL_H_