Add a new SequencedSocketData class.
[chromium-blink-merge.git] / content / child / npapi / np_channel_base.h
blobd718936425fece594816ab5f54414afe710bb83f
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 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, IPC::Channel::Mode mode,
104 ChannelFactory factory, base::MessageLoopProxy* ipc_message_loop,
105 bool create_pipe_now, base::WaitableEvent* shutdown_event);
107 // Sends a message to all instances.
108 static void Broadcast(IPC::Message* message);
110 // Called on the worker thread
111 NPChannelBase();
113 virtual void CleanUp() { }
115 // Implemented by derived classes to handle control messages
116 virtual bool OnControlMessageReceived(const IPC::Message& msg);
118 // IPC::Listener implementation:
119 bool OnMessageReceived(const IPC::Message& msg) override;
120 void OnChannelConnected(int32 peer_pid) override;
121 void OnChannelError() override;
123 void set_send_unblocking_only_during_unblock_dispatch() {
124 send_unblocking_only_during_unblock_dispatch_ = true;
127 virtual bool Init(base::MessageLoopProxy* ipc_message_loop,
128 bool create_pipe_now,
129 base::WaitableEvent* shutdown_event);
131 scoped_ptr<IPC::SyncChannel> channel_;
132 IPC::ChannelHandle channel_handle_;
134 private:
135 IPC::Channel::Mode mode_;
136 // This tracks the number of routes registered without an NPObject. It's used
137 // to manage the lifetime of this object. See comment for AddRoute() and
138 // RemoveRoute().
139 int non_npobject_count_;
140 int peer_pid_;
142 // true when in the middle of a RemoveRoute call
143 bool in_remove_route_;
145 // Keep track of all the registered NPObjects proxies/stubs so that when the
146 // channel is closed we can inform them.
147 typedef base::hash_map<int, NPObjectBase*> ListenerMap;
148 ListenerMap npobject_listeners_;
150 typedef base::hash_map<int, NPObject*> ProxyMap;
151 ProxyMap proxy_map_;
153 typedef base::hash_map<NPObject*, int> StubMap;
154 StubMap stub_map_;
156 typedef base::hash_map<struct _NPP*, int> OwnerToRouteMap;
157 OwnerToRouteMap owner_to_route_;
159 typedef base::hash_map<int, struct _NPP*> RouteToOwnerMap;
160 RouteToOwnerMap route_to_owner_;
162 // Used on the plugin side to represent any object received that does
163 // not belong to a plugin instance.
164 struct _NPP* default_owner_;
166 // Used to implement message routing functionality to WebPlugin[Delegate]
167 // objects
168 MessageRouter router_;
170 // A channel is invalid if it is disconnected as a result of a channel
171 // error. This flag is used to indicate the same.
172 bool channel_valid_;
174 // Track whether we're dispatching a message with the unblock flag; works like
175 // a refcount, 0 when we're not.
176 int in_unblock_dispatch_;
178 // If true, sync messages will only be marked as unblocking if the channel is
179 // in the middle of dispatching an unblocking message. The non-renderer
180 // process wants to avoid setting the unblock flag on its sync messages
181 // unless necessary, since it can potentially introduce reentrancy into
182 // WebKit in ways that it doesn't expect (i.e. causing layout during paint).
183 // However to avoid deadlock, we must ensure that any message that's sent as
184 // a result of a sync call from the renderer must unblock the renderer. We
185 // additionally have to do this for async messages from the renderer that
186 // have the unblock flag set, since they could be followed by a sync message
187 // that won't get dispatched until the call to the renderer is complete.
188 bool send_unblocking_only_during_unblock_dispatch_;
190 DISALLOW_COPY_AND_ASSIGN(NPChannelBase);
193 } // namespace content
195 #endif // CONTENT_CHILD_NPAPI_NP_CHANNEL_BASE_H_