| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/geolocation/geolocation_dispatcher_host.h" | |
| 6 | |
| 7 #include <map> | |
| 8 #include <set> | |
| 9 #include <utility> | |
| 10 | |
| 11 #include "chrome/common/geoposition.h" | |
| 12 #include "chrome/common/render_messages.h" | |
| 13 #include "content/browser/geolocation/geolocation_permission_context.h" | |
| 14 #include "content/browser/geolocation/geolocation_provider.h" | |
| 15 #include "content/browser/renderer_host/render_message_filter.h" | |
| 16 #include "content/browser/renderer_host/render_process_host.h" | |
| 17 #include "content/browser/renderer_host/render_view_host.h" | |
| 18 | |
| 19 namespace { | |
| 20 class GeolocationDispatcherHostImpl : public GeolocationDispatcherHost, | |
| 21 public GeolocationObserver { | |
| 22 public: | |
| 23 GeolocationDispatcherHostImpl( | |
| 24 int render_process_id, | |
| 25 GeolocationPermissionContext* geolocation_permission_context); | |
| 26 | |
| 27 // GeolocationDispatcherHost | |
| 28 virtual bool OnMessageReceived(const IPC::Message& msg, bool* msg_was_ok); | |
| 29 | |
| 30 // GeolocationObserver | |
| 31 virtual void OnLocationUpdate(const Geoposition& position); | |
| 32 | |
| 33 private: | |
| 34 virtual ~GeolocationDispatcherHostImpl(); | |
| 35 | |
| 36 void OnRequestPermission( | |
| 37 int render_view_id, int bridge_id, const GURL& requesting_frame); | |
| 38 void OnCancelPermissionRequest( | |
| 39 int render_view_id, int bridge_id, const GURL& requesting_frame); | |
| 40 void OnStartUpdating( | |
| 41 int render_view_id, const GURL& requesting_frame, | |
| 42 bool enable_high_accuracy); | |
| 43 void OnStopUpdating(int render_view_id); | |
| 44 | |
| 45 // Updates the |location_arbitrator_| with the currently required update | |
| 46 // options, based on |renderer_update_options_|. | |
| 47 void RefreshGeolocationObserverOptions(); | |
| 48 | |
| 49 int render_process_id_; | |
| 50 scoped_refptr<GeolocationPermissionContext> geolocation_permission_context_; | |
| 51 | |
| 52 // Iterated when sending location updates to renderer processes. The fan out | |
| 53 // to individual bridge IDs happens renderer side, in order to minimize | |
| 54 // context switches. | |
| 55 // Only used on the IO thread. | |
| 56 std::set<int> geolocation_renderer_ids_; | |
| 57 // Maps renderer_id to the location arbitrator update options that correspond | |
| 58 // to this particular bridge. | |
| 59 std::map<int, GeolocationObserverOptions> renderer_update_options_; | |
| 60 // Only set whilst we are registered with the arbitrator. | |
| 61 GeolocationProvider* location_provider_; | |
| 62 | |
| 63 DISALLOW_COPY_AND_ASSIGN(GeolocationDispatcherHostImpl); | |
| 64 }; | |
| 65 | |
| 66 GeolocationDispatcherHostImpl::GeolocationDispatcherHostImpl( | |
| 67 int render_process_id, | |
| 68 GeolocationPermissionContext* geolocation_permission_context) | |
| 69 : render_process_id_(render_process_id), | |
| 70 geolocation_permission_context_(geolocation_permission_context), | |
| 71 location_provider_(NULL) { | |
| 72 // This is initialized by ResourceMessageFilter. Do not add any non-trivial | |
| 73 // initialization here, defer to OnRegisterBridge which is triggered whenever | |
| 74 // a javascript geolocation object is actually initialized. | |
| 75 } | |
| 76 | |
| 77 GeolocationDispatcherHostImpl::~GeolocationDispatcherHostImpl() { | |
| 78 if (location_provider_) | |
| 79 location_provider_->RemoveObserver(this); | |
| 80 } | |
| 81 | |
| 82 bool GeolocationDispatcherHostImpl::OnMessageReceived( | |
| 83 const IPC::Message& msg, bool* msg_was_ok) { | |
| 84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 85 *msg_was_ok = true; | |
| 86 bool handled = true; | |
| 87 IPC_BEGIN_MESSAGE_MAP_EX(GeolocationDispatcherHostImpl, msg, *msg_was_ok) | |
| 88 IPC_MESSAGE_HANDLER(ViewHostMsg_Geolocation_CancelPermissionRequest, | |
| 89 OnCancelPermissionRequest) | |
| 90 IPC_MESSAGE_HANDLER(ViewHostMsg_Geolocation_RequestPermission, | |
| 91 OnRequestPermission) | |
| 92 IPC_MESSAGE_HANDLER(ViewHostMsg_Geolocation_StartUpdating, | |
| 93 OnStartUpdating) | |
| 94 IPC_MESSAGE_HANDLER(ViewHostMsg_Geolocation_StopUpdating, | |
| 95 OnStopUpdating) | |
| 96 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 97 IPC_END_MESSAGE_MAP() | |
| 98 return handled; | |
| 99 } | |
| 100 | |
| 101 void GeolocationDispatcherHostImpl::OnLocationUpdate( | |
| 102 const Geoposition& geoposition) { | |
| 103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 104 for (std::set<int>::iterator it = geolocation_renderer_ids_.begin(); | |
| 105 it != geolocation_renderer_ids_.end(); ++it) { | |
| 106 Send(new ViewMsg_Geolocation_PositionUpdated(*it, geoposition)); | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 void GeolocationDispatcherHostImpl::OnRequestPermission( | |
| 111 int render_view_id, | |
| 112 int bridge_id, | |
| 113 const GURL& requesting_frame) { | |
| 114 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 115 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" | |
| 116 << render_view_id << ":" << bridge_id; | |
| 117 geolocation_permission_context_->RequestGeolocationPermission( | |
| 118 render_process_id_, render_view_id, bridge_id, | |
| 119 requesting_frame); | |
| 120 } | |
| 121 | |
| 122 void GeolocationDispatcherHostImpl::OnCancelPermissionRequest( | |
| 123 int render_view_id, | |
| 124 int bridge_id, | |
| 125 const GURL& requesting_frame) { | |
| 126 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 127 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" | |
| 128 << render_view_id << ":" << bridge_id; | |
| 129 geolocation_permission_context_->CancelGeolocationPermissionRequest( | |
| 130 render_process_id_, render_view_id, bridge_id, | |
| 131 requesting_frame); | |
| 132 } | |
| 133 | |
| 134 void GeolocationDispatcherHostImpl::OnStartUpdating( | |
| 135 int render_view_id, | |
| 136 const GURL& requesting_frame, | |
| 137 bool enable_high_accuracy) { | |
| 138 // StartUpdating() can be invoked as a result of high-accuracy mode | |
| 139 // being enabled / disabled. No need to record the dispatcher again. | |
| 140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 141 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" | |
| 142 << render_view_id; | |
| 143 if (!geolocation_renderer_ids_.count(render_view_id)) | |
| 144 geolocation_renderer_ids_.insert(render_view_id); | |
| 145 | |
| 146 renderer_update_options_[render_view_id] = | |
| 147 GeolocationObserverOptions(enable_high_accuracy); | |
| 148 RefreshGeolocationObserverOptions(); | |
| 149 } | |
| 150 | |
| 151 void GeolocationDispatcherHostImpl::OnStopUpdating(int render_view_id) { | |
| 152 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 153 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" | |
| 154 << render_view_id; | |
| 155 if (renderer_update_options_.erase(render_view_id)) | |
| 156 RefreshGeolocationObserverOptions(); | |
| 157 | |
| 158 DCHECK_EQ(1U, geolocation_renderer_ids_.count(render_view_id)); | |
| 159 geolocation_renderer_ids_.erase(render_view_id); | |
| 160 } | |
| 161 | |
| 162 void GeolocationDispatcherHostImpl::RefreshGeolocationObserverOptions() { | |
| 163 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 164 if (renderer_update_options_.empty()) { | |
| 165 if (location_provider_) { | |
| 166 location_provider_->RemoveObserver(this); | |
| 167 location_provider_ = NULL; | |
| 168 } | |
| 169 } else { | |
| 170 if (!location_provider_) | |
| 171 location_provider_ = GeolocationProvider::GetInstance(); | |
| 172 // Re-add to re-establish our options, in case they changed. | |
| 173 location_provider_->AddObserver( | |
| 174 this, | |
| 175 GeolocationObserverOptions::Collapse(renderer_update_options_)); | |
| 176 } | |
| 177 } | |
| 178 } // namespace | |
| 179 | |
| 180 GeolocationDispatcherHost* GeolocationDispatcherHost::New( | |
| 181 int render_process_id, | |
| 182 GeolocationPermissionContext* geolocation_permission_context) { | |
| 183 return new GeolocationDispatcherHostImpl( | |
| 184 render_process_id, | |
| 185 geolocation_permission_context); | |
| 186 } | |
| OLD | NEW |