[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef PPAPI_PROXY_DISPATCHER_H_ |
| 6 | #define PPAPI_PROXY_DISPATCHER_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include "base/linked_ptr.h" |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 13 | #include "base/process.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 14 | #include "base/scoped_ptr.h" |
| 15 | #include "ipc/ipc_channel.h" |
| 16 | #include "ipc/ipc_channel_handle.h" |
| 17 | #include "ipc/ipc_message.h" |
| 18 | #include "ppapi/c/pp_module.h" |
| 19 | #include "ppapi/proxy/callback_tracker.h" |
| 20 | #include "ppapi/proxy/interface_id.h" |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame^] | 21 | #include "ppapi/proxy/interface_proxy.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 22 | #include "ppapi/proxy/plugin_var_tracker.h" |
| 23 | |
| 24 | class MessageLoop; |
| 25 | struct PPB_Var_Deprecated; |
| 26 | |
| 27 | namespace base { |
| 28 | class WaitableEvent; |
| 29 | } |
| 30 | |
| 31 | namespace IPC { |
| 32 | class SyncChannel; |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 33 | class TestSink; |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | namespace pp { |
| 37 | namespace proxy { |
| 38 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 39 | class VarSerializationRules; |
| 40 | |
| 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 |
| 43 | // is where the call ends up being executed. |
| 44 | // |
| 45 | // Plugin side | Browser side |
| 46 | // -------------------------------------|-------------------------------------- |
| 47 | // | |
| 48 | // "Source" | "Target" |
| 49 | // InterfaceProxy ----------------------> InterfaceProxy |
| 50 | // | |
| 51 | // | |
| 52 | // "Target" | "Source" |
| 53 | // InterfaceProxy <---------------------- InterfaceProxy |
| 54 | // | |
| 55 | class Dispatcher : public IPC::Channel::Listener, |
| 56 | public IPC::Message::Sender { |
| 57 | public: |
| 58 | typedef const void* (*GetInterfaceFunc)(const char*); |
[email protected] | 9c7baaef | 2010-11-10 05:42:46 | [diff] [blame] | 59 | typedef int32_t (*InitModuleFunc)(PP_Module, GetInterfaceFunc); |
| 60 | typedef void (*ShutdownModuleFunc)(); |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 61 | |
| 62 | ~Dispatcher(); |
| 63 | |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 64 | // You must call this function before anything else. Returns true on success. |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 65 | bool InitWithChannel(MessageLoop* ipc_message_loop, |
[email protected] | 42ce94e | 2010-12-08 19:28:09 | [diff] [blame] | 66 | const IPC::ChannelHandle& channel_handle, |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 67 | bool is_client, |
| 68 | base::WaitableEvent* shutdown_event); |
| 69 | |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 70 | // Alternative to InitWithChannel() for unit tests that want to send all |
| 71 | // messages sent via this dispatcher to the given test sink. The test sink |
| 72 | // must outlive this class. |
| 73 | void InitWithTestSink(IPC::TestSink* test_sink); |
| 74 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 75 | // Returns true if the dispatcher is on the plugin side, or false if it's the |
| 76 | // browser side. |
| 77 | virtual bool IsPlugin() const = 0; |
| 78 | |
| 79 | VarSerializationRules* serialization_rules() const { |
| 80 | return serialization_rules_.get(); |
| 81 | } |
| 82 | PP_Module pp_module() const { |
| 83 | return pp_module_; |
| 84 | } |
| 85 | |
| 86 | // Wrapper for calling the local GetInterface function. |
| 87 | const void* GetLocalInterface(const char* interface); |
| 88 | |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 89 | // Returns the remote process' handle. For the host dispatcher, this will be |
| 90 | // the plugin process, and for the plugin dispatcher, this will be the |
| 91 | // renderer process. This is used for sharing memory and such and is |
| 92 | // guaranteed valid (unless the remote process has suddenly died). |
| 93 | base::ProcessHandle remote_process_handle() const { |
| 94 | return remote_process_handle_; |
| 95 | } |
| 96 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 97 | // Called if the remote side is declaring to us which interfaces it supports |
| 98 | // so we don't have to query for each one. We'll pre-create proxies for |
| 99 | // each of the given interfaces. |
| 100 | |
| 101 | // IPC::Message::Sender implementation. |
| 102 | virtual bool Send(IPC::Message* msg); |
| 103 | |
| 104 | // IPC::Channel::Listener implementation. |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 105 | virtual bool OnMessageReceived(const IPC::Message& msg); |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 106 | |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 107 | // Will be NULL in some unit tests. |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 108 | IPC::SyncChannel* channel() const { |
| 109 | return channel_.get(); |
| 110 | } |
| 111 | |
| 112 | CallbackTracker& callback_tracker() { |
| 113 | return callback_tracker_; |
| 114 | } |
| 115 | |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame^] | 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 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 127 | protected: |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 128 | Dispatcher(base::ProcessHandle remote_process_handle, |
| 129 | GetInterfaceFunc local_get_interface); |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 130 | |
| 131 | // Setter for the derived classes to set the appropriate var serialization. |
| 132 | // Takes ownership of the given pointer, which must be on the heap. |
| 133 | void SetSerializationRules(VarSerializationRules* var_serialization_rules); |
| 134 | |
| 135 | void set_pp_module(PP_Module module) { |
| 136 | pp_module_ = module; |
| 137 | } |
| 138 | |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame^] | 139 | bool disallow_trusted_interfaces() const { |
| 140 | return disallow_trusted_interfaces_; |
| 141 | } |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 142 | |
| 143 | private: |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 144 | // Set by the derived classed to indicate the module ID corresponding to |
| 145 | // this dispatcher. |
| 146 | PP_Module pp_module_; |
| 147 | |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 148 | base::ProcessHandle remote_process_handle_; // See getter above. |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 149 | |
| 150 | // When we're unit testing, this will indicate the sink for the messages to |
| 151 | // be deposited so they can be inspected by the test. When non-NULL, this |
| 152 | // indicates that the channel should not be used. |
| 153 | IPC::TestSink* test_sink_; |
| 154 | |
| 155 | // Will be null for some tests when there is a test_sink_. |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 156 | scoped_ptr<IPC::SyncChannel> channel_; |
| 157 | |
| 158 | bool disallow_trusted_interfaces_; |
| 159 | |
| 160 | GetInterfaceFunc local_get_interface_; |
| 161 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 162 | CallbackTracker callback_tracker_; |
| 163 | |
| 164 | scoped_ptr<VarSerializationRules> serialization_rules_; |
| 165 | |
| 166 | DISALLOW_COPY_AND_ASSIGN(Dispatcher); |
| 167 | }; |
| 168 | |
| 169 | } // namespace proxy |
| 170 | } // namespace pp |
| 171 | |
| 172 | #endif // PPAPI_PROXY_DISPATCHER_H_ |