| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/extensions/api/location/location_manager.h" | 5 #include "chrome/browser/extensions/api/location/location_manager.h" |
| 6 | 6 |
| 7 #include <math.h> | 7 #include <math.h> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "chrome/browser/chrome_notification_types.h" | |
| 14 #include "chrome/browser/profiles/profile.h" | |
| 15 #include "chrome/common/extensions/api/location.h" | 13 #include "chrome/common/extensions/api/location.h" |
| 16 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 17 #include "content/public/browser/geolocation_provider.h" | 15 #include "content/public/browser/geolocation_provider.h" |
| 18 #include "content/public/browser/notification_details.h" | |
| 19 #include "content/public/browser/notification_source.h" | |
| 20 #include "content/public/common/geoposition.h" | 16 #include "content/public/common/geoposition.h" |
| 21 #include "extensions/browser/event_router.h" | 17 #include "extensions/browser/event_router.h" |
| 18 #include "extensions/browser/extension_registry.h" |
| 22 #include "extensions/browser/extension_system.h" | 19 #include "extensions/browser/extension_system.h" |
| 23 #include "extensions/common/extension.h" | 20 #include "extensions/common/extension.h" |
| 24 #include "extensions/common/permissions/permission_set.h" | 21 #include "extensions/common/permissions/permission_set.h" |
| 25 #include "extensions/common/permissions/permissions_data.h" | 22 #include "extensions/common/permissions/permissions_data.h" |
| 26 | 23 |
| 27 using content::BrowserThread; | 24 using content::BrowserThread; |
| 28 | 25 |
| 29 // TODO(vadimt): Add tests. | 26 // TODO(vadimt): Add tests. |
| 30 namespace extensions { | 27 namespace extensions { |
| 31 | 28 |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 | 238 |
| 242 void LocationRequest::OnPositionReported(const content::Geoposition& position) { | 239 void LocationRequest::OnPositionReported(const content::Geoposition& position) { |
| 243 for (UpdatePolicyVector::iterator it = update_policies_.begin(); | 240 for (UpdatePolicyVector::iterator it = update_policies_.begin(); |
| 244 it != update_policies_.end(); | 241 it != update_policies_.end(); |
| 245 ++it) { | 242 ++it) { |
| 246 (*it)->OnPositionReported(position); | 243 (*it)->OnPositionReported(position); |
| 247 } | 244 } |
| 248 } | 245 } |
| 249 | 246 |
| 250 LocationManager::LocationManager(content::BrowserContext* context) | 247 LocationManager::LocationManager(content::BrowserContext* context) |
| 251 : profile_(Profile::FromBrowserContext(context)) { | 248 : browser_context_(context), extension_registry_observer_(this) { |
| 252 registrar_.Add(this, | 249 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); |
| 253 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, | |
| 254 content::Source<Profile>(profile_)); | |
| 255 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | |
| 256 content::Source<Profile>(profile_)); | |
| 257 } | 250 } |
| 258 | 251 |
| 259 void LocationManager::AddLocationRequest( | 252 void LocationManager::AddLocationRequest( |
| 260 const std::string& extension_id, | 253 const std::string& extension_id, |
| 261 const std::string& request_name, | 254 const std::string& request_name, |
| 262 const double* distance_update_threshold_meters, | 255 const double* distance_update_threshold_meters, |
| 263 const double* time_between_updates_ms) { | 256 const double* time_between_updates_ms) { |
| 264 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 257 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 265 // TODO(vadimt): Consider resuming requests after restarting the browser. | 258 // TODO(vadimt): Consider resuming requests after restarting the browser. |
| 266 | 259 |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 event_name = location::OnLocationUpdate::kEventName; | 329 event_name = location::OnLocationUpdate::kEventName; |
| 337 } else { | 330 } else { |
| 338 // Set data for onLocationError event. | 331 // Set data for onLocationError event. |
| 339 // TODO(vadimt): Set name. | 332 // TODO(vadimt): Set name. |
| 340 args->AppendString(position.error_message); | 333 args->AppendString(position.error_message); |
| 341 event_name = location::OnLocationError::kEventName; | 334 event_name = location::OnLocationError::kEventName; |
| 342 } | 335 } |
| 343 | 336 |
| 344 scoped_ptr<Event> event(new Event(event_name, args.Pass())); | 337 scoped_ptr<Event> event(new Event(event_name, args.Pass())); |
| 345 | 338 |
| 346 EventRouter::Get(profile_) | 339 EventRouter::Get(browser_context_) |
| 347 ->DispatchEventToExtension(extension_id, event.Pass()); | 340 ->DispatchEventToExtension(extension_id, event.Pass()); |
| 348 } | 341 } |
| 349 | 342 |
| 350 void LocationManager::Observe(int type, | 343 void LocationManager::OnExtensionLoaded( |
| 351 const content::NotificationSource& source, | 344 content::BrowserContext* browser_context, |
| 352 const content::NotificationDetails& details) { | 345 const Extension* extension) { |
| 353 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 346 // Grants permission to use geolocation once an extension with "location" |
| 347 // permission is loaded. |
| 348 if (extension->permissions_data()->HasAPIPermission( |
| 349 APIPermission::kLocation)) { |
| 350 content::GeolocationProvider::GetInstance() |
| 351 ->UserDidOptIntoLocationServices(); |
| 352 } |
| 353 } |
| 354 | 354 |
| 355 switch (type) { | 355 void LocationManager::OnExtensionUnloaded( |
| 356 case chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED: { | 356 content::BrowserContext* browser_context, |
| 357 // Grants permission to use geolocation once an extension with "location" | 357 const Extension* extension, |
| 358 // permission is loaded. | 358 UnloadedExtensionInfo::Reason reason) { |
| 359 const Extension* extension = | 359 // Delete all requests from the unloaded extension. |
| 360 content::Details<const Extension>(details).ptr(); | 360 location_requests_.erase(extension->id()); |
| 361 | |
| 362 if (extension->permissions_data()->HasAPIPermission( | |
| 363 APIPermission::kLocation)) { | |
| 364 content::GeolocationProvider::GetInstance()-> | |
| 365 UserDidOptIntoLocationServices(); | |
| 366 } | |
| 367 break; | |
| 368 } | |
| 369 case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: { | |
| 370 // Delete all requests from the unloaded extension. | |
| 371 const Extension* extension = | |
| 372 content::Details<const UnloadedExtensionInfo>(details)->extension; | |
| 373 location_requests_.erase(extension->id()); | |
| 374 break; | |
| 375 } | |
| 376 default: | |
| 377 NOTREACHED(); | |
| 378 break; | |
| 379 } | |
| 380 } | 361 } |
| 381 | 362 |
| 382 static base::LazyInstance<BrowserContextKeyedAPIFactory<LocationManager> > | 363 static base::LazyInstance<BrowserContextKeyedAPIFactory<LocationManager> > |
| 383 g_factory = LAZY_INSTANCE_INITIALIZER; | 364 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 384 | 365 |
| 385 // static | 366 // static |
| 386 BrowserContextKeyedAPIFactory<LocationManager>* | 367 BrowserContextKeyedAPIFactory<LocationManager>* |
| 387 LocationManager::GetFactoryInstance() { | 368 LocationManager::GetFactoryInstance() { |
| 388 return g_factory.Pointer(); | 369 return g_factory.Pointer(); |
| 389 } | 370 } |
| 390 | 371 |
| 391 // static | 372 // static |
| 392 LocationManager* LocationManager::Get(content::BrowserContext* context) { | 373 LocationManager* LocationManager::Get(content::BrowserContext* context) { |
| 393 return BrowserContextKeyedAPIFactory<LocationManager>::Get(context); | 374 return BrowserContextKeyedAPIFactory<LocationManager>::Get(context); |
| 394 } | 375 } |
| 395 | 376 |
| 396 } // namespace extensions | 377 } // namespace extensions |
| OLD | NEW |