OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #include "chrome/browser/geolocation/geolocation_dispatcher_host.h" | 5 #include "chrome/browser/geolocation/geolocation_dispatcher_host.h" |
6 | 6 |
7 #include <map> | |
8 #include <set> | |
9 #include <utility> | |
10 | |
7 #include "chrome/common/geoposition.h" | 11 #include "chrome/common/geoposition.h" |
8 #include "chrome/browser/geolocation/geolocation_permission_context.h" | 12 #include "chrome/browser/geolocation/geolocation_permission_context.h" |
13 #include "chrome/browser/geolocation/location_arbitrator.h" | |
9 #include "chrome/browser/renderer_host/render_process_host.h" | 14 #include "chrome/browser/renderer_host/render_process_host.h" |
10 #include "chrome/browser/renderer_host/render_view_host.h" | 15 #include "chrome/browser/renderer_host/render_view_host.h" |
11 #include "chrome/browser/renderer_host/render_view_host_notification_task.h" | 16 #include "chrome/browser/renderer_host/render_view_host_notification_task.h" |
12 #include "chrome/browser/renderer_host/resource_message_filter.h" | 17 #include "chrome/browser/renderer_host/resource_message_filter.h" |
13 #include "chrome/common/render_messages.h" | 18 #include "chrome/common/render_messages.h" |
14 #include "ipc/ipc_message.h" | 19 #include "ipc/ipc_message.h" |
15 | 20 |
16 GeolocationDispatcherHost::GeolocationDispatcherHost( | 21 namespace { |
22 class GeolocationDispatcherHostImpl : public GeolocationDispatcherHost, | |
23 public GeolocationArbitrator::Delegate { | |
24 public: | |
25 GeolocationDispatcherHostImpl( | |
26 int resource_message_filter_process_id, | |
27 GeolocationPermissionContext* geolocation_permission_context); | |
28 | |
29 // GeolocationDispatcherHost | |
30 // Called to possibly handle the incoming IPC message. Returns true if | |
31 // handled. Called in the browser process. | |
32 virtual bool OnMessageReceived(const IPC::Message& msg, bool* msg_was_ok); | |
33 | |
34 // GeolocationArbitrator::Delegate | |
35 virtual void OnLocationUpdate(const Geoposition& position); | |
36 | |
37 private: | |
38 friend class base::RefCountedThreadSafe<GeolocationDispatcherHostImpl>; | |
39 virtual ~GeolocationDispatcherHostImpl(); | |
40 | |
41 void OnRegisterDispatcher(int render_view_id); | |
42 void OnUnregisterDispatcher(int render_view_id); | |
43 void OnRequestPermission( | |
44 int render_view_id, int bridge_id, const GURL& requesting_frame); | |
45 void OnCancelPermissionRequest( | |
46 int render_view_id, int bridge_id, const GURL& requesting_frame); | |
47 void OnStartUpdating( | |
48 int render_view_id, int bridge_id, const GURL& requesting_frame, | |
49 bool enable_high_accuracy); | |
50 void OnStopUpdating(int render_view_id, int bridge_id); | |
51 void OnSuspend(int render_view_id, int bridge_id); | |
52 void OnResume(int render_view_id, int bridge_id); | |
53 | |
54 // Updates the |location_arbitrator_| with the currently required update | |
55 // options, based on |bridge_update_options_|. | |
56 void RefreshUpdateOptions(); | |
57 | |
58 int resource_message_filter_process_id_; | |
59 scoped_refptr<GeolocationPermissionContext> geolocation_permission_context_; | |
60 | |
61 // Iterated when sending location updates to renderer processes. The fan out | |
62 // to individual bridge IDs happens renderer side, in order to minimize | |
63 // context switches. | |
64 // Only used on the IO thread. | |
65 std::set<int> geolocation_renderer_ids_; | |
66 // Maps <renderer_id, bridge_id> to the location arbitrator update options | |
67 // that correspond to this particular bridge. | |
68 typedef std::map<std::pair<int, int>, GeolocationArbitrator::UpdateOptions> | |
69 BridgeUpdateOptionsMap; | |
70 BridgeUpdateOptionsMap bridge_update_options_; | |
71 // Only set whilst we are registered with the arbitrator. | |
72 scoped_refptr<GeolocationArbitrator> location_arbitrator_; | |
73 | |
74 DISALLOW_COPY_AND_ASSIGN(GeolocationDispatcherHostImpl); | |
75 }; | |
76 | |
77 GeolocationDispatcherHostImpl::GeolocationDispatcherHostImpl( | |
17 int resource_message_filter_process_id, | 78 int resource_message_filter_process_id, |
18 GeolocationPermissionContext* geolocation_permission_context) | 79 GeolocationPermissionContext* geolocation_permission_context) |
19 : resource_message_filter_process_id_(resource_message_filter_process_id), | 80 : resource_message_filter_process_id_(resource_message_filter_process_id), |
20 geolocation_permission_context_(geolocation_permission_context) { | 81 geolocation_permission_context_(geolocation_permission_context) { |
21 // This is initialized by ResourceMessageFilter. Do not add any non-trivial | 82 // This is initialized by ResourceMessageFilter. Do not add any non-trivial |
22 // initialization here, defer to OnRegisterBridge which is triggered whenever | 83 // initialization here, defer to OnRegisterBridge which is triggered whenever |
23 // a javascript geolocation object is actually initialized. | 84 // a javascript geolocation object is actually initialized. |
24 } | 85 } |
25 | 86 |
26 GeolocationDispatcherHost::~GeolocationDispatcherHost() { | 87 GeolocationDispatcherHostImpl::~GeolocationDispatcherHostImpl() { |
27 if (location_arbitrator_) | 88 if (location_arbitrator_) |
28 location_arbitrator_->RemoveObserver(this); | 89 location_arbitrator_->RemoveObserver(this); |
29 } | 90 } |
30 | 91 |
31 bool GeolocationDispatcherHost::OnMessageReceived( | 92 bool GeolocationDispatcherHostImpl::OnMessageReceived( |
32 const IPC::Message& msg, bool* msg_was_ok) { | 93 const IPC::Message& msg, bool* msg_was_ok) { |
94 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); | |
33 *msg_was_ok = true; | 95 *msg_was_ok = true; |
34 bool handled = true; | 96 bool handled = true; |
35 IPC_BEGIN_MESSAGE_MAP_EX(GeolocationDispatcherHost, msg, *msg_was_ok) | 97 IPC_BEGIN_MESSAGE_MAP_EX(GeolocationDispatcherHostImpl, msg, *msg_was_ok) |
36 IPC_MESSAGE_HANDLER(ViewHostMsg_Geolocation_RegisterDispatcher, | 98 IPC_MESSAGE_HANDLER(ViewHostMsg_Geolocation_RegisterDispatcher, |
37 OnRegisterDispatcher) | 99 OnRegisterDispatcher) |
38 IPC_MESSAGE_HANDLER(ViewHostMsg_Geolocation_UnregisterDispatcher, | 100 IPC_MESSAGE_HANDLER(ViewHostMsg_Geolocation_UnregisterDispatcher, |
39 OnUnregisterDispatcher) | 101 OnUnregisterDispatcher) |
40 IPC_MESSAGE_HANDLER(ViewHostMsg_Geolocation_CancelPermissionRequest, | 102 IPC_MESSAGE_HANDLER(ViewHostMsg_Geolocation_CancelPermissionRequest, |
41 OnCancelPermissionRequest) | 103 OnCancelPermissionRequest) |
42 IPC_MESSAGE_HANDLER(ViewHostMsg_Geolocation_RequestPermission, | 104 IPC_MESSAGE_HANDLER(ViewHostMsg_Geolocation_RequestPermission, |
43 OnRequestPermission) | 105 OnRequestPermission) |
44 IPC_MESSAGE_HANDLER(ViewHostMsg_Geolocation_StartUpdating, | 106 IPC_MESSAGE_HANDLER(ViewHostMsg_Geolocation_StartUpdating, |
45 OnStartUpdating) | 107 OnStartUpdating) |
46 IPC_MESSAGE_HANDLER(ViewHostMsg_Geolocation_StopUpdating, | 108 IPC_MESSAGE_HANDLER(ViewHostMsg_Geolocation_StopUpdating, |
47 OnStopUpdating) | 109 OnStopUpdating) |
48 IPC_MESSAGE_HANDLER(ViewHostMsg_Geolocation_Suspend, | 110 IPC_MESSAGE_HANDLER(ViewHostMsg_Geolocation_Suspend, |
49 OnSuspend) | 111 OnSuspend) |
50 IPC_MESSAGE_HANDLER(ViewHostMsg_Geolocation_Resume, | 112 IPC_MESSAGE_HANDLER(ViewHostMsg_Geolocation_Resume, |
51 OnResume) | 113 OnResume) |
52 IPC_MESSAGE_UNHANDLED(handled = false) | 114 IPC_MESSAGE_UNHANDLED(handled = false) |
53 IPC_END_MESSAGE_MAP() | 115 IPC_END_MESSAGE_MAP() |
54 return handled; | 116 return handled; |
55 } | 117 } |
56 | 118 |
57 void GeolocationDispatcherHost::OnLocationUpdate( | 119 void GeolocationDispatcherHostImpl::OnLocationUpdate( |
58 const Geoposition& geoposition) { | 120 const Geoposition& geoposition) { |
59 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); | 121 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
60 | 122 |
61 for (std::set<int>::iterator it = geolocation_renderer_ids_.begin(); | 123 for (std::set<int>::iterator it = geolocation_renderer_ids_.begin(); |
62 it != geolocation_renderer_ids_.end(); ++it) { | 124 it != geolocation_renderer_ids_.end(); ++it) { |
63 IPC::Message* message = | 125 IPC::Message* message = |
64 new ViewMsg_Geolocation_PositionUpdated(*it, geoposition); | 126 new ViewMsg_Geolocation_PositionUpdated(*it, geoposition); |
65 CallRenderViewHost(resource_message_filter_process_id_, *it, | 127 CallRenderViewHost(resource_message_filter_process_id_, *it, |
66 &RenderViewHost::Send, message); | 128 &RenderViewHost::Send, message); |
67 } | 129 } |
68 } | 130 } |
69 | 131 |
70 void GeolocationDispatcherHost::OnRegisterDispatcher(int render_view_id) { | 132 void GeolocationDispatcherHostImpl::OnRegisterDispatcher(int render_view_id) { |
71 RegisterDispatcher( | 133 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
72 resource_message_filter_process_id_, render_view_id); | 134 DCHECK_EQ(0u, geolocation_renderer_ids_.count(render_view_id)); |
135 geolocation_renderer_ids_.insert(render_view_id); | |
andreip3000
2010/06/08 14:59:53
Maybe it is obvious but I am still learning the co
| |
73 } | 136 } |
74 | 137 |
75 void GeolocationDispatcherHost::OnUnregisterDispatcher(int render_view_id) { | 138 void GeolocationDispatcherHostImpl::OnUnregisterDispatcher(int render_view_id) { |
76 UnregisterDispatcher( | 139 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
77 resource_message_filter_process_id_, render_view_id); | 140 |
141 DCHECK_EQ(1u, geolocation_renderer_ids_.count(render_view_id)); | |
142 geolocation_renderer_ids_.erase(render_view_id); | |
78 } | 143 } |
79 | 144 |
80 void GeolocationDispatcherHost::OnRequestPermission( | 145 void GeolocationDispatcherHostImpl::OnRequestPermission( |
81 int render_view_id, int bridge_id, const GURL& requesting_frame) { | 146 int render_view_id, int bridge_id, const GURL& requesting_frame) { |
147 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); | |
82 DLOG(INFO) << __FUNCTION__ << " " << resource_message_filter_process_id_ | 148 DLOG(INFO) << __FUNCTION__ << " " << resource_message_filter_process_id_ |
83 << ":" << render_view_id << ":" << bridge_id; | 149 << ":" << render_view_id << ":" << bridge_id; |
84 geolocation_permission_context_->RequestGeolocationPermission( | 150 geolocation_permission_context_->RequestGeolocationPermission( |
85 resource_message_filter_process_id_, render_view_id, bridge_id, | 151 resource_message_filter_process_id_, render_view_id, bridge_id, |
86 requesting_frame); | 152 requesting_frame); |
87 } | 153 } |
88 | 154 |
89 void GeolocationDispatcherHost::OnCancelPermissionRequest( | 155 void GeolocationDispatcherHostImpl::OnCancelPermissionRequest( |
90 int render_view_id, int bridge_id, const GURL& requesting_frame) { | 156 int render_view_id, int bridge_id, const GURL& requesting_frame) { |
157 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); | |
91 DLOG(INFO) << __FUNCTION__ << " " << resource_message_filter_process_id_ | 158 DLOG(INFO) << __FUNCTION__ << " " << resource_message_filter_process_id_ |
92 << ":" << render_view_id << ":" << bridge_id; | 159 << ":" << render_view_id << ":" << bridge_id; |
93 geolocation_permission_context_->CancelGeolocationPermissionRequest( | 160 geolocation_permission_context_->CancelGeolocationPermissionRequest( |
94 resource_message_filter_process_id_, render_view_id, bridge_id, | 161 resource_message_filter_process_id_, render_view_id, bridge_id, |
95 requesting_frame); | 162 requesting_frame); |
96 } | 163 } |
97 | 164 |
98 void GeolocationDispatcherHost::OnStartUpdating( | 165 void GeolocationDispatcherHostImpl::OnStartUpdating( |
99 int render_view_id, int bridge_id, const GURL& requesting_frame, | 166 int render_view_id, int bridge_id, const GURL& requesting_frame, |
100 bool enable_high_accuracy) { | 167 bool enable_high_accuracy) { |
101 // WebKit sends the startupdating request before checking permissions, to | 168 // WebKit sends the startupdating request before checking permissions, to |
102 // optimize the no-location-available case and reduce latency in the success | 169 // optimize the no-location-available case and reduce latency in the success |
103 // case (location lookup happens in parallel with the permission request). | 170 // case (location lookup happens in parallel with the permission request). |
171 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); | |
104 DLOG(INFO) << __FUNCTION__ << " " << resource_message_filter_process_id_ | 172 DLOG(INFO) << __FUNCTION__ << " " << resource_message_filter_process_id_ |
105 << ":" << render_view_id << ":" << bridge_id; | 173 << ":" << render_view_id << ":" << bridge_id; |
106 bridge_update_options_[std::make_pair(render_view_id, bridge_id)] = | 174 bridge_update_options_[std::make_pair(render_view_id, bridge_id)] = |
107 GeolocationArbitrator::UpdateOptions(enable_high_accuracy); | 175 GeolocationArbitrator::UpdateOptions(enable_high_accuracy); |
108 location_arbitrator_ = | 176 location_arbitrator_ = |
109 geolocation_permission_context_->StartUpdatingRequested( | 177 geolocation_permission_context_->StartUpdatingRequested( |
110 resource_message_filter_process_id_, render_view_id, bridge_id, | 178 resource_message_filter_process_id_, render_view_id, bridge_id, |
111 requesting_frame); | 179 requesting_frame); |
112 RefreshUpdateOptions(); | 180 RefreshUpdateOptions(); |
113 } | 181 } |
114 | 182 |
115 void GeolocationDispatcherHost::OnStopUpdating( | 183 void GeolocationDispatcherHostImpl::OnStopUpdating( |
116 int render_view_id, int bridge_id) { | 184 int render_view_id, int bridge_id) { |
185 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); | |
117 DLOG(INFO) << __FUNCTION__ << " " << resource_message_filter_process_id_ | 186 DLOG(INFO) << __FUNCTION__ << " " << resource_message_filter_process_id_ |
118 << ":" << render_view_id << ":" << bridge_id; | 187 << ":" << render_view_id << ":" << bridge_id; |
119 if (bridge_update_options_.erase(std::make_pair(render_view_id, bridge_id))) | 188 if (bridge_update_options_.erase(std::make_pair(render_view_id, bridge_id))) |
120 RefreshUpdateOptions(); | 189 RefreshUpdateOptions(); |
121 geolocation_permission_context_->StopUpdatingRequested( | 190 geolocation_permission_context_->StopUpdatingRequested( |
122 resource_message_filter_process_id_, render_view_id, bridge_id); | 191 resource_message_filter_process_id_, render_view_id, bridge_id); |
123 } | 192 } |
124 | 193 |
125 void GeolocationDispatcherHost::OnSuspend( | 194 void GeolocationDispatcherHostImpl::OnSuspend( |
126 int render_view_id, int bridge_id) { | 195 int render_view_id, int bridge_id) { |
196 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); | |
127 DLOG(INFO) << __FUNCTION__ << " " << resource_message_filter_process_id_ | 197 DLOG(INFO) << __FUNCTION__ << " " << resource_message_filter_process_id_ |
128 << ":" << render_view_id << ":" << bridge_id; | 198 << ":" << render_view_id << ":" << bridge_id; |
129 // TODO(bulach): connect this with GeolocationArbitrator. | 199 // TODO(bulach): connect this with GeolocationArbitrator. |
130 } | 200 } |
131 | 201 |
132 void GeolocationDispatcherHost::OnResume( | 202 void GeolocationDispatcherHostImpl::OnResume( |
133 int render_view_id, int bridge_id) { | 203 int render_view_id, int bridge_id) { |
204 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); | |
134 DLOG(INFO) << __FUNCTION__ << " " << resource_message_filter_process_id_ | 205 DLOG(INFO) << __FUNCTION__ << " " << resource_message_filter_process_id_ |
135 << ":" << render_view_id << ":" << bridge_id; | 206 << ":" << render_view_id << ":" << bridge_id; |
136 // TODO(bulach): connect this with GeolocationArbitrator. | 207 // TODO(bulach): connect this with GeolocationArbitrator. |
137 } | 208 } |
138 | 209 |
139 void GeolocationDispatcherHost::RegisterDispatcher( | 210 void GeolocationDispatcherHostImpl::RefreshUpdateOptions() { |
140 int process_id, int render_view_id) { | |
141 DCHECK_EQ(resource_message_filter_process_id_, process_id); | |
142 if (!ChromeThread::CurrentlyOn(ChromeThread::IO)) { | |
143 ChromeThread::PostTask( | |
144 ChromeThread::IO, FROM_HERE, | |
145 NewRunnableMethod( | |
146 this, &GeolocationDispatcherHost::RegisterDispatcher, | |
147 process_id, render_view_id)); | |
148 return; | |
149 } | |
150 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); | 211 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
151 | |
152 DCHECK_EQ(0u, geolocation_renderer_ids_.count(render_view_id)); | |
153 geolocation_renderer_ids_.insert(render_view_id); | |
154 } | |
155 | |
156 void GeolocationDispatcherHost::UnregisterDispatcher( | |
157 int process_id, int render_view_id) { | |
158 DCHECK_EQ(resource_message_filter_process_id_, process_id); | |
159 if (!ChromeThread::CurrentlyOn(ChromeThread::IO)) { | |
160 ChromeThread::PostTask( | |
161 ChromeThread::IO, FROM_HERE, | |
162 NewRunnableMethod( | |
163 this, &GeolocationDispatcherHost::UnregisterDispatcher, | |
164 process_id, render_view_id)); | |
165 return; | |
166 } | |
167 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); | |
168 | |
169 DCHECK_EQ(1u, geolocation_renderer_ids_.count(render_view_id)); | |
170 geolocation_renderer_ids_.erase(render_view_id); | |
171 } | |
172 | |
173 void GeolocationDispatcherHost::RefreshUpdateOptions() { | |
174 DCHECK(location_arbitrator_); | 212 DCHECK(location_arbitrator_); |
175 if (bridge_update_options_.empty()) { | 213 if (bridge_update_options_.empty()) { |
176 location_arbitrator_->RemoveObserver(this); | 214 location_arbitrator_->RemoveObserver(this); |
177 location_arbitrator_ = NULL; | 215 location_arbitrator_ = NULL; |
178 } else { | 216 } else { |
179 location_arbitrator_->AddObserver( | 217 location_arbitrator_->AddObserver( |
180 this, | 218 this, |
181 GeolocationArbitrator::UpdateOptions::Collapse( | 219 GeolocationArbitrator::UpdateOptions::Collapse( |
182 bridge_update_options_)); | 220 bridge_update_options_)); |
183 } | 221 } |
184 } | 222 } |
223 } // namespace | |
224 | |
225 GeolocationDispatcherHost* GeolocationDispatcherHost::New( | |
226 int resource_message_filter_process_id, | |
227 GeolocationPermissionContext* geolocation_permission_context) { | |
228 return new GeolocationDispatcherHostImpl(resource_message_filter_process_id, | |
229 geolocation_permission_context); | |
230 } | |
OLD | NEW |