blob: e7b513507e297523ca5379797a5e48dd39d9bdf3 [file] [log] [blame]
[email protected]9891f382012-05-07 22:00:341// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]c2932f5e2010-11-03 03:22:332// 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_HOST_DISPATCHER_H_
6#define PPAPI_PROXY_HOST_DISPATCHER_H_
7
8#include <map>
9#include <string>
10#include <vector>
11
[email protected]8be45842012-04-13 19:49:2912#include "base/memory/ref_counted.h"
[email protected]3b63f8f42011-03-28 01:54:1513#include "base/memory/scoped_ptr.h"
[email protected]5d84d012010-12-02 17:17:2114#include "base/process.h"
[email protected]8be45842012-04-13 19:49:2915#include "ipc/ipc_channel_proxy.h"
[email protected]c2932f5e2010-11-03 03:22:3316#include "ppapi/c/pp_instance.h"
17#include "ppapi/proxy/dispatcher.h"
[email protected]c2932f5e2010-11-03 03:22:3318
[email protected]4f15d2842011-02-15 17:36:3319struct PPB_Proxy_Private;
[email protected]c2932f5e2010-11-03 03:22:3320
[email protected]208aad792011-05-26 19:05:2821namespace ppapi {
[email protected]208aad792011-05-26 19:05:2822
[email protected]4d2efd22011-08-18 21:58:0223struct Preferences;
24
[email protected]c2932f5e2010-11-03 03:22:3325namespace proxy {
26
[email protected]f0a04c42011-08-26 22:43:2027class PPAPI_PROXY_EXPORT HostDispatcher : public Dispatcher {
[email protected]c2932f5e2010-11-03 03:22:3328 public:
[email protected]8be45842012-04-13 19:49:2929 // This interface receives notifications about sync messages being sent by
30 // the dispatcher to the plugin process. It is used to detect a hung plugin.
31 //
32 // Note that there can be nested sync messages, so the begin/end status
33 // actually represents a stack of blocking messages.
34 class SyncMessageStatusReceiver : public IPC::ChannelProxy::MessageFilter {
35 public:
36 virtual ~SyncMessageStatusReceiver() {}
37
38 // Notification that a sync message is about to be sent out.
39 virtual void BeginBlockOnSyncMessage() = 0;
40
41 // Notification that a sync message reply was received and the dispatcher
42 // is no longer blocked on a sync message.
43 virtual void EndBlockOnSyncMessage() = 0;
44 };
45
46 // Constructor for the renderer side. This will take a reference to the
47 // SyncMessageStatusReceiver.
[email protected]c2932f5e2010-11-03 03:22:3348 //
[email protected]e2614c62011-04-16 22:12:4549 // You must call InitHostWithChannel after the constructor.
[email protected]5d84d012010-12-02 17:17:2150 HostDispatcher(base::ProcessHandle host_process_handle,
[email protected]c2932f5e2010-11-03 03:22:3351 PP_Module module,
[email protected]5ed5a3e2012-04-13 22:46:4252 PP_GetInterface_Func local_get_interface,
[email protected]8be45842012-04-13 19:49:2953 SyncMessageStatusReceiver* sync_status);
[email protected]c2932f5e2010-11-03 03:22:3354 ~HostDispatcher();
55
[email protected]e2614c62011-04-16 22:12:4556 // You must call this function before anything else. Returns true on success.
57 // The delegate pointer must outlive this class, ownership is not
58 // transferred.
[email protected]7a1f7c6f2011-05-10 21:17:4859 virtual bool InitHostWithChannel(Delegate* delegate,
60 const IPC::ChannelHandle& channel_handle,
[email protected]208aad792011-05-26 19:05:2861 bool is_client,
[email protected]4d2efd22011-08-18 21:58:0262 const Preferences& preferences);
[email protected]e2614c62011-04-16 22:12:4563
[email protected]c2932f5e2010-11-03 03:22:3364 // The host side maintains a mapping from PP_Instance to Dispatcher so
65 // that we can send the messages to the right channel.
66 static HostDispatcher* GetForInstance(PP_Instance instance);
67 static void SetForInstance(PP_Instance instance,
68 HostDispatcher* dispatcher);
69 static void RemoveForInstance(PP_Instance instance);
70
[email protected]4f15d2842011-02-15 17:36:3371 // Returns the host's notion of our PP_Module. This will be different than
72 // the plugin's notion of its PP_Module because the plugin process may be
73 // used by multiple renderer processes.
74 //
75 // Use this value instead of a value from the plugin whenever talking to the
76 // host.
77 PP_Module pp_module() const { return pp_module_; }
78
[email protected]c2932f5e2010-11-03 03:22:3379 // Dispatcher overrides.
[email protected]7cf40912010-12-09 18:25:0380 virtual bool IsPlugin() const;
[email protected]037f63f2011-03-13 19:44:4681 virtual bool Send(IPC::Message* msg);
[email protected]c2932f5e2010-11-03 03:22:3382
[email protected]465faa22011-02-08 16:31:4683 // IPC::Channel::Listener.
84 virtual bool OnMessageReceived(const IPC::Message& msg);
[email protected]a08ebea2011-02-13 17:50:2085 virtual void OnChannelError();
[email protected]465faa22011-02-08 16:31:4686
87 // Proxied version of calling GetInterface on the plugin. This will check
88 // if the plugin supports the given interface (with caching) and returns the
89 // pointer to the proxied interface if it is supported. Returns NULL if the
90 // given interface isn't supported by the plugin or the proxy.
[email protected]5c966022011-09-13 18:09:3791 const void* GetProxiedInterface(const std::string& iface_name);
[email protected]4deeb432011-02-17 23:59:3992
[email protected]037f63f2011-03-13 19:44:4693 // See the value below. Call this when processing a scripting message from
[email protected]5c966022011-09-13 18:09:3794 // the plugin that can be reentered. This is set to false at the beginning
95 // of processing of each message from the plugin.
[email protected]037f63f2011-03-13 19:44:4696 void set_allow_plugin_reentrancy() {
97 allow_plugin_reentrancy_ = true;
98 }
99
[email protected]4f15d2842011-02-15 17:36:33100 // Returns the proxy interface for talking to the implementation.
[email protected]2cc062242011-03-10 21:16:34101 const PPB_Proxy_Private* ppb_proxy() const { return ppb_proxy_; }
[email protected]4f15d2842011-02-15 17:36:33102
[email protected]5c966022011-09-13 18:09:37103 protected:
104 // Overridden from Dispatcher.
105 virtual void OnInvalidMessageReceived();
106
[email protected]c2932f5e2010-11-03 03:22:33107 private:
[email protected]a9b16dd2012-01-31 05:00:26108 void OnHostMsgLogWithSource(PP_Instance instance,
109 int int_log_level,
110 const std::string& source,
111 const std::string& value);
112
[email protected]8be45842012-04-13 19:49:29113 scoped_refptr<SyncMessageStatusReceiver> sync_status_;
114
[email protected]4f15d2842011-02-15 17:36:33115 PP_Module pp_module_;
116
[email protected]912f3d6c2011-06-29 18:26:36117 // Maps interface name to whether that interface is supported. If an interface
118 // name is not in the map, that implies that we haven't queried for it yet.
[email protected]5c966022011-09-13 18:09:37119 typedef base::hash_map<std::string, bool> PluginSupportedMap;
120 PluginSupportedMap plugin_supported_;
[email protected]6239d342011-05-06 22:55:47121
[email protected]2cc062242011-03-10 21:16:34122 // Guaranteed non-NULL.
[email protected]4f15d2842011-02-15 17:36:33123 const PPB_Proxy_Private* ppb_proxy_;
124
[email protected]037f63f2011-03-13 19:44:46125 // Set to true when the plugin is in a state that it can be reentered by a
126 // sync message from the host. We allow reentrancy only when we're processing
127 // a sync message from the renderer that is a scripting command. When the
128 // plugin is in this state, it needs to accept reentrancy since scripting may
129 // ultimately call back into the plugin.
130 bool allow_plugin_reentrancy_;
131
[email protected]c2932f5e2010-11-03 03:22:33132 DISALLOW_COPY_AND_ASSIGN(HostDispatcher);
133};
134
[email protected]3502a99692011-04-18 20:51:18135// Create this object on the stack to prevent the module (and hence the
136// dispatcher) from being deleted out from under you. This is necessary when
137// calling some scripting functions that may delete the plugin.
138//
[email protected]9891f382012-05-07 22:00:34139// This class does nothing if used on the plugin side.
[email protected]3502a99692011-04-18 20:51:18140class ScopedModuleReference {
141 public:
[email protected]0a6d5f62011-11-11 08:04:07142 explicit ScopedModuleReference(Dispatcher* dispatcher);
[email protected]3502a99692011-04-18 20:51:18143 ~ScopedModuleReference();
144
145 private:
146 HostDispatcher* dispatcher_;
147
148 DISALLOW_COPY_AND_ASSIGN(ScopedModuleReference);
149};
150
[email protected]c2932f5e2010-11-03 03:22:33151} // namespace proxy
[email protected]4d2efd22011-08-18 21:58:02152} // namespace ppapi
[email protected]c2932f5e2010-11-03 03:22:33153
154#endif // PPAPI_PROXY_HOST_DISPATCHER_H_