OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 WEBKIT_PLUGINS_PPAPI_MESSAGE_CHANNEL_H_ |
| 6 #define WEBKIT_PLUGINS_PPAPI_MESSAGE_CHANNEL_H_ |
| 7 |
| 8 #include "third_party/npapi/bindings/npruntime.h" |
| 9 #include "webkit/plugins/ppapi/resource.h" |
| 10 |
| 11 struct PP_Var; |
| 12 |
| 13 namespace webkit { |
| 14 namespace ppapi { |
| 15 |
| 16 class PluginInstance; |
| 17 |
| 18 // MessageChannel implements bidirectional postMessage functionality, allowing |
| 19 // calls from JavaScript to plugins and vice-versa. See |
| 20 // PPB_Instance::PostMessage and PPP_Instance::HandleMessage for more |
| 21 // information. |
| 22 // |
| 23 // Currently, only 1 MessageChannel can exist, to implement postMessage |
| 24 // functionality for the instance interfaces. In the future, when we create a |
| 25 // MessagePort type in PPAPI, those may be implemented here as well with some |
| 26 // refactoring. |
| 27 // - Separate message ports won't require the passthrough object. |
| 28 // - The message target won't be limited to instance, and should support |
| 29 // either plugin-provided or JS objects. |
| 30 // TODO(dmichael): Add support for separate MessagePorts. |
| 31 class MessageChannel { |
| 32 public: |
| 33 // MessageChannelNPObject is a simple struct that adds a pointer back to a |
| 34 // MessageChannel instance. This way, we can use an NPObject to allow |
| 35 // JavaScript interactions without forcing MessageChannel to inherit from |
| 36 // NPObject. |
| 37 struct MessageChannelNPObject : public NPObject { |
| 38 MessageChannelNPObject(); |
| 39 ~MessageChannelNPObject(); |
| 40 |
| 41 MessageChannel* message_channel; |
| 42 }; |
| 43 |
| 44 explicit MessageChannel(PluginInstance* instance); |
| 45 ~MessageChannel(); |
| 46 |
| 47 void PostMessageToJavaScript(PP_Var message_data); |
| 48 void PostMessageToNative(PP_Var message_data); |
| 49 |
| 50 // Return the NPObject* to which we should forward any calls which aren't |
| 51 // related to postMessage. Note that this can be NULL; it only gets set if |
| 52 // there is a scriptable 'InstanceObject' associated with this channel's |
| 53 // instance. |
| 54 NPObject* passthrough_object() { |
| 55 return passthrough_object_; |
| 56 } |
| 57 void set_passthrough_object(NPObject* passthrough) { |
| 58 passthrough_object_ = passthrough; |
| 59 } |
| 60 |
| 61 NPObject* np_object() { return np_object_; } |
| 62 |
| 63 PluginInstance* instance() { |
| 64 return instance_; |
| 65 } |
| 66 |
| 67 private: |
| 68 PluginInstance* instance_; |
| 69 |
| 70 // We pass all non-postMessage calls through to the passthrough_object_. |
| 71 // This way, a plugin can use PPB_Class or PPP_Class_Deprecated and also |
| 72 // postMessage. This is necessary to support backwards-compatibility, and |
| 73 // also trusted plugins for which we will continue to support synchronous |
| 74 // scripting. |
| 75 NPObject* passthrough_object_; |
| 76 |
| 77 // The NPObject we use to expose postMessage to JavaScript. |
| 78 MessageChannelNPObject* np_object_; |
| 79 |
| 80 // An NPVariant referring to the JavaScript function we use to send a message |
| 81 // to a JavaScript target. |
| 82 NPVariant onmessage_invoker_; |
| 83 |
| 84 bool EvaluateOnMessageInvoker(); |
| 85 |
| 86 DISALLOW_COPY_AND_ASSIGN(MessageChannel); |
| 87 }; |
| 88 |
| 89 } // namespace ppapi |
| 90 } // namespace webkit |
| 91 |
| 92 #endif // WEBKIT_PLUGINS_PPAPI_MESSAGE_CHANNEL_H_ |
| 93 |
OLD | NEW |