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_
11 #include "base/basictypes.h"
12 #include "base/hash_tables.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/process.h"
16 #include "build/build_config.h"
17 #include "ppapi/c/dev/ppb_console_dev.h"
18 #include "ppapi/c/pp_rect.h"
19 #include "ppapi/c/pp_instance.h"
20 #include "ppapi/proxy/dispatcher.h"
21 #include "ppapi/shared_impl/ppapi_preferences.h"
22 #include "ppapi/shared_impl/ppb_view_shared.h"
23 #include "ppapi/shared_impl/tracked_callback.h"
31 class PPB_Instance_API
;
32 class ResourceCreationAPI
;
37 // Used to keep track of per-instance data.
44 PP_Bool flash_fullscreen
; // Used for PPB_FlashFullscreen.
46 // When non-NULL, indicates the callback to execute when mouse lock is lost.
47 scoped_refptr
<TrackedCallback
> mouse_lock_callback
;
50 class PPAPI_PROXY_EXPORT PluginDispatcher
52 public base::SupportsWeakPtr
<PluginDispatcher
> {
54 class PPAPI_PROXY_EXPORT PluginDelegate
: public ProxyChannel::Delegate
{
56 // Returns the set used for globally uniquifying PP_Instances. This same
57 // set must be returned for all channels.
59 // DEREFERENCE ONLY ON THE I/O THREAD.
60 virtual std::set
<PP_Instance
>* GetGloballySeenInstanceIDSet() = 0;
62 // Registers the plugin dispatcher and returns an ID.
63 // Plugin dispatcher IDs will be used to dispatch messages from the browser.
64 // Each call to Register() has to be matched with a call to Unregister().
65 virtual uint32
Register(PluginDispatcher
* plugin_dispatcher
) = 0;
66 virtual void Unregister(uint32 plugin_dispatcher_id
) = 0;
69 // Constructor for the plugin side. The init and shutdown functions will be
70 // will be automatically called when requested by the renderer side. The
71 // module ID will be set upon receipt of the InitializeModule message.
73 // You must call InitPluginWithChannel after the constructor.
74 PluginDispatcher(PP_GetInterface_Func get_interface
,
76 virtual ~PluginDispatcher();
78 // The plugin side maintains a mapping from PP_Instance to Dispatcher so
79 // that we can send the messages to the right channel if there are multiple
80 // renderers sharing the same plugin. This mapping is maintained by
81 // DidCreateInstance/DidDestroyInstance.
82 static PluginDispatcher
* GetForInstance(PP_Instance instance
);
84 // Same as GetForInstance but retrieves the instance from the given resource
85 // object as a convenience. Returns NULL on failure.
86 static PluginDispatcher
* GetForResource(const Resource
* resource
);
88 // Implements the GetInterface function for the plugin to call to retrieve
89 // a browser interface.
90 static const void* GetBrowserInterface(const char* interface_name
);
92 // Logs the given log message to the given instance, or, if the instance is
93 // invalid, to all instances associated with all dispatchers. Used for
94 // global log messages.
95 static void LogWithSource(PP_Instance instance
,
96 PP_LogLevel_Dev level
,
97 const std::string
& source
,
98 const std::string
& value
);
100 const void* GetPluginInterface(const std::string
& interface_name
);
102 // You must call this function before anything else. Returns true on success.
103 // The delegate pointer must outlive this class, ownership is not
105 bool InitPluginWithChannel(PluginDelegate
* delegate
,
106 const IPC::ChannelHandle
& channel_handle
,
109 // Dispatcher overrides.
110 virtual bool IsPlugin() const;
111 virtual bool Send(IPC::Message
* msg
);
113 // IPC::Listener implementation.
114 virtual bool OnMessageReceived(const IPC::Message
& msg
);
115 virtual void OnChannelError();
117 // Keeps track of which dispatcher to use for each instance, active instances
118 // and tracks associated data like the current size.
119 void DidCreateInstance(PP_Instance instance
);
120 void DidDestroyInstance(PP_Instance instance
);
122 // Gets the data for an existing instance, or NULL if the instance id doesn't
123 // correspond to a known instance.
124 InstanceData
* GetInstanceData(PP_Instance instance
);
126 // Returns the corresponding API. These are APIs not associated with a
127 // resource. Guaranteed non-NULL.
128 thunk::PPB_Instance_API
* GetInstanceAPI();
129 thunk::ResourceCreationAPI
* GetResourceCreationAPI();
131 // Returns the Preferences.
132 const Preferences
& preferences() const { return preferences_
; }
134 uint32
plugin_dispatcher_id() const { return plugin_dispatcher_id_
; }
135 bool incognito() const { return incognito_
; }
138 friend class PluginDispatcherTest
;
140 // Notifies all live instances that they're now closed. This is used when
141 // a renderer crashes or some other error is received.
142 void ForceFreeAllInstances();
144 // IPC message handlers.
145 void OnMsgSupportsInterface(const std::string
& interface_name
, bool* result
);
146 void OnMsgSetPreferences(const Preferences
& prefs
);
148 PluginDelegate
* plugin_delegate_
;
150 // Contains all the plugin interfaces we've queried. The mapped value will
151 // be the pointer to the interface pointer supplied by the plugin if it's
152 // supported, or NULL if it's not supported. This allows us to cache failures
153 // and not req-query if a plugin doesn't support the interface.
154 typedef base::hash_map
<std::string
, const void*> InterfaceMap
;
155 InterfaceMap plugin_interfaces_
;
157 typedef base::hash_map
<PP_Instance
, InstanceData
> InstanceDataMap
;
158 InstanceDataMap instance_map_
;
160 // The preferences sent from the host. We only want to set this once, which
161 // is what the received_preferences_ indicates. See OnMsgSetPreferences.
162 bool received_preferences_
;
163 Preferences preferences_
;
165 uint32 plugin_dispatcher_id_
;
167 // Set to true when the instances associated with this dispatcher are
171 DISALLOW_COPY_AND_ASSIGN(PluginDispatcher
);
177 #endif // PPAPI_PROXY_PLUGIN_DISPATCHER_H_