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