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