• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef PPAPI_PROXY_PLUGIN_DISPATCHER_H_
6 #define PPAPI_PROXY_PLUGIN_DISPATCHER_H_
7 
8 #include <set>
9 #include <string>
10 
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"
28 
29 namespace IPC {
30 class SyncMessageFilter;
31 }
32 
33 namespace ppapi {
34 
35 struct Preferences;
36 class Resource;
37 
38 namespace thunk {
39 class PPB_Instance_API;
40 class ResourceCreationAPI;
41 }
42 
43 namespace proxy {
44 
45 // Used to keep track of per-instance data.
46 struct InstanceData {
47   InstanceData();
48   ~InstanceData();
49 
50   ViewData view;
51 
52   // When non-NULL, indicates the callback to execute when mouse lock is lost.
53   scoped_refptr<TrackedCallback> mouse_lock_callback;
54 
55   // A map of singleton resources which are lazily created.
56   typedef std::map<SingletonResourceID, scoped_refptr<Resource> >
57       SingletonResourceMap;
58   SingletonResourceMap singleton_resources;
59 
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;
66 
67   // The message handler which should handle JavaScript->Plugin messages, if
68   // one has been registered, otherwise NULL.
69   scoped_ptr<MessageHandler> message_handler;
70 };
71 
72 class PPAPI_PROXY_EXPORT PluginDispatcher
73     : public Dispatcher,
74       public base::SupportsWeakPtr<PluginDispatcher> {
75  public:
76   class PPAPI_PROXY_EXPORT PluginDelegate : public ProxyChannel::Delegate {
77    public:
78     // Returns the set used for globally uniquifying PP_Instances. This same
79     // set must be returned for all channels.
80     //
81     // DEREFERENCE ONLY ON THE I/O THREAD.
82     virtual std::set<PP_Instance>* GetGloballySeenInstanceIDSet() = 0;
83 
84     // Registers the plugin dispatcher and returns an ID.
85     // Plugin dispatcher IDs will be used to dispatch messages from the browser.
86     // Each call to Register() has to be matched with a call to Unregister().
87     virtual uint32 Register(PluginDispatcher* plugin_dispatcher) = 0;
88     virtual void Unregister(uint32 plugin_dispatcher_id) = 0;
89   };
90 
91   // Constructor for the plugin side. The init and shutdown functions will be
92   // will be automatically called when requested by the renderer side. The
93   // module ID will be set upon receipt of the InitializeModule message.
94   //
95   // Note about permissions: On the plugin side, the dispatcher and the plugin
96   // run in the same address space (including in nacl). This means that the
97   // permissions here are subject to malicious modification and bypass, and
98   // an exploited or malicious plugin could send any IPC messages and just
99   // bypass the permissions. All permissions must be checked "for realz" in the
100   // host process when receiving messages. We check them on the plugin side
101   // primarily to keep honest plugins honest, especially with respect to
102   // dev interfaces that they "shouldn't" be using.
103   //
104   // You must call InitPluginWithChannel after the constructor.
105   PluginDispatcher(PP_GetInterface_Func get_interface,
106                    const PpapiPermissions& permissions,
107                    bool incognito);
108   virtual ~PluginDispatcher();
109 
110   // The plugin side maintains a mapping from PP_Instance to Dispatcher so
111   // that we can send the messages to the right channel if there are multiple
112   // renderers sharing the same plugin. This mapping is maintained by
113   // DidCreateInstance/DidDestroyInstance.
114   static PluginDispatcher* GetForInstance(PP_Instance instance);
115 
116   // Same as GetForInstance but retrieves the instance from the given resource
117   // object as a convenience. Returns NULL on failure.
118   static PluginDispatcher* GetForResource(const Resource* resource);
119 
120   // Implements the GetInterface function for the plugin to call to retrieve
121   // a browser interface.
122   static const void* GetBrowserInterface(const char* interface_name);
123 
124   // Logs the given log message to the given instance, or, if the instance is
125   // invalid, to all instances associated with all dispatchers. Used for
126   // global log messages.
127   static void LogWithSource(PP_Instance instance,
128                             PP_LogLevel level,
129                             const std::string& source,
130                             const std::string& value);
131 
132   const void* GetPluginInterface(const std::string& interface_name);
133 
134   // You must call this function before anything else. Returns true on success.
135   // The delegate pointer must outlive this class, ownership is not
136   // transferred.
137   bool InitPluginWithChannel(PluginDelegate* delegate,
138                              base::ProcessId peer_pid,
139                              const IPC::ChannelHandle& channel_handle,
140                              bool is_client);
141 
142   // Dispatcher overrides.
143   virtual bool IsPlugin() const;
144   virtual bool Send(IPC::Message* msg);
145 
146   // IPC::Listener implementation.
147   virtual bool OnMessageReceived(const IPC::Message& msg);
148   virtual void OnChannelError();
149 
150   // Keeps track of which dispatcher to use for each instance, active instances
151   // and tracks associated data like the current size.
152   void DidCreateInstance(PP_Instance instance);
153   void DidDestroyInstance(PP_Instance instance);
154 
155   // Gets the data for an existing instance, or NULL if the instance id doesn't
156   // correspond to a known instance.
157   InstanceData* GetInstanceData(PP_Instance instance);
158 
159   // Returns the corresponding API. These are APIs not associated with a
160   // resource. Guaranteed non-NULL.
161   thunk::PPB_Instance_API* GetInstanceAPI();
162   thunk::ResourceCreationAPI* GetResourceCreationAPI();
163 
164   // Returns the Preferences.
preferences()165   const Preferences& preferences() const { return preferences_; }
166 
plugin_dispatcher_id()167   uint32 plugin_dispatcher_id() const { return plugin_dispatcher_id_; }
incognito()168   bool incognito() const { return incognito_; }
169 
170  private:
171   friend class PluginDispatcherTest;
172 
173   // Notifies all live instances that they're now closed. This is used when
174   // a renderer crashes or some other error is received.
175   void ForceFreeAllInstances();
176 
177   // IPC message handlers.
178   void OnMsgSupportsInterface(const std::string& interface_name, bool* result);
179   void OnMsgSetPreferences(const Preferences& prefs);
180 
181   virtual bool SendMessage(IPC::Message* msg);
182 
183   PluginDelegate* plugin_delegate_;
184 
185   // Contains all the plugin interfaces we've queried. The mapped value will
186   // be the pointer to the interface pointer supplied by the plugin if it's
187   // supported, or NULL if it's not supported. This allows us to cache failures
188   // and not req-query if a plugin doesn't support the interface.
189   typedef base::hash_map<std::string, const void*> InterfaceMap;
190   InterfaceMap plugin_interfaces_;
191 
192   typedef base::ScopedPtrHashMap<PP_Instance, InstanceData> InstanceDataMap;
193   InstanceDataMap instance_map_;
194 
195   // The preferences sent from the host. We only want to set this once, which
196   // is what the received_preferences_ indicates. See OnMsgSetPreferences.
197   bool received_preferences_;
198   Preferences preferences_;
199 
200   uint32 plugin_dispatcher_id_;
201 
202   // Set to true when the instances associated with this dispatcher are
203   // incognito mode.
204   bool incognito_;
205 
206   // A filter for sending messages from threads other than the main thread.
207   scoped_refptr<IPC::SyncMessageFilter> sync_filter_;
208 
209   DISALLOW_COPY_AND_ASSIGN(PluginDispatcher);
210 };
211 
212 }  // namespace proxy
213 }  // namespace ppapi
214 
215 #endif  // PPAPI_PROXY_PLUGIN_DISPATCHER_H_
216