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"
27 #if defined(OS_CHROMEOS)
30 DeviceLocalAccountPolicyBroker
* GetBroker(content::BrowserContext
* context
) {
31 Profile
* profile
= Profile::FromBrowserContext(context
);
33 if (chromeos::ProfileHelper::IsSigninProfile(profile
))
36 if (!user_manager::UserManager::IsInitialized()) {
37 // Bail out in unit tests that don't have a UserManager.
41 const user_manager::User
* user
=
42 chromeos::ProfileHelper::Get()->GetUserByProfile(profile
);
46 BrowserPolicyConnectorChromeOS
* connector
=
47 g_browser_process
->platform_part()->browser_policy_connector_chromeos();
48 DeviceLocalAccountPolicyService
* service
=
49 connector
->GetDeviceLocalAccountPolicyService();
53 return service
->GetBrokerForUser(user
->email());
60 SchemaRegistryServiceFactory
* SchemaRegistryServiceFactory::GetInstance() {
61 return Singleton
<SchemaRegistryServiceFactory
>::get();
65 SchemaRegistryService
* SchemaRegistryServiceFactory::GetForContext(
66 content::BrowserContext
* context
) {
67 return GetInstance()->GetForContextInternal(context
);
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
92 if (context
->IsOffTheRecord())
94 RegistryMap::const_iterator it
= registries_
.find(context
);
95 CHECK(it
!= registries_
.end());
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
);
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()));
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())
133 RegistryMap::iterator it
= registries_
.find(context
);
134 if (it
!= registries_
.end())
135 it
->second
->Shutdown();
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
) {
154 void SchemaRegistryServiceFactory::CreateServiceNow(
155 content::BrowserContext
* context
) {}
157 } // namespace policy