- add sources.
[platform/framework/web/crosswalk.git] / src / content / renderer / pepper / message_channel.h
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 CONTENT_RENDERER_PEPPER_MESSAGE_CHANNEL_H_
6 #define CONTENT_RENDERER_PEPPER_MESSAGE_CHANNEL_H_
7
8 #include <deque>
9 #include <list>
10
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"
15
16 struct PP_Var;
17
18 namespace ppapi {
19 class ScopedPPVar;
20 }
21
22 namespace content {
23
24 class PepperPluginInstanceImpl;
25
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.
30 //
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();
48
49     base::WeakPtr<MessageChannel> message_channel;
50   };
51
52   explicit MessageChannel(PepperPluginInstanceImpl* instance);
53   ~MessageChannel();
54
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);
58
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);
65
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_;
72   }
73   void SetPassthroughObject(NPObject* passthrough);
74
75   NPObject* np_object() { return np_object_; }
76
77   PepperPluginInstanceImpl* instance() {
78     return instance_;
79   }
80
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();
86
87  private:
88   // Struct for storing the result of a NPVariant being converted to a PP_Var.
89   struct VarConversionResult;
90
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);
98
99   PepperPluginInstanceImpl* instance_;
100
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_;
107
108   // The NPObject we use to expose postMessage to JavaScript.
109   MessageChannelNPObject* np_object_;
110
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);
118
119   void DrainEarlyMessageQueue();
120
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.
129   };
130   EarlyMessageQueueState early_message_queue_state_;
131
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_;
137
138   // This is used to ensure pending tasks will not fire after this object is
139   // destroyed.
140   base::WeakPtrFactory<MessageChannel> weak_ptr_factory_;
141
142   DISALLOW_COPY_AND_ASSIGN(MessageChannel);
143 };
144
145 }  // namespace content
146
147 #endif  // CONTENT_RENDERER_PEPPER_MESSAGE_CHANNEL_H_