OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/browser/geolocation/geolocation_dispatcher_host.h" | 5 #include "content/browser/geolocation/geolocation_dispatcher_host.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
11 #include "content/browser/frame_host/render_frame_host_impl.h" | |
11 #include "content/browser/renderer_host/render_message_filter.h" | 12 #include "content/browser/renderer_host/render_message_filter.h" |
12 #include "content/browser/renderer_host/render_process_host_impl.h" | 13 #include "content/browser/renderer_host/render_process_host_impl.h" |
13 #include "content/browser/renderer_host/render_view_host_impl.h" | 14 #include "content/browser/renderer_host/render_view_host_impl.h" |
14 #include "content/browser/web_contents/web_contents_impl.h" | 15 #include "content/browser/web_contents/web_contents_impl.h" |
15 #include "content/public/browser/browser_context.h" | 16 #include "content/public/browser/browser_context.h" |
16 #include "content/public/browser/geolocation_permission_context.h" | 17 #include "content/public/browser/geolocation_permission_context.h" |
17 #include "content/public/common/geoposition.h" | 18 #include "content/public/common/geoposition.h" |
18 #include "content/common/geolocation_messages.h" | 19 #include "content/common/geolocation_messages.h" |
19 | 20 |
20 namespace content { | 21 namespace content { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
57 case Geoposition::ERROR_CODE_TIMEOUT: | 58 case Geoposition::ERROR_CODE_TIMEOUT: |
58 code = GEOPOSITION_ERROR_CODE_TIMEOUT; | 59 code = GEOPOSITION_ERROR_CODE_TIMEOUT; |
59 break; | 60 break; |
60 } | 61 } |
61 UMA_HISTOGRAM_ENUMERATION("Geolocation.LocationUpdate.ErrorCode", | 62 UMA_HISTOGRAM_ENUMERATION("Geolocation.LocationUpdate.ErrorCode", |
62 code, | 63 code, |
63 GEOPOSITION_ERROR_CODE_COUNT); | 64 GEOPOSITION_ERROR_CODE_COUNT); |
64 } | 65 } |
65 | 66 |
66 void SendGeolocationPermissionResponse(int render_process_id, | 67 void SendGeolocationPermissionResponse(int render_process_id, |
67 int render_view_id, | 68 int render_frame_id, |
68 int bridge_id, | 69 int bridge_id, |
69 bool allowed) { | 70 bool allowed) { |
70 RenderViewHostImpl* render_view_host = | 71 RenderFrameHost* render_frame_host = |
71 RenderViewHostImpl::FromID(render_process_id, render_view_id); | 72 RenderFrameHost::FromID(render_process_id, render_frame_id); |
72 if (!render_view_host) | 73 if (!render_frame_host) |
73 return; | 74 return; |
74 render_view_host->Send( | 75 render_frame_host->Send( |
75 new GeolocationMsg_PermissionSet(render_view_id, bridge_id, allowed)); | 76 new GeolocationMsg_PermissionSet(render_frame_id, bridge_id, allowed)); |
76 | 77 |
77 if (allowed) | 78 if (allowed) |
78 GeolocationProviderImpl::GetInstance()->UserDidOptIntoLocationServices(); | 79 GeolocationProviderImpl::GetInstance()->UserDidOptIntoLocationServices(); |
79 } | 80 } |
80 | 81 |
81 } // namespace | 82 } // namespace |
82 | 83 |
83 GeolocationDispatcherHost::GeolocationDispatcherHost( | 84 GeolocationDispatcherHost::GeolocationDispatcherHost( |
84 WebContents* web_contents) | 85 WebContents* web_contents) |
85 : WebContentsObserver(web_contents), | 86 : WebContentsObserver(web_contents), |
86 watching_requested_(false), | 87 paused_(false) { |
87 paused_(false), | |
88 high_accuracy_(false) { | |
89 // This is initialized by WebContentsImpl. Do not add any non-trivial | 88 // This is initialized by WebContentsImpl. Do not add any non-trivial |
90 // initialization here, defer to OnStartUpdating which is triggered whenever | 89 // initialization here, defer to OnStartUpdating which is triggered whenever |
91 // a javascript geolocation object is actually initialized. | 90 // a javascript geolocation object is actually initialized. |
92 } | 91 } |
93 | 92 |
94 GeolocationDispatcherHost::~GeolocationDispatcherHost() { | 93 GeolocationDispatcherHost::~GeolocationDispatcherHost() { |
95 } | 94 } |
96 | 95 |
96 void GeolocationDispatcherHost::RenderFrameDeleted( | |
97 RenderFrameHost* render_frame_host) { | |
98 OnStopUpdating(render_frame_host); | |
99 } | |
100 | |
97 void GeolocationDispatcherHost::RenderViewHostChanged( | 101 void GeolocationDispatcherHost::RenderViewHostChanged( |
98 RenderViewHost* old_host, | 102 RenderViewHost* old_host, |
99 RenderViewHost* new_host) { | 103 RenderViewHost* new_host) { |
100 watching_requested_ = false; | 104 updating_frames_.clear(); |
101 paused_ = false; | 105 paused_ = false; |
102 geolocation_subscription_.reset(); | 106 geolocation_subscription_.reset(); |
103 } | 107 } |
104 | 108 |
105 bool GeolocationDispatcherHost::OnMessageReceived(const IPC::Message& msg) { | 109 bool GeolocationDispatcherHost::OnMessageReceived( |
110 const IPC::Message& msg, RenderFrameHost* render_frame_host) { | |
106 bool handled = true; | 111 bool handled = true; |
107 IPC_BEGIN_MESSAGE_MAP(GeolocationDispatcherHost, msg) | 112 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(GeolocationDispatcherHost, msg, |
113 render_frame_host) | |
114 IPC_MESSAGE_HANDLER(GeolocationHostMsg_RequestPermission, | |
115 OnRequestPermission) | |
108 IPC_MESSAGE_HANDLER(GeolocationHostMsg_CancelPermissionRequest, | 116 IPC_MESSAGE_HANDLER(GeolocationHostMsg_CancelPermissionRequest, |
109 OnCancelPermissionRequest) | 117 OnCancelPermissionRequest) |
110 IPC_MESSAGE_HANDLER(GeolocationHostMsg_RequestPermission, | |
111 OnRequestPermission) | |
112 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StartUpdating, OnStartUpdating) | 118 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StartUpdating, OnStartUpdating) |
113 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StopUpdating, OnStopUpdating) | 119 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StopUpdating, OnStopUpdating) |
114 IPC_MESSAGE_UNHANDLED(handled = false) | 120 IPC_MESSAGE_UNHANDLED(handled = false) |
115 IPC_END_MESSAGE_MAP() | 121 IPC_END_MESSAGE_MAP() |
116 return handled; | 122 return handled; |
117 } | 123 } |
118 | 124 |
119 void GeolocationDispatcherHost::OnLocationUpdate( | 125 void GeolocationDispatcherHost::OnLocationUpdate( |
120 const Geoposition& geoposition) { | 126 const Geoposition& geoposition) { |
121 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 127 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
122 | 128 |
123 RecordGeopositionErrorCode(geoposition.error_code); | 129 RecordGeopositionErrorCode(geoposition.error_code); |
124 if (!paused_) | 130 if (paused_) |
125 Send(new GeolocationMsg_PositionUpdated(routing_id(), geoposition)); | 131 return; |
132 | |
133 for (std::map<RenderFrameHost*, bool>::iterator i = updating_frames_.begin(); | |
134 i != updating_frames_.end(); ++i) { | |
135 i->first->Send(new GeolocationMsg_PositionUpdated( | |
136 i->first->GetRoutingID(), geoposition)); | |
137 } | |
126 } | 138 } |
127 | 139 |
128 void GeolocationDispatcherHost::OnRequestPermission( | 140 void GeolocationDispatcherHost::OnRequestPermission( |
141 RenderFrameHost* render_frame_host, | |
129 int bridge_id, | 142 int bridge_id, |
130 const GURL& requesting_frame, | 143 const GURL& requesting_frame, |
131 bool user_gesture) { | 144 bool user_gesture) { |
132 GeolocationPermissionContext* context = | 145 GeolocationPermissionContext* context = |
133 web_contents()->GetBrowserContext()->GetGeolocationPermissionContext(); | 146 web_contents()->GetBrowserContext()->GetGeolocationPermissionContext(); |
134 int render_process_id = web_contents()->GetRenderProcessHost()->GetID(); | 147 int render_process_id = render_frame_host->GetProcess()->GetID(); |
135 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); | 148 int render_frame_id = render_frame_host->GetRoutingID(); |
136 if (context) { | 149 if (context) { |
137 context->RequestGeolocationPermission( | 150 context->RequestGeolocationPermission( |
138 web_contents(), | 151 web_contents(), |
139 bridge_id, | 152 bridge_id, |
140 requesting_frame, | 153 requesting_frame, |
141 user_gesture, | 154 user_gesture, |
142 base::Bind(&SendGeolocationPermissionResponse, | 155 base::Bind(&SendGeolocationPermissionResponse, |
143 render_process_id, | 156 render_process_id, |
144 render_view_id, | 157 render_frame_id, |
145 bridge_id)); | 158 bridge_id)); |
146 } else { | 159 } else { |
147 SendGeolocationPermissionResponse( | 160 SendGeolocationPermissionResponse( |
148 render_process_id, render_view_id, bridge_id, true); | 161 render_process_id, render_frame_id, bridge_id, true); |
149 } | 162 } |
150 } | 163 } |
151 | 164 |
152 void GeolocationDispatcherHost::OnCancelPermissionRequest( | 165 void GeolocationDispatcherHost::OnCancelPermissionRequest( |
166 RenderFrameHost* render_frame_host, | |
153 int bridge_id, | 167 int bridge_id, |
154 const GURL& requesting_frame) { | 168 const GURL& requesting_frame) { |
155 GeolocationPermissionContext* context = | 169 GeolocationPermissionContext* context = |
156 web_contents()->GetBrowserContext()->GetGeolocationPermissionContext(); | 170 web_contents()->GetBrowserContext()->GetGeolocationPermissionContext(); |
157 if (context) { | 171 if (context) { |
158 context->CancelGeolocationPermissionRequest( | 172 context->CancelGeolocationPermissionRequest( |
159 web_contents(), bridge_id, requesting_frame); | 173 web_contents(), bridge_id, requesting_frame); |
160 } | 174 } |
161 } | 175 } |
162 | 176 |
163 void GeolocationDispatcherHost::OnStartUpdating( | 177 void GeolocationDispatcherHost::OnStartUpdating( |
178 RenderFrameHost* render_frame_host, | |
164 const GURL& requesting_frame, | 179 const GURL& requesting_frame, |
165 bool enable_high_accuracy) { | 180 bool enable_high_accuracy) { |
166 // StartUpdating() can be invoked as a result of high-accuracy mode | 181 // StartUpdating() can be invoked as a result of high-accuracy mode |
167 // being enabled / disabled. No need to record the dispatcher again. | 182 // being enabled / disabled. No need to record the dispatcher again. |
168 UMA_HISTOGRAM_BOOLEAN( | 183 UMA_HISTOGRAM_BOOLEAN( |
169 "Geolocation.GeolocationDispatcherHostImpl.EnableHighAccuracy", | 184 "Geolocation.GeolocationDispatcherHostImpl.EnableHighAccuracy", |
170 enable_high_accuracy); | 185 enable_high_accuracy); |
171 | 186 |
172 watching_requested_ = true; | 187 updating_frames_[render_frame_host] = enable_high_accuracy; |
173 high_accuracy_ = enable_high_accuracy; | |
174 RefreshGeolocationOptions(); | 188 RefreshGeolocationOptions(); |
175 } | 189 } |
176 | 190 |
177 void GeolocationDispatcherHost::OnStopUpdating() { | 191 void GeolocationDispatcherHost::OnStopUpdating( |
178 watching_requested_ = false; | 192 RenderFrameHost* render_frame_host) { |
193 updating_frames_.erase(render_frame_host); | |
179 RefreshGeolocationOptions(); | 194 RefreshGeolocationOptions(); |
180 } | 195 } |
181 | 196 |
182 void GeolocationDispatcherHost::PauseOrResume(bool should_pause) { | 197 void GeolocationDispatcherHost::PauseOrResume(bool should_pause) { |
183 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 198 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
184 paused_ = should_pause; | 199 paused_ = should_pause; |
185 RefreshGeolocationOptions(); | 200 RefreshGeolocationOptions(); |
186 } | 201 } |
187 | 202 |
188 void GeolocationDispatcherHost::RefreshGeolocationOptions() { | 203 void GeolocationDispatcherHost::RefreshGeolocationOptions() { |
189 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 204 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
190 | 205 |
191 if (watching_requested_ && !paused_) { | 206 if (!updating_frames_.empty() && !paused_) { |
bulach
2014/05/30 15:03:12
nit: while at it, it may be better to invert with
jam
2014/05/30 16:36:54
Done.
| |
207 bool high_accuracy = false; | |
208 for (std::map<RenderFrameHost*, bool>::iterator i = | |
209 updating_frames_.begin(); i != updating_frames_.end(); ++i) { | |
210 if (i->second) { | |
211 high_accuracy = true; | |
212 break; | |
213 } | |
214 } | |
192 geolocation_subscription_ = GeolocationProvider::GetInstance()-> | 215 geolocation_subscription_ = GeolocationProvider::GetInstance()-> |
193 AddLocationUpdateCallback( | 216 AddLocationUpdateCallback( |
194 base::Bind(&GeolocationDispatcherHost::OnLocationUpdate, | 217 base::Bind(&GeolocationDispatcherHost::OnLocationUpdate, |
195 base::Unretained(this)), | 218 base::Unretained(this)), |
196 high_accuracy_); | 219 high_accuracy); |
197 } else { | 220 } else { |
198 geolocation_subscription_.reset(); | 221 geolocation_subscription_.reset(); |
199 } | 222 } |
200 } | 223 } |
201 | 224 |
202 } // namespace content | 225 } // namespace content |
OLD | NEW |