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