blob: 345f8eab6872f31ce7e7d35777b7923450b15d3a [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]c47317e2012-06-20 22:35:3112#include "base/compiler_specific.h"
dmichael6b328f3d2014-09-29 23:49:0213#include "base/memory/weak_ptr.h"
14#include "base/observer_list.h"
[email protected]15756592013-07-25 14:17:5315#include "base/process/process.h"
[email protected]74122042014-04-25 00:07:3016#include "ipc/message_filter.h"
[email protected]c2932f5e2010-11-03 03:22:3317#include "ppapi/c/pp_instance.h"
18#include "ppapi/proxy/dispatcher.h"
[email protected]c2932f5e2010-11-03 03:22:3319
[email protected]4f15d2842011-02-15 17:36:3320struct PPB_Proxy_Private;
[email protected]c2932f5e2010-11-03 03:22:3321
[email protected]208aad792011-05-26 19:05:2822namespace ppapi {
[email protected]208aad792011-05-26 19:05:2823
[email protected]4d2efd22011-08-18 21:58:0224struct Preferences;
25
[email protected]c2932f5e2010-11-03 03:22:3326namespace proxy {
27
[email protected]f0a04c42011-08-26 22:43:2028class PPAPI_PROXY_EXPORT HostDispatcher : public Dispatcher {
[email protected]c2932f5e2010-11-03 03:22:3329 public:
[email protected]8be45842012-04-13 19:49:2930 // This interface receives notifications about sync messages being sent by
dmichael6b328f3d2014-09-29 23:49:0231 // 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.
[email protected]8be45842012-04-13 19:49:2934 //
35 // Note that there can be nested sync messages, so the begin/end status
36 // actually represents a stack of blocking messages.
dmichael6b328f3d2014-09-29 23:49:0237 class SyncMessageStatusObserver {
[email protected]8be45842012-04-13 19:49:2938 public:
[email protected]8be45842012-04-13 19:49:2939 // Notification that a sync message is about to be sent out.
40 virtual void BeginBlockOnSyncMessage() = 0;
41
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;
[email protected]512d03f2012-06-26 01:06:0645
46 protected:
dmichael6b328f3d2014-09-29 23:49:0247 virtual ~SyncMessageStatusObserver() {}
[email protected]8be45842012-04-13 19:49:2948 };
49
50 // Constructor for the renderer side. This will take a reference to the
51 // SyncMessageStatusReceiver.
[email protected]c2932f5e2010-11-03 03:22:3352 //
[email protected]e2614c62011-04-16 22:12:4553 // You must call InitHostWithChannel after the constructor.
[email protected]f0ecb552012-05-11 22:09:1154 HostDispatcher(PP_Module module,
[email protected]5ed5a3e2012-04-13 22:46:4255 PP_GetInterface_Func local_get_interface,
[email protected]195d4cde2012-10-02 18:12:4156 const PpapiPermissions& permissions);
[email protected]c2932f5e2010-11-03 03:22:3357 ~HostDispatcher();
58
[email protected]e2614c62011-04-16 22:12:4559 // You must call this function before anything else. Returns true on success.
60 // The delegate pointer must outlive this class, ownership is not
61 // transferred.
[email protected]7a1f7c6f2011-05-10 21:17:4862 virtual bool InitHostWithChannel(Delegate* delegate,
[email protected]108fd342013-01-04 20:46:5463 base::ProcessId peer_pid,
[email protected]7a1f7c6f2011-05-10 21:17:4864 const IPC::ChannelHandle& channel_handle,
[email protected]208aad792011-05-26 19:05:2865 bool is_client,
[email protected]4d2efd22011-08-18 21:58:0266 const Preferences& preferences);
[email protected]e2614c62011-04-16 22:12:4567
[email protected]c2932f5e2010-11-03 03:22:3368 // 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);
74
[email protected]4f15d2842011-02-15 17:36:3375 // 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.
78 //
79 // Use this value instead of a value from the plugin whenever talking to the
80 // host.
81 PP_Module pp_module() const { return pp_module_; }
82
[email protected]c2932f5e2010-11-03 03:22:3383 // Dispatcher overrides.
[email protected]7cf40912010-12-09 18:25:0384 virtual bool IsPlugin() const;
[email protected]037f63f2011-03-13 19:44:4685 virtual bool Send(IPC::Message* msg);
[email protected]c2932f5e2010-11-03 03:22:3386
[email protected]c47317e2012-06-20 22:35:3187 // IPC::Listener.
mostynb699af3c2014-10-06 18:03:3488 virtual bool OnMessageReceived(const IPC::Message& msg) override;
89 virtual void OnChannelError() override;
[email protected]465faa22011-02-08 16:31:4690
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.
[email protected]5c966022011-09-13 18:09:3795 const void* GetProxiedInterface(const std::string& iface_name);
[email protected]4deeb432011-02-17 23:59:3996
[email protected]037f63f2011-03-13 19:44:4697 // See the value below. Call this when processing a scripting message from
[email protected]5c966022011-09-13 18:09:3798 // the plugin that can be reentered. This is set to false at the beginning
99 // of processing of each message from the plugin.
[email protected]037f63f2011-03-13 19:44:46100 void set_allow_plugin_reentrancy() {
101 allow_plugin_reentrancy_ = true;
102 }
103
[email protected]4f15d2842011-02-15 17:36:33104 // Returns the proxy interface for talking to the implementation.
[email protected]2cc062242011-03-10 21:16:34105 const PPB_Proxy_Private* ppb_proxy() const { return ppb_proxy_; }
[email protected]4f15d2842011-02-15 17:36:33106
dmichael6b328f3d2014-09-29 23:49:02107 // 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);
113
[email protected]13eda482014-02-26 18:30:48114 void AddFilter(IPC::Listener* listener);
115
[email protected]5c966022011-09-13 18:09:37116 protected:
117 // Overridden from Dispatcher.
118 virtual void OnInvalidMessageReceived();
119
[email protected]c2932f5e2010-11-03 03:22:33120 private:
[email protected]a9b16dd2012-01-31 05:00:26121 void OnHostMsgLogWithSource(PP_Instance instance,
122 int int_log_level,
123 const std::string& source,
124 const std::string& value);
[email protected]a9b16dd2012-01-31 05:00:26125
dmichael6b328f3d2014-09-29 23:49:02126 void RemoveSyncMessageStatusObserver(SyncMessageStatusObserver* obs);
dmichael1ea0de32014-09-26 21:10:12127
[email protected]4f15d2842011-02-15 17:36:33128 PP_Module pp_module_;
129
[email protected]912f3d6c2011-06-29 18:26:36130 // 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.
[email protected]5c966022011-09-13 18:09:37132 typedef base::hash_map<std::string, bool> PluginSupportedMap;
133 PluginSupportedMap plugin_supported_;
[email protected]6239d342011-05-06 22:55:47134
[email protected]2cc062242011-03-10 21:16:34135 // Guaranteed non-NULL.
[email protected]4f15d2842011-02-15 17:36:33136 const PPB_Proxy_Private* ppb_proxy_;
137
[email protected]037f63f2011-03-13 19:44:46138 // 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_;
144
dmichael6b328f3d2014-09-29 23:49:02145 ObserverList<SyncMessageStatusObserver> sync_status_observer_list_;
146
[email protected]13eda482014-02-26 18:30:48147 std::vector<IPC::Listener*> filters_;
148
dmichael6b328f3d2014-09-29 23:49:02149 base::WeakPtrFactory<HostDispatcher> weak_ptr_factory_;
150
[email protected]c2932f5e2010-11-03 03:22:33151 DISALLOW_COPY_AND_ASSIGN(HostDispatcher);
152};
153
[email protected]3502a99692011-04-18 20:51:18154// 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.
157//
[email protected]9891f382012-05-07 22:00:34158// This class does nothing if used on the plugin side.
[email protected]3502a99692011-04-18 20:51:18159class ScopedModuleReference {
160 public:
[email protected]0a6d5f62011-11-11 08:04:07161 explicit ScopedModuleReference(Dispatcher* dispatcher);
[email protected]3502a99692011-04-18 20:51:18162 ~ScopedModuleReference();
163
164 private:
165 HostDispatcher* dispatcher_;
166
167 DISALLOW_COPY_AND_ASSIGN(ScopedModuleReference);
168};
169
[email protected]c2932f5e2010-11-03 03:22:33170} // namespace proxy
[email protected]4d2efd22011-08-18 21:58:02171} // namespace ppapi
[email protected]c2932f5e2010-11-03 03:22:33172
173#endif // PPAPI_PROXY_HOST_DISPATCHER_H_