schema_registry_service_factory.cc revision 6e8cce623b6e4fe0c9e4af605d675dd9d0338c38
1// Copyright 2013 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/policy/schema_registry_service_factory.h"
6
7#include "base/logging.h"
8#include "chrome/browser/policy/schema_registry_service.h"
9#include "components/keyed_service/content/browser_context_dependency_manager.h"
10#include "components/policy/core/common/schema.h"
11#include "components/policy/core/common/schema_registry.h"
12#include "content/public/browser/browser_context.h"
13
14#if defined(OS_CHROMEOS)
15#include "chrome/browser/browser_process.h"
16#include "chrome/browser/browser_process_platform_part_chromeos.h"
17#include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
18#include "chrome/browser/chromeos/policy/device_local_account_policy_service.h"
19#include "chrome/browser/chromeos/profiles/profile_helper.h"
20#include "chrome/browser/profiles/profile.h"
21#include "components/user_manager/user.h"
22#include "components/user_manager/user_manager.h"
23#endif
24
25namespace policy {
26
27#if defined(OS_CHROMEOS)
28namespace {
29
30DeviceLocalAccountPolicyBroker* GetBroker(content::BrowserContext* context) {
31  Profile* profile = Profile::FromBrowserContext(context);
32
33  if (chromeos::ProfileHelper::IsSigninProfile(profile))
34    return NULL;
35
36  if (!user_manager::UserManager::IsInitialized()) {
37    // Bail out in unit tests that don't have a UserManager.
38    return NULL;
39  }
40
41  user_manager::User* user =
42      chromeos::ProfileHelper::Get()->GetUserByProfile(profile);
43  if (!user)
44    return NULL;
45
46  BrowserPolicyConnectorChromeOS* connector =
47      g_browser_process->platform_part()->browser_policy_connector_chromeos();
48  DeviceLocalAccountPolicyService* service =
49      connector->GetDeviceLocalAccountPolicyService();
50  if (!service)
51    return NULL;
52
53  return service->GetBrokerForUser(user->email());
54}
55
56}  // namespace
57#endif  // OS_CHROMEOS
58
59// static
60SchemaRegistryServiceFactory* SchemaRegistryServiceFactory::GetInstance() {
61  return Singleton<SchemaRegistryServiceFactory>::get();
62}
63
64// static
65SchemaRegistryService* SchemaRegistryServiceFactory::GetForContext(
66    content::BrowserContext* context) {
67  return GetInstance()->GetForContextInternal(context);
68}
69
70// static
71scoped_ptr<SchemaRegistryService>
72SchemaRegistryServiceFactory::CreateForContext(
73    content::BrowserContext* context,
74    const Schema& chrome_schema,
75    CombinedSchemaRegistry* global_registry) {
76  return GetInstance()->CreateForContextInternal(
77      context, chrome_schema, global_registry);
78}
79
80SchemaRegistryServiceFactory::SchemaRegistryServiceFactory()
81    : BrowserContextKeyedBaseFactory(
82          "SchemaRegistryService",
83          BrowserContextDependencyManager::GetInstance()) {}
84
85SchemaRegistryServiceFactory::~SchemaRegistryServiceFactory() {}
86
87SchemaRegistryService* SchemaRegistryServiceFactory::GetForContextInternal(
88    content::BrowserContext* context) {
89  // Off-the-record Profiles get their policy from the main Profile's
90  // PolicyService, and don't need their own SchemaRegistry nor any policy
91  // providers.
92  if (context->IsOffTheRecord())
93    return NULL;
94  RegistryMap::const_iterator it = registries_.find(context);
95  CHECK(it != registries_.end());
96  return it->second;
97}
98
99scoped_ptr<SchemaRegistryService>
100SchemaRegistryServiceFactory::CreateForContextInternal(
101    content::BrowserContext* context,
102    const Schema& chrome_schema,
103    CombinedSchemaRegistry* global_registry) {
104  DCHECK(!context->IsOffTheRecord());
105  DCHECK(registries_.find(context) == registries_.end());
106
107  scoped_ptr<SchemaRegistry> registry;
108
109#if defined(OS_CHROMEOS)
110  DeviceLocalAccountPolicyBroker* broker = GetBroker(context);
111  if (broker) {
112    // The SchemaRegistry for a device-local account is owned by its
113    // DeviceLocalAccountPolicyBroker, which uses the registry to fetch and
114    // cache policy even if there is no active session for that account.
115    // Use a ForwardingSchemaRegistry that wraps this SchemaRegistry.
116    registry.reset(new ForwardingSchemaRegistry(broker->schema_registry()));
117  }
118#endif
119
120  if (!registry)
121    registry.reset(new SchemaRegistry);
122
123  scoped_ptr<SchemaRegistryService> service(new SchemaRegistryService(
124      registry.Pass(), chrome_schema, global_registry));
125  registries_[context] = service.get();
126  return service.Pass();
127}
128
129void SchemaRegistryServiceFactory::BrowserContextShutdown(
130    content::BrowserContext* context) {
131  if (context->IsOffTheRecord())
132    return;
133  RegistryMap::iterator it = registries_.find(context);
134  if (it != registries_.end())
135    it->second->Shutdown();
136  else
137    NOTREACHED();
138}
139
140void SchemaRegistryServiceFactory::BrowserContextDestroyed(
141    content::BrowserContext* context) {
142  registries_.erase(context);
143  BrowserContextKeyedBaseFactory::BrowserContextDestroyed(context);
144}
145
146void SchemaRegistryServiceFactory::SetEmptyTestingFactory(
147    content::BrowserContext* context) {}
148
149bool SchemaRegistryServiceFactory::HasTestingFactory(
150    content::BrowserContext* context) {
151  return false;
152}
153
154void SchemaRegistryServiceFactory::CreateServiceNow(
155    content::BrowserContext* context) {}
156
157}  // namespace policy
158