chromeos: dbus: add Bluetooth properties support
[chromium-blink-merge.git] / content / common / np_channel_base.h
blobc931949309d3fb28b56d21cecbb8dccc2ac748a5
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.
5 #ifndef CONTENT_COMMON_NP_CHANNEL_BASE_H_
6 #define CONTENT_COMMON_NP_CHANNEL_BASE_H_
7 #pragma once
9 #include <string>
11 #include "base/basictypes.h"
12 #include "base/hash_tables.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.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 #include "ui/gfx/native_widget_types.h"
21 namespace base {
22 class MessageLoopProxy;
25 #if defined(COMPILER_GCC)
26 namespace BASE_HASH_NAMESPACE {
28 template<>
29 struct hash<NPObject*> {
30 std::size_t operator()(NPObject* const& ptr) const {
31 return hash<size_t>()(reinterpret_cast<size_t>(ptr));
35 } // namespace __gnu_cxx
36 #elif defined(COMPILER_MSVC)
37 namespace stdext {
39 template<>
40 inline size_t hash_value(NPObject* const& ptr) {
41 return hash_value(reinterpret_cast<size_t>(ptr));
44 } // namespace stdext
45 #endif // COMPILER
47 // Encapsulates an IPC channel between a renderer and another process. Used to
48 // proxy access to NP objects.
49 class NPChannelBase : public IPC::Channel::Listener,
50 public IPC::Message::Sender,
51 public base::RefCountedThreadSafe<NPChannelBase> {
52 public:
54 // WebPlugin[Delegate] call these on construction and destruction to setup
55 // the routing and manage lifetime of this object (they pass NULL for
56 // npobject). These are also called by NPObjectProxy and NPObjectStub (which
57 // pass themselves for npobject). However the latter don't control the
58 // lifetime of this object because we don't want a leak of an NPObject to
59 // keep the channel around longer than necessary.
60 void AddRoute(int route_id, IPC::Channel::Listener* listener,
61 NPObjectBase* npobject);
62 void RemoveRoute(int route_id);
65 void AddMappingForNPObjectProxy(int route_id, NPObject* object);
66 void RemoveMappingForNPObjectProxy(int route_id);
68 void AddMappingForNPObjectStub(int route_id, NPObject* object);
69 void RemoveMappingForNPObjectStub(int route_id, NPObject* object);
71 NPObject* GetExistingNPObjectProxy(int route_id);
72 int GetExistingRouteForNPObjectStub(NPObject* npobject);
75 // IPC::Message::Sender implementation:
76 virtual bool Send(IPC::Message* msg) OVERRIDE;
78 int peer_pid() { return peer_pid_; }
79 IPC::ChannelHandle channel_handle() const { return channel_handle_; }
81 // Returns the number of open NPObject channels in this process.
82 static int Count();
84 // Returns a new route id.
85 virtual int GenerateRouteID() = 0;
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_;
93 // Returns the most recent NPChannelBase to have received a message
94 // in this process.
95 static NPChannelBase* GetCurrentChannel();
97 static void CleanupChannels();
99 // Returns the NPObjectBase object for the route id passed in.
100 // Returns NULL on failure.
101 NPObjectBase* GetNPObjectListenerForRoute(int route_id);
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(
107 gfx::NativeViewId containing_window);
109 protected:
110 typedef NPChannelBase* (*ChannelFactory)();
112 friend class base::RefCountedThreadSafe<NPChannelBase>;
114 virtual ~NPChannelBase();
116 // Returns a NPChannelBase derived object for the given channel name.
117 // If an existing channel exists returns that object, otherwise creates a
118 // new one. Even though on creation the object is refcounted, each caller
119 // must still ref count the returned value. When there are no more routes
120 // on the channel and its ref count is 0, the object deletes itself.
121 static NPChannelBase* GetChannel(
122 const IPC::ChannelHandle& channel_handle, IPC::Channel::Mode mode,
123 ChannelFactory factory, base::MessageLoopProxy* ipc_message_loop,
124 bool create_pipe_now, base::WaitableEvent* shutdown_event);
126 // Sends a message to all instances.
127 static void Broadcast(IPC::Message* message);
129 // Called on the worker thread
130 NPChannelBase();
132 virtual void CleanUp() { }
134 // Implemented by derived classes to handle control messages
135 virtual bool OnControlMessageReceived(const IPC::Message& msg);
137 // IPC::Channel::Listener implementation:
138 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
139 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE;
140 virtual void OnChannelError() OVERRIDE;
142 void set_send_unblocking_only_during_unblock_dispatch() {
143 send_unblocking_only_during_unblock_dispatch_ = true;
146 virtual bool Init(base::MessageLoopProxy* ipc_message_loop,
147 bool create_pipe_now,
148 base::WaitableEvent* shutdown_event);
150 scoped_ptr<IPC::SyncChannel> channel_;
151 IPC::ChannelHandle channel_handle_;
153 private:
154 IPC::Channel::Mode mode_;
155 // This tracks the number of routes registered without an NPObject. It's used
156 // to manage the lifetime of this object. See comment for AddRoute() and
157 // RemoveRoute().
158 int non_npobject_count_;
159 int peer_pid_;
161 // true when in the middle of a RemoveRoute call
162 bool in_remove_route_;
164 // Keep track of all the registered NPObjects proxies/stubs so that when the
165 // channel is closed we can inform them.
166 typedef base::hash_map<int, NPObjectBase*> ListenerMap;
167 ListenerMap npobject_listeners_;
169 typedef base::hash_map<int, NPObject*> ProxyMap;
170 ProxyMap proxy_map_;
172 typedef base::hash_map<NPObject*, int> StubMap;
173 StubMap stub_map_;
175 // Used to implement message routing functionality to WebPlugin[Delegate]
176 // objects
177 MessageRouter router_;
179 // A channel is invalid if it is disconnected as a result of a channel
180 // error. This flag is used to indicate the same.
181 bool channel_valid_;
183 // Track whether we're dispatching a message with the unblock flag; works like
184 // a refcount, 0 when we're not.
185 int in_unblock_dispatch_;
187 // If true, sync messages will only be marked as unblocking if the channel is
188 // in the middle of dispatching an unblocking message. The non-renderer
189 // process wants to avoid setting the unblock flag on its sync messages
190 // unless necessary, since it can potentially introduce reentrancy into
191 // WebKit in ways that it doesn't expect (i.e. causing layout during paint).
192 // However to avoid deadlock, we must ensure that any message that's sent as
193 // a result of a sync call from the renderer must unblock the renderer. We
194 // additionally have to do this for async messages from the renderer that
195 // have the unblock flag set, since they could be followed by a sync message
196 // that won't get dispatched until the call to the renderer is complete.
197 bool send_unblocking_only_during_unblock_dispatch_;
199 DISALLOW_COPY_AND_ASSIGN(NPChannelBase);
202 #endif // CONTENT_COMMON_NP_CHANNEL_BASE_H_