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/login/users/user.h" 18 #include "chrome/browser/chromeos/login/users/user_manager.h" 19 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" 20 #include "chrome/browser/chromeos/policy/device_local_account_policy_service.h" 21 #include "chrome/browser/chromeos/profiles/profile_helper.h" 22 #include "chrome/browser/profiles/profile.h" 23 #endif 24 25 namespace policy { 26 27 #if defined(OS_CHROMEOS) 28 namespace { 29 30 DeviceLocalAccountPolicyBroker* GetBroker(content::BrowserContext* context) { 31 Profile* profile = Profile::FromBrowserContext(context); 32 33 if (chromeos::ProfileHelper::IsSigninProfile(profile)) 34 return NULL; 35 36 if (!chromeos::UserManager::IsInitialized()) { 37 // Bail out in unit tests that don't have a UserManager. 38 return NULL; 39 } 40 41 chromeos::UserManager* user_manager = chromeos::UserManager::Get(); 42 chromeos::User* user = user_manager->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 60 SchemaRegistryServiceFactory* SchemaRegistryServiceFactory::GetInstance() { 61 return Singleton<SchemaRegistryServiceFactory>::get(); 62 } 63 64 // static 65 SchemaRegistryService* SchemaRegistryServiceFactory::GetForContext( 66 content::BrowserContext* context) { 67 return GetInstance()->GetForContextInternal(context); 68 } 69 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); 78 } 79 80 SchemaRegistryServiceFactory::SchemaRegistryServiceFactory() 81 : BrowserContextKeyedBaseFactory( 82 "SchemaRegistryService", 83 BrowserContextDependencyManager::GetInstance()) {} 84 85 SchemaRegistryServiceFactory::~SchemaRegistryServiceFactory() {} 86 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; 97 } 98 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()); 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 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(); 138 } 139 140 void SchemaRegistryServiceFactory::BrowserContextDestroyed( 141 content::BrowserContext* context) { 142 registries_.erase(context); 143 BrowserContextKeyedBaseFactory::BrowserContextDestroyed(context); 144 } 145 146 void SchemaRegistryServiceFactory::SetEmptyTestingFactory( 147 content::BrowserContext* context) {} 148 149 bool SchemaRegistryServiceFactory::HasTestingFactory( 150 content::BrowserContext* context) { 151 return false; 152 } 153 154 void SchemaRegistryServiceFactory::CreateServiceNow( 155 content::BrowserContext* context) {} 156 157 } // namespace policy 158