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
|
// 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 "base/bind.h"
#include "base/bind_helpers.h"
#include "chrome/browser/policy/cloud_policy_service.h"
#include "chrome/browser/policy/policy_types.h"
#include "chrome/common/pref_names.h"
namespace policy {
UserCloudPolicyManager::UserCloudPolicyManager(
scoped_ptr<CloudPolicyStore> store,
bool wait_for_policy_fetch)
: CloudPolicyManager(store.Pass()),
wait_for_policy_fetch_(wait_for_policy_fetch) {}
UserCloudPolicyManager::~UserCloudPolicyManager() {
if (cloud_policy_client())
cloud_policy_client()->RemoveObserver(this);
}
// static
scoped_ptr<UserCloudPolicyManager> UserCloudPolicyManager::Create(
Profile* profile,
bool wait_for_policy_fetch) {
scoped_ptr<CloudPolicyStore> store =
CloudPolicyStore::CreateUserPolicyStore(profile);
return scoped_ptr<UserCloudPolicyManager>(
new UserCloudPolicyManager(store.Pass(), wait_for_policy_fetch));
}
void UserCloudPolicyManager::Initialize(
PrefService* local_state,
DeviceManagementService* device_management_service,
UserAffiliation user_affiliation) {
DCHECK(device_management_service);
DCHECK(local_state);
local_state_ = local_state;
scoped_ptr<CloudPolicyClient> client(
new CloudPolicyClient(std::string(), std::string(), user_affiliation,
POLICY_SCOPE_USER, NULL,
device_management_service));
InitializeService(client.Pass());
cloud_policy_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 (cloud_policy_client()->is_registered()) {
cloud_policy_service()->RefreshPolicy(
base::Bind(&UserCloudPolicyManager::OnInitialPolicyFetchComplete,
base::Unretained(this)));
}
} else {
CancelWaitForPolicyFetch();
}
}
void UserCloudPolicyManager::ShutdownAndRemovePolicy() {
if (cloud_policy_client())
cloud_policy_client()->RemoveObserver(this);
ShutdownService();
local_state_ = NULL;
cloud_policy_store()->Clear();
}
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 (cloud_policy_service() && local_state_)
StartRefreshScheduler(local_state_, prefs::kUserPolicyRefreshRate);
}
bool UserCloudPolicyManager::IsClientRegistered() const {
return cloud_policy_client() && cloud_policy_client()->is_registered();
}
void UserCloudPolicyManager::RegisterClient(const std::string& access_token) {
DCHECK(cloud_policy_client()) << "Callers must invoke Initialize() first";
if (!cloud_policy_client()->is_registered()) {
DVLOG(1) << "Registering client with access token: " << access_token;
cloud_policy_client()->Register(access_token);
}
}
bool UserCloudPolicyManager::IsInitializationComplete() const {
return CloudPolicyManager::IsInitializationComplete() &&
!wait_for_policy_fetch_;
}
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) {
DCHECK_EQ(cloud_policy_client(), client);
if (wait_for_policy_fetch_) {
// If we're blocked on the policy fetch, now is a good time to issue it.
if (cloud_policy_client()->is_registered()) {
cloud_policy_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) {
DCHECK_EQ(cloud_policy_client(), client);
CancelWaitForPolicyFetch();
}
void UserCloudPolicyManager::OnInitialPolicyFetchComplete() {
CancelWaitForPolicyFetch();
}
} // namespace policy
|