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 "components/policy/core/common/cloud/user_cloud_policy_manager.h"
6
7#include "base/bind.h"
8#include "base/bind_helpers.h"
9#include "base/sequenced_task_runner.h"
10#include "components/policy/core/common/cloud/cloud_external_data_manager.h"
11#include "components/policy/core/common/cloud/cloud_policy_constants.h"
12#include "components/policy/core/common/cloud/cloud_policy_service.h"
13#include "components/policy/core/common/cloud/user_cloud_policy_store.h"
14#include "components/policy/core/common/policy_pref_names.h"
15#include "components/policy/core/common/policy_types.h"
16#include "net/url_request/url_request_context_getter.h"
17
18namespace em = enterprise_management;
19
20namespace policy {
21
22UserCloudPolicyManager::UserCloudPolicyManager(
23    scoped_ptr<UserCloudPolicyStore> store,
24    const base::FilePath& component_policy_cache_path,
25    scoped_ptr<CloudExternalDataManager> external_data_manager,
26    const scoped_refptr<base::SequencedTaskRunner>& task_runner,
27    const scoped_refptr<base::SequencedTaskRunner>& file_task_runner,
28    const scoped_refptr<base::SequencedTaskRunner>& io_task_runner)
29    : CloudPolicyManager(
30          PolicyNamespaceKey(GetChromeUserPolicyType(), std::string()),
31          store.get(),
32          task_runner,
33          file_task_runner,
34          io_task_runner),
35      store_(store.Pass()),
36      component_policy_cache_path_(component_policy_cache_path),
37      external_data_manager_(external_data_manager.Pass()) {}
38
39UserCloudPolicyManager::~UserCloudPolicyManager() {}
40
41void UserCloudPolicyManager::Shutdown() {
42  if (external_data_manager_)
43    external_data_manager_->Disconnect();
44  CloudPolicyManager::Shutdown();
45}
46
47void UserCloudPolicyManager::SetSigninUsername(const std::string& username) {
48  store_->SetSigninUsername(username);
49}
50
51void UserCloudPolicyManager::Connect(
52    PrefService* local_state,
53    scoped_refptr<net::URLRequestContextGetter> request_context,
54    scoped_ptr<CloudPolicyClient> client) {
55  core()->Connect(client.Pass());
56  core()->StartRefreshScheduler();
57  core()->TrackRefreshDelayPref(local_state,
58                                policy_prefs::kUserPolicyRefreshRate);
59  if (external_data_manager_)
60    external_data_manager_->Connect(request_context);
61
62  CreateComponentCloudPolicyService(component_policy_cache_path_,
63                                    request_context);
64}
65
66// static
67scoped_ptr<CloudPolicyClient>
68UserCloudPolicyManager::CreateCloudPolicyClient(
69    DeviceManagementService* device_management_service,
70    scoped_refptr<net::URLRequestContextGetter> request_context) {
71  return make_scoped_ptr(
72      new CloudPolicyClient(
73          std::string(),
74          std::string(),
75          kPolicyVerificationKeyHash,
76          USER_AFFILIATION_NONE,
77          NULL,
78          device_management_service,
79          request_context)).Pass();
80}
81
82void UserCloudPolicyManager::DisconnectAndRemovePolicy() {
83  if (external_data_manager_)
84    external_data_manager_->Disconnect();
85  core()->Disconnect();
86
87  // store_->Clear() will publish the updated, empty policy. The component
88  // policy service must be cleared before OnStoreLoaded() is issued, so that
89  // component policies are also empty at CheckAndPublishPolicy().
90  ClearAndDestroyComponentCloudPolicyService();
91
92  // When the |store_| is cleared, it informs the |external_data_manager_| that
93  // all external data references have been removed, causing the
94  // |external_data_manager_| to clear its cache as well.
95  store_->Clear();
96}
97
98bool UserCloudPolicyManager::IsClientRegistered() const {
99  return client() && client()->is_registered();
100}
101
102}  // namespace policy
103