| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_RENDERER_PEPPER_MESSAGE_CHANNEL_H_ | 5 #ifndef CONTENT_RENDERER_PEPPER_MESSAGE_CHANNEL_H_ |
| 6 #define CONTENT_RENDERER_PEPPER_MESSAGE_CHANNEL_H_ | 6 #define CONTENT_RENDERER_PEPPER_MESSAGE_CHANNEL_H_ |
| 7 | 7 |
| 8 #include <deque> | 8 #include <deque> |
| 9 #include <list> | 9 #include <list> |
| 10 #include <map> | 10 #include <map> |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 struct MessageChannelNPObject : public NPObject { | 46 struct MessageChannelNPObject : public NPObject { |
| 47 MessageChannelNPObject(); | 47 MessageChannelNPObject(); |
| 48 ~MessageChannelNPObject(); | 48 ~MessageChannelNPObject(); |
| 49 | 49 |
| 50 base::WeakPtr<MessageChannel> message_channel; | 50 base::WeakPtr<MessageChannel> message_channel; |
| 51 }; | 51 }; |
| 52 | 52 |
| 53 explicit MessageChannel(PepperPluginInstanceImpl* instance); | 53 explicit MessageChannel(PepperPluginInstanceImpl* instance); |
| 54 ~MessageChannel(); | 54 ~MessageChannel(); |
| 55 | 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 | 56 // Post a message to the onmessage handler for this channel's instance |
| 61 // asynchronously. | 57 // asynchronously. |
| 62 void PostMessageToJavaScript(PP_Var message_data); | 58 void PostMessageToJavaScript(PP_Var message_data); |
| 63 // Post a message to the PPP_Instance HandleMessage function for this | 59 |
| 64 // channel's instance. | 60 // Post a message to the plugin's HandleMessage function for this channel's |
| 65 void PostMessageToNative(PP_Var message_data); | 61 // instance. |
| 62 void PostMessageToNative(const NPVariant* message_data); |
| 66 | 63 |
| 67 // Return the NPObject* to which we should forward any calls which aren't | 64 // 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 | 65 // 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 | 66 // there is a scriptable 'InstanceObject' associated with this channel's |
| 70 // instance. | 67 // instance. |
| 71 NPObject* passthrough_object() { return passthrough_object_; } | 68 NPObject* passthrough_object() { return passthrough_object_; } |
| 72 void SetPassthroughObject(NPObject* passthrough); | 69 void SetPassthroughObject(NPObject* passthrough); |
| 73 | 70 |
| 74 NPObject* np_object() { return np_object_; } | 71 NPObject* np_object() { return np_object_; } |
| 75 | 72 |
| 76 PepperPluginInstanceImpl* instance() { return instance_; } | 73 PepperPluginInstanceImpl* instance() { return instance_; } |
| 77 | 74 |
| 78 // Messages sent to JavaScript are queued by default. After the DOM is | 75 // Messages are queued initially. After the PepperPluginInstanceImpl is ready |
| 79 // set up for the plugin, users of MessageChannel should call | 76 // to send and handle messages, users of MessageChannel should call |
| 80 // StopQueueingJavaScriptMessages to start dispatching messages to JavaScript. | 77 // Start(). |
| 81 void QueueJavaScriptMessages(); | 78 void Start(); |
| 82 void StopQueueingJavaScriptMessages(); | |
| 83 | 79 |
| 84 bool GetReadOnlyProperty(NPIdentifier key, NPVariant* value) const; | 80 bool GetReadOnlyProperty(NPIdentifier key, NPVariant* value) const; |
| 85 void SetReadOnlyProperty(PP_Var key, PP_Var value); | 81 void SetReadOnlyProperty(PP_Var key, PP_Var value); |
| 86 | 82 |
| 87 private: | 83 private: |
| 88 // Struct for storing the result of a NPVariant being converted to a PP_Var. | 84 // Struct for storing the result of a NPVariant being converted to a PP_Var. |
| 89 struct VarConversionResult; | 85 struct VarConversionResult; |
| 90 | 86 |
| 91 // This is called when an NPVariant is finished being converted. | 87 void EnqueuePluginMessage(const NPVariant* variant); |
| 92 // |result_iteartor| is an iterator into |converted_var_queue_| where the | 88 |
| 93 // result should be stored. | 89 void FromV8ValueComplete(VarConversionResult* result_holder, |
| 94 void NPVariantToPPVarComplete( | 90 const ppapi::ScopedPPVar& result_var, |
| 95 const std::list<VarConversionResult>::iterator& result_iterator, | 91 bool success); |
| 96 const ppapi::ScopedPPVar& result, | 92 void DrainCompletedPluginMessages(); |
| 97 bool success); | |
| 98 | 93 |
| 99 PepperPluginInstanceImpl* instance_; | 94 PepperPluginInstanceImpl* instance_; |
| 100 | 95 |
| 101 // We pass all non-postMessage calls through to the passthrough_object_. | 96 // 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 | 97 // This way, a plugin can use PPB_Class or PPP_Class_Deprecated and also |
| 103 // postMessage. This is necessary to support backwards-compatibility, and | 98 // postMessage. This is necessary to support backwards-compatibility, and |
| 104 // also trusted plugins for which we will continue to support synchronous | 99 // also trusted plugins for which we will continue to support synchronous |
| 105 // scripting. | 100 // scripting. |
| 106 NPObject* passthrough_object_; | 101 NPObject* passthrough_object_; |
| 107 | 102 |
| 108 // The NPObject we use to expose postMessage to JavaScript. | 103 // The NPObject we use to expose postMessage to JavaScript. |
| 109 MessageChannelNPObject* np_object_; | 104 MessageChannelNPObject* np_object_; |
| 110 | 105 |
| 111 // Post a message to the onmessage handler for this channel's instance | 106 // Post a message to the onmessage handler for this channel's instance |
| 112 // synchronously. This is used by PostMessageToJavaScript. | 107 // synchronously. This is used by PostMessageToJavaScript. |
| 113 void PostMessageToJavaScriptImpl( | 108 void PostMessageToJavaScriptImpl( |
| 114 const blink::WebSerializedScriptValue& message_data); | 109 const blink::WebSerializedScriptValue& message_data); |
| 115 // Post a message to the PPP_Instance HandleMessage function for this | 110 // Post a message to the PPP_Instance HandleMessage function for this |
| 116 // channel's instance. This is used by PostMessageToNative. | 111 // channel's instance. This is used by PostMessageToNative. |
| 117 void PostMessageToNativeImpl(PP_Var message_data); | 112 void PostMessageToNativeImpl(PP_Var message_data); |
| 118 | 113 |
| 119 void DrainEarlyMessageQueue(); | 114 void DrainEarlyMessageQueue(); |
| 120 | 115 |
| 121 // TODO(teravest): Remove all the tricky DRAIN_CANCELLED logic once | |
| 122 // PluginInstance::ResetAsProxied() is gone. | |
| 123 std::deque<blink::WebSerializedScriptValue> early_message_queue_; | 116 std::deque<blink::WebSerializedScriptValue> early_message_queue_; |
| 124 enum EarlyMessageQueueState { | 117 enum EarlyMessageQueueState { |
| 125 QUEUE_MESSAGES, // Queue JS messages. | 118 QUEUE_MESSAGES, // Queue JS messages. |
| 126 SEND_DIRECTLY, // Post JS messages directly. | 119 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 }; | 120 }; |
| 130 EarlyMessageQueueState early_message_queue_state_; | 121 EarlyMessageQueueState early_message_queue_state_; |
| 131 | 122 |
| 132 // This queue stores vars that have been converted from NPVariants. Because | 123 // This queue stores vars that are being sent to the plugin. Because |
| 133 // conversion can happen asynchronously, the queue stores the var until all | 124 // conversion can happen asynchronously for object types, the queue stores |
| 134 // previous vars have been converted before calling PostMessage to ensure that | 125 // the var until all previous vars have been converted and sent. This |
| 135 // the order in which messages are processed is preserved. | 126 // preserves the order in which JS->plugin messages are processed. |
| 136 std::list<VarConversionResult> converted_var_queue_; | 127 // |
| 128 // Note we rely on raw VarConversionResult* pointers remaining valid after |
| 129 // calls to push_back or pop_front; hence why we're using list. (deque would |
| 130 // probably also work, but is less clearly specified). |
| 131 std::list<VarConversionResult> plugin_message_queue_; |
| 137 | 132 |
| 138 std::map<NPIdentifier, ppapi::ScopedPPVar> internal_properties_; | 133 std::map<NPIdentifier, ppapi::ScopedPPVar> internal_properties_; |
| 139 | 134 |
| 140 // This is used to ensure pending tasks will not fire after this object is | 135 // This is used to ensure pending tasks will not fire after this object is |
| 141 // destroyed. | 136 // destroyed. |
| 142 base::WeakPtrFactory<MessageChannel> weak_ptr_factory_; | 137 base::WeakPtrFactory<MessageChannel> weak_ptr_factory_; |
| 143 | 138 |
| 144 DISALLOW_COPY_AND_ASSIGN(MessageChannel); | 139 DISALLOW_COPY_AND_ASSIGN(MessageChannel); |
| 145 }; | 140 }; |
| 146 | 141 |
| 147 } // namespace content | 142 } // namespace content |
| 148 | 143 |
| 149 #endif // CONTENT_RENDERER_PEPPER_MESSAGE_CHANNEL_H_ | 144 #endif // CONTENT_RENDERER_PEPPER_MESSAGE_CHANNEL_H_ |
| OLD | NEW |