1// Copyright (c) 2011 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/browser_policy_connector.h"
6
7#include "base/command_line.h"
8#include "base/path_service.h"
9#include "chrome/browser/browser_process.h"
10#include "chrome/browser/policy/cloud_policy_subsystem.h"
11#include "chrome/browser/policy/configuration_policy_pref_store.h"
12#include "chrome/browser/policy/configuration_policy_provider.h"
13#include "chrome/browser/policy/dummy_configuration_policy_provider.h"
14#include "chrome/common/chrome_paths.h"
15#include "chrome/common/chrome_switches.h"
16
17#if defined(OS_WIN)
18#include "chrome/browser/policy/configuration_policy_provider_win.h"
19#elif defined(OS_MACOSX)
20#include "chrome/browser/policy/configuration_policy_provider_mac.h"
21#elif defined(OS_POSIX)
22#include "chrome/browser/policy/config_dir_policy_provider.h"
23#endif
24
25#if defined(OS_CHROMEOS)
26#include "chrome/browser/chromeos/cros/cros_library.h"
27#include "chrome/browser/policy/device_policy_cache.h"
28#include "chrome/browser/policy/device_policy_identity_strategy.h"
29#include "chrome/browser/policy/enterprise_install_attributes.h"
30#endif
31
32namespace policy {
33
34BrowserPolicyConnector::BrowserPolicyConnector()
35    : ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {
36  managed_platform_provider_.reset(CreateManagedPlatformProvider());
37  recommended_platform_provider_.reset(CreateRecommendedPlatformProvider());
38
39#if defined(OS_CHROMEOS)
40  CommandLine* command_line = CommandLine::ForCurrentProcess();
41  if (command_line->HasSwitch(switches::kEnableDevicePolicy)) {
42    identity_strategy_.reset(new DevicePolicyIdentityStrategy());
43    install_attributes_.reset(new EnterpriseInstallAttributes(
44        chromeos::CrosLibrary::Get()->GetCryptohomeLibrary()));
45    cloud_policy_subsystem_.reset(new CloudPolicySubsystem(
46        identity_strategy_.get(),
47        new DevicePolicyCache(identity_strategy_.get(),
48                              install_attributes_.get())));
49
50    // Initialize the subsystem once the message loops are spinning.
51    MessageLoop::current()->PostTask(
52        FROM_HERE,
53        method_factory_.NewRunnableMethod(&BrowserPolicyConnector::Initialize));
54  }
55#endif
56}
57
58BrowserPolicyConnector::BrowserPolicyConnector(
59    ConfigurationPolicyProvider* managed_platform_provider,
60    ConfigurationPolicyProvider* recommended_platform_provider)
61    : managed_platform_provider_(managed_platform_provider),
62      recommended_platform_provider_(recommended_platform_provider),
63      ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {}
64
65BrowserPolicyConnector::~BrowserPolicyConnector() {
66  if (cloud_policy_subsystem_.get())
67    cloud_policy_subsystem_->Shutdown();
68  cloud_policy_subsystem_.reset();
69#if defined(OS_CHROMEOS)
70  identity_strategy_.reset();
71#endif
72}
73
74ConfigurationPolicyProvider*
75    BrowserPolicyConnector::GetManagedPlatformProvider() const {
76  return managed_platform_provider_.get();
77}
78
79ConfigurationPolicyProvider*
80    BrowserPolicyConnector::GetManagedCloudProvider() const {
81  if (cloud_policy_subsystem_.get())
82    return cloud_policy_subsystem_->GetManagedPolicyProvider();
83
84  return NULL;
85}
86
87ConfigurationPolicyProvider*
88    BrowserPolicyConnector::GetRecommendedPlatformProvider() const {
89  return recommended_platform_provider_.get();
90}
91
92ConfigurationPolicyProvider*
93    BrowserPolicyConnector::GetRecommendedCloudProvider() const {
94  if (cloud_policy_subsystem_.get())
95    return cloud_policy_subsystem_->GetRecommendedPolicyProvider();
96
97  return NULL;
98}
99
100ConfigurationPolicyProvider*
101    BrowserPolicyConnector::CreateManagedPlatformProvider() {
102  const ConfigurationPolicyProvider::PolicyDefinitionList* policy_list =
103      ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList();
104#if defined(OS_WIN)
105  return new ConfigurationPolicyProviderWin(policy_list);
106#elif defined(OS_MACOSX)
107  return new ConfigurationPolicyProviderMac(policy_list);
108#elif defined(OS_POSIX)
109  FilePath config_dir_path;
110  if (PathService::Get(chrome::DIR_POLICY_FILES, &config_dir_path)) {
111    return new ConfigDirPolicyProvider(
112        policy_list,
113        config_dir_path.Append(FILE_PATH_LITERAL("managed")));
114  } else {
115    return new DummyConfigurationPolicyProvider(policy_list);
116  }
117#else
118  return new DummyConfigurationPolicyProvider(policy_list);
119#endif
120}
121
122ConfigurationPolicyProvider*
123    BrowserPolicyConnector::CreateRecommendedPlatformProvider() {
124  const ConfigurationPolicyProvider::PolicyDefinitionList* policy_list =
125      ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList();
126#if defined(OS_POSIX) && !defined(OS_MACOSX)
127  FilePath config_dir_path;
128  if (PathService::Get(chrome::DIR_POLICY_FILES, &config_dir_path)) {
129    return new ConfigDirPolicyProvider(
130        policy_list,
131        config_dir_path.Append(FILE_PATH_LITERAL("recommended")));
132  } else {
133    return new DummyConfigurationPolicyProvider(policy_list);
134  }
135#else
136  return new DummyConfigurationPolicyProvider(policy_list);
137#endif
138}
139
140void BrowserPolicyConnector::SetCredentials(const std::string& owner_email,
141                                            const std::string& gaia_token) {
142#if defined(OS_CHROMEOS)
143  if (identity_strategy_.get())
144    identity_strategy_->SetAuthCredentials(owner_email, gaia_token);
145#endif
146}
147
148bool BrowserPolicyConnector::IsEnterpriseManaged() {
149#if defined(OS_CHROMEOS)
150  return install_attributes_.get() && install_attributes_->IsEnterpriseDevice();
151#else
152  return false;
153#endif
154}
155
156EnterpriseInstallAttributes::LockResult
157    BrowserPolicyConnector::LockDevice(const std::string& user) {
158#if defined(OS_CHROMEOS)
159  if (install_attributes_.get())
160    return install_attributes_->LockDevice(user);
161#endif
162
163  return EnterpriseInstallAttributes::LOCK_BACKEND_ERROR;
164}
165
166std::string BrowserPolicyConnector::GetEnterpriseDomain() {
167#if defined(OS_CHROMEOS)
168  if (install_attributes_.get())
169    return install_attributes_->GetDomain();
170#endif
171
172  return std::string();
173}
174
175void BrowserPolicyConnector::StopAutoRetry() {
176  if (cloud_policy_subsystem_.get())
177    cloud_policy_subsystem_->StopAutoRetry();
178}
179
180void BrowserPolicyConnector::FetchPolicy() {
181#if defined(OS_CHROMEOS)
182  if (identity_strategy_.get())
183    return identity_strategy_->FetchPolicy();
184#endif
185}
186
187void BrowserPolicyConnector::Initialize() {
188  // TODO(jkummerow, mnissler): Move this out of the browser startup path.
189  if (cloud_policy_subsystem_.get()) {
190    cloud_policy_subsystem_->Initialize(
191        g_browser_process->local_state(),
192        g_browser_process->system_request_context());
193  }
194}
195
196}  // namespace
197