Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / user_network_configuration_updater_factory.cc
blobe13ed617a4b8da8a2fe01406cc2e16205422294b
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/chromeos/policy/user_network_configuration_updater_factory.h"
7 #include "base/memory/singleton.h"
8 #include "chrome/browser/chromeos/policy/user_network_configuration_updater.h"
9 #include "chrome/browser/chromeos/profiles/profile_helper.h"
10 #include "chrome/browser/policy/profile_policy_connector.h"
11 #include "chrome/browser/policy/profile_policy_connector_factory.h"
12 #include "chrome/browser/profiles/incognito_helpers.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chromeos/network/network_handler.h"
15 #include "components/keyed_service/content/browser_context_dependency_manager.h"
16 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
17 #include "components/user_manager/user.h"
18 #include "components/user_manager/user_manager.h"
20 namespace policy {
22 // static
23 UserNetworkConfigurationUpdater*
24 UserNetworkConfigurationUpdaterFactory::GetForProfile(Profile* profile) {
25 return static_cast<UserNetworkConfigurationUpdater*>(
26 GetInstance()->GetServiceForBrowserContext(profile, true));
29 // static
30 UserNetworkConfigurationUpdaterFactory*
31 UserNetworkConfigurationUpdaterFactory::GetInstance() {
32 return base::Singleton<UserNetworkConfigurationUpdaterFactory>::get();
35 UserNetworkConfigurationUpdaterFactory::UserNetworkConfigurationUpdaterFactory()
36 : BrowserContextKeyedServiceFactory(
37 "UserNetworkConfigurationUpdater",
38 BrowserContextDependencyManager::GetInstance()) {
39 DependsOn(ProfilePolicyConnectorFactory::GetInstance());
42 UserNetworkConfigurationUpdaterFactory::
43 ~UserNetworkConfigurationUpdaterFactory() {}
45 content::BrowserContext*
46 UserNetworkConfigurationUpdaterFactory::GetBrowserContextToUse(
47 content::BrowserContext* context) const {
48 return chrome::GetBrowserContextRedirectedInIncognito(context);
51 bool
52 UserNetworkConfigurationUpdaterFactory::ServiceIsCreatedWithBrowserContext()
53 const {
54 return true;
57 bool UserNetworkConfigurationUpdaterFactory::ServiceIsNULLWhileTesting() const {
58 return true;
61 KeyedService* UserNetworkConfigurationUpdaterFactory::BuildServiceInstanceFor(
62 content::BrowserContext* context) const {
63 Profile* profile = Profile::FromBrowserContext(context);
64 if (chromeos::ProfileHelper::IsSigninProfile(profile))
65 return NULL; // On the login screen only device network policies apply.
67 const user_manager::User* user =
68 chromeos::ProfileHelper::Get()->GetUserByProfile(profile);
69 DCHECK(user);
70 // Currently, only the network policy of the primary user is supported. See
71 // also http://crbug.com/310685 .
72 if (user != user_manager::UserManager::Get()->GetPrimaryUser())
73 return NULL;
75 const bool allow_trusted_certs_from_policy = user->HasGaiaAccount();
77 ProfilePolicyConnector* profile_connector =
78 ProfilePolicyConnectorFactory::GetForBrowserContext(context);
80 return UserNetworkConfigurationUpdater::CreateForUserPolicy(
81 profile,
82 allow_trusted_certs_from_policy,
83 *user,
84 profile_connector->policy_service(),
85 chromeos::NetworkHandler::Get()->managed_network_configuration_handler())
86 .release();
89 } // namespace policy