WebViewGuest: Add missing break statement.
[chromium-blink-merge.git] / ppapi / proxy / plugin_dispatcher.h
blobd071e61d1723f00d2c3b335d0fa831744a072f5c
1 // Copyright (c) 2012 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.
5 #ifndef PPAPI_PROXY_PLUGIN_DISPATCHER_H_
6 #define PPAPI_PROXY_PLUGIN_DISPATCHER_H_
8 #include <set>
9 #include <string>
11 #include "base/basictypes.h"
12 #include "base/containers/hash_tables.h"
13 #include "base/containers/scoped_ptr_hash_map.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/process/process.h"
17 #include "build/build_config.h"
18 #include "ipc/ipc_sync_channel.h"
19 #include "ppapi/c/pp_instance.h"
20 #include "ppapi/c/pp_rect.h"
21 #include "ppapi/c/ppb_console.h"
22 #include "ppapi/proxy/dispatcher.h"
23 #include "ppapi/shared_impl/ppapi_preferences.h"
24 #include "ppapi/shared_impl/ppb_view_shared.h"
25 #include "ppapi/shared_impl/singleton_resource_id.h"
26 #include "ppapi/shared_impl/tracked_callback.h"
28 namespace IPC {
29 class SyncMessageFilter;
32 namespace ppapi {
34 struct Preferences;
35 class Resource;
37 namespace thunk {
38 class PPB_Instance_API;
39 class ResourceCreationAPI;
42 namespace proxy {
44 // Used to keep track of per-instance data.
45 struct InstanceData {
46 InstanceData();
47 ~InstanceData();
49 ViewData view;
51 // When non-NULL, indicates the callback to execute when mouse lock is lost.
52 scoped_refptr<TrackedCallback> mouse_lock_callback;
54 // A map of singleton resources which are lazily created.
55 typedef std::map<SingletonResourceID, scoped_refptr<Resource> >
56 SingletonResourceMap;
57 SingletonResourceMap singleton_resources;
59 // Calls to |RequestSurroundingText()| are done by posted tasks. Track whether
60 // a) a task is pending, to avoid redundant calls, and b) whether we should
61 // actually call |RequestSurroundingText()|, to avoid stale calls (i.e.,
62 // calling when we shouldn't).
63 bool is_request_surrounding_text_pending;
64 bool should_do_request_surrounding_text;
67 class PPAPI_PROXY_EXPORT PluginDispatcher
68 : public Dispatcher,
69 public base::SupportsWeakPtr<PluginDispatcher> {
70 public:
71 class PPAPI_PROXY_EXPORT PluginDelegate : public ProxyChannel::Delegate {
72 public:
73 // Returns the set used for globally uniquifying PP_Instances. This same
74 // set must be returned for all channels.
76 // DEREFERENCE ONLY ON THE I/O THREAD.
77 virtual std::set<PP_Instance>* GetGloballySeenInstanceIDSet() = 0;
79 // Registers the plugin dispatcher and returns an ID.
80 // Plugin dispatcher IDs will be used to dispatch messages from the browser.
81 // Each call to Register() has to be matched with a call to Unregister().
82 virtual uint32 Register(PluginDispatcher* plugin_dispatcher) = 0;
83 virtual void Unregister(uint32 plugin_dispatcher_id) = 0;
86 // Constructor for the plugin side. The init and shutdown functions will be
87 // will be automatically called when requested by the renderer side. The
88 // module ID will be set upon receipt of the InitializeModule message.
90 // Note about permissions: On the plugin side, the dispatcher and the plugin
91 // run in the same address space (including in nacl). This means that the
92 // permissions here are subject to malicious modification and bypass, and
93 // an exploited or malicious plugin could send any IPC messages and just
94 // bypass the permissions. All permissions must be checked "for realz" in the
95 // host process when receiving messages. We check them on the plugin side
96 // primarily to keep honest plugins honest, especially with respect to
97 // dev interfaces that they "shouldn't" be using.
99 // You must call InitPluginWithChannel after the constructor.
100 PluginDispatcher(PP_GetInterface_Func get_interface,
101 const PpapiPermissions& permissions,
102 bool incognito);
103 virtual ~PluginDispatcher();
105 // The plugin side maintains a mapping from PP_Instance to Dispatcher so
106 // that we can send the messages to the right channel if there are multiple
107 // renderers sharing the same plugin. This mapping is maintained by
108 // DidCreateInstance/DidDestroyInstance.
109 static PluginDispatcher* GetForInstance(PP_Instance instance);
111 // Same as GetForInstance but retrieves the instance from the given resource
112 // object as a convenience. Returns NULL on failure.
113 static PluginDispatcher* GetForResource(const Resource* resource);
115 // Implements the GetInterface function for the plugin to call to retrieve
116 // a browser interface.
117 static const void* GetBrowserInterface(const char* interface_name);
119 // Logs the given log message to the given instance, or, if the instance is
120 // invalid, to all instances associated with all dispatchers. Used for
121 // global log messages.
122 static void LogWithSource(PP_Instance instance,
123 PP_LogLevel level,
124 const std::string& source,
125 const std::string& value);
127 const void* GetPluginInterface(const std::string& interface_name);
129 // You must call this function before anything else. Returns true on success.
130 // The delegate pointer must outlive this class, ownership is not
131 // transferred.
132 bool InitPluginWithChannel(PluginDelegate* delegate,
133 base::ProcessId peer_pid,
134 const IPC::ChannelHandle& channel_handle,
135 bool is_client);
137 // Dispatcher overrides.
138 virtual bool IsPlugin() const;
139 virtual bool Send(IPC::Message* msg);
141 // IPC::Listener implementation.
142 virtual bool OnMessageReceived(const IPC::Message& msg);
143 virtual void OnChannelError();
145 // Keeps track of which dispatcher to use for each instance, active instances
146 // and tracks associated data like the current size.
147 void DidCreateInstance(PP_Instance instance);
148 void DidDestroyInstance(PP_Instance instance);
150 // Gets the data for an existing instance, or NULL if the instance id doesn't
151 // correspond to a known instance.
152 InstanceData* GetInstanceData(PP_Instance instance);
154 // Returns the corresponding API. These are APIs not associated with a
155 // resource. Guaranteed non-NULL.
156 thunk::PPB_Instance_API* GetInstanceAPI();
157 thunk::ResourceCreationAPI* GetResourceCreationAPI();
159 // Returns the Preferences.
160 const Preferences& preferences() const { return preferences_; }
162 uint32 plugin_dispatcher_id() const { return plugin_dispatcher_id_; }
163 bool incognito() const { return incognito_; }
165 private:
166 friend class PluginDispatcherTest;
168 // Notifies all live instances that they're now closed. This is used when
169 // a renderer crashes or some other error is received.
170 void ForceFreeAllInstances();
172 // IPC message handlers.
173 void OnMsgSupportsInterface(const std::string& interface_name, bool* result);
174 void OnMsgSetPreferences(const Preferences& prefs);
176 virtual bool SendMessage(IPC::Message* msg);
178 PluginDelegate* plugin_delegate_;
180 // Contains all the plugin interfaces we've queried. The mapped value will
181 // be the pointer to the interface pointer supplied by the plugin if it's
182 // supported, or NULL if it's not supported. This allows us to cache failures
183 // and not req-query if a plugin doesn't support the interface.
184 typedef base::hash_map<std::string, const void*> InterfaceMap;
185 InterfaceMap plugin_interfaces_;
187 typedef base::ScopedPtrHashMap<PP_Instance, InstanceData> InstanceDataMap;
188 InstanceDataMap instance_map_;
190 // The preferences sent from the host. We only want to set this once, which
191 // is what the received_preferences_ indicates. See OnMsgSetPreferences.
192 bool received_preferences_;
193 Preferences preferences_;
195 uint32 plugin_dispatcher_id_;
197 // Set to true when the instances associated with this dispatcher are
198 // incognito mode.
199 bool incognito_;
201 // A filter for sending messages from threads other than the main thread.
202 scoped_refptr<IPC::SyncMessageFilter> sync_filter_;
204 DISALLOW_COPY_AND_ASSIGN(PluginDispatcher);
207 } // namespace proxy
208 } // namespace ppapi
210 #endif // PPAPI_PROXY_PLUGIN_DISPATCHER_H_