[SyncFS] Build indexes from FileTracker entries on disk.
[chromium-blink-merge.git] / content / child / npapi / np_channel_base.h
blob0f6e11fe36678270421acce2356bcf99a7b8e4d9
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.
5 #ifndef CONTENT_CHILD_NPAPI_NP_CHANNEL_BASE_H_
6 #define CONTENT_CHILD_NPAPI_NP_CHANNEL_BASE_H_
8 #include <string>
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/process.h"
15 #include "content/child/npapi/npobject_base.h"
16 #include "content/common/message_router.h"
17 #include "ipc/ipc_channel_handle.h"
18 #include "ipc/ipc_sync_channel.h"
20 namespace base {
21 class MessageLoopProxy;
24 #if defined(COMPILER_GCC)
25 namespace BASE_HASH_NAMESPACE {
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));
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));
41 } // namespace __gnu_cxx
42 #elif defined(COMPILER_MSVC)
43 namespace stdext {
45 template<>
46 inline size_t hash_value(NPObject* const& ptr) {
47 return hash_value(reinterpret_cast<size_t>(ptr));
50 } // namespace stdext
51 #endif // COMPILER
53 namespace content {
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:
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);
71 void AddMappingForNPObjectProxy(int route_id, NPObject* object);
72 void RemoveMappingForNPObjectProxy(int route_id);
74 void AddMappingForNPObjectStub(int route_id, NPObject* object);
75 void RemoveMappingForNPObjectStub(int route_id, NPObject* object);
77 void AddMappingForNPObjectOwner(int route_id, struct _NPP* owner);
78 void SetDefaultNPObjectOwner(struct _NPP* owner);
79 void RemoveMappingForNPObjectOwner(int route_id);
81 NPObject* GetExistingNPObjectProxy(int route_id);
82 int GetExistingRouteForNPObjectStub(NPObject* npobject);
83 struct _NPP* GetExistingNPObjectOwner(int route_id);
84 int GetExistingRouteForNPObjectOwner(struct _NPP* owner);
86 // IPC::Sender implementation:
87 virtual bool Send(IPC::Message* msg) OVERRIDE;
89 base::ProcessId peer_pid() { return channel_->GetPeerPID(); }
90 IPC::ChannelHandle channel_handle() const { return channel_handle_; }
92 // Returns the number of open NPObject channels in this process.
93 static int Count();
95 // Returns a new route id.
96 virtual int GenerateRouteID() = 0;
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_;
104 // Returns the most recent NPChannelBase to have received a message
105 // in this process.
106 static NPChannelBase* GetCurrentChannel();
108 static void CleanupChannels();
110 // Returns the NPObjectBase object for the route id passed in.
111 // Returns NULL on failure.
112 NPObjectBase* GetNPObjectListenerForRoute(int route_id);
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);
119 protected:
120 typedef NPChannelBase* (*ChannelFactory)();
122 friend class base::RefCountedThreadSafe<NPChannelBase>;
124 virtual ~NPChannelBase();
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);
136 // Sends a message to all instances.
137 static void Broadcast(IPC::Message* message);
139 // Called on the worker thread
140 NPChannelBase();
142 virtual void CleanUp() { }
144 // Implemented by derived classes to handle control messages
145 virtual bool OnControlMessageReceived(const IPC::Message& msg);
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;
152 void set_send_unblocking_only_during_unblock_dispatch() {
153 send_unblocking_only_during_unblock_dispatch_ = true;
156 virtual bool Init(base::MessageLoopProxy* ipc_message_loop,
157 bool create_pipe_now,
158 base::WaitableEvent* shutdown_event);
160 scoped_ptr<IPC::SyncChannel> channel_;
161 IPC::ChannelHandle channel_handle_;
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_;
171 // true when in the middle of a RemoveRoute call
172 bool in_remove_route_;
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_;
179 typedef base::hash_map<int, NPObject*> ProxyMap;
180 ProxyMap proxy_map_;
182 typedef base::hash_map<NPObject*, int> StubMap;
183 StubMap stub_map_;
185 typedef base::hash_map<struct _NPP*, int> OwnerToRouteMap;
186 OwnerToRouteMap owner_to_route_;
188 typedef base::hash_map<int, struct _NPP*> RouteToOwnerMap;
189 RouteToOwnerMap route_to_owner_;
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_;
195 // Used to implement message routing functionality to WebPlugin[Delegate]
196 // objects
197 MessageRouter router_;
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_;
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_;
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_;
219 DISALLOW_COPY_AND_ASSIGN(NPChannelBase);
222 } // namespace content
224 #endif // CONTENT_CHILD_NPAPI_NP_CHANNEL_BASE_H_