Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / policy / schema_registry_service_factory.cc
blob5893022867b32df24182a109db78d5f0fa0c418c
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.
5 #include "chrome/browser/policy/schema_registry_service_factory.h"
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"
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
25 namespace policy {
27 #if defined(OS_CHROMEOS)
28 namespace {
30 DeviceLocalAccountPolicyBroker* GetBroker(content::BrowserContext* context) {
31 Profile* profile = Profile::FromBrowserContext(context);
33 if (chromeos::ProfileHelper::IsSigninProfile(profile))
34 return NULL;
36 if (!user_manager::UserManager::IsInitialized()) {
37 // Bail out in unit tests that don't have a UserManager.
38 return NULL;
41 const user_manager::User* user =
42 chromeos::ProfileHelper::Get()->GetUserByProfile(profile);
43 if (!user)
44 return NULL;
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;
53 return service->GetBrokerForUser(user->email());
56 } // namespace
57 #endif // OS_CHROMEOS
59 // static
60 SchemaRegistryServiceFactory* SchemaRegistryServiceFactory::GetInstance() {
61 return Singleton<SchemaRegistryServiceFactory>::get();
64 // static
65 SchemaRegistryService* SchemaRegistryServiceFactory::GetForContext(
66 content::BrowserContext* context) {
67 return GetInstance()->GetForContextInternal(context);
70 // static
71 scoped_ptr<SchemaRegistryService>
72 SchemaRegistryServiceFactory::CreateForContext(
73 content::BrowserContext* context,
74 const Schema& chrome_schema,
75 CombinedSchemaRegistry* global_registry) {
76 return GetInstance()->CreateForContextInternal(
77 context, chrome_schema, global_registry);
80 SchemaRegistryServiceFactory::SchemaRegistryServiceFactory()
81 : BrowserContextKeyedBaseFactory(
82 "SchemaRegistryService",
83 BrowserContextDependencyManager::GetInstance()) {}
85 SchemaRegistryServiceFactory::~SchemaRegistryServiceFactory() {}
87 SchemaRegistryService* 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;
99 scoped_ptr<SchemaRegistryService>
100 SchemaRegistryServiceFactory::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());
107 scoped_ptr<SchemaRegistry> registry;
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()));
118 #endif
120 if (!registry)
121 registry.reset(new SchemaRegistry);
123 scoped_ptr<SchemaRegistryService> service(new SchemaRegistryService(
124 registry.Pass(), chrome_schema, global_registry));
125 registries_[context] = service.get();
126 return service.Pass();
129 void 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();
140 void SchemaRegistryServiceFactory::BrowserContextDestroyed(
141 content::BrowserContext* context) {
142 registries_.erase(context);
143 BrowserContextKeyedBaseFactory::BrowserContextDestroyed(context);
146 void SchemaRegistryServiceFactory::SetEmptyTestingFactory(
147 content::BrowserContext* context) {}
149 bool SchemaRegistryServiceFactory::HasTestingFactory(
150 content::BrowserContext* context) {
151 return false;
154 void SchemaRegistryServiceFactory::CreateServiceNow(
155 content::BrowserContext* context) {}
157 } // namespace policy