OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_CHILD_NPAPI_NP_CHANNEL_BASE_H_ | 5 #ifndef CONTENT_CHILD_NPAPI_NP_CHANNEL_BASE_H_ |
6 #define CONTENT_CHILD_NPAPI_NP_CHANNEL_BASE_H_ | 6 #define CONTENT_CHILD_NPAPI_NP_CHANNEL_BASE_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/containers/hash_tables.h" | 12 #include "base/containers/hash_tables.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
16 #include "base/process/process.h" | 16 #include "base/process/process.h" |
17 #include "content/child/npapi/npobject_base.h" | |
18 #include "content/common/message_router.h" | 17 #include "content/common/message_router.h" |
19 #include "ipc/ipc_channel_handle.h" | 18 #include "ipc/ipc_channel_handle.h" |
20 #include "ipc/ipc_sync_channel.h" | 19 #include "ipc/ipc_sync_channel.h" |
21 | 20 |
22 namespace base { | 21 namespace base { |
23 class SingleThreadTaskRunner; | 22 class SingleThreadTaskRunner; |
24 } | 23 } |
25 | 24 |
26 namespace content { | 25 namespace content { |
27 | 26 |
28 // Encapsulates an IPC channel between a renderer and another process. Used to | 27 // Encapsulates an IPC channel between a renderer and another process. Used to |
29 // proxy access to NP objects. | 28 // proxy access to NP objects. |
30 class NPChannelBase : public IPC::Listener, | 29 class NPChannelBase : public IPC::Listener, |
31 public IPC::Sender, | 30 public IPC::Sender, |
32 public base::RefCountedThreadSafe<NPChannelBase> { | 31 public base::RefCountedThreadSafe<NPChannelBase> { |
33 public: | 32 public: |
34 | |
35 // WebPlugin[Delegate] call these on construction and destruction to setup | 33 // WebPlugin[Delegate] call these on construction and destruction to setup |
36 // the routing and manage lifetime of this object (they pass NULL for | 34 // the routing and manage lifetime of this object. |
37 // npobject). These are also called by NPObjectProxy and NPObjectStub (which | 35 void AddRoute(int route_id, IPC::Listener* listener); |
38 // pass themselves for npobject). However the latter don't control the | |
39 // lifetime of this object because we don't want a leak of an NPObject to | |
40 // keep the channel around longer than necessary. | |
41 void AddRoute(int route_id, IPC::Listener* listener, NPObjectBase* npobject); | |
42 void RemoveRoute(int route_id); | 36 void RemoveRoute(int route_id); |
43 | 37 |
44 void AddMappingForNPObjectProxy(int route_id, NPObject* object); | |
45 void RemoveMappingForNPObjectProxy(int route_id); | |
46 | |
47 void AddMappingForNPObjectStub(int route_id, NPObject* object); | |
48 void RemoveMappingForNPObjectStub(int route_id, NPObject* object); | |
49 | |
50 void AddMappingForNPObjectOwner(int route_id, struct _NPP* owner); | |
51 void SetDefaultNPObjectOwner(struct _NPP* owner); | |
52 void RemoveMappingForNPObjectOwner(int route_id); | |
53 | |
54 NPObject* GetExistingNPObjectProxy(int route_id); | |
55 int GetExistingRouteForNPObjectStub(NPObject* npobject); | |
56 struct _NPP* GetExistingNPObjectOwner(int route_id); | |
57 int GetExistingRouteForNPObjectOwner(struct _NPP* owner); | |
58 | |
59 // IPC::Sender implementation: | 38 // IPC::Sender implementation: |
60 bool Send(IPC::Message* msg) override; | 39 bool Send(IPC::Message* msg) override; |
61 | 40 |
62 base::ProcessId peer_pid() { return channel_->GetPeerPID(); } | 41 base::ProcessId peer_pid() { return channel_->GetPeerPID(); } |
63 IPC::ChannelHandle channel_handle() const { return channel_handle_; } | 42 IPC::ChannelHandle channel_handle() const { return channel_handle_; } |
64 | 43 |
65 // Returns the number of open NPObject channels in this process. | 44 // Returns the number of open NPObject channels in this process. |
66 static int Count(); | 45 static int Count(); |
67 | 46 |
68 // Returns a new route id. | 47 // Returns a new route id. |
69 virtual int GenerateRouteID() = 0; | 48 virtual int GenerateRouteID() = 0; |
70 | 49 |
71 // Returns whether the channel is valid or not. A channel is invalid | 50 // Returns whether the channel is valid or not. A channel is invalid |
72 // if it is disconnected due to a channel error. | 51 // if it is disconnected due to a channel error. |
73 bool channel_valid() { | 52 bool channel_valid() { |
74 return channel_valid_; | 53 return channel_valid_; |
75 } | 54 } |
76 | 55 |
77 // Returns the most recent NPChannelBase to have received a message | 56 // Returns the most recent NPChannelBase to have received a message |
78 // in this process. | 57 // in this process. |
79 static NPChannelBase* GetCurrentChannel(); | 58 static NPChannelBase* GetCurrentChannel(); |
80 | 59 |
81 static void CleanupChannels(); | 60 static void CleanupChannels(); |
82 | 61 |
83 // Returns the NPObjectBase object for the route id passed in. | |
84 // Returns NULL on failure. | |
85 NPObjectBase* GetNPObjectListenerForRoute(int route_id); | |
86 | |
87 // Returns the event that's set when a call to the renderer causes a modal | 62 // Returns the event that's set when a call to the renderer causes a modal |
88 // dialog to come up. The default implementation returns NULL. Derived | 63 // dialog to come up. The default implementation returns NULL. Derived |
89 // classes should override this method if this functionality is required. | 64 // classes should override this method if this functionality is required. |
90 virtual base::WaitableEvent* GetModalDialogEvent(int render_view_id); | 65 virtual base::WaitableEvent* GetModalDialogEvent(int render_view_id); |
91 | 66 |
92 protected: | 67 protected: |
93 typedef NPChannelBase* (*ChannelFactory)(); | 68 typedef NPChannelBase* (*ChannelFactory)(); |
94 | 69 |
95 friend class base::RefCountedThreadSafe<NPChannelBase>; | 70 friend class base::RefCountedThreadSafe<NPChannelBase>; |
96 | 71 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 IPC::Channel::Mode mode_; | 115 IPC::Channel::Mode mode_; |
141 // This tracks the number of routes registered without an NPObject. It's used | 116 // This tracks the number of routes registered without an NPObject. It's used |
142 // to manage the lifetime of this object. See comment for AddRoute() and | 117 // to manage the lifetime of this object. See comment for AddRoute() and |
143 // RemoveRoute(). | 118 // RemoveRoute(). |
144 int non_npobject_count_; | 119 int non_npobject_count_; |
145 int peer_pid_; | 120 int peer_pid_; |
146 | 121 |
147 // true when in the middle of a RemoveRoute call | 122 // true when in the middle of a RemoveRoute call |
148 bool in_remove_route_; | 123 bool in_remove_route_; |
149 | 124 |
150 // Keep track of all the registered NPObjects proxies/stubs so that when the | |
151 // channel is closed we can inform them. | |
152 typedef base::hash_map<int, NPObjectBase*> ListenerMap; | |
153 ListenerMap npobject_listeners_; | |
154 | |
155 typedef base::hash_map<int, NPObject*> ProxyMap; | |
156 ProxyMap proxy_map_; | |
157 | |
158 typedef base::hash_map<NPObject*, int> StubMap; | |
159 StubMap stub_map_; | |
160 | |
161 typedef base::hash_map<struct _NPP*, int> OwnerToRouteMap; | |
162 OwnerToRouteMap owner_to_route_; | |
163 | |
164 typedef base::hash_map<int, struct _NPP*> RouteToOwnerMap; | |
165 RouteToOwnerMap route_to_owner_; | |
166 | |
167 // Used on the plugin side to represent any object received that does | |
168 // not belong to a plugin instance. | |
169 struct _NPP* default_owner_; | |
170 | |
171 // Used to implement message routing functionality to WebPlugin[Delegate] | 125 // Used to implement message routing functionality to WebPlugin[Delegate] |
172 // objects | 126 // objects |
173 MessageRouter router_; | 127 MessageRouter router_; |
174 | 128 |
175 // A channel is invalid if it is disconnected as a result of a channel | 129 // A channel is invalid if it is disconnected as a result of a channel |
176 // error. This flag is used to indicate the same. | 130 // error. This flag is used to indicate the same. |
177 bool channel_valid_; | 131 bool channel_valid_; |
178 | 132 |
179 // Track whether we're dispatching a message with the unblock flag; works like | 133 // Track whether we're dispatching a message with the unblock flag; works like |
180 // a refcount, 0 when we're not. | 134 // a refcount, 0 when we're not. |
(...skipping 10 matching lines...) Expand all Loading... |
191 // have the unblock flag set, since they could be followed by a sync message | 145 // have the unblock flag set, since they could be followed by a sync message |
192 // that won't get dispatched until the call to the renderer is complete. | 146 // that won't get dispatched until the call to the renderer is complete. |
193 bool send_unblocking_only_during_unblock_dispatch_; | 147 bool send_unblocking_only_during_unblock_dispatch_; |
194 | 148 |
195 DISALLOW_COPY_AND_ASSIGN(NPChannelBase); | 149 DISALLOW_COPY_AND_ASSIGN(NPChannelBase); |
196 }; | 150 }; |
197 | 151 |
198 } // namespace content | 152 } // namespace content |
199 | 153 |
200 #endif // CONTENT_CHILD_NPAPI_NP_CHANNEL_BASE_H_ | 154 #endif // CONTENT_CHILD_NPAPI_NP_CHANNEL_BASE_H_ |
OLD | NEW |