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_HOST_DISPATCHER_H_
6 #define PPAPI_PROXY_HOST_DISPATCHER_H_
12 #include "base/compiler_specific.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/observer_list.h"
15 #include "base/process/process.h"
16 #include "ipc/message_filter.h"
17 #include "ppapi/c/pp_instance.h"
18 #include "ppapi/proxy/dispatcher.h"
20 struct PPB_Proxy_Private
;
28 class PPAPI_PROXY_EXPORT HostDispatcher
: public Dispatcher
{
30 // This interface receives notifications about sync messages being sent by
31 // the dispatcher to the plugin process. Some parts of Chrome may need to
32 // know whether we are sending a synchronous message to the plugin; e.g. to
33 // detect a hung plugin or to avoid re-entering JavaScript.
35 // Note that there can be nested sync messages, so the begin/end status
36 // actually represents a stack of blocking messages.
37 class SyncMessageStatusObserver
{
39 // Notification that a sync message is about to be sent out.
40 virtual void BeginBlockOnSyncMessage() = 0;
42 // Notification that a sync message reply was received and the dispatcher
43 // is no longer blocked on a sync message.
44 virtual void EndBlockOnSyncMessage() = 0;
47 virtual ~SyncMessageStatusObserver() {}
50 // Constructor for the renderer side. This will take a reference to the
51 // SyncMessageStatusReceiver.
53 // You must call InitHostWithChannel after the constructor.
54 HostDispatcher(PP_Module module
,
55 PP_GetInterface_Func local_get_interface
,
56 const PpapiPermissions
& permissions
);
59 // You must call this function before anything else. Returns true on success.
60 // The delegate pointer must outlive this class, ownership is not
62 virtual bool InitHostWithChannel(Delegate
* delegate
,
63 base::ProcessId peer_pid
,
64 const IPC::ChannelHandle
& channel_handle
,
66 const Preferences
& preferences
);
68 // The host side maintains a mapping from PP_Instance to Dispatcher so
69 // that we can send the messages to the right channel.
70 static HostDispatcher
* GetForInstance(PP_Instance instance
);
71 static void SetForInstance(PP_Instance instance
,
72 HostDispatcher
* dispatcher
);
73 static void RemoveForInstance(PP_Instance instance
);
75 // Returns the host's notion of our PP_Module. This will be different than
76 // the plugin's notion of its PP_Module because the plugin process may be
77 // used by multiple renderer processes.
79 // Use this value instead of a value from the plugin whenever talking to the
81 PP_Module
pp_module() const { return pp_module_
; }
83 // Dispatcher overrides.
84 bool IsPlugin() const override
;
85 bool Send(IPC::Message
* msg
) override
;
88 bool OnMessageReceived(const IPC::Message
& msg
) override
;
89 void OnChannelError() override
;
91 // Proxied version of calling GetInterface on the plugin. This will check
92 // if the plugin supports the given interface (with caching) and returns the
93 // pointer to the proxied interface if it is supported. Returns NULL if the
94 // given interface isn't supported by the plugin or the proxy.
95 const void* GetProxiedInterface(const std::string
& iface_name
);
97 // See the value below. Call this when processing a scripting message from
98 // the plugin that can be reentered. This is set to false at the beginning
99 // of processing of each message from the plugin.
100 void set_allow_plugin_reentrancy() {
101 allow_plugin_reentrancy_
= true;
104 // Returns the proxy interface for talking to the implementation.
105 const PPB_Proxy_Private
* ppb_proxy() const { return ppb_proxy_
; }
107 // Register an observer that will be invoked when the dispatcher begins
108 // sending a sync message and finishes sending a sync message.
109 // Returns a Closure that can be used to unregister the observer (the Closure
110 // is bound to a weak pointer, so is safe to call even after the
111 // HostDispatcher is gone.)
112 base::Closure
AddSyncMessageStatusObserver(SyncMessageStatusObserver
* obs
);
114 void AddFilter(IPC::Listener
* listener
);
117 // Overridden from Dispatcher.
118 void OnInvalidMessageReceived() override
;
121 void OnHostMsgLogWithSource(PP_Instance instance
,
123 const std::string
& source
,
124 const std::string
& value
);
126 void RemoveSyncMessageStatusObserver(SyncMessageStatusObserver
* obs
);
128 PP_Module pp_module_
;
130 // Maps interface name to whether that interface is supported. If an interface
131 // name is not in the map, that implies that we haven't queried for it yet.
132 typedef base::hash_map
<std::string
, bool> PluginSupportedMap
;
133 PluginSupportedMap plugin_supported_
;
135 // Guaranteed non-NULL.
136 const PPB_Proxy_Private
* ppb_proxy_
;
138 // Set to true when the plugin is in a state that it can be reentered by a
139 // sync message from the host. We allow reentrancy only when we're processing
140 // a sync message from the renderer that is a scripting command. When the
141 // plugin is in this state, it needs to accept reentrancy since scripting may
142 // ultimately call back into the plugin.
143 bool allow_plugin_reentrancy_
;
145 base::ObserverList
<SyncMessageStatusObserver
> sync_status_observer_list_
;
147 std::vector
<IPC::Listener
*> filters_
;
149 base::WeakPtrFactory
<HostDispatcher
> weak_ptr_factory_
;
151 DISALLOW_COPY_AND_ASSIGN(HostDispatcher
);
154 // Create this object on the stack to prevent the module (and hence the
155 // dispatcher) from being deleted out from under you. This is necessary when
156 // calling some scripting functions that may delete the plugin.
158 // This class does nothing if used on the plugin side.
159 class ScopedModuleReference
{
161 explicit ScopedModuleReference(Dispatcher
* dispatcher
);
162 ~ScopedModuleReference();
165 HostDispatcher
* dispatcher_
;
167 DISALLOW_COPY_AND_ASSIGN(ScopedModuleReference
);
173 #endif // PPAPI_PROXY_HOST_DISPATCHER_H_