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