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