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/browser_context_keyed_service/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 namespace policy {
15
16 // static
GetInstance()17 SchemaRegistryServiceFactory* SchemaRegistryServiceFactory::GetInstance() {
18 return Singleton<SchemaRegistryServiceFactory>::get();
19 }
20
21 // static
GetForContext(content::BrowserContext * context)22 SchemaRegistryService* SchemaRegistryServiceFactory::GetForContext(
23 content::BrowserContext* context) {
24 return GetInstance()->GetForContextInternal(context);
25 }
26
27 // static
28 scoped_ptr<SchemaRegistryService>
CreateForContext(content::BrowserContext * context,const Schema & chrome_schema,CombinedSchemaRegistry * global_registry)29 SchemaRegistryServiceFactory::CreateForContext(
30 content::BrowserContext* context,
31 const Schema& chrome_schema,
32 CombinedSchemaRegistry* global_registry) {
33 return GetInstance()->CreateForContextInternal(
34 context, chrome_schema, global_registry);
35 }
36
SchemaRegistryServiceFactory()37 SchemaRegistryServiceFactory::SchemaRegistryServiceFactory()
38 : BrowserContextKeyedBaseFactory(
39 "SchemaRegistryService",
40 BrowserContextDependencyManager::GetInstance()) {}
41
~SchemaRegistryServiceFactory()42 SchemaRegistryServiceFactory::~SchemaRegistryServiceFactory() {}
43
GetForContextInternal(content::BrowserContext * context)44 SchemaRegistryService* SchemaRegistryServiceFactory::GetForContextInternal(
45 content::BrowserContext* context) {
46 // Off-the-record Profiles get their policy from the main Profile's
47 // PolicyService, and don't need their own SchemaRegistry nor any policy
48 // providers.
49 if (context->IsOffTheRecord())
50 return NULL;
51 RegistryMap::const_iterator it = registries_.find(context);
52 CHECK(it != registries_.end());
53 return it->second;
54 }
55
56 scoped_ptr<SchemaRegistryService>
CreateForContextInternal(content::BrowserContext * context,const Schema & chrome_schema,CombinedSchemaRegistry * global_registry)57 SchemaRegistryServiceFactory::CreateForContextInternal(
58 content::BrowserContext* context,
59 const Schema& chrome_schema,
60 CombinedSchemaRegistry* global_registry) {
61 DCHECK(!context->IsOffTheRecord());
62 DCHECK(registries_.find(context) == registries_.end());
63 SchemaRegistryService* registry =
64 new SchemaRegistryService(chrome_schema, global_registry);
65 registries_[context] = registry;
66 return make_scoped_ptr(registry);
67 }
68
BrowserContextShutdown(content::BrowserContext * context)69 void SchemaRegistryServiceFactory::BrowserContextShutdown(
70 content::BrowserContext* context) {
71 if (context->IsOffTheRecord())
72 return;
73 RegistryMap::iterator it = registries_.find(context);
74 if (it != registries_.end())
75 it->second->Shutdown();
76 else
77 NOTREACHED();
78 }
79
BrowserContextDestroyed(content::BrowserContext * context)80 void SchemaRegistryServiceFactory::BrowserContextDestroyed(
81 content::BrowserContext* context) {
82 registries_.erase(context);
83 BrowserContextKeyedBaseFactory::BrowserContextDestroyed(context);
84 }
85
SetEmptyTestingFactory(content::BrowserContext * context)86 void SchemaRegistryServiceFactory::SetEmptyTestingFactory(
87 content::BrowserContext* context) {}
88
CreateServiceNow(content::BrowserContext * context)89 void SchemaRegistryServiceFactory::CreateServiceNow(
90 content::BrowserContext* context) {}
91
92 } // namespace policy
93