Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / content / child / npapi / np_channel_base.h
blob3a8c9e36dac57b543e87fa302306ceef9f5edcd0
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 SingleThreadTaskRunner;
24 namespace content {
26 // Encapsulates an IPC channel between a renderer and another process. Used to
27 // proxy access to NP objects.
28 class NPChannelBase : public IPC::Listener,
29 public IPC::Sender,
30 public base::RefCountedThreadSafe<NPChannelBase> {
31 public:
33 // WebPlugin[Delegate] call these on construction and destruction to setup
34 // the routing and manage lifetime of this object (they pass NULL for
35 // npobject). These are also called by NPObjectProxy and NPObjectStub (which
36 // pass themselves for npobject). However the latter don't control the
37 // lifetime of this object because we don't want a leak of an NPObject to
38 // keep the channel around longer than necessary.
39 void AddRoute(int route_id, IPC::Listener* listener, NPObjectBase* npobject);
40 void RemoveRoute(int route_id);
42 void AddMappingForNPObjectProxy(int route_id, NPObject* object);
43 void RemoveMappingForNPObjectProxy(int route_id);
45 void AddMappingForNPObjectStub(int route_id, NPObject* object);
46 void RemoveMappingForNPObjectStub(int route_id, NPObject* object);
48 void AddMappingForNPObjectOwner(int route_id, struct _NPP* owner);
49 void SetDefaultNPObjectOwner(struct _NPP* owner);
50 void RemoveMappingForNPObjectOwner(int route_id);
52 NPObject* GetExistingNPObjectProxy(int route_id);
53 int GetExistingRouteForNPObjectStub(NPObject* npobject);
54 struct _NPP* GetExistingNPObjectOwner(int route_id);
55 int GetExistingRouteForNPObjectOwner(struct _NPP* owner);
57 // IPC::Sender implementation:
58 bool Send(IPC::Message* msg) override;
60 base::ProcessId peer_pid() { return channel_->GetPeerPID(); }
61 IPC::ChannelHandle channel_handle() const { return channel_handle_; }
63 // Returns the number of open NPObject channels in this process.
64 static int Count();
66 // Returns a new route id.
67 virtual int GenerateRouteID() = 0;
69 // Returns whether the channel is valid or not. A channel is invalid
70 // if it is disconnected due to a channel error.
71 bool channel_valid() {
72 return channel_valid_;
75 // Returns the most recent NPChannelBase to have received a message
76 // in this process.
77 static NPChannelBase* GetCurrentChannel();
79 static void CleanupChannels();
81 // Returns the NPObjectBase object for the route id passed in.
82 // Returns NULL on failure.
83 NPObjectBase* GetNPObjectListenerForRoute(int route_id);
85 // Returns the event that's set when a call to the renderer causes a modal
86 // dialog to come up. The default implementation returns NULL. Derived
87 // classes should override this method if this functionality is required.
88 virtual base::WaitableEvent* GetModalDialogEvent(int render_view_id);
90 protected:
91 typedef NPChannelBase* (*ChannelFactory)();
93 friend class base::RefCountedThreadSafe<NPChannelBase>;
95 ~NPChannelBase() override;
97 // Returns a NPChannelBase derived object for the given channel name.
98 // If an existing channel exists returns that object, otherwise creates a
99 // new one. Even though on creation the object is refcounted, each caller
100 // must still ref count the returned value. When there are no more routes
101 // on the channel and its ref count is 0, the object deletes itself.
102 static NPChannelBase* GetChannel(
103 const IPC::ChannelHandle& channel_handle,
104 IPC::Channel::Mode mode,
105 ChannelFactory factory,
106 base::SingleThreadTaskRunner* ipc_task_runner,
107 bool create_pipe_now,
108 base::WaitableEvent* shutdown_event);
110 // Sends a message to all instances.
111 static void Broadcast(IPC::Message* message);
113 // Called on the worker thread
114 NPChannelBase();
116 virtual void CleanUp() { }
118 // Implemented by derived classes to handle control messages
119 virtual bool OnControlMessageReceived(const IPC::Message& msg);
121 // IPC::Listener implementation:
122 bool OnMessageReceived(const IPC::Message& msg) override;
123 void OnChannelConnected(int32 peer_pid) override;
124 void OnChannelError() override;
126 void set_send_unblocking_only_during_unblock_dispatch() {
127 send_unblocking_only_during_unblock_dispatch_ = true;
130 virtual bool Init(base::SingleThreadTaskRunner* ipc_task_runner,
131 bool create_pipe_now,
132 base::WaitableEvent* shutdown_event);
134 scoped_ptr<IPC::SyncChannel> channel_;
135 IPC::ChannelHandle channel_handle_;
137 private:
138 IPC::Channel::Mode mode_;
139 // This tracks the number of routes registered without an NPObject. It's used
140 // to manage the lifetime of this object. See comment for AddRoute() and
141 // RemoveRoute().
142 int non_npobject_count_;
143 int peer_pid_;
145 // true when in the middle of a RemoveRoute call
146 bool in_remove_route_;
148 // Keep track of all the registered NPObjects proxies/stubs so that when the
149 // channel is closed we can inform them.
150 typedef base::hash_map<int, NPObjectBase*> ListenerMap;
151 ListenerMap npobject_listeners_;
153 typedef base::hash_map<int, NPObject*> ProxyMap;
154 ProxyMap proxy_map_;
156 typedef base::hash_map<NPObject*, int> StubMap;
157 StubMap stub_map_;
159 typedef base::hash_map<struct _NPP*, int> OwnerToRouteMap;
160 OwnerToRouteMap owner_to_route_;
162 typedef base::hash_map<int, struct _NPP*> RouteToOwnerMap;
163 RouteToOwnerMap route_to_owner_;
165 // Used on the plugin side to represent any object received that does
166 // not belong to a plugin instance.
167 struct _NPP* default_owner_;
169 // Used to implement message routing functionality to WebPlugin[Delegate]
170 // objects
171 MessageRouter router_;
173 // A channel is invalid if it is disconnected as a result of a channel
174 // error. This flag is used to indicate the same.
175 bool channel_valid_;
177 // Track whether we're dispatching a message with the unblock flag; works like
178 // a refcount, 0 when we're not.
179 int in_unblock_dispatch_;
181 // If true, sync messages will only be marked as unblocking if the channel is
182 // in the middle of dispatching an unblocking message. The non-renderer
183 // process wants to avoid setting the unblock flag on its sync messages
184 // unless necessary, since it can potentially introduce reentrancy into
185 // WebKit in ways that it doesn't expect (i.e. causing layout during paint).
186 // However to avoid deadlock, we must ensure that any message that's sent as
187 // a result of a sync call from the renderer must unblock the renderer. We
188 // additionally have to do this for async messages from the renderer that
189 // have the unblock flag set, since they could be followed by a sync message
190 // that won't get dispatched until the call to the renderer is complete.
191 bool send_unblocking_only_during_unblock_dispatch_;
193 DISALLOW_COPY_AND_ASSIGN(NPChannelBase);
196 } // namespace content
198 #endif // CONTENT_CHILD_NPAPI_NP_CHANNEL_BASE_H_