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
3// found in the LICENSE file.
4
5#include "chrome/browser/geolocation/geolocation_permission_context_factory.h"
6
7#include "chrome/browser/profiles/incognito_helpers.h"
8#include "chrome/browser/profiles/profile.h"
9#include "chrome/common/pref_names.h"
10#include "components/keyed_service/content/browser_context_dependency_manager.h"
11#include "components/pref_registry/pref_registry_syncable.h"
12#if defined(OS_ANDROID)
13#include "chrome/browser/geolocation/geolocation_permission_context_android.h"
14#else
15#include "chrome/browser/geolocation/geolocation_permission_context.h"
16#endif
17
18namespace {
19
20class Service : public KeyedService {
21 public:
22  explicit Service(Profile* profile) {
23#if defined(OS_ANDROID)
24    context_ = new GeolocationPermissionContextAndroid(profile);
25#else
26    context_ = new GeolocationPermissionContext(profile);
27#endif
28  }
29
30  GeolocationPermissionContext* context() {
31    return context_.get();
32  }
33
34  virtual void Shutdown() OVERRIDE {
35    context()->ShutdownOnUIThread();
36  }
37
38 private:
39  scoped_refptr<GeolocationPermissionContext> context_;
40
41  DISALLOW_COPY_AND_ASSIGN(Service);
42};
43
44}  // namespace
45
46// static
47GeolocationPermissionContext*
48GeolocationPermissionContextFactory::GetForProfile(Profile* profile) {
49  return static_cast<Service*>(
50      GetInstance()->GetServiceForBrowserContext(profile, true))->context();
51}
52
53// static
54GeolocationPermissionContextFactory*
55GeolocationPermissionContextFactory::GetInstance() {
56  return Singleton<GeolocationPermissionContextFactory>::get();
57}
58
59GeolocationPermissionContextFactory::GeolocationPermissionContextFactory()
60    : BrowserContextKeyedServiceFactory(
61          "GeolocationPermissionContext",
62          BrowserContextDependencyManager::GetInstance()) {
63}
64
65GeolocationPermissionContextFactory::~GeolocationPermissionContextFactory() {
66}
67
68KeyedService*
69GeolocationPermissionContextFactory::BuildServiceInstanceFor(
70    content::BrowserContext* profile) const {
71  return new Service(static_cast<Profile*>(profile));
72}
73
74void GeolocationPermissionContextFactory::RegisterProfilePrefs(
75    user_prefs::PrefRegistrySyncable* registry) {
76#if defined(OS_ANDROID)
77  registry->RegisterBooleanPref(
78      prefs::kGeolocationEnabled,
79      true,
80      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
81#endif
82}
83
84content::BrowserContext*
85GeolocationPermissionContextFactory::GetBrowserContextToUse(
86    content::BrowserContext* context) const {
87  return chrome::GetBrowserContextOwnInstanceInIncognito(context);
88}
89