| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 PPAPI_PROXY_DISPATCHER_H_ | 5 #ifndef PPAPI_PROXY_DISPATCHER_H_ |
| 6 #define PPAPI_PROXY_DISPATCHER_H_ | 6 #define PPAPI_PROXY_DISPATCHER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/linked_ptr.h" | 12 #include "base/linked_ptr.h" |
| 13 #include "base/process.h" | 13 #include "base/process.h" |
| 14 #include "base/scoped_ptr.h" | 14 #include "base/scoped_ptr.h" |
| 15 #include "ipc/ipc_channel.h" | 15 #include "ipc/ipc_channel.h" |
| 16 #include "ipc/ipc_channel_handle.h" | 16 #include "ipc/ipc_channel_handle.h" |
| 17 #include "ipc/ipc_message.h" | 17 #include "ipc/ipc_message.h" |
| 18 #include "ppapi/c/pp_module.h" | 18 #include "ppapi/c/pp_module.h" |
| 19 #include "ppapi/proxy/callback_tracker.h" | 19 #include "ppapi/proxy/callback_tracker.h" |
| 20 #include "ppapi/proxy/interface_id.h" | 20 #include "ppapi/proxy/interface_id.h" |
| 21 #include "ppapi/proxy/interface_proxy.h" |
| 21 #include "ppapi/proxy/plugin_var_tracker.h" | 22 #include "ppapi/proxy/plugin_var_tracker.h" |
| 22 | 23 |
| 23 class MessageLoop; | 24 class MessageLoop; |
| 24 struct PPB_Var_Deprecated; | 25 struct PPB_Var_Deprecated; |
| 25 | 26 |
| 26 namespace base { | 27 namespace base { |
| 27 class WaitableEvent; | 28 class WaitableEvent; |
| 28 } | 29 } |
| 29 | 30 |
| 30 namespace IPC { | 31 namespace IPC { |
| 31 class SyncChannel; | 32 class SyncChannel; |
| 32 class TestSink; | 33 class TestSink; |
| 33 } | 34 } |
| 34 | 35 |
| 35 namespace pp { | 36 namespace pp { |
| 36 namespace proxy { | 37 namespace proxy { |
| 37 | 38 |
| 38 class InterfaceProxy; | |
| 39 class VarSerializationRules; | 39 class VarSerializationRules; |
| 40 | 40 |
| 41 // An interface proxy can represent either end of a cross-process interface | 41 // An interface proxy can represent either end of a cross-process interface |
| 42 // call. The "source" side is where the call is invoked, and the "target" side | 42 // call. The "source" side is where the call is invoked, and the "target" side |
| 43 // is where the call ends up being executed. | 43 // is where the call ends up being executed. |
| 44 // | 44 // |
| 45 // Plugin side | Browser side | 45 // Plugin side | Browser side |
| 46 // -------------------------------------|-------------------------------------- | 46 // -------------------------------------|-------------------------------------- |
| 47 // | | 47 // | |
| 48 // "Source" | "Target" | 48 // "Source" | "Target" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 79 VarSerializationRules* serialization_rules() const { | 79 VarSerializationRules* serialization_rules() const { |
| 80 return serialization_rules_.get(); | 80 return serialization_rules_.get(); |
| 81 } | 81 } |
| 82 PP_Module pp_module() const { | 82 PP_Module pp_module() const { |
| 83 return pp_module_; | 83 return pp_module_; |
| 84 } | 84 } |
| 85 | 85 |
| 86 // Wrapper for calling the local GetInterface function. | 86 // Wrapper for calling the local GetInterface function. |
| 87 const void* GetLocalInterface(const char* interface); | 87 const void* GetLocalInterface(const char* interface); |
| 88 | 88 |
| 89 // Implements PPP_GetInterface and PPB_GetInterface on the "source" side. It | |
| 90 // will check if the remote side supports this interface as a target, and | |
| 91 // create a proxy if it does. A local implementation of that interface backed | |
| 92 // by the proxy will be returned on success. If the interface is unproxyable | |
| 93 // or not supported by the remote side, returns NULL. | |
| 94 const void* GetProxiedInterface(const std::string& interface); | |
| 95 | |
| 96 // Returns the remote process' handle. For the host dispatcher, this will be | 89 // Returns the remote process' handle. For the host dispatcher, this will be |
| 97 // the plugin process, and for the plugin dispatcher, this will be the | 90 // the plugin process, and for the plugin dispatcher, this will be the |
| 98 // renderer process. This is used for sharing memory and such and is | 91 // renderer process. This is used for sharing memory and such and is |
| 99 // guaranteed valid (unless the remote process has suddenly died). | 92 // guaranteed valid (unless the remote process has suddenly died). |
| 100 base::ProcessHandle remote_process_handle() const { | 93 base::ProcessHandle remote_process_handle() const { |
| 101 return remote_process_handle_; | 94 return remote_process_handle_; |
| 102 } | 95 } |
| 103 | 96 |
| 104 // Called if the remote side is declaring to us which interfaces it supports | 97 // Called if the remote side is declaring to us which interfaces it supports |
| 105 // so we don't have to query for each one. We'll pre-create proxies for | 98 // so we don't have to query for each one. We'll pre-create proxies for |
| 106 // each of the given interfaces. | 99 // each of the given interfaces. |
| 107 | 100 |
| 108 // IPC::Message::Sender implementation. | 101 // IPC::Message::Sender implementation. |
| 109 virtual bool Send(IPC::Message* msg); | 102 virtual bool Send(IPC::Message* msg); |
| 110 | 103 |
| 111 // IPC::Channel::Listener implementation. | 104 // IPC::Channel::Listener implementation. |
| 112 virtual bool OnMessageReceived(const IPC::Message& msg); | 105 virtual bool OnMessageReceived(const IPC::Message& msg); |
| 113 | 106 |
| 114 // Will be NULL in some unit tests. | 107 // Will be NULL in some unit tests. |
| 115 IPC::SyncChannel* channel() const { | 108 IPC::SyncChannel* channel() const { |
| 116 return channel_.get(); | 109 return channel_.get(); |
| 117 } | 110 } |
| 118 | 111 |
| 119 CallbackTracker& callback_tracker() { | 112 CallbackTracker& callback_tracker() { |
| 120 return callback_tracker_; | 113 return callback_tracker_; |
| 121 } | 114 } |
| 122 | 115 |
| 116 // Retrieves the information associated with the given interface, identified |
| 117 // either by name or ID. Each function searches either PPP or PPB interfaces. |
| 118 static const InterfaceProxy::Info* GetPPBInterfaceInfo( |
| 119 const std::string& name); |
| 120 static const InterfaceProxy::Info* GetPPBInterfaceInfo( |
| 121 InterfaceID id); |
| 122 static const InterfaceProxy::Info* GetPPPInterfaceInfo( |
| 123 const std::string& name); |
| 124 static const InterfaceProxy::Info* GetPPPInterfaceInfo( |
| 125 InterfaceID id); |
| 126 |
| 123 protected: | 127 protected: |
| 124 Dispatcher(base::ProcessHandle remote_process_handle, | 128 Dispatcher(base::ProcessHandle remote_process_handle, |
| 125 GetInterfaceFunc local_get_interface); | 129 GetInterfaceFunc local_get_interface); |
| 126 | 130 |
| 127 // Setter for the derived classes to set the appropriate var serialization. | 131 // Setter for the derived classes to set the appropriate var serialization. |
| 128 // Takes ownership of the given pointer, which must be on the heap. | 132 // Takes ownership of the given pointer, which must be on the heap. |
| 129 void SetSerializationRules(VarSerializationRules* var_serialization_rules); | 133 void SetSerializationRules(VarSerializationRules* var_serialization_rules); |
| 130 | 134 |
| 131 void set_pp_module(PP_Module module) { | 135 void set_pp_module(PP_Module module) { |
| 132 pp_module_ = module; | 136 pp_module_ = module; |
| 133 } | 137 } |
| 134 | 138 |
| 135 // Allows the PluginDispatcher to add a magic proxy for PPP_Class, bypassing | 139 bool disallow_trusted_interfaces() const { |
| 136 // the normal "do you support this proxy" stuff and the big lookup of | 140 return disallow_trusted_interfaces_; |
| 137 // name to proxy object. Takes ownership of the pointer. | 141 } |
| 138 void InjectProxy(InterfaceID id, | |
| 139 const std::string& name, | |
| 140 InterfaceProxy* proxy); | |
| 141 | 142 |
| 142 private: | 143 private: |
| 143 typedef std::map< std::string, linked_ptr<InterfaceProxy> > ProxyMap; | |
| 144 | |
| 145 // Message handlers | |
| 146 void OnMsgSupportsInterface(const std::string& interface_name, bool* result); | |
| 147 void OnMsgDeclareInterfaces(const std::vector<std::string>& interfaces); | |
| 148 | |
| 149 // Allocates a new proxy on the heap corresponding to the given interface | |
| 150 // name, or returns NULL if that interface name isn't known proxyable. The | |
| 151 // caller owns the returned pointer. | |
| 152 // | |
| 153 // The interface_functions gives the pointer to the local interfece when this | |
| 154 // is a target proxy. When creating a source proxy, set this to NULL. | |
| 155 InterfaceProxy* CreateProxyForInterface( | |
| 156 const std::string& interface_name, | |
| 157 const void* interface_functions); | |
| 158 | |
| 159 // Returns true if the remote side supports the given interface as the | |
| 160 // target of an IPC call. | |
| 161 bool RemoteSupportsTargetInterface(const std::string& interface); | |
| 162 | |
| 163 // Sets up a proxy as the target for the given interface, if it is supported. | |
| 164 // Returns true if this process implements the given interface and it is | |
| 165 // proxyable. | |
| 166 bool SetupProxyForTargetInterface(const std::string& interface); | |
| 167 | |
| 168 bool IsInterfaceTrusted(const std::string& interface); | |
| 169 | |
| 170 // Set by the derived classed to indicate the module ID corresponding to | 144 // Set by the derived classed to indicate the module ID corresponding to |
| 171 // this dispatcher. | 145 // this dispatcher. |
| 172 PP_Module pp_module_; | 146 PP_Module pp_module_; |
| 173 | 147 |
| 174 base::ProcessHandle remote_process_handle_; // See getter above. | 148 base::ProcessHandle remote_process_handle_; // See getter above. |
| 175 | 149 |
| 176 // When we're unit testing, this will indicate the sink for the messages to | 150 // When we're unit testing, this will indicate the sink for the messages to |
| 177 // be deposited so they can be inspected by the test. When non-NULL, this | 151 // be deposited so they can be inspected by the test. When non-NULL, this |
| 178 // indicates that the channel should not be used. | 152 // indicates that the channel should not be used. |
| 179 IPC::TestSink* test_sink_; | 153 IPC::TestSink* test_sink_; |
| 180 | 154 |
| 181 // Will be null for some tests when there is a test_sink_. | 155 // Will be null for some tests when there is a test_sink_. |
| 182 scoped_ptr<IPC::SyncChannel> channel_; | 156 scoped_ptr<IPC::SyncChannel> channel_; |
| 183 | 157 |
| 184 bool disallow_trusted_interfaces_; | 158 bool disallow_trusted_interfaces_; |
| 185 | 159 |
| 186 GetInterfaceFunc local_get_interface_; | 160 GetInterfaceFunc local_get_interface_; |
| 187 | 161 |
| 188 ProxyMap proxies_; | |
| 189 InterfaceProxy* id_to_proxy_[INTERFACE_ID_COUNT]; | |
| 190 | |
| 191 // True if the remote side has declared which interfaces it supports in | |
| 192 // advance. When set, it means if we don't already have a source proxy for | |
| 193 // the requested interface, that the remote side doesn't support it and | |
| 194 // we don't need to query. | |
| 195 // | |
| 196 // This is just an optimization. The browser has a fixed set of interfaces | |
| 197 // it supports, and the many plugins will end up querying many of them. By | |
| 198 // having the browser just send all of those interfaces in one message, we | |
| 199 // can avoid a bunch of IPC chatter to set up each interface. | |
| 200 bool declared_supported_remote_interfaces_; | |
| 201 | |
| 202 CallbackTracker callback_tracker_; | 162 CallbackTracker callback_tracker_; |
| 203 | 163 |
| 204 scoped_ptr<VarSerializationRules> serialization_rules_; | 164 scoped_ptr<VarSerializationRules> serialization_rules_; |
| 205 | 165 |
| 206 DISALLOW_COPY_AND_ASSIGN(Dispatcher); | 166 DISALLOW_COPY_AND_ASSIGN(Dispatcher); |
| 207 }; | 167 }; |
| 208 | 168 |
| 209 } // namespace proxy | 169 } // namespace proxy |
| 210 } // namespace pp | 170 } // namespace pp |
| 211 | 171 |
| 212 #endif // PPAPI_PROXY_DISPATCHER_H_ | 172 #endif // PPAPI_PROXY_DISPATCHER_H_ |
| OLD | NEW |