| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 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. | |
| 4 | |
| 5 #ifndef CONTENT_CHILD_NP_CHANNEL_BASE_H_ | |
| 6 #define CONTENT_CHILD_NP_CHANNEL_BASE_H_ | |
| 7 | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/basictypes.h" | |
| 11 #include "base/containers/hash_tables.h" | |
| 12 #include "base/memory/ref_counted.h" | |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/process.h" | |
| 15 #include "content/child/npobject_base.h" | |
| 16 #include "content/common/message_router.h" | |
| 17 #include "ipc/ipc_channel_handle.h" | |
| 18 #include "ipc/ipc_sync_channel.h" | |
| 19 | |
| 20 namespace base { | |
| 21 class MessageLoopProxy; | |
| 22 } | |
| 23 | |
| 24 #if defined(COMPILER_GCC) | |
| 25 namespace BASE_HASH_NAMESPACE { | |
| 26 | |
| 27 template<> | |
| 28 struct hash<NPObject*> { | |
| 29 std::size_t operator()(NPObject* const& ptr) const { | |
| 30 return hash<size_t>()(reinterpret_cast<size_t>(ptr)); | |
| 31 } | |
| 32 }; | |
| 33 | |
| 34 template<> | |
| 35 struct hash<struct _NPP*> { | |
| 36 std::size_t operator()(struct _NPP* const& ptr) const { | |
| 37 return hash<size_t>()(reinterpret_cast<size_t>(ptr)); | |
| 38 } | |
| 39 }; | |
| 40 | |
| 41 } // namespace __gnu_cxx | |
| 42 #elif defined(COMPILER_MSVC) | |
| 43 namespace stdext { | |
| 44 | |
| 45 template<> | |
| 46 inline size_t hash_value(NPObject* const& ptr) { | |
| 47 return hash_value(reinterpret_cast<size_t>(ptr)); | |
| 48 } | |
| 49 | |
| 50 } // namespace stdext | |
| 51 #endif // COMPILER | |
| 52 | |
| 53 namespace content { | |
| 54 | |
| 55 // Encapsulates an IPC channel between a renderer and another process. Used to | |
| 56 // proxy access to NP objects. | |
| 57 class NPChannelBase : public IPC::Listener, | |
| 58 public IPC::Sender, | |
| 59 public base::RefCountedThreadSafe<NPChannelBase> { | |
| 60 public: | |
| 61 | |
| 62 // WebPlugin[Delegate] call these on construction and destruction to setup | |
| 63 // the routing and manage lifetime of this object (they pass NULL for | |
| 64 // npobject). These are also called by NPObjectProxy and NPObjectStub (which | |
| 65 // pass themselves for npobject). However the latter don't control the | |
| 66 // lifetime of this object because we don't want a leak of an NPObject to | |
| 67 // keep the channel around longer than necessary. | |
| 68 void AddRoute(int route_id, IPC::Listener* listener, NPObjectBase* npobject); | |
| 69 void RemoveRoute(int route_id); | |
| 70 | |
| 71 void AddMappingForNPObjectProxy(int route_id, NPObject* object); | |
| 72 void RemoveMappingForNPObjectProxy(int route_id); | |
| 73 | |
| 74 void AddMappingForNPObjectStub(int route_id, NPObject* object); | |
| 75 void RemoveMappingForNPObjectStub(int route_id, NPObject* object); | |
| 76 | |
| 77 void AddMappingForNPObjectOwner(int route_id, struct _NPP* owner); | |
| 78 void SetDefaultNPObjectOwner(struct _NPP* owner); | |
| 79 void RemoveMappingForNPObjectOwner(int route_id); | |
| 80 | |
| 81 NPObject* GetExistingNPObjectProxy(int route_id); | |
| 82 int GetExistingRouteForNPObjectStub(NPObject* npobject); | |
| 83 struct _NPP* GetExistingNPObjectOwner(int route_id); | |
| 84 int GetExistingRouteForNPObjectOwner(struct _NPP* owner); | |
| 85 | |
| 86 // IPC::Sender implementation: | |
| 87 virtual bool Send(IPC::Message* msg) OVERRIDE; | |
| 88 | |
| 89 base::ProcessId peer_pid() { return channel_->peer_pid(); } | |
| 90 IPC::ChannelHandle channel_handle() const { return channel_handle_; } | |
| 91 | |
| 92 // Returns the number of open NPObject channels in this process. | |
| 93 static int Count(); | |
| 94 | |
| 95 // Returns a new route id. | |
| 96 virtual int GenerateRouteID() = 0; | |
| 97 | |
| 98 // Returns whether the channel is valid or not. A channel is invalid | |
| 99 // if it is disconnected due to a channel error. | |
| 100 bool channel_valid() { | |
| 101 return channel_valid_; | |
| 102 } | |
| 103 | |
| 104 // Returns the most recent NPChannelBase to have received a message | |
| 105 // in this process. | |
| 106 static NPChannelBase* GetCurrentChannel(); | |
| 107 | |
| 108 static void CleanupChannels(); | |
| 109 | |
| 110 // Returns the NPObjectBase object for the route id passed in. | |
| 111 // Returns NULL on failure. | |
| 112 NPObjectBase* GetNPObjectListenerForRoute(int route_id); | |
| 113 | |
| 114 // Returns the event that's set when a call to the renderer causes a modal | |
| 115 // dialog to come up. The default implementation returns NULL. Derived | |
| 116 // classes should override this method if this functionality is required. | |
| 117 virtual base::WaitableEvent* GetModalDialogEvent(int render_view_id); | |
| 118 | |
| 119 protected: | |
| 120 typedef NPChannelBase* (*ChannelFactory)(); | |
| 121 | |
| 122 friend class base::RefCountedThreadSafe<NPChannelBase>; | |
| 123 | |
| 124 virtual ~NPChannelBase(); | |
| 125 | |
| 126 // Returns a NPChannelBase derived object for the given channel name. | |
| 127 // If an existing channel exists returns that object, otherwise creates a | |
| 128 // new one. Even though on creation the object is refcounted, each caller | |
| 129 // must still ref count the returned value. When there are no more routes | |
| 130 // on the channel and its ref count is 0, the object deletes itself. | |
| 131 static NPChannelBase* GetChannel( | |
| 132 const IPC::ChannelHandle& channel_handle, IPC::Channel::Mode mode, | |
| 133 ChannelFactory factory, base::MessageLoopProxy* ipc_message_loop, | |
| 134 bool create_pipe_now, base::WaitableEvent* shutdown_event); | |
| 135 | |
| 136 // Sends a message to all instances. | |
| 137 static void Broadcast(IPC::Message* message); | |
| 138 | |
| 139 // Called on the worker thread | |
| 140 NPChannelBase(); | |
| 141 | |
| 142 virtual void CleanUp() { } | |
| 143 | |
| 144 // Implemented by derived classes to handle control messages | |
| 145 virtual bool OnControlMessageReceived(const IPC::Message& msg); | |
| 146 | |
| 147 // IPC::Listener implementation: | |
| 148 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE; | |
| 149 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE; | |
| 150 virtual void OnChannelError() OVERRIDE; | |
| 151 | |
| 152 void set_send_unblocking_only_during_unblock_dispatch() { | |
| 153 send_unblocking_only_during_unblock_dispatch_ = true; | |
| 154 } | |
| 155 | |
| 156 virtual bool Init(base::MessageLoopProxy* ipc_message_loop, | |
| 157 bool create_pipe_now, | |
| 158 base::WaitableEvent* shutdown_event); | |
| 159 | |
| 160 scoped_ptr<IPC::SyncChannel> channel_; | |
| 161 IPC::ChannelHandle channel_handle_; | |
| 162 | |
| 163 private: | |
| 164 IPC::Channel::Mode mode_; | |
| 165 // This tracks the number of routes registered without an NPObject. It's used | |
| 166 // to manage the lifetime of this object. See comment for AddRoute() and | |
| 167 // RemoveRoute(). | |
| 168 int non_npobject_count_; | |
| 169 int peer_pid_; | |
| 170 | |
| 171 // true when in the middle of a RemoveRoute call | |
| 172 bool in_remove_route_; | |
| 173 | |
| 174 // Keep track of all the registered NPObjects proxies/stubs so that when the | |
| 175 // channel is closed we can inform them. | |
| 176 typedef base::hash_map<int, NPObjectBase*> ListenerMap; | |
| 177 ListenerMap npobject_listeners_; | |
| 178 | |
| 179 typedef base::hash_map<int, NPObject*> ProxyMap; | |
| 180 ProxyMap proxy_map_; | |
| 181 | |
| 182 typedef base::hash_map<NPObject*, int> StubMap; | |
| 183 StubMap stub_map_; | |
| 184 | |
| 185 typedef base::hash_map<struct _NPP*, int> OwnerToRouteMap; | |
| 186 OwnerToRouteMap owner_to_route_; | |
| 187 | |
| 188 typedef base::hash_map<int, struct _NPP*> RouteToOwnerMap; | |
| 189 RouteToOwnerMap route_to_owner_; | |
| 190 | |
| 191 // Used on the plugin side to represent any object received that does | |
| 192 // not belong to a plugin instance. | |
| 193 struct _NPP* default_owner_; | |
| 194 | |
| 195 // Used to implement message routing functionality to WebPlugin[Delegate] | |
| 196 // objects | |
| 197 MessageRouter router_; | |
| 198 | |
| 199 // A channel is invalid if it is disconnected as a result of a channel | |
| 200 // error. This flag is used to indicate the same. | |
| 201 bool channel_valid_; | |
| 202 | |
| 203 // Track whether we're dispatching a message with the unblock flag; works like | |
| 204 // a refcount, 0 when we're not. | |
| 205 int in_unblock_dispatch_; | |
| 206 | |
| 207 // If true, sync messages will only be marked as unblocking if the channel is | |
| 208 // in the middle of dispatching an unblocking message. The non-renderer | |
| 209 // process wants to avoid setting the unblock flag on its sync messages | |
| 210 // unless necessary, since it can potentially introduce reentrancy into | |
| 211 // WebKit in ways that it doesn't expect (i.e. causing layout during paint). | |
| 212 // However to avoid deadlock, we must ensure that any message that's sent as | |
| 213 // a result of a sync call from the renderer must unblock the renderer. We | |
| 214 // additionally have to do this for async messages from the renderer that | |
| 215 // have the unblock flag set, since they could be followed by a sync message | |
| 216 // that won't get dispatched until the call to the renderer is complete. | |
| 217 bool send_unblocking_only_during_unblock_dispatch_; | |
| 218 | |
| 219 DISALLOW_COPY_AND_ASSIGN(NPChannelBase); | |
| 220 }; | |
| 221 | |
| 222 } // namespace content | |
| 223 | |
| 224 #endif // CONTENT_CHILD_NP_CHANNEL_BASE_H_ | |
| OLD | NEW |