[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 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_PLUGIN_DISPATCHER_H_ |
| 6 | #define PPAPI_PROXY_PLUGIN_DISPATCHER_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | |
[email protected] | 373a95a | 2011-07-01 16:58:14 | [diff] [blame] | 10 | #include "base/basictypes.h" |
[email protected] | f56279c | 2011-02-02 18:12:31 | [diff] [blame] | 11 | #include "base/hash_tables.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 12 | #include "base/memory/scoped_ptr.h" |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 13 | #include "base/process.h" |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 14 | #include "build/build_config.h" |
[email protected] | f56279c | 2011-02-02 18:12:31 | [diff] [blame] | 15 | #include "ppapi/c/pp_rect.h" |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 16 | #include "ppapi/c/pp_instance.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 17 | #include "ppapi/proxy/dispatcher.h" |
[email protected] | 6239d34 | 2011-05-06 22:55:47 | [diff] [blame] | 18 | #include "ppapi/shared_impl/function_group_base.h" |
[email protected] | 208aad79 | 2011-05-26 19:05:28 | [diff] [blame] | 19 | #include "ppapi/shared_impl/ppapi_preferences.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 20 | |
| 21 | class MessageLoop; |
| 22 | |
| 23 | namespace base { |
| 24 | class WaitableEvent; |
| 25 | } |
| 26 | |
[email protected] | 208aad79 | 2011-05-26 19:05:28 | [diff] [blame] | 27 | namespace ppapi { |
| 28 | struct Preferences; |
| 29 | } |
| 30 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 31 | namespace pp { |
| 32 | namespace proxy { |
| 33 | |
[email protected] | f56279c | 2011-02-02 18:12:31 | [diff] [blame] | 34 | // Used to keep track of per-instance data. |
| 35 | struct InstanceData { |
[email protected] | 6a160e7 | 2011-05-04 20:23:45 | [diff] [blame] | 36 | InstanceData() : fullscreen(PP_FALSE) {} |
[email protected] | f56279c | 2011-02-02 18:12:31 | [diff] [blame] | 37 | PP_Rect position; |
[email protected] | 6a160e7 | 2011-05-04 20:23:45 | [diff] [blame] | 38 | PP_Bool fullscreen; |
[email protected] | f56279c | 2011-02-02 18:12:31 | [diff] [blame] | 39 | }; |
| 40 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 41 | class PluginDispatcher : public Dispatcher { |
| 42 | public: |
[email protected] | d259a8e | 2011-05-18 22:31:09 | [diff] [blame] | 43 | class PluginDelegate : public ProxyChannel::Delegate { |
| 44 | public: |
| 45 | // Returns the set used for globally uniquifying PP_Instances. This same |
| 46 | // set must be returned for all channels. |
| 47 | // |
| 48 | // DEREFERENCE ONLY ON THE I/O THREAD. |
| 49 | virtual std::set<PP_Instance>* GetGloballySeenInstanceIDSet() = 0; |
| 50 | |
| 51 | // Returns the WebKit forwarding object used to make calls into WebKit. |
| 52 | // Necessary only on the plugin side. |
| 53 | virtual ppapi::WebKitForwarding* GetWebKitForwarding() = 0; |
| 54 | |
| 55 | // Posts the given task to the WebKit thread associated with this plugin |
| 56 | // process. The WebKit thread should be lazily created if it does not |
| 57 | // exist yet. |
| 58 | virtual void PostToWebKitThread(const tracked_objects::Location& from_here, |
| 59 | const base::Closure& task) = 0; |
| 60 | |
| 61 | // Sends the given message to the browser. Identical semantics to |
| 62 | // IPC::Message::Sender interface. |
| 63 | virtual bool SendToBrowser(IPC::Message* msg) = 0; |
[email protected] | 373a95a | 2011-07-01 16:58:14 | [diff] [blame] | 64 | |
| 65 | // Registers the plugin dispatcher and returns an ID. |
| 66 | // Plugin dispatcher IDs will be used to dispatch messages from the browser. |
| 67 | // Each call to Register() has to be matched with a call to Unregister(). |
| 68 | virtual uint32 Register(PluginDispatcher* plugin_dispatcher) = 0; |
| 69 | virtual void Unregister(uint32 plugin_dispatcher_id) = 0; |
[email protected] | d259a8e | 2011-05-18 22:31:09 | [diff] [blame] | 70 | }; |
| 71 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 72 | // Constructor for the plugin side. The init and shutdown functions will be |
| 73 | // will be automatically called when requested by the renderer side. The |
| 74 | // module ID will be set upon receipt of the InitializeModule message. |
| 75 | // |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 76 | // You must call InitPluginWithChannel after the constructor. |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 77 | PluginDispatcher(base::ProcessHandle remote_process_handle, |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 78 | GetInterfaceFunc get_interface); |
[email protected] | 4f15d284 | 2011-02-15 17:36:33 | [diff] [blame] | 79 | virtual ~PluginDispatcher(); |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 80 | |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 81 | // The plugin side maintains a mapping from PP_Instance to Dispatcher so |
| 82 | // that we can send the messages to the right channel if there are multiple |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 83 | // renderers sharing the same plugin. This mapping is maintained by |
| 84 | // DidCreateInstance/DidDestroyInstance. |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 85 | static PluginDispatcher* GetForInstance(PP_Instance instance); |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 86 | |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 87 | static const void* GetInterfaceFromDispatcher(const char* interface); |
| 88 | |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 89 | // You must call this function before anything else. Returns true on success. |
| 90 | // The delegate pointer must outlive this class, ownership is not |
| 91 | // transferred. |
[email protected] | d259a8e | 2011-05-18 22:31:09 | [diff] [blame] | 92 | bool InitPluginWithChannel(PluginDelegate* delegate, |
| 93 | const IPC::ChannelHandle& channel_handle, |
| 94 | bool is_client); |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 95 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 96 | // Dispatcher overrides. |
[email protected] | 7cf4091 | 2010-12-09 18:25:03 | [diff] [blame] | 97 | virtual bool IsPlugin() const; |
[email protected] | b00bbb3 | 2011-03-30 19:02:14 | [diff] [blame] | 98 | virtual bool Send(IPC::Message* msg); |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 99 | |
| 100 | // IPC::Channel::Listener implementation. |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 101 | virtual bool OnMessageReceived(const IPC::Message& msg); |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 102 | virtual void OnChannelError(); |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 103 | |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 104 | // Keeps track of which dispatcher to use for each instance, active instances |
| 105 | // and tracks associated data like the current size. |
[email protected] | f56279c | 2011-02-02 18:12:31 | [diff] [blame] | 106 | void DidCreateInstance(PP_Instance instance); |
| 107 | void DidDestroyInstance(PP_Instance instance); |
| 108 | |
| 109 | // Gets the data for an existing instance, or NULL if the instance id doesn't |
[email protected] | 6239d34 | 2011-05-06 22:55:47 | [diff] [blame] | 110 | // correspond to a known instance. |
[email protected] | f56279c | 2011-02-02 18:12:31 | [diff] [blame] | 111 | InstanceData* GetInstanceData(PP_Instance instance); |
| 112 | |
[email protected] | 7a1f7c6f | 2011-05-10 21:17:48 | [diff] [blame] | 113 | // Posts the given task to the WebKit thread. |
| 114 | void PostToWebKitThread(const tracked_objects::Location& from_here, |
| 115 | const base::Closure& task); |
| 116 | |
[email protected] | d259a8e | 2011-05-18 22:31:09 | [diff] [blame] | 117 | // Calls the PluginDelegate.SendToBrowser function. |
| 118 | bool SendToBrowser(IPC::Message* msg); |
| 119 | |
[email protected] | 7a1f7c6f | 2011-05-10 21:17:48 | [diff] [blame] | 120 | // Returns the WebKitForwarding object used to forward events to WebKit. |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 121 | ppapi::WebKitForwarding* GetWebKitForwarding(); |
[email protected] | 7a1f7c6f | 2011-05-10 21:17:48 | [diff] [blame] | 122 | |
[email protected] | 208aad79 | 2011-05-26 19:05:28 | [diff] [blame] | 123 | // Returns the Preferences. |
| 124 | const ppapi::Preferences& preferences() const { return preferences_; } |
| 125 | |
[email protected] | 6239d34 | 2011-05-06 22:55:47 | [diff] [blame] | 126 | // Returns the "new-style" function API for the given interface ID, creating |
| 127 | // it if necessary. |
| 128 | // TODO(brettw) this is in progress. It should be merged with the target |
| 129 | // proxies so there is one list to consult. |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 130 | ppapi::FunctionGroupBase* GetFunctionAPI( |
[email protected] | 6239d34 | 2011-05-06 22:55:47 | [diff] [blame] | 131 | pp::proxy::InterfaceID id); |
| 132 | |
[email protected] | 373a95a | 2011-07-01 16:58:14 | [diff] [blame] | 133 | uint32 plugin_dispatcher_id() const { return plugin_dispatcher_id_; } |
| 134 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 135 | private: |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 136 | friend class PluginDispatcherTest; |
| 137 | |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 138 | // Notifies all live instances that they're now closed. This is used when |
| 139 | // a renderer crashes or some other error is received. |
| 140 | void ForceFreeAllInstances(); |
| 141 | |
[email protected] | 176c7392 | 2010-12-03 17:32:19 | [diff] [blame] | 142 | // IPC message handlers. |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 143 | void OnMsgSupportsInterface(const std::string& interface_name, bool* result); |
[email protected] | 208aad79 | 2011-05-26 19:05:28 | [diff] [blame] | 144 | void OnMsgSetPreferences(const ::ppapi::Preferences& prefs); |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 145 | |
[email protected] | d259a8e | 2011-05-18 22:31:09 | [diff] [blame] | 146 | PluginDelegate* plugin_delegate_; |
| 147 | |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 148 | // All target proxies currently created. These are ones that receive |
| 149 | // messages. |
| 150 | scoped_ptr<InterfaceProxy> target_proxies_[INTERFACE_ID_COUNT]; |
| 151 | |
[email protected] | 6239d34 | 2011-05-06 22:55:47 | [diff] [blame] | 152 | // Function proxies created for "new-style" FunctionGroups. |
| 153 | // TODO(brettw) this is in progress. It should be merged with the target |
| 154 | // proxies so there is one list to consult. |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 155 | scoped_ptr< ::ppapi::FunctionGroupBase > |
[email protected] | 6239d34 | 2011-05-06 22:55:47 | [diff] [blame] | 156 | function_proxies_[INTERFACE_ID_COUNT]; |
| 157 | |
[email protected] | f56279c | 2011-02-02 18:12:31 | [diff] [blame] | 158 | typedef base::hash_map<PP_Instance, InstanceData> InstanceDataMap; |
| 159 | InstanceDataMap instance_map_; |
| 160 | |
[email protected] | 208aad79 | 2011-05-26 19:05:28 | [diff] [blame] | 161 | // The preferences sent from the host. We only want to set this once, which |
| 162 | // is what the received_preferences_ indicates. See OnMsgSetPreferences. |
| 163 | bool received_preferences_; |
| 164 | ppapi::Preferences preferences_; |
| 165 | |
[email protected] | 373a95a | 2011-07-01 16:58:14 | [diff] [blame] | 166 | uint32 plugin_dispatcher_id_; |
| 167 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 168 | DISALLOW_COPY_AND_ASSIGN(PluginDispatcher); |
| 169 | }; |
| 170 | |
| 171 | } // namespace proxy |
| 172 | } // namespace pp |
| 173 | |
| 174 | #endif // PPAPI_PROXY_PLUGIN_DISPATCHER_H_ |