summaryrefslogtreecommitdiffstats
path: root/chrome/browser/policy/user_cloud_policy_manager.cc
blob: ccbc55dda31fbab82898fc60c9342ed7d4902193 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/policy/user_cloud_policy_manager.h"

#include <string>

#include "base/command_line.h"
#include "base/file_path.h"
#include "base/message_loop.h"
#include "base/path_service.h"
#include "chrome/browser/policy/cloud_policy_refresh_scheduler.h"
#include "chrome/browser/policy/cloud_policy_service.h"
#include "chrome/browser/policy/policy_constants.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"

#if defined(OS_CHROMEOS)
#include "chrome/browser/policy/user_cloud_policy_store_chromeos.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/session_manager_client.h"
#endif

namespace policy {

namespace {

#if defined(OS_CHROMEOS)
// Paths for the legacy policy caches in the profile directory.
// TODO(mnissler): Remove once the number of pre-M20 clients becomes negligible.

// Subdirectory in the user's profile for storing user policies.
const FilePath::CharType kPolicyDir[] = FILE_PATH_LITERAL("Device Management");
// File in the above directory for stroing user policy dmtokens.
const FilePath::CharType kTokenCacheFile[] = FILE_PATH_LITERAL("Token");
// File in the above directory for storing user policy data.
const FilePath::CharType kPolicyCacheFile[] = FILE_PATH_LITERAL("Policy");
#endif

}  // namespace

UserCloudPolicyManager::UserCloudPolicyManager(
    scoped_ptr<CloudPolicyStore> store,
    bool wait_for_policy_fetch)
    : wait_for_policy_fetch_(wait_for_policy_fetch),
      wait_for_policy_refresh_(false),
      store_(store.Pass()) {
  store_->Load();
  store_->AddObserver(this);
}

UserCloudPolicyManager::~UserCloudPolicyManager() {
  Shutdown();
  store_->RemoveObserver(this);
}

#if defined(OS_CHROMEOS)
// static
scoped_ptr<UserCloudPolicyManager> UserCloudPolicyManager::Create(
    bool wait_for_policy_fetch) {
  FilePath profile_dir;
  CHECK(PathService::Get(chrome::DIR_USER_DATA, &profile_dir));
  CommandLine* command_line = CommandLine::ForCurrentProcess();
  const FilePath policy_dir =
      profile_dir
          .Append(command_line->GetSwitchValuePath(switches::kLoginProfile))
          .Append(kPolicyDir);
  const FilePath policy_cache_file = policy_dir.Append(kPolicyCacheFile);
  const FilePath token_cache_file = policy_dir.Append(kTokenCacheFile);

  scoped_ptr<CloudPolicyStore> store(
      new UserCloudPolicyStoreChromeOS(
          chromeos::DBusThreadManager::Get()->GetSessionManagerClient(),
          token_cache_file, policy_cache_file));
  return scoped_ptr<UserCloudPolicyManager>(
      new UserCloudPolicyManager(store.Pass(), wait_for_policy_fetch));
}
#endif

void UserCloudPolicyManager::Initialize(PrefService* prefs,
                                        DeviceManagementService* service,
                                        UserAffiliation user_affiliation) {
  DCHECK(!service_.get());
  prefs_ = prefs;
  scoped_ptr<CloudPolicyClient> client(
      new CloudPolicyClient(std::string(), std::string(), user_affiliation,
                            POLICY_SCOPE_USER, NULL, service));
  service_.reset(new CloudPolicyService(client.Pass(), store_.get()));
  service_->client()->AddObserver(this);

  if (wait_for_policy_fetch_) {
    // If we are supposed to wait for a policy fetch, we trigger an explicit
    // policy refresh at startup that allows us to unblock initialization once
    // done. The refresh scheduler only gets started once that refresh
    // completes. Note that we might have to wait for registration to happen,
    // see OnRegistrationStateChanged() below.
    if (service_->client()->is_registered()) {
      service_->RefreshPolicy(
          base::Bind(&UserCloudPolicyManager::OnInitialPolicyFetchComplete,
                     base::Unretained(this)));
    }
  } else {
    CancelWaitForPolicyFetch();
  }
}

void UserCloudPolicyManager::Shutdown() {
  refresh_scheduler_.reset();
  if (service_.get())
    service_->client()->RemoveObserver(this);
  service_.reset();
  prefs_ = NULL;
}

void UserCloudPolicyManager::CancelWaitForPolicyFetch() {
  wait_for_policy_fetch_ = false;
  CheckAndPublishPolicy();

  // Now that |wait_for_policy_fetch_| is guaranteed to be false, the scheduler
  // can be started.
  if (service_.get() && !refresh_scheduler_.get() && prefs_) {
    refresh_scheduler_.reset(
        new CloudPolicyRefreshScheduler(
            service_->client(), store_.get(), prefs_,
            prefs::kUserPolicyRefreshRate,
            MessageLoop::current()->message_loop_proxy()));
  }
}

bool UserCloudPolicyManager::IsInitializationComplete() const {
  return store_->is_initialized() && !wait_for_policy_fetch_;
}

void UserCloudPolicyManager::RefreshPolicies() {
  if (service_.get()) {
    wait_for_policy_refresh_ = true;
    service_->RefreshPolicy(
        base::Bind(&UserCloudPolicyManager::OnRefreshComplete,
                   base::Unretained(this)));
  } else {
    OnRefreshComplete();
  }
}

void UserCloudPolicyManager::OnPolicyFetched(CloudPolicyClient* client) {
  // No action required. If we're blocked on a policy fetch, we'll learn about
  // completion of it through OnInitialPolicyFetchComplete().
}

void UserCloudPolicyManager::OnRegistrationStateChanged(
    CloudPolicyClient* client) {
  if (wait_for_policy_fetch_) {
    // If we're blocked on the policy fetch, now is a good time to issue it.
    if (service_->client()->is_registered()) {
      service_->RefreshPolicy(
          base::Bind(&UserCloudPolicyManager::OnInitialPolicyFetchComplete,
                     base::Unretained(this)));
    } else {
      // If the client has switched to not registered, we bail out as this
      // indicates the cloud policy setup flow has been aborted.
      CancelWaitForPolicyFetch();
    }
  }
}

void UserCloudPolicyManager::OnClientError(CloudPolicyClient* client) {
  CancelWaitForPolicyFetch();
}

void UserCloudPolicyManager::CheckAndPublishPolicy() {
  if (IsInitializationComplete() && !wait_for_policy_refresh_) {
    scoped_ptr<PolicyBundle> bundle(new PolicyBundle());
    bundle->Get(POLICY_DOMAIN_CHROME, std::string()).CopyFrom(
        store_->policy_map());
    UpdatePolicy(bundle.Pass());
  }
}

void UserCloudPolicyManager::OnStoreLoaded(CloudPolicyStore* store) {
  CheckAndPublishPolicy();
}

void UserCloudPolicyManager::OnStoreError(CloudPolicyStore* store) {
  // No action required, the old policy is still valid.
}

void UserCloudPolicyManager::OnInitialPolicyFetchComplete() {
  CancelWaitForPolicyFetch();
}

void UserCloudPolicyManager::OnRefreshComplete() {
  wait_for_policy_refresh_ = false;
  CheckAndPublishPolicy();
}

}  // namespace policy