Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(253)

Side by Side Diff: chrome/browser/policy/user_cloud_policy_manager.cc

Issue 10919131: Break out base functionality of UserCloudPolicyManager. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/policy/user_cloud_policy_manager.h" 5 #include "chrome/browser/policy/user_cloud_policy_manager.h"
6 6
7 #include <string> 7 #include <string>
Joao da Silva 2012/09/07 12:44:13 nit: should be in .h
Mattias Nissler (ping if slow) 2012/09/07 13:12:59 Done.
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
Joao da Silva 2012/09/07 12:44:13 nit: not used
Mattias Nissler (ping if slow) 2012/09/07 13:12:59 Done.
10 #include "base/file_path.h" 10 #include "base/file_path.h"
11 #include "base/message_loop.h" 11 #include "base/message_loop.h"
12 #include "base/path_service.h" 12 #include "base/path_service.h"
13 #include "chrome/browser/policy/cloud_policy_refresh_scheduler.h" 13 #include "chrome/browser/policy/cloud_policy_refresh_scheduler.h"
14 #include "chrome/browser/policy/cloud_policy_service.h" 14 #include "chrome/browser/policy/cloud_policy_service.h"
Joao da Silva 2012/09/07 12:44:13 Nit: these 4 aren't used
Mattias Nissler (ping if slow) 2012/09/07 13:12:59 Done.
15 #include "chrome/browser/policy/policy_constants.h" 15 #include "chrome/browser/policy/policy_constants.h"
16 #include "chrome/common/chrome_paths.h" 16 #include "chrome/common/chrome_paths.h"
17 #include "chrome/common/chrome_switches.h" 17 #include "chrome/common/chrome_switches.h"
Joao da Silva 2012/09/07 12:44:13 Nit: these 2 aren't used
Mattias Nissler (ping if slow) 2012/09/07 13:12:59 Done.
18 #include "chrome/common/pref_names.h" 18 #include "chrome/common/pref_names.h"
19 19
20 #if defined(OS_CHROMEOS) 20 #if defined(OS_CHROMEOS)
21 #include "chrome/browser/policy/user_cloud_policy_store_chromeos.h" 21 #include "chrome/browser/policy/user_cloud_policy_store_chromeos.h"
22 #include "chromeos/dbus/dbus_thread_manager.h" 22 #include "chromeos/dbus/dbus_thread_manager.h"
23 #include "chromeos/dbus/session_manager_client.h" 23 #include "chromeos/dbus/session_manager_client.h"
24 #endif 24 #endif
Joao da Silva 2012/09/07 12:44:13 nit: these 3 aren't used
Mattias Nissler (ping if slow) 2012/09/07 13:12:59 Done.
25 25
26 namespace policy { 26 namespace policy {
27 27
28 namespace { 28 namespace {
29 29
30 #if defined(OS_CHROMEOS) 30 #if defined(OS_CHROMEOS)
31 // Paths for the legacy policy caches in the profile directory. 31 // Paths for the legacy policy caches in the profile directory.
32 // TODO(mnissler): Remove once the number of pre-M20 clients becomes negligible. 32 // TODO(mnissler): Remove once the number of pre-M20 clients becomes negligible.
33 33
34 // Subdirectory in the user's profile for storing user policies. 34 // Subdirectory in the user's profile for storing user policies.
35 const FilePath::CharType kPolicyDir[] = FILE_PATH_LITERAL("Device Management"); 35 const FilePath::CharType kPolicyDir[] = FILE_PATH_LITERAL("Device Management");
36 // File in the above directory for storing user policy dmtokens. 36 // File in the above directory for storing user policy dmtokens.
37 const FilePath::CharType kTokenCacheFile[] = FILE_PATH_LITERAL("Token"); 37 const FilePath::CharType kTokenCacheFile[] = FILE_PATH_LITERAL("Token");
38 // File in the above directory for storing user policy data. 38 // File in the above directory for storing user policy data.
39 const FilePath::CharType kPolicyCacheFile[] = FILE_PATH_LITERAL("Policy"); 39 const FilePath::CharType kPolicyCacheFile[] = FILE_PATH_LITERAL("Policy");
40 #endif 40 #endif
Joao da Silva 2012/09/07 12:44:13 These constants aren't used
Mattias Nissler (ping if slow) 2012/09/07 13:12:59 True. We probably missed removing all this cruft w
41 41
42 } // namespace 42 } // namespace
43 43
44 UserCloudPolicyManager::UserCloudPolicyManager( 44 UserCloudPolicyManager::UserCloudPolicyManager(
45 scoped_ptr<CloudPolicyStore> store, 45 scoped_ptr<CloudPolicyStore> store,
46 bool wait_for_policy_fetch) 46 bool wait_for_policy_fetch)
47 : wait_for_policy_fetch_(wait_for_policy_fetch), 47 : CloudPolicyManager(store.Pass()),
48 wait_for_policy_refresh_(false), 48 wait_for_policy_fetch_(wait_for_policy_fetch) {}
49 store_(store.Pass()) {
50 store_->Load();
51 store_->AddObserver(this);
52 }
53 49
54 UserCloudPolicyManager::~UserCloudPolicyManager() { 50 UserCloudPolicyManager::~UserCloudPolicyManager() {
55 Shutdown(); 51 if (cloud_policy_client())
56 store_->RemoveObserver(this); 52 cloud_policy_client()->RemoveObserver(this);
57 } 53 }
58 54
59 // static 55 // static
60 scoped_ptr<UserCloudPolicyManager> UserCloudPolicyManager::Create( 56 scoped_ptr<UserCloudPolicyManager> UserCloudPolicyManager::Create(
61 Profile* profile, 57 Profile* profile,
62 bool wait_for_policy_fetch) { 58 bool wait_for_policy_fetch) {
63 scoped_ptr<CloudPolicyStore> store = 59 scoped_ptr<CloudPolicyStore> store =
64 CloudPolicyStore::CreateUserPolicyStore(profile); 60 CloudPolicyStore::CreateUserPolicyStore(profile);
65 return scoped_ptr<UserCloudPolicyManager>( 61 return scoped_ptr<UserCloudPolicyManager>(
66 new UserCloudPolicyManager(store.Pass(), wait_for_policy_fetch)); 62 new UserCloudPolicyManager(store.Pass(), wait_for_policy_fetch));
67 } 63 }
68 64
69 void UserCloudPolicyManager::Initialize(PrefService* prefs, 65 void UserCloudPolicyManager::Initialize(
70 DeviceManagementService* service, 66 PrefService* local_state,
71 UserAffiliation user_affiliation) { 67 DeviceManagementService* device_management_service,
72 DCHECK(service); 68 UserAffiliation user_affiliation) {
73 DCHECK(prefs); 69 DCHECK(device_management_service);
74 DCHECK(!service_.get()); 70 DCHECK(local_state);
75 prefs_ = prefs; 71 local_state_ = local_state;
76 scoped_ptr<CloudPolicyClient> client( 72 scoped_ptr<CloudPolicyClient> client(
77 new CloudPolicyClient(std::string(), std::string(), user_affiliation, 73 new CloudPolicyClient(std::string(), std::string(), user_affiliation,
78 POLICY_SCOPE_USER, NULL, service)); 74 POLICY_SCOPE_USER, NULL,
79 service_.reset(new CloudPolicyService(client.Pass(), store_.get())); 75 device_management_service));
80 service_->client()->AddObserver(this); 76 InitializeService(client.Pass());
77 cloud_policy_client()->AddObserver(this);
81 78
82 if (wait_for_policy_fetch_) { 79 if (wait_for_policy_fetch_) {
83 // If we are supposed to wait for a policy fetch, we trigger an explicit 80 // If we are supposed to wait for a policy fetch, we trigger an explicit
84 // policy refresh at startup that allows us to unblock initialization once 81 // policy refresh at startup that allows us to unblock initialization once
85 // done. The refresh scheduler only gets started once that refresh 82 // done. The refresh scheduler only gets started once that refresh
86 // completes. Note that we might have to wait for registration to happen, 83 // completes. Note that we might have to wait for registration to happen,
87 // see OnRegistrationStateChanged() below. 84 // see OnRegistrationStateChanged() below.
88 if (service_->client()->is_registered()) { 85 if (cloud_policy_client()->is_registered()) {
89 service_->RefreshPolicy( 86 cloud_policy_service()->RefreshPolicy(
90 base::Bind(&UserCloudPolicyManager::OnInitialPolicyFetchComplete, 87 base::Bind(&UserCloudPolicyManager::OnInitialPolicyFetchComplete,
91 base::Unretained(this))); 88 base::Unretained(this)));
92 } 89 }
93 } else { 90 } else {
94 CancelWaitForPolicyFetch(); 91 CancelWaitForPolicyFetch();
95 } 92 }
96 } 93 }
97 94
98 void UserCloudPolicyManager::ShutdownAndRemovePolicy() { 95 void UserCloudPolicyManager::ShutdownAndRemovePolicy() {
99 Shutdown(); 96 if (cloud_policy_client())
100 store_->Clear(); 97 cloud_policy_client()->RemoveObserver(this);
98 ShutdownService();
99 local_state_ = NULL;
100 cloud_policy_store()->Clear();
101 } 101 }
102 102
103 void UserCloudPolicyManager::CancelWaitForPolicyFetch() { 103 void UserCloudPolicyManager::CancelWaitForPolicyFetch() {
104 wait_for_policy_fetch_ = false; 104 wait_for_policy_fetch_ = false;
105 CheckAndPublishPolicy(); 105 CheckAndPublishPolicy();
106 106
107 // Now that |wait_for_policy_fetch_| is guaranteed to be false, the scheduler 107 // Now that |wait_for_policy_fetch_| is guaranteed to be false, the scheduler
108 // can be started. 108 // can be started.
109 if (service_.get() && !refresh_scheduler_.get() && prefs_) { 109 if (cloud_policy_service() && local_state_)
110 refresh_scheduler_.reset( 110 StartRefreshScheduler(local_state_, prefs::kUserPolicyRefreshRate);
111 new CloudPolicyRefreshScheduler(
112 service_->client(), store_.get(), prefs_,
113 prefs::kUserPolicyRefreshRate,
114 MessageLoop::current()->message_loop_proxy()));
115 }
116 } 111 }
117 112
118 bool UserCloudPolicyManager::IsClientRegistered() const { 113 bool UserCloudPolicyManager::IsClientRegistered() const {
119 if (!service_.get()) 114 if (!cloud_policy_service())
120 return false; 115 return false;
121 return service_->client()->is_registered(); 116 return cloud_policy_client()->is_registered();
Joao da Silva 2012/09/07 12:44:13 The first line should test cloud_policy_client().
Mattias Nissler (ping if slow) 2012/09/07 13:12:59 Done.
122 } 117 }
123 118
124 void UserCloudPolicyManager::RegisterClient(const std::string& access_token) { 119 void UserCloudPolicyManager::RegisterClient(const std::string& access_token) {
125 DCHECK(cloud_policy_service()) << "Callers must invoke Initialize() first"; 120 DCHECK(cloud_policy_service()) << "Callers must invoke Initialize() first";
Joao da Silva 2012/09/07 12:44:13 DCHECK(cloud_policy_client())
Mattias Nissler (ping if slow) 2012/09/07 13:12:59 Done.
126 if (!cloud_policy_service()->client()->is_registered()) { 121 if (!cloud_policy_client()->is_registered()) {
127 DVLOG(1) << "Registering client with access token: " << access_token; 122 DVLOG(1) << "Registering client with access token: " << access_token;
128 cloud_policy_service()->client()->Register(access_token); 123 cloud_policy_client()->Register(access_token);
129 } 124 }
130 } 125 }
131 126
132 bool UserCloudPolicyManager::IsInitializationComplete() const { 127 bool UserCloudPolicyManager::IsInitializationComplete() const {
133 return store_->is_initialized() && !wait_for_policy_fetch_; 128 return CloudPolicyManager::IsInitializationComplete() &&
134 } 129 !wait_for_policy_fetch_;
135
136 void UserCloudPolicyManager::RefreshPolicies() {
137 if (service_.get()) {
138 wait_for_policy_refresh_ = true;
139 service_->RefreshPolicy(
140 base::Bind(&UserCloudPolicyManager::OnRefreshComplete,
141 base::Unretained(this)));
142 } else {
143 OnRefreshComplete();
144 }
145 } 130 }
146 131
147 void UserCloudPolicyManager::OnPolicyFetched(CloudPolicyClient* client) { 132 void UserCloudPolicyManager::OnPolicyFetched(CloudPolicyClient* client) {
148 // No action required. If we're blocked on a policy fetch, we'll learn about 133 // No action required. If we're blocked on a policy fetch, we'll learn about
149 // completion of it through OnInitialPolicyFetchComplete(). 134 // completion of it through OnInitialPolicyFetchComplete().
150 } 135 }
151 136
152 void UserCloudPolicyManager::OnRegistrationStateChanged( 137 void UserCloudPolicyManager::OnRegistrationStateChanged(
153 CloudPolicyClient* client) { 138 CloudPolicyClient* client) {
Joao da Silva 2012/09/07 12:44:13 DCHECK_EQ(cloud_policy_client(), client) ?
Mattias Nissler (ping if slow) 2012/09/07 13:12:59 Done.
154 if (wait_for_policy_fetch_) { 139 if (wait_for_policy_fetch_) {
155 // If we're blocked on the policy fetch, now is a good time to issue it. 140 // If we're blocked on the policy fetch, now is a good time to issue it.
156 if (service_->client()->is_registered()) { 141 if (cloud_policy_client()->is_registered()) {
157 service_->RefreshPolicy( 142 cloud_policy_service()->RefreshPolicy(
158 base::Bind(&UserCloudPolicyManager::OnInitialPolicyFetchComplete, 143 base::Bind(&UserCloudPolicyManager::OnInitialPolicyFetchComplete,
159 base::Unretained(this))); 144 base::Unretained(this)));
160 } else { 145 } else {
161 // If the client has switched to not registered, we bail out as this 146 // If the client has switched to not registered, we bail out as this
162 // indicates the cloud policy setup flow has been aborted. 147 // indicates the cloud policy setup flow has been aborted.
163 CancelWaitForPolicyFetch(); 148 CancelWaitForPolicyFetch();
164 } 149 }
165 } 150 }
166 } 151 }
167 152
168 void UserCloudPolicyManager::OnClientError(CloudPolicyClient* client) { 153 void UserCloudPolicyManager::OnClientError(CloudPolicyClient* client) {
169 CancelWaitForPolicyFetch(); 154 CancelWaitForPolicyFetch();
170 } 155 }
171 156
172 void UserCloudPolicyManager::CheckAndPublishPolicy() {
173 if (IsInitializationComplete() && !wait_for_policy_refresh_) {
174 scoped_ptr<PolicyBundle> bundle(new PolicyBundle());
175 bundle->Get(POLICY_DOMAIN_CHROME, std::string()).CopyFrom(
176 store_->policy_map());
177 UpdatePolicy(bundle.Pass());
178 }
179 }
180
181 void UserCloudPolicyManager::OnStoreLoaded(CloudPolicyStore* store) {
182 CheckAndPublishPolicy();
183 }
184
185 void UserCloudPolicyManager::OnStoreError(CloudPolicyStore* store) {
186 // No action required, the old policy is still valid.
187 }
188
189 void UserCloudPolicyManager::OnInitialPolicyFetchComplete() { 157 void UserCloudPolicyManager::OnInitialPolicyFetchComplete() {
190 CancelWaitForPolicyFetch(); 158 CancelWaitForPolicyFetch();
191 } 159 }
192 160
193 void UserCloudPolicyManager::OnRefreshComplete() {
194 wait_for_policy_refresh_ = false;
195 CheckAndPublishPolicy();
196 }
197
198 void UserCloudPolicyManager::Shutdown() {
199 refresh_scheduler_.reset();
200 if (service_.get())
201 service_->client()->RemoveObserver(this);
202 service_.reset();
203 prefs_ = NULL;
204 }
205
206 } // namespace policy 161 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698