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/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" 16 17 struct PP_Var; 18 19 namespace ppapi { 20 class ScopedPPVar; 21 } 22 23 namespace content { 24 25 class PepperPluginInstanceImpl; 26 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 30 // information. 31 // 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 35 // refactoring. 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 { 41 public: 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 45 // NPObject. 46 struct MessageChannelNPObject : public NPObject { 47 MessageChannelNPObject(); 48 ~MessageChannelNPObject(); 49 50 base::WeakPtr<MessageChannel> message_channel; 51 }; 52 53 explicit MessageChannel(PepperPluginInstanceImpl* instance); 54 ~MessageChannel(); 55 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); 59 60 // Post a message to the onmessage handler for this channel's instance 61 // asynchronously. 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); 66 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 70 // instance. passthrough_object()71 NPObject* passthrough_object() { 72 return passthrough_object_; 73 } 74 void SetPassthroughObject(NPObject* passthrough); 75 np_object()76 NPObject* np_object() { return np_object_; } 77 instance()78 PepperPluginInstanceImpl* instance() { 79 return instance_; 80 } 81 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(); 87 88 bool GetReadOnlyProperty(NPIdentifier key, NPVariant* value) const; 89 void SetReadOnlyProperty(PP_Var key, PP_Var value); 90 91 private: 92 // Struct for storing the result of a NPVariant being converted to a PP_Var. 93 struct VarConversionResult; 94 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, 101 bool success); 102 103 PepperPluginInstanceImpl* instance_; 104 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 109 // scripting. 110 NPObject* passthrough_object_; 111 112 // The NPObject we use to expose postMessage to JavaScript. 113 MessageChannelNPObject* np_object_; 114 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); 122 123 void DrainEarlyMessageQueue(); 124 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. 133 }; 134 EarlyMessageQueueState early_message_queue_state_; 135 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_; 141 142 std::map<NPIdentifier, ppapi::ScopedPPVar> internal_properties_; 143 144 // This is used to ensure pending tasks will not fire after this object is 145 // destroyed. 146 base::WeakPtrFactory<MessageChannel> weak_ptr_factory_; 147 148 DISALLOW_COPY_AND_ASSIGN(MessageChannel); 149 }; 150 151 } // namespace content 152 153 #endif // CONTENT_RENDERER_PEPPER_MESSAGE_CHANNEL_H_ 154