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