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_
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"
21 class MessageLoopProxy
;
24 #if defined(COMPILER_GCC)
25 namespace BASE_HASH_NAMESPACE
{
28 struct hash
<NPObject
*> {
29 std::size_t operator()(NPObject
* const& ptr
) const {
30 return hash
<size_t>()(reinterpret_cast<size_t>(ptr
));
34 } // namespace __gnu_cxx
35 #elif defined(COMPILER_MSVC)
39 inline size_t hash_value(NPObject
* const& ptr
) {
40 return hash_value(reinterpret_cast<size_t>(ptr
));
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
,
52 public base::RefCountedThreadSafe
<NPChannelBase
> {
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
);
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::Sender implementation:
76 virtual bool Send(IPC::Message
* msg
) OVERRIDE
;
78 base::ProcessId
peer_pid() { return channel_
->peer_pid(); }
79 IPC::ChannelHandle
channel_handle() const { return channel_handle_
; }
81 // Returns the number of open NPObject channels in this process.
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
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(int render_view_id
);
109 typedef NPChannelBase
* (*ChannelFactory
)();
111 friend class base::RefCountedThreadSafe
<NPChannelBase
>;
113 virtual ~NPChannelBase();
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
);
125 // Sends a message to all instances.
126 static void Broadcast(IPC::Message
* message
);
128 // Called on the worker thread
131 virtual void CleanUp() { }
133 // Implemented by derived classes to handle control messages
134 virtual bool OnControlMessageReceived(const IPC::Message
& msg
);
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
;
141 void set_send_unblocking_only_during_unblock_dispatch() {
142 send_unblocking_only_during_unblock_dispatch_
= true;
145 virtual bool Init(base::MessageLoopProxy
* ipc_message_loop
,
146 bool create_pipe_now
,
147 base::WaitableEvent
* shutdown_event
);
149 scoped_ptr
<IPC::SyncChannel
> channel_
;
150 IPC::ChannelHandle channel_handle_
;
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
157 int non_npobject_count_
;
160 // true when in the middle of a RemoveRoute call
161 bool in_remove_route_
;
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_
;
168 typedef base::hash_map
<int, NPObject
*> ProxyMap
;
171 typedef base::hash_map
<NPObject
*, int> StubMap
;
174 // Used to implement message routing functionality to WebPlugin[Delegate]
176 MessageRouter router_
;
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.
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_
;
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_
;
198 DISALLOW_COPY_AND_ASSIGN(NPChannelBase
);
201 } // namespace content
203 #endif // CONTENT_COMMON_NP_CHANNEL_BASE_H_