Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / ppapi / proxy / plugin_dispatcher.h
blob159e69caab2fb84a3663afd6bf2419c978b01d6c
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/proxy/message_handler.h"
24 #include "ppapi/shared_impl/ppapi_preferences.h"
25 #include "ppapi/shared_impl/ppb_view_shared.h"
26 #include "ppapi/shared_impl/singleton_resource_id.h"
27 #include "ppapi/shared_impl/tracked_callback.h"
29 namespace IPC {
30 class SyncMessageFilter;
33 namespace ppapi {
35 struct Preferences;
36 class Resource;
38 namespace thunk {
39 class PPB_Instance_API;
40 class ResourceCreationAPI;
43 namespace proxy {
45 // Used to keep track of per-instance data.
46 struct InstanceData {
47 InstanceData();
48 ~InstanceData();
50 ViewData view;
52 // When non-NULL, indicates the callback to execute when mouse lock is lost.
53 scoped_refptr<TrackedCallback> mouse_lock_callback;
55 // A map of singleton resources which are lazily created.
56 typedef std::map<SingletonResourceID, scoped_refptr<Resource> >
57 SingletonResourceMap;
58 SingletonResourceMap singleton_resources;
60 // Calls to |RequestSurroundingText()| are done by posted tasks. Track whether
61 // a) a task is pending, to avoid redundant calls, and b) whether we should
62 // actually call |RequestSurroundingText()|, to avoid stale calls (i.e.,
63 // calling when we shouldn't).
64 bool is_request_surrounding_text_pending;
65 bool should_do_request_surrounding_text;
67 // The message handler which should handle JavaScript->Plugin messages, if
68 // one has been registered, otherwise NULL.
69 scoped_ptr<MessageHandler> message_handler;
71 // Flush info for PpapiCommandBufferProxy::OrderingBarrier().
72 struct FlushInfo {
73 FlushInfo();
74 ~FlushInfo();
75 bool flush_pending;
76 HostResource resource;
77 int32 put_offset;
79 FlushInfo flush_info_;
82 class PPAPI_PROXY_EXPORT PluginDispatcher
83 : public Dispatcher,
84 public base::SupportsWeakPtr<PluginDispatcher> {
85 public:
86 class PPAPI_PROXY_EXPORT PluginDelegate : public ProxyChannel::Delegate {
87 public:
88 // Returns the set used for globally uniquifying PP_Instances. This same
89 // set must be returned for all channels.
91 // DEREFERENCE ONLY ON THE I/O THREAD.
92 virtual std::set<PP_Instance>* GetGloballySeenInstanceIDSet() = 0;
94 // Registers the plugin dispatcher and returns an ID.
95 // Plugin dispatcher IDs will be used to dispatch messages from the browser.
96 // Each call to Register() has to be matched with a call to Unregister().
97 virtual uint32 Register(PluginDispatcher* plugin_dispatcher) = 0;
98 virtual void Unregister(uint32 plugin_dispatcher_id) = 0;
101 // Constructor for the plugin side. The init and shutdown functions will be
102 // will be automatically called when requested by the renderer side. The
103 // module ID will be set upon receipt of the InitializeModule message.
105 // Note about permissions: On the plugin side, the dispatcher and the plugin
106 // run in the same address space (including in nacl). This means that the
107 // permissions here are subject to malicious modification and bypass, and
108 // an exploited or malicious plugin could send any IPC messages and just
109 // bypass the permissions. All permissions must be checked "for realz" in the
110 // host process when receiving messages. We check them on the plugin side
111 // primarily to keep honest plugins honest, especially with respect to
112 // dev interfaces that they "shouldn't" be using.
114 // You must call InitPluginWithChannel after the constructor.
115 PluginDispatcher(PP_GetInterface_Func get_interface,
116 const PpapiPermissions& permissions,
117 bool incognito);
118 virtual ~PluginDispatcher();
120 // The plugin side maintains a mapping from PP_Instance to Dispatcher so
121 // that we can send the messages to the right channel if there are multiple
122 // renderers sharing the same plugin. This mapping is maintained by
123 // DidCreateInstance/DidDestroyInstance.
124 static PluginDispatcher* GetForInstance(PP_Instance instance);
126 // Same as GetForInstance but retrieves the instance from the given resource
127 // object as a convenience. Returns NULL on failure.
128 static PluginDispatcher* GetForResource(const Resource* resource);
130 // Implements the GetInterface function for the plugin to call to retrieve
131 // a browser interface.
132 static const void* GetBrowserInterface(const char* interface_name);
134 // Logs the given log message to the given instance, or, if the instance is
135 // invalid, to all instances associated with all dispatchers. Used for
136 // global log messages.
137 static void LogWithSource(PP_Instance instance,
138 PP_LogLevel level,
139 const std::string& source,
140 const std::string& value);
142 const void* GetPluginInterface(const std::string& interface_name);
144 // You must call this function before anything else. Returns true on success.
145 // The delegate pointer must outlive this class, ownership is not
146 // transferred.
147 bool InitPluginWithChannel(PluginDelegate* delegate,
148 base::ProcessId peer_pid,
149 const IPC::ChannelHandle& channel_handle,
150 bool is_client);
152 // Dispatcher overrides.
153 bool IsPlugin() const override;
154 // Send the message to the renderer. If |msg| is a synchronous message, we
155 // will unlock the ProxyLock so that we can handle incoming messages from the
156 // renderer.
157 bool Send(IPC::Message* msg) override;
159 // Unlike |Send()|, this function continues to hold the Pepper proxy lock
160 // until we are finished sending |msg|, even if it is a synchronous message.
161 bool SendAndStayLocked(IPC::Message* msg);
163 // IPC::Listener implementation.
164 bool OnMessageReceived(const IPC::Message& msg) override;
165 void OnChannelError() override;
167 // Keeps track of which dispatcher to use for each instance, active instances
168 // and tracks associated data like the current size.
169 void DidCreateInstance(PP_Instance instance);
170 void DidDestroyInstance(PP_Instance instance);
172 // Gets the data for an existing instance, or NULL if the instance id doesn't
173 // correspond to a known instance.
174 InstanceData* GetInstanceData(PP_Instance instance);
176 // Returns the corresponding API. These are APIs not associated with a
177 // resource. Guaranteed non-NULL.
178 thunk::PPB_Instance_API* GetInstanceAPI();
179 thunk::ResourceCreationAPI* GetResourceCreationAPI();
181 // Returns the Preferences.
182 const Preferences& preferences() const { return preferences_; }
184 uint32 plugin_dispatcher_id() const { return plugin_dispatcher_id_; }
185 bool incognito() const { return incognito_; }
187 private:
188 friend class PluginDispatcherTest;
190 // Notifies all live instances that they're now closed. This is used when
191 // a renderer crashes or some other error is received.
192 void ForceFreeAllInstances();
194 // IPC message handlers.
195 void OnMsgSupportsInterface(const std::string& interface_name, bool* result);
196 void OnMsgSetPreferences(const Preferences& prefs);
198 virtual bool SendMessage(IPC::Message* msg);
200 PluginDelegate* plugin_delegate_;
202 // Contains all the plugin interfaces we've queried. The mapped value will
203 // be the pointer to the interface pointer supplied by the plugin if it's
204 // supported, or NULL if it's not supported. This allows us to cache failures
205 // and not req-query if a plugin doesn't support the interface.
206 typedef base::hash_map<std::string, const void*> InterfaceMap;
207 InterfaceMap plugin_interfaces_;
209 typedef base::ScopedPtrHashMap<PP_Instance, scoped_ptr<InstanceData>>
210 InstanceDataMap;
211 InstanceDataMap instance_map_;
213 // The preferences sent from the host. We only want to set this once, which
214 // is what the received_preferences_ indicates. See OnMsgSetPreferences.
215 bool received_preferences_;
216 Preferences preferences_;
218 uint32 plugin_dispatcher_id_;
220 // Set to true when the instances associated with this dispatcher are
221 // incognito mode.
222 bool incognito_;
224 // A filter for sending messages from threads other than the main thread.
225 scoped_refptr<IPC::SyncMessageFilter> sync_filter_;
227 DISALLOW_COPY_AND_ASSIGN(PluginDispatcher);
230 } // namespace proxy
231 } // namespace ppapi
233 #endif // PPAPI_PROXY_PLUGIN_DISPATCHER_H_