[email protected] | 9891f38 | 2012-05-07 22:00:34 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 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_HOST_DISPATCHER_H_ |
| 6 | #define PPAPI_PROXY_HOST_DISPATCHER_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
[email protected] | c47317e | 2012-06-20 22:35:31 | [diff] [blame] | 12 | #include "base/compiler_specific.h" |
dmichael | 6b328f3d | 2014-09-29 23:49:02 | [diff] [blame] | 13 | #include "base/memory/weak_ptr.h" |
| 14 | #include "base/observer_list.h" |
[email protected] | 1575659 | 2013-07-25 14:17:53 | [diff] [blame] | 15 | #include "base/process/process.h" |
[email protected] | 7412204 | 2014-04-25 00:07:30 | [diff] [blame] | 16 | #include "ipc/message_filter.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 17 | #include "ppapi/c/pp_instance.h" |
| 18 | #include "ppapi/proxy/dispatcher.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 19 | |
[email protected] | 4f15d284 | 2011-02-15 17:36:33 | [diff] [blame] | 20 | struct PPB_Proxy_Private; |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 21 | |
[email protected] | 208aad79 | 2011-05-26 19:05:28 | [diff] [blame] | 22 | namespace ppapi { |
[email protected] | 208aad79 | 2011-05-26 19:05:28 | [diff] [blame] | 23 | |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 24 | struct Preferences; |
| 25 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 26 | namespace proxy { |
| 27 | |
[email protected] | f0a04c4 | 2011-08-26 22:43:20 | [diff] [blame] | 28 | class PPAPI_PROXY_EXPORT HostDispatcher : public Dispatcher { |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 29 | public: |
[email protected] | 8be4584 | 2012-04-13 19:49:29 | [diff] [blame] | 30 | // This interface receives notifications about sync messages being sent by |
dmichael | 6b328f3d | 2014-09-29 23:49:02 | [diff] [blame] | 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. |
[email protected] | 8be4584 | 2012-04-13 19:49:29 | [diff] [blame] | 34 | // |
| 35 | // Note that there can be nested sync messages, so the begin/end status |
| 36 | // actually represents a stack of blocking messages. |
dmichael | 6b328f3d | 2014-09-29 23:49:02 | [diff] [blame] | 37 | class SyncMessageStatusObserver { |
[email protected] | 8be4584 | 2012-04-13 19:49:29 | [diff] [blame] | 38 | public: |
[email protected] | 8be4584 | 2012-04-13 19:49:29 | [diff] [blame] | 39 | // 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] | 512d03f | 2012-06-26 01:06:06 | [diff] [blame] | 45 | |
| 46 | protected: |
dmichael | 6b328f3d | 2014-09-29 23:49:02 | [diff] [blame] | 47 | virtual ~SyncMessageStatusObserver() {} |
[email protected] | 8be4584 | 2012-04-13 19:49:29 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | // Constructor for the renderer side. This will take a reference to the |
| 51 | // SyncMessageStatusReceiver. |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 52 | // |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 53 | // You must call InitHostWithChannel after the constructor. |
[email protected] | f0ecb55 | 2012-05-11 22:09:11 | [diff] [blame] | 54 | HostDispatcher(PP_Module module, |
[email protected] | 5ed5a3e | 2012-04-13 22:46:42 | [diff] [blame] | 55 | PP_GetInterface_Func local_get_interface, |
[email protected] | 195d4cde | 2012-10-02 18:12:41 | [diff] [blame] | 56 | const PpapiPermissions& permissions); |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 57 | ~HostDispatcher(); |
| 58 | |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 59 | // 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] | 7a1f7c6f | 2011-05-10 21:17:48 | [diff] [blame] | 62 | virtual bool InitHostWithChannel(Delegate* delegate, |
[email protected] | 108fd34 | 2013-01-04 20:46:54 | [diff] [blame] | 63 | base::ProcessId peer_pid, |
[email protected] | 7a1f7c6f | 2011-05-10 21:17:48 | [diff] [blame] | 64 | const IPC::ChannelHandle& channel_handle, |
[email protected] | 208aad79 | 2011-05-26 19:05:28 | [diff] [blame] | 65 | bool is_client, |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 66 | const Preferences& preferences); |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 67 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 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); |
| 74 | |
[email protected] | 4f15d284 | 2011-02-15 17:36:33 | [diff] [blame] | 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. |
| 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] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 83 | // Dispatcher overrides. |
[email protected] | 7cf4091 | 2010-12-09 18:25:03 | [diff] [blame] | 84 | virtual bool IsPlugin() const; |
[email protected] | 037f63f | 2011-03-13 19:44:46 | [diff] [blame] | 85 | virtual bool Send(IPC::Message* msg); |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 86 | |
[email protected] | c47317e | 2012-06-20 22:35:31 | [diff] [blame] | 87 | // IPC::Listener. |
mostynb | 699af3c | 2014-10-06 18:03:34 | [diff] [blame] | 88 | virtual bool OnMessageReceived(const IPC::Message& msg) override; |
| 89 | virtual void OnChannelError() override; |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 90 | |
| 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] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 95 | const void* GetProxiedInterface(const std::string& iface_name); |
[email protected] | 4deeb43 | 2011-02-17 23:59:39 | [diff] [blame] | 96 | |
[email protected] | 037f63f | 2011-03-13 19:44:46 | [diff] [blame] | 97 | // See the value below. Call this when processing a scripting message from |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 98 | // 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] | 037f63f | 2011-03-13 19:44:46 | [diff] [blame] | 100 | void set_allow_plugin_reentrancy() { |
| 101 | allow_plugin_reentrancy_ = true; |
| 102 | } |
| 103 | |
[email protected] | 4f15d284 | 2011-02-15 17:36:33 | [diff] [blame] | 104 | // Returns the proxy interface for talking to the implementation. |
[email protected] | 2cc06224 | 2011-03-10 21:16:34 | [diff] [blame] | 105 | const PPB_Proxy_Private* ppb_proxy() const { return ppb_proxy_; } |
[email protected] | 4f15d284 | 2011-02-15 17:36:33 | [diff] [blame] | 106 | |
dmichael | 6b328f3d | 2014-09-29 23:49:02 | [diff] [blame] | 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); |
| 113 | |
[email protected] | 13eda48 | 2014-02-26 18:30:48 | [diff] [blame] | 114 | void AddFilter(IPC::Listener* listener); |
| 115 | |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 116 | protected: |
| 117 | // Overridden from Dispatcher. |
| 118 | virtual void OnInvalidMessageReceived(); |
| 119 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 120 | private: |
[email protected] | a9b16dd | 2012-01-31 05:00:26 | [diff] [blame] | 121 | void OnHostMsgLogWithSource(PP_Instance instance, |
| 122 | int int_log_level, |
| 123 | const std::string& source, |
| 124 | const std::string& value); |
[email protected] | a9b16dd | 2012-01-31 05:00:26 | [diff] [blame] | 125 | |
dmichael | 6b328f3d | 2014-09-29 23:49:02 | [diff] [blame] | 126 | void RemoveSyncMessageStatusObserver(SyncMessageStatusObserver* obs); |
dmichael | 1ea0de3 | 2014-09-26 21:10:12 | [diff] [blame] | 127 | |
[email protected] | 4f15d284 | 2011-02-15 17:36:33 | [diff] [blame] | 128 | PP_Module pp_module_; |
| 129 | |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 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. |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 132 | typedef base::hash_map<std::string, bool> PluginSupportedMap; |
| 133 | PluginSupportedMap plugin_supported_; |
[email protected] | 6239d34 | 2011-05-06 22:55:47 | [diff] [blame] | 134 | |
[email protected] | 2cc06224 | 2011-03-10 21:16:34 | [diff] [blame] | 135 | // Guaranteed non-NULL. |
[email protected] | 4f15d284 | 2011-02-15 17:36:33 | [diff] [blame] | 136 | const PPB_Proxy_Private* ppb_proxy_; |
| 137 | |
[email protected] | 037f63f | 2011-03-13 19:44:46 | [diff] [blame] | 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_; |
| 144 | |
dmichael | 6b328f3d | 2014-09-29 23:49:02 | [diff] [blame] | 145 | ObserverList<SyncMessageStatusObserver> sync_status_observer_list_; |
| 146 | |
[email protected] | 13eda48 | 2014-02-26 18:30:48 | [diff] [blame] | 147 | std::vector<IPC::Listener*> filters_; |
| 148 | |
dmichael | 6b328f3d | 2014-09-29 23:49:02 | [diff] [blame] | 149 | base::WeakPtrFactory<HostDispatcher> weak_ptr_factory_; |
| 150 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 151 | DISALLOW_COPY_AND_ASSIGN(HostDispatcher); |
| 152 | }; |
| 153 | |
[email protected] | 3502a9969 | 2011-04-18 20:51:18 | [diff] [blame] | 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. |
| 157 | // |
[email protected] | 9891f38 | 2012-05-07 22:00:34 | [diff] [blame] | 158 | // This class does nothing if used on the plugin side. |
[email protected] | 3502a9969 | 2011-04-18 20:51:18 | [diff] [blame] | 159 | class ScopedModuleReference { |
| 160 | public: |
[email protected] | 0a6d5f6 | 2011-11-11 08:04:07 | [diff] [blame] | 161 | explicit ScopedModuleReference(Dispatcher* dispatcher); |
[email protected] | 3502a9969 | 2011-04-18 20:51:18 | [diff] [blame] | 162 | ~ScopedModuleReference(); |
| 163 | |
| 164 | private: |
| 165 | HostDispatcher* dispatcher_; |
| 166 | |
| 167 | DISALLOW_COPY_AND_ASSIGN(ScopedModuleReference); |
| 168 | }; |
| 169 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 170 | } // namespace proxy |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 171 | } // namespace ppapi |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 172 | |
| 173 | #endif // PPAPI_PROXY_HOST_DISPATCHER_H_ |