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#include <map>
11
12#include "base/basictypes.h"
13#include "base/memory/weak_ptr.h"
14#include "gin/handle.h"
15#include "gin/interceptor.h"
16#include "gin/wrappable.h"
17#include "ppapi/proxy/host_dispatcher.h"
18#include "ppapi/shared_impl/resource.h"
19#include "third_party/WebKit/public/web/WebSerializedScriptValue.h"
20#include "v8/include/v8.h"
21
22struct PP_Var;
23
24namespace gin {
25class Arguments;
26}  // namespace gin
27
28namespace ppapi {
29class ScopedPPVar;
30}  // namespace ppapi
31
32namespace content {
33
34class PepperPluginInstanceImpl;
35class PluginObject;
36
37// MessageChannel implements bidirectional postMessage functionality, allowing
38// calls from JavaScript to plugins and vice-versa. See
39// PPB_Messaging::PostMessage and PPP_Messaging::HandleMessage for more
40// information.
41//
42// Currently, only 1 MessageChannel can exist, to implement postMessage
43// functionality for the instance interfaces.  In the future, when we create a
44// MessagePort type in PPAPI, those may be implemented here as well with some
45// refactoring.
46//   - Separate message ports won't require the passthrough object.
47//   - The message target won't be limited to instance, and should support
48//     either plugin-provided or JS objects.
49// TODO(dmichael):  Add support for separate MessagePorts.
50class MessageChannel :
51    public gin::Wrappable<MessageChannel>,
52    public gin::NamedPropertyInterceptor,
53    public ppapi::proxy::HostDispatcher::SyncMessageStatusObserver {
54 public:
55  static gin::WrapperInfo kWrapperInfo;
56
57  // Creates a MessageChannel, returning a pointer to it and sets |result| to
58  // the v8 object which is backed by the message channel. The returned pointer
59  // is only valid as long as the object in |result| is alive.
60  static MessageChannel* Create(PepperPluginInstanceImpl* instance,
61                                v8::Persistent<v8::Object>* result);
62
63  virtual ~MessageChannel();
64
65  // Called when the instance is deleted. The MessageChannel might outlive the
66  // plugin instance because it is garbage collected.
67  void InstanceDeleted();
68
69  // Post a message to the onmessage handler for this channel's instance
70  // asynchronously.
71  void PostMessageToJavaScript(PP_Var message_data);
72
73  // Messages are queued initially. After the PepperPluginInstanceImpl is ready
74  // to send and handle messages, users of MessageChannel should call
75  // Start().
76  void Start();
77
78  // Set the V8Object to which we should forward any calls which aren't
79  // related to postMessage. Note that this can be empty; it only gets set if
80  // there is a scriptable 'InstanceObject' associated with this channel's
81  // instance.
82  void SetPassthroughObject(v8::Handle<v8::Object> passthrough);
83
84  PepperPluginInstanceImpl* instance() { return instance_; }
85
86  void SetReadOnlyProperty(PP_Var key, PP_Var value);
87
88 private:
89  // Struct for storing the result of a v8 object being converted to a PP_Var.
90  struct VarConversionResult;
91
92  explicit MessageChannel(PepperPluginInstanceImpl* instance);
93
94  // gin::NamedPropertyInterceptor
95  virtual v8::Local<v8::Value> GetNamedProperty(
96      v8::Isolate* isolate,
97      const std::string& property) OVERRIDE;
98  virtual bool SetNamedProperty(v8::Isolate* isolate,
99                                const std::string& property,
100                                v8::Local<v8::Value> value) OVERRIDE;
101  virtual std::vector<std::string> EnumerateNamedProperties(
102      v8::Isolate* isolate) OVERRIDE;
103
104  // gin::Wrappable
105  virtual gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
106      v8::Isolate* isolate) OVERRIDE;
107
108  // ppapi::proxy::HostDispatcher::SyncMessageStatusObserver
109  virtual void BeginBlockOnSyncMessage() OVERRIDE;
110  virtual void EndBlockOnSyncMessage() OVERRIDE;
111
112  // Post a message to the plugin's HandleMessage function for this channel's
113  // instance.
114  void PostMessageToNative(gin::Arguments* args);
115  // Post a message to the plugin's HandleBlocking Message function for this
116  // channel's instance synchronously, and return a result.
117  void PostBlockingMessageToNative(gin::Arguments* args);
118
119  // Post a message to the onmessage handler for this channel's instance
120  // synchronously.  This is used by PostMessageToJavaScript.
121  void PostMessageToJavaScriptImpl(
122      const blink::WebSerializedScriptValue& message_data);
123
124  PluginObject* GetPluginObject(v8::Isolate* isolate);
125
126  void EnqueuePluginMessage(v8::Handle<v8::Value> v8_value);
127
128  void FromV8ValueComplete(VarConversionResult* result_holder,
129                           const ppapi::ScopedPPVar& result_var,
130                           bool success);
131
132  // Drain the queue of messages that are going to the plugin. All "completed"
133  // messages at the head of the queue will be sent; any messages awaiting
134  // conversion as well as messages after that in the queue will not be sent.
135  void DrainCompletedPluginMessages();
136  // Drain the queue of messages that are going to JavaScript.
137  void DrainJSMessageQueue();
138  // PostTask to call DrainJSMessageQueue() soon. Use this when you want to
139  // send the messages, but can't immediately (e.g., because the instance is
140  // not ready or JavaScript is on the stack).
141  void DrainJSMessageQueueSoon();
142
143  void UnregisterSyncMessageStatusObserver();
144
145  PepperPluginInstanceImpl* instance_;
146
147  // We pass all non-postMessage calls through to the passthrough_object_.
148  // This way, a plugin can use PPB_Class or PPP_Class_Deprecated and also
149  // postMessage.  This is necessary to support backwards-compatibility, and
150  // also trusted plugins for which we will continue to support synchronous
151  // scripting.
152  v8::Persistent<v8::Object> passthrough_object_;
153
154  enum MessageQueueState {
155    WAITING_TO_START,  // Waiting for Start() to be called. Queue messages.
156    QUEUE_MESSAGES,  // Queue messages temporarily.
157    SEND_DIRECTLY,   // Post messages directly.
158  };
159
160  // This queue stores values being posted to JavaScript.
161  std::deque<blink::WebSerializedScriptValue> js_message_queue_;
162  MessageQueueState js_message_queue_state_;
163  // When the renderer is sending a blocking message to the plugin, we will
164  // queue Plugin->JS messages temporarily to avoid re-entering JavaScript. This
165  // counts how many blocking renderer->plugin messages are on the stack so that
166  // we only begin sending messages to JavaScript again when the depth reaches
167  // zero.
168  int blocking_message_depth_;
169
170  // This queue stores vars that are being sent to the plugin. Because
171  // conversion can happen asynchronously for object types, the queue stores
172  // the var until all previous vars have been converted and sent. This
173  // preserves the order in which JS->plugin messages are processed.
174  //
175  // Note we rely on raw VarConversionResult* pointers remaining valid after
176  // calls to push_back or pop_front; hence why we're using list. (deque would
177  // probably also work, but is less clearly specified).
178  std::list<VarConversionResult> plugin_message_queue_;
179  MessageQueueState plugin_message_queue_state_;
180
181  std::map<std::string, ppapi::ScopedPPVar> internal_named_properties_;
182
183  // A callback to invoke at shutdown to ensure we unregister ourselves as
184  // Observers for sync messages.
185  base::Closure unregister_observer_callback_;
186
187  // This is used to ensure pending tasks will not fire after this object is
188  // destroyed.
189  base::WeakPtrFactory<MessageChannel> weak_ptr_factory_;
190
191  DISALLOW_COPY_AND_ASSIGN(MessageChannel);
192};
193
194}  // namespace content
195
196#endif  // CONTENT_RENDERER_PEPPER_MESSAGE_CHANNEL_H_
197