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/chromeos/policy/user_network_configuration_updater_factory.h" 6 7 #include "base/memory/singleton.h" 8 #include "chrome/browser/chromeos/login/users/user.h" 9 #include "chrome/browser/chromeos/login/users/user_manager.h" 10 #include "chrome/browser/chromeos/policy/user_network_configuration_updater.h" 11 #include "chrome/browser/chromeos/profiles/profile_helper.h" 12 #include "chrome/browser/policy/profile_policy_connector.h" 13 #include "chrome/browser/policy/profile_policy_connector_factory.h" 14 #include "chrome/browser/profiles/incognito_helpers.h" 15 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/common/pref_names.h" 17 #include "chromeos/network/network_handler.h" 18 #include "components/keyed_service/content/browser_context_dependency_manager.h" 19 #include "components/policy/core/common/cloud/cloud_policy_constants.h" 20 21 namespace policy { 22 23 // static 24 UserNetworkConfigurationUpdater* GetForProfile(Profile * profile)25UserNetworkConfigurationUpdaterFactory::GetForProfile(Profile* profile) { 26 return static_cast<UserNetworkConfigurationUpdater*>( 27 GetInstance()->GetServiceForBrowserContext(profile, true)); 28 } 29 30 // static 31 UserNetworkConfigurationUpdaterFactory* GetInstance()32UserNetworkConfigurationUpdaterFactory::GetInstance() { 33 return Singleton<UserNetworkConfigurationUpdaterFactory>::get(); 34 } 35 UserNetworkConfigurationUpdaterFactory()36UserNetworkConfigurationUpdaterFactory::UserNetworkConfigurationUpdaterFactory() 37 : BrowserContextKeyedServiceFactory( 38 "UserNetworkConfigurationUpdater", 39 BrowserContextDependencyManager::GetInstance()) { 40 DependsOn(ProfilePolicyConnectorFactory::GetInstance()); 41 } 42 43 UserNetworkConfigurationUpdaterFactory:: ~UserNetworkConfigurationUpdaterFactory()44 ~UserNetworkConfigurationUpdaterFactory() {} 45 46 content::BrowserContext* GetBrowserContextToUse(content::BrowserContext * context) const47UserNetworkConfigurationUpdaterFactory::GetBrowserContextToUse( 48 content::BrowserContext* context) const { 49 return chrome::GetBrowserContextRedirectedInIncognito(context); 50 } 51 52 bool ServiceIsCreatedWithBrowserContext() const53UserNetworkConfigurationUpdaterFactory::ServiceIsCreatedWithBrowserContext() 54 const { 55 return true; 56 } 57 ServiceIsNULLWhileTesting() const58bool UserNetworkConfigurationUpdaterFactory::ServiceIsNULLWhileTesting() const { 59 return true; 60 } 61 BuildServiceInstanceFor(content::BrowserContext * context) const62KeyedService* UserNetworkConfigurationUpdaterFactory::BuildServiceInstanceFor( 63 content::BrowserContext* context) const { 64 Profile* profile = static_cast<Profile*>(context); 65 if (chromeos::ProfileHelper::IsSigninProfile(profile)) 66 return NULL; // On the login screen only device network policies apply. 67 68 chromeos::UserManager* user_manager = chromeos::UserManager::Get(); 69 chromeos::User* user = user_manager->GetUserByProfile(profile); 70 DCHECK(user); 71 // Currently, only the network policy of the primary user is supported. See 72 // also http://crbug.com/310685 . 73 if (user != user_manager->GetPrimaryUser()) 74 return NULL; 75 76 const bool allow_trusted_certs_from_policy = 77 user->GetType() == chromeos::User::USER_TYPE_REGULAR; 78 79 ProfilePolicyConnector* profile_connector = 80 ProfilePolicyConnectorFactory::GetForProfile(profile); 81 82 return UserNetworkConfigurationUpdater::CreateForUserPolicy( 83 profile, 84 allow_trusted_certs_from_policy, 85 *user, 86 profile_connector->policy_service(), 87 chromeos::NetworkHandler::Get()->managed_network_configuration_handler()) 88 .release(); 89 } 90 91 } // namespace policy 92