blob: ae6da6045e0e04daf303ecad183f61b6e3491f09 [file] [log] [blame]
[email protected]a08ebea2011-02-13 17:50:201// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]c2932f5e2010-11-03 03:22:332// 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]373a95a2011-07-01 16:58:1410#include "base/basictypes.h"
[email protected]f56279c2011-02-02 18:12:3111#include "base/hash_tables.h"
[email protected]3b63f8f42011-03-28 01:54:1512#include "base/memory/scoped_ptr.h"
[email protected]5d84d012010-12-02 17:17:2113#include "base/process.h"
[email protected]a08ebea2011-02-13 17:50:2014#include "build/build_config.h"
[email protected]f56279c2011-02-02 18:12:3115#include "ppapi/c/pp_rect.h"
[email protected]4614f192011-01-21 00:26:4316#include "ppapi/c/pp_instance.h"
[email protected]c2932f5e2010-11-03 03:22:3317#include "ppapi/proxy/dispatcher.h"
[email protected]6239d342011-05-06 22:55:4718#include "ppapi/shared_impl/function_group_base.h"
[email protected]208aad792011-05-26 19:05:2819#include "ppapi/shared_impl/ppapi_preferences.h"
[email protected]c2932f5e2010-11-03 03:22:3320
21class MessageLoop;
22
23namespace base {
24class WaitableEvent;
25}
26
[email protected]208aad792011-05-26 19:05:2827namespace ppapi {
28struct Preferences;
29}
30
[email protected]c2932f5e2010-11-03 03:22:3331namespace pp {
32namespace proxy {
33
[email protected]f56279c2011-02-02 18:12:3134// Used to keep track of per-instance data.
35struct InstanceData {
[email protected]6a160e72011-05-04 20:23:4536 InstanceData() : fullscreen(PP_FALSE) {}
[email protected]f56279c2011-02-02 18:12:3137 PP_Rect position;
[email protected]6a160e72011-05-04 20:23:4538 PP_Bool fullscreen;
[email protected]f56279c2011-02-02 18:12:3139};
40
[email protected]c2932f5e2010-11-03 03:22:3341class PluginDispatcher : public Dispatcher {
42 public:
[email protected]d259a8e2011-05-18 22:31:0943 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]373a95a2011-07-01 16:58:1464
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]d259a8e2011-05-18 22:31:0970 };
71
[email protected]c2932f5e2010-11-03 03:22:3372 // 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]e2614c62011-04-16 22:12:4576 // You must call InitPluginWithChannel after the constructor.
[email protected]5d84d012010-12-02 17:17:2177 PluginDispatcher(base::ProcessHandle remote_process_handle,
[email protected]a08ebea2011-02-13 17:50:2078 GetInterfaceFunc get_interface);
[email protected]4f15d2842011-02-15 17:36:3379 virtual ~PluginDispatcher();
[email protected]c2932f5e2010-11-03 03:22:3380
[email protected]4614f192011-01-21 00:26:4381 // 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]465faa22011-02-08 16:31:4683 // renderers sharing the same plugin. This mapping is maintained by
84 // DidCreateInstance/DidDestroyInstance.
[email protected]4614f192011-01-21 00:26:4385 static PluginDispatcher* GetForInstance(PP_Instance instance);
[email protected]4614f192011-01-21 00:26:4386
[email protected]a08ebea2011-02-13 17:50:2087 static const void* GetInterfaceFromDispatcher(const char* interface);
88
[email protected]e2614c62011-04-16 22:12:4589 // 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]d259a8e2011-05-18 22:31:0992 bool InitPluginWithChannel(PluginDelegate* delegate,
93 const IPC::ChannelHandle& channel_handle,
94 bool is_client);
[email protected]e2614c62011-04-16 22:12:4595
[email protected]c2932f5e2010-11-03 03:22:3396 // Dispatcher overrides.
[email protected]7cf40912010-12-09 18:25:0397 virtual bool IsPlugin() const;
[email protected]b00bbb32011-03-30 19:02:1498 virtual bool Send(IPC::Message* msg);
[email protected]c2932f5e2010-11-03 03:22:3399
100 // IPC::Channel::Listener implementation.
[email protected]a95986a82010-12-24 06:19:28101 virtual bool OnMessageReceived(const IPC::Message& msg);
[email protected]a08ebea2011-02-13 17:50:20102 virtual void OnChannelError();
[email protected]c2932f5e2010-11-03 03:22:33103
[email protected]465faa22011-02-08 16:31:46104 // Keeps track of which dispatcher to use for each instance, active instances
105 // and tracks associated data like the current size.
[email protected]f56279c2011-02-02 18:12:31106 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]6239d342011-05-06 22:55:47110 // correspond to a known instance.
[email protected]f56279c2011-02-02 18:12:31111 InstanceData* GetInstanceData(PP_Instance instance);
112
[email protected]7a1f7c6f2011-05-10 21:17:48113 // 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]d259a8e2011-05-18 22:31:09117 // Calls the PluginDelegate.SendToBrowser function.
118 bool SendToBrowser(IPC::Message* msg);
119
[email protected]7a1f7c6f2011-05-10 21:17:48120 // Returns the WebKitForwarding object used to forward events to WebKit.
[email protected]55cdf6052011-05-13 19:22:53121 ppapi::WebKitForwarding* GetWebKitForwarding();
[email protected]7a1f7c6f2011-05-10 21:17:48122
[email protected]208aad792011-05-26 19:05:28123 // Returns the Preferences.
124 const ppapi::Preferences& preferences() const { return preferences_; }
125
[email protected]6239d342011-05-06 22:55:47126 // 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]55cdf6052011-05-13 19:22:53130 ppapi::FunctionGroupBase* GetFunctionAPI(
[email protected]6239d342011-05-06 22:55:47131 pp::proxy::InterfaceID id);
132
[email protected]373a95a2011-07-01 16:58:14133 uint32 plugin_dispatcher_id() const { return plugin_dispatcher_id_; }
134
[email protected]c2932f5e2010-11-03 03:22:33135 private:
[email protected]465faa22011-02-08 16:31:46136 friend class PluginDispatcherTest;
137
[email protected]a08ebea2011-02-13 17:50:20138 // 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]176c73922010-12-03 17:32:19142 // IPC message handlers.
[email protected]465faa22011-02-08 16:31:46143 void OnMsgSupportsInterface(const std::string& interface_name, bool* result);
[email protected]208aad792011-05-26 19:05:28144 void OnMsgSetPreferences(const ::ppapi::Preferences& prefs);
[email protected]c2932f5e2010-11-03 03:22:33145
[email protected]d259a8e2011-05-18 22:31:09146 PluginDelegate* plugin_delegate_;
147
[email protected]465faa22011-02-08 16:31:46148 // All target proxies currently created. These are ones that receive
149 // messages.
150 scoped_ptr<InterfaceProxy> target_proxies_[INTERFACE_ID_COUNT];
151
[email protected]6239d342011-05-06 22:55:47152 // 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]55cdf6052011-05-13 19:22:53155 scoped_ptr< ::ppapi::FunctionGroupBase >
[email protected]6239d342011-05-06 22:55:47156 function_proxies_[INTERFACE_ID_COUNT];
157
[email protected]f56279c2011-02-02 18:12:31158 typedef base::hash_map<PP_Instance, InstanceData> InstanceDataMap;
159 InstanceDataMap instance_map_;
160
[email protected]208aad792011-05-26 19:05:28161 // 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]373a95a2011-07-01 16:58:14166 uint32 plugin_dispatcher_id_;
167
[email protected]c2932f5e2010-11-03 03:22:33168 DISALLOW_COPY_AND_ASSIGN(PluginDispatcher);
169};
170
171} // namespace proxy
172} // namespace pp
173
174#endif // PPAPI_PROXY_PLUGIN_DISPATCHER_H_