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_
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"
21 class SingleThreadTaskRunner
;
25 class AttachmentBroker
;
30 // Encapsulates an IPC channel between a renderer and another process. Used to
31 // proxy access to NP objects.
32 class NPChannelBase
: public IPC::Listener
,
34 public base::RefCountedThreadSafe
<NPChannelBase
> {
37 // WebPlugin[Delegate] call these on construction and destruction to setup
38 // the routing and manage lifetime of this object (they pass NULL for
39 // npobject). These are also called by NPObjectProxy and NPObjectStub (which
40 // pass themselves for npobject). However the latter don't control the
41 // lifetime of this object because we don't want a leak of an NPObject to
42 // keep the channel around longer than necessary.
43 void AddRoute(int route_id
, IPC::Listener
* listener
, NPObjectBase
* npobject
);
44 void RemoveRoute(int route_id
);
46 void AddMappingForNPObjectProxy(int route_id
, NPObject
* object
);
47 void RemoveMappingForNPObjectProxy(int route_id
);
49 void AddMappingForNPObjectStub(int route_id
, NPObject
* object
);
50 void RemoveMappingForNPObjectStub(int route_id
, NPObject
* object
);
52 void AddMappingForNPObjectOwner(int route_id
, struct _NPP
* owner
);
53 void SetDefaultNPObjectOwner(struct _NPP
* owner
);
54 void RemoveMappingForNPObjectOwner(int route_id
);
56 NPObject
* GetExistingNPObjectProxy(int route_id
);
57 int GetExistingRouteForNPObjectStub(NPObject
* npobject
);
58 struct _NPP
* GetExistingNPObjectOwner(int route_id
);
59 int GetExistingRouteForNPObjectOwner(struct _NPP
* owner
);
61 // IPC::Sender implementation:
62 bool Send(IPC::Message
* msg
) override
;
64 base::ProcessId
peer_pid() { return channel_
->GetPeerPID(); }
65 IPC::ChannelHandle
channel_handle() const { return channel_handle_
; }
67 // Returns the number of open NPObject channels in this process.
70 // Returns a new route id.
71 virtual int GenerateRouteID() = 0;
73 // Returns whether the channel is valid or not. A channel is invalid
74 // if it is disconnected due to a channel error.
75 bool channel_valid() {
76 return channel_valid_
;
79 // Returns the most recent NPChannelBase to have received a message
81 static NPChannelBase
* GetCurrentChannel();
83 static void CleanupChannels();
85 // Returns the NPObjectBase object for the route id passed in.
86 // Returns NULL on failure.
87 NPObjectBase
* GetNPObjectListenerForRoute(int route_id
);
89 // Returns the event that's set when a call to the renderer causes a modal
90 // dialog to come up. The default implementation returns NULL. Derived
91 // classes should override this method if this functionality is required.
92 virtual base::WaitableEvent
* GetModalDialogEvent(int render_view_id
);
95 typedef NPChannelBase
* (*ChannelFactory
)();
97 friend class base::RefCountedThreadSafe
<NPChannelBase
>;
99 ~NPChannelBase() override
;
101 // Returns a NPChannelBase derived object for the given channel name.
102 // If an existing channel exists returns that object, otherwise creates a
103 // new one. Even though on creation the object is refcounted, each caller
104 // must still ref count the returned value. When there are no more routes
105 // on the channel and its ref count is 0, the object deletes itself.
106 static NPChannelBase
* GetChannel(
107 const IPC::ChannelHandle
& channel_handle
,
108 IPC::Channel::Mode mode
,
109 ChannelFactory factory
,
110 base::SingleThreadTaskRunner
* ipc_task_runner
,
111 bool create_pipe_now
,
112 base::WaitableEvent
* shutdown_event
,
113 IPC::AttachmentBroker
* broker
);
115 // Sends a message to all instances.
116 static void Broadcast(IPC::Message
* message
);
118 // Called on the worker thread
121 virtual void CleanUp() { }
123 // Implemented by derived classes to handle control messages
124 virtual bool OnControlMessageReceived(const IPC::Message
& msg
);
126 // IPC::Listener implementation:
127 bool OnMessageReceived(const IPC::Message
& msg
) override
;
128 void OnChannelConnected(int32 peer_pid
) override
;
129 void OnChannelError() override
;
131 void set_send_unblocking_only_during_unblock_dispatch() {
132 send_unblocking_only_during_unblock_dispatch_
= true;
135 virtual bool Init(base::SingleThreadTaskRunner
* ipc_task_runner
,
136 bool create_pipe_now
,
137 base::WaitableEvent
* shutdown_event
,
138 IPC::AttachmentBroker
* broker
);
140 scoped_ptr
<IPC::SyncChannel
> channel_
;
141 IPC::ChannelHandle channel_handle_
;
144 IPC::Channel::Mode mode_
;
145 // This tracks the number of routes registered without an NPObject. It's used
146 // to manage the lifetime of this object. See comment for AddRoute() and
148 int non_npobject_count_
;
151 // true when in the middle of a RemoveRoute call
152 bool in_remove_route_
;
154 // Keep track of all the registered NPObjects proxies/stubs so that when the
155 // channel is closed we can inform them.
156 typedef base::hash_map
<int, NPObjectBase
*> ListenerMap
;
157 ListenerMap npobject_listeners_
;
159 typedef base::hash_map
<int, NPObject
*> ProxyMap
;
162 typedef base::hash_map
<NPObject
*, int> StubMap
;
165 typedef base::hash_map
<struct _NPP
*, int> OwnerToRouteMap
;
166 OwnerToRouteMap owner_to_route_
;
168 typedef base::hash_map
<int, struct _NPP
*> RouteToOwnerMap
;
169 RouteToOwnerMap route_to_owner_
;
171 // Used on the plugin side to represent any object received that does
172 // not belong to a plugin instance.
173 struct _NPP
* default_owner_
;
175 // Used to implement message routing functionality to WebPlugin[Delegate]
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.
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 } // namespace content
204 #endif // CONTENT_CHILD_NPAPI_NP_CHANNEL_BASE_H_