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

Side by Side Diff: chrome/browser/chromeos/policy/device_local_account_policy_service.cc

Issue 342233005: Move ownership of the ComponentCloudPolicyService to the broker. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fixed comments Created 6 years, 6 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/chromeos/policy/device_local_account_policy_service.h" 5 #include "chrome/browser/chromeos/policy/device_local_account_policy_service.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/command_line.h"
10 #include "base/file_util.h" 12 #include "base/file_util.h"
11 #include "base/files/file_enumerator.h" 13 #include "base/files/file_enumerator.h"
12 #include "base/logging.h" 14 #include "base/logging.h"
13 #include "base/message_loop/message_loop.h" 15 #include "base/message_loop/message_loop.h"
14 #include "base/message_loop/message_loop_proxy.h" 16 #include "base/message_loop/message_loop_proxy.h"
15 #include "base/path_service.h" 17 #include "base/path_service.h"
16 #include "base/sequenced_task_runner.h" 18 #include "base/sequenced_task_runner.h"
17 #include "base/stl_util.h" 19 #include "base/stl_util.h"
18 #include "base/strings/string_number_conversions.h" 20 #include "base/strings/string_number_conversions.h"
19 #include "chrome/browser/browser_process.h" 21 #include "chrome/browser/browser_process.h"
20 #include "chrome/browser/chromeos/policy/device_local_account.h" 22 #include "chrome/browser/chromeos/policy/device_local_account.h"
21 #include "chrome/browser/chromeos/policy/device_local_account_external_data_serv ice.h" 23 #include "chrome/browser/chromeos/policy/device_local_account_external_data_serv ice.h"
22 #include "chrome/browser/chromeos/policy/device_local_account_policy_store.h" 24 #include "chrome/browser/chromeos/policy/device_local_account_policy_store.h"
23 #include "chrome/browser/chromeos/settings/device_settings_service.h" 25 #include "chrome/browser/chromeos/settings/device_settings_service.h"
24 #include "chrome/common/chrome_content_client.h" 26 #include "chrome/common/chrome_content_client.h"
25 #include "chromeos/chromeos_paths.h" 27 #include "chromeos/chromeos_paths.h"
26 #include "chromeos/dbus/session_manager_client.h" 28 #include "chromeos/dbus/session_manager_client.h"
27 #include "chromeos/settings/cros_settings_names.h" 29 #include "chromeos/settings/cros_settings_names.h"
28 #include "chromeos/settings/cros_settings_provider.h" 30 #include "chromeos/settings/cros_settings_provider.h"
29 #include "components/policy/core/common/cloud/cloud_policy_client.h" 31 #include "components/policy/core/common/cloud/cloud_policy_client.h"
30 #include "components/policy/core/common/cloud/cloud_policy_constants.h" 32 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
31 #include "components/policy/core/common/cloud/cloud_policy_refresh_scheduler.h" 33 #include "components/policy/core/common/cloud/cloud_policy_refresh_scheduler.h"
32 #include "components/policy/core/common/cloud/device_management_service.h" 34 #include "components/policy/core/common/cloud/device_management_service.h"
35 #include "components/policy/core/common/cloud/resource_cache.h"
33 #include "components/policy/core/common/cloud/system_policy_request_context.h" 36 #include "components/policy/core/common/cloud/system_policy_request_context.h"
37 #include "components/policy/core/common/policy_switches.h"
34 #include "content/public/browser/browser_thread.h" 38 #include "content/public/browser/browser_thread.h"
35 #include "net/url_request/url_request_context_getter.h" 39 #include "net/url_request/url_request_context_getter.h"
36 #include "policy/policy_constants.h" 40 #include "policy/policy_constants.h"
37 #include "policy/proto/device_management_backend.pb.h" 41 #include "policy/proto/device_management_backend.pb.h"
38 #include "url/gurl.h" 42 #include "url/gurl.h"
39 43
40 namespace em = enterprise_management; 44 namespace em = enterprise_management;
41 45
42 namespace policy { 46 namespace policy {
43 47
(...skipping 21 matching lines...) Expand all
65 scoped_ptr<CloudPolicyClient> client( 69 scoped_ptr<CloudPolicyClient> client(
66 new CloudPolicyClient(std::string(), std::string(), 70 new CloudPolicyClient(std::string(), std::string(),
67 kPolicyVerificationKeyHash, 71 kPolicyVerificationKeyHash,
68 USER_AFFILIATION_MANAGED, 72 USER_AFFILIATION_MANAGED,
69 NULL, device_management_service, request_context)); 73 NULL, device_management_service, request_context));
70 client->SetupRegistration(policy_data->request_token(), 74 client->SetupRegistration(policy_data->request_token(),
71 policy_data->device_id()); 75 policy_data->device_id());
72 return client.Pass(); 76 return client.Pass();
73 } 77 }
74 78
75 // Get the subdirectory of the cache directory in which force-installed 79 // Get the subdirectory of the force-installed extension cache and the component
76 // extensions are cached for |account_id|. This is also used for the 80 // policy cache used for |account_id|.
77 // component policy cache. 81 std::string GetCacheSubdirectoryForAccountID(const std::string& account_id) {
78 std::string EncodeAccountId(const std::string& account_id) {
79 return base::HexEncode(account_id.c_str(), account_id.size()); 82 return base::HexEncode(account_id.c_str(), account_id.size());
80 } 83 }
81 84
82 // Cleans up the cache directory by removing subdirectories that are not found 85 // Cleans up the cache directory by removing subdirectories that are not found
83 // in |subdirectories_to_keep|. Only caches whose cache directory is found in 86 // in |subdirectories_to_keep|. Only caches whose cache directory is found in
84 // |subdirectories_to_keep| may be running while the clean-up is in progress. 87 // |subdirectories_to_keep| may be running while the clean-up is in progress.
85 void DeleteOrphanedCaches( 88 void DeleteOrphanedCaches(
86 const base::FilePath& cache_root_dir, 89 const base::FilePath& cache_root_dir,
87 const std::set<std::string>& subdirectories_to_keep) { 90 const std::set<std::string>& subdirectories_to_keep) {
88 base::FileEnumerator enumerator(cache_root_dir, 91 base::FileEnumerator enumerator(cache_root_dir,
89 false, 92 false,
90 base::FileEnumerator::DIRECTORIES); 93 base::FileEnumerator::DIRECTORIES);
91 for (base::FilePath path = enumerator.Next(); !path.empty(); 94 for (base::FilePath path = enumerator.Next(); !path.empty();
92 path = enumerator.Next()) { 95 path = enumerator.Next()) {
93 const std::string subdirectory(path.BaseName().MaybeAsASCII()); 96 const std::string subdirectory(path.BaseName().MaybeAsASCII());
94 if (!ContainsKey(subdirectories_to_keep, subdirectory)) 97 if (!ContainsKey(subdirectories_to_keep, subdirectory))
95 base::DeleteFile(path, true); 98 base::DeleteFile(path, true);
96 } 99 }
97 } 100 }
98 101
99 // Removes the subdirectory belonging to |account_id_to_delete| from the cache 102 // Removes the subdirectory belonging to |account_id_to_delete| from the cache
100 // directory. No cache belonging to |account_id_to_delete| may be running while 103 // directory. No cache belonging to |account_id_to_delete| may be running while
101 // the removal is in progress. 104 // the removal is in progress.
102 void DeleteObsoleteExtensionCache(const std::string& account_id_to_delete) { 105 void DeleteObsoleteExtensionCache(const std::string& account_id_to_delete) {
103 base::FilePath cache_root_dir; 106 base::FilePath cache_root_dir;
104 CHECK(PathService::Get(chromeos::DIR_DEVICE_LOCAL_ACCOUNT_EXTENSIONS, 107 CHECK(PathService::Get(chromeos::DIR_DEVICE_LOCAL_ACCOUNT_EXTENSIONS,
105 &cache_root_dir)); 108 &cache_root_dir));
106 const base::FilePath path = 109 const base::FilePath path = cache_root_dir.Append(
107 cache_root_dir.Append(EncodeAccountId(account_id_to_delete)); 110 GetCacheSubdirectoryForAccountID(account_id_to_delete));
108 if (base::DirectoryExists(path)) 111 if (base::DirectoryExists(path))
109 base::DeleteFile(path, true); 112 base::DeleteFile(path, true);
110 } 113 }
111 114
112 } // namespace 115 } // namespace
113 116
114 DeviceLocalAccountPolicyBroker::DeviceLocalAccountPolicyBroker( 117 DeviceLocalAccountPolicyBroker::DeviceLocalAccountPolicyBroker(
115 const DeviceLocalAccount& account, 118 const DeviceLocalAccount& account,
116 const base::FilePath& component_policy_cache_path, 119 const base::FilePath& component_policy_cache_path,
117 scoped_ptr<DeviceLocalAccountPolicyStore> store, 120 scoped_ptr<DeviceLocalAccountPolicyStore> store,
118 scoped_refptr<DeviceLocalAccountExternalDataManager> external_data_manager, 121 scoped_refptr<DeviceLocalAccountExternalDataManager> external_data_manager,
122 const base::Closure& policy_update_callback,
119 const scoped_refptr<base::SequencedTaskRunner>& task_runner) 123 const scoped_refptr<base::SequencedTaskRunner>& task_runner)
120 : account_id_(account.account_id), 124 : account_id_(account.account_id),
121 user_id_(account.user_id), 125 user_id_(account.user_id),
122 component_policy_cache_path_(component_policy_cache_path), 126 component_policy_cache_path_(component_policy_cache_path),
123 store_(store.Pass()), 127 store_(store.Pass()),
124 external_data_manager_(external_data_manager), 128 external_data_manager_(external_data_manager),
125 core_(PolicyNamespaceKey(dm_protocol::kChromePublicAccountPolicyType, 129 core_(PolicyNamespaceKey(dm_protocol::kChromePublicAccountPolicyType,
126 store_->account_id()), 130 store_->account_id()),
127 store_.get(), 131 store_.get(),
128 task_runner) { 132 task_runner),
133 policy_update_callback_(policy_update_callback) {
129 base::FilePath cache_root_dir; 134 base::FilePath cache_root_dir;
130 CHECK(PathService::Get(chromeos::DIR_DEVICE_LOCAL_ACCOUNT_EXTENSIONS, 135 CHECK(PathService::Get(chromeos::DIR_DEVICE_LOCAL_ACCOUNT_EXTENSIONS,
131 &cache_root_dir)); 136 &cache_root_dir));
132 extension_loader_ = new chromeos::DeviceLocalAccountExternalPolicyLoader( 137 extension_loader_ = new chromeos::DeviceLocalAccountExternalPolicyLoader(
133 store_.get(), cache_root_dir.Append(EncodeAccountId(account.account_id))); 138 store_.get(),
139 cache_root_dir.Append(
140 GetCacheSubdirectoryForAccountID(account.account_id)));
141 store_->AddObserver(this);
134 } 142 }
135 143
136 DeviceLocalAccountPolicyBroker::~DeviceLocalAccountPolicyBroker() { 144 DeviceLocalAccountPolicyBroker::~DeviceLocalAccountPolicyBroker() {
145 store_->RemoveObserver(this);
137 external_data_manager_->SetPolicyStore(NULL); 146 external_data_manager_->SetPolicyStore(NULL);
138 external_data_manager_->Disconnect(); 147 external_data_manager_->Disconnect();
139 } 148 }
140 149
141 void DeviceLocalAccountPolicyBroker::Initialize() { 150 void DeviceLocalAccountPolicyBroker::Initialize() {
142 store_->Load(); 151 store_->Load();
143 } 152 }
144 153
145 void DeviceLocalAccountPolicyBroker::ConnectIfPossible( 154 void DeviceLocalAccountPolicyBroker::ConnectIfPossible(
146 chromeos::DeviceSettingsService* device_settings_service, 155 chromeos::DeviceSettingsService* device_settings_service,
147 DeviceManagementService* device_management_service, 156 DeviceManagementService* device_management_service,
148 scoped_refptr<net::URLRequestContextGetter> request_context) { 157 scoped_refptr<net::URLRequestContextGetter> request_context) {
149 if (core_.client()) 158 if (core_.client())
150 return; 159 return;
151 160
152 scoped_ptr<CloudPolicyClient> client(CreateClient(device_settings_service, 161 scoped_ptr<CloudPolicyClient> client(CreateClient(device_settings_service,
153 device_management_service, 162 device_management_service,
154 request_context)); 163 request_context));
155 if (!client) 164 if (!client)
156 return; 165 return;
157 166
158 core_.Connect(client.Pass()); 167 core_.Connect(client.Pass());
159 external_data_manager_->Connect(request_context); 168 external_data_manager_->Connect(request_context);
160 core_.StartRefreshScheduler(); 169 core_.StartRefreshScheduler();
161 UpdateRefreshDelay(); 170 UpdateRefreshDelay();
171 CreateComponentCloudPolicyService(request_context);
162 } 172 }
163 173
164 void DeviceLocalAccountPolicyBroker::UpdateRefreshDelay() { 174 void DeviceLocalAccountPolicyBroker::UpdateRefreshDelay() {
165 if (core_.refresh_scheduler()) { 175 if (core_.refresh_scheduler()) {
166 const base::Value* policy_value = 176 const base::Value* policy_value =
167 store_->policy_map().GetValue(key::kPolicyRefreshRate); 177 store_->policy_map().GetValue(key::kPolicyRefreshRate);
168 int delay = 0; 178 int delay = 0;
169 if (policy_value && policy_value->GetAsInteger(&delay)) 179 if (policy_value && policy_value->GetAsInteger(&delay))
170 core_.refresh_scheduler()->SetRefreshDelay(delay); 180 core_.refresh_scheduler()->SetRefreshDelay(delay);
171 } 181 }
172 } 182 }
173 183
174 std::string DeviceLocalAccountPolicyBroker::GetDisplayName() const { 184 std::string DeviceLocalAccountPolicyBroker::GetDisplayName() const {
175 std::string display_name; 185 std::string display_name;
176 const base::Value* display_name_value = 186 const base::Value* display_name_value =
177 store_->policy_map().GetValue(policy::key::kUserDisplayName); 187 store_->policy_map().GetValue(policy::key::kUserDisplayName);
178 if (display_name_value) 188 if (display_name_value)
179 display_name_value->GetAsString(&display_name); 189 display_name_value->GetAsString(&display_name);
180 return display_name; 190 return display_name;
181 } 191 }
182 192
183 base::FilePath DeviceLocalAccountPolicyBroker::GetComponentPolicyCachePath() 193 void DeviceLocalAccountPolicyBroker::OnStoreLoaded(CloudPolicyStore* store) {
184 const { 194 UpdateRefreshDelay();
185 return component_policy_cache_path_; 195 policy_update_callback_.Run();
196 }
197
198 void DeviceLocalAccountPolicyBroker::OnStoreError(CloudPolicyStore* store) {
199 policy_update_callback_.Run();
200 }
201
202 void DeviceLocalAccountPolicyBroker::OnComponentCloudPolicyUpdated() {
203 policy_update_callback_.Run();
204 }
205
206 void DeviceLocalAccountPolicyBroker::CreateComponentCloudPolicyService(
207 const scoped_refptr<net::URLRequestContextGetter>& request_context) {
208 if (CommandLine::ForCurrentProcess()->HasSwitch(
209 switches::kDisableComponentCloudPolicy)) {
210 // Disabled via the command line.
211 return;
212 }
213
214 scoped_ptr<ResourceCache> resource_cache(
215 new ResourceCache(component_policy_cache_path_,
216 content::BrowserThread::GetMessageLoopProxyForThread(
217 content::BrowserThread::FILE)));
218
219 component_policy_service_.reset(new ComponentCloudPolicyService(
220 this,
221 &schema_registry_,
222 core(),
223 resource_cache.Pass(),
224 request_context,
225 content::BrowserThread::GetMessageLoopProxyForThread(
226 content::BrowserThread::FILE),
227 content::BrowserThread::GetMessageLoopProxyForThread(
228 content::BrowserThread::IO)));
186 } 229 }
187 230
188 DeviceLocalAccountPolicyService::DeviceLocalAccountPolicyService( 231 DeviceLocalAccountPolicyService::DeviceLocalAccountPolicyService(
189 chromeos::SessionManagerClient* session_manager_client, 232 chromeos::SessionManagerClient* session_manager_client,
190 chromeos::DeviceSettingsService* device_settings_service, 233 chromeos::DeviceSettingsService* device_settings_service,
191 chromeos::CrosSettings* cros_settings, 234 chromeos::CrosSettings* cros_settings,
192 scoped_refptr<base::SequencedTaskRunner> store_background_task_runner, 235 scoped_refptr<base::SequencedTaskRunner> store_background_task_runner,
193 scoped_refptr<base::SequencedTaskRunner> extension_cache_task_runner, 236 scoped_refptr<base::SequencedTaskRunner> extension_cache_task_runner,
194 scoped_refptr<base::SequencedTaskRunner> 237 scoped_refptr<base::SequencedTaskRunner>
195 external_data_service_backend_task_runner, 238 external_data_service_backend_task_runner,
196 scoped_refptr<base::SequencedTaskRunner> io_task_runner, 239 scoped_refptr<base::SequencedTaskRunner> io_task_runner,
197 scoped_refptr<net::URLRequestContextGetter> request_context) 240 scoped_refptr<net::URLRequestContextGetter> request_context)
198 : session_manager_client_(session_manager_client), 241 : session_manager_client_(session_manager_client),
199 device_settings_service_(device_settings_service), 242 device_settings_service_(device_settings_service),
200 cros_settings_(cros_settings), 243 cros_settings_(cros_settings),
201 device_management_service_(NULL), 244 device_management_service_(NULL),
202 waiting_for_cros_settings_(false), 245 waiting_for_cros_settings_(false),
203 orphan_cache_deletion_state_(NOT_STARTED), 246 orphan_extension_cache_deletion_state_(NOT_STARTED),
204 store_background_task_runner_(store_background_task_runner), 247 store_background_task_runner_(store_background_task_runner),
205 extension_cache_task_runner_(extension_cache_task_runner), 248 extension_cache_task_runner_(extension_cache_task_runner),
206 request_context_(request_context), 249 request_context_(request_context),
207 local_accounts_subscription_(cros_settings_->AddSettingsObserver( 250 local_accounts_subscription_(cros_settings_->AddSettingsObserver(
208 chromeos::kAccountsPrefDeviceLocalAccounts, 251 chromeos::kAccountsPrefDeviceLocalAccounts,
209 base::Bind(&DeviceLocalAccountPolicyService:: 252 base::Bind(&DeviceLocalAccountPolicyService::
210 UpdateAccountListIfNonePending, 253 UpdateAccountListIfNonePending,
211 base::Unretained(this)))), 254 base::Unretained(this)))),
212 weak_factory_(this) { 255 weak_factory_(this) {
213 CHECK(PathService::Get(chromeos::DIR_DEVICE_LOCAL_ACCOUNT_COMPONENT_POLICY, 256 CHECK(PathService::Get(chromeos::DIR_DEVICE_LOCAL_ACCOUNT_COMPONENT_POLICY,
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
253 296
254 return entry->second; 297 return entry->second;
255 } 298 }
256 299
257 bool DeviceLocalAccountPolicyService::IsPolicyAvailableForUser( 300 bool DeviceLocalAccountPolicyService::IsPolicyAvailableForUser(
258 const std::string& user_id) { 301 const std::string& user_id) {
259 DeviceLocalAccountPolicyBroker* broker = GetBrokerForUser(user_id); 302 DeviceLocalAccountPolicyBroker* broker = GetBrokerForUser(user_id);
260 return broker && broker->core()->store()->is_managed(); 303 return broker && broker->core()->store()->is_managed();
261 } 304 }
262 305
263 scoped_refptr<net::URLRequestContextGetter>
264 DeviceLocalAccountPolicyService::request_context() const {
265 return request_context_;
266 }
267
268 void DeviceLocalAccountPolicyService::AddObserver(Observer* observer) { 306 void DeviceLocalAccountPolicyService::AddObserver(Observer* observer) {
269 observers_.AddObserver(observer); 307 observers_.AddObserver(observer);
270 } 308 }
271 309
272 void DeviceLocalAccountPolicyService::RemoveObserver(Observer* observer) { 310 void DeviceLocalAccountPolicyService::RemoveObserver(Observer* observer) {
273 observers_.RemoveObserver(observer); 311 observers_.RemoveObserver(observer);
274 } 312 }
275 313
276 void DeviceLocalAccountPolicyService::OnStoreLoaded(CloudPolicyStore* store) {
277 DeviceLocalAccountPolicyBroker* broker = GetBrokerForStore(store);
278 DCHECK(broker);
279 if (!broker)
280 return;
281 broker->UpdateRefreshDelay();
282 FOR_EACH_OBSERVER(Observer, observers_, OnPolicyUpdated(broker->user_id()));
283 }
284
285 void DeviceLocalAccountPolicyService::OnStoreError(CloudPolicyStore* store) {
286 DeviceLocalAccountPolicyBroker* broker = GetBrokerForStore(store);
287 DCHECK(broker);
288 if (!broker)
289 return;
290 FOR_EACH_OBSERVER(Observer, observers_, OnPolicyUpdated(broker->user_id()));
291 }
292
293 bool DeviceLocalAccountPolicyService::IsExtensionCacheDirectoryBusy( 314 bool DeviceLocalAccountPolicyService::IsExtensionCacheDirectoryBusy(
294 const std::string& account_id) { 315 const std::string& account_id) {
295 return busy_extension_cache_directories_.find(account_id) != 316 return busy_extension_cache_directories_.find(account_id) !=
296 busy_extension_cache_directories_.end(); 317 busy_extension_cache_directories_.end();
297 } 318 }
298 319
299 void DeviceLocalAccountPolicyService::StartExtensionCachesIfPossible() { 320 void DeviceLocalAccountPolicyService::StartExtensionCachesIfPossible() {
300 for (PolicyBrokerMap::iterator it = policy_brokers_.begin(); 321 for (PolicyBrokerMap::iterator it = policy_brokers_.begin();
301 it != policy_brokers_.end(); ++it) { 322 it != policy_brokers_.end(); ++it) {
302 if (!it->second->extension_loader()->IsCacheRunning() && 323 if (!it->second->extension_loader()->IsCacheRunning() &&
(...skipping 10 matching lines...) Expand all
313 if (it->second->account_id() == account_id) { 334 if (it->second->account_id() == account_id) {
314 DCHECK(!it->second->extension_loader()->IsCacheRunning()); 335 DCHECK(!it->second->extension_loader()->IsCacheRunning());
315 it->second->extension_loader()->StartCache(extension_cache_task_runner_); 336 it->second->extension_loader()->StartCache(extension_cache_task_runner_);
316 return true; 337 return true;
317 } 338 }
318 } 339 }
319 return false; 340 return false;
320 } 341 }
321 342
322 void DeviceLocalAccountPolicyService::OnOrphanedExtensionCachesDeleted() { 343 void DeviceLocalAccountPolicyService::OnOrphanedExtensionCachesDeleted() {
323 DCHECK_EQ(IN_PROGRESS, orphan_cache_deletion_state_); 344 DCHECK_EQ(IN_PROGRESS, orphan_extension_cache_deletion_state_);
324 345
325 orphan_cache_deletion_state_ = DONE; 346 orphan_extension_cache_deletion_state_ = DONE;
326 StartExtensionCachesIfPossible(); 347 StartExtensionCachesIfPossible();
327 } 348 }
328 349
329 void DeviceLocalAccountPolicyService::OnObsoleteExtensionCacheShutdown( 350 void DeviceLocalAccountPolicyService::OnObsoleteExtensionCacheShutdown(
330 const std::string& account_id) { 351 const std::string& account_id) {
331 DCHECK_NE(NOT_STARTED, orphan_cache_deletion_state_); 352 DCHECK_NE(NOT_STARTED, orphan_extension_cache_deletion_state_);
332 DCHECK(IsExtensionCacheDirectoryBusy(account_id)); 353 DCHECK(IsExtensionCacheDirectoryBusy(account_id));
333 354
334 // The account with |account_id| was deleted and the broker for it has shut 355 // The account with |account_id| was deleted and the broker for it has shut
335 // down completely. 356 // down completely.
336 357
337 if (StartExtensionCacheForAccountIfPresent(account_id)) { 358 if (StartExtensionCacheForAccountIfPresent(account_id)) {
338 // If another account with the same ID was created in the meantime, its 359 // If another account with the same ID was created in the meantime, its
339 // extension cache is started, reusing the cache directory. The directory no 360 // extension cache is started, reusing the cache directory. The directory no
340 // longer needs to be marked as busy in this case. 361 // longer needs to be marked as busy in this case.
341 busy_extension_cache_directories_.erase(account_id); 362 busy_extension_cache_directories_.erase(account_id);
342 return; 363 return;
343 } 364 }
344 365
345 // If no account with |account_id| exists anymore, the cache directory should 366 // If no account with |account_id| exists anymore, the cache directory should
346 // be removed. The directory must stay marked as busy while the removal is in 367 // be removed. The directory must stay marked as busy while the removal is in
347 // progress. 368 // progress.
348 extension_cache_task_runner_->PostTaskAndReply( 369 extension_cache_task_runner_->PostTaskAndReply(
349 FROM_HERE, 370 FROM_HERE,
350 base::Bind(&DeleteObsoleteExtensionCache, account_id), 371 base::Bind(&DeleteObsoleteExtensionCache, account_id),
351 base::Bind(&DeviceLocalAccountPolicyService:: 372 base::Bind(&DeviceLocalAccountPolicyService::
352 OnObsoleteExtensionCacheDeleted, 373 OnObsoleteExtensionCacheDeleted,
353 weak_factory_.GetWeakPtr(), 374 weak_factory_.GetWeakPtr(),
354 account_id)); 375 account_id));
355 } 376 }
356 377
357 void DeviceLocalAccountPolicyService::OnObsoleteExtensionCacheDeleted( 378 void DeviceLocalAccountPolicyService::OnObsoleteExtensionCacheDeleted(
358 const std::string& account_id) { 379 const std::string& account_id) {
359 DCHECK_EQ(DONE, orphan_cache_deletion_state_); 380 DCHECK_EQ(DONE, orphan_extension_cache_deletion_state_);
360 DCHECK(IsExtensionCacheDirectoryBusy(account_id)); 381 DCHECK(IsExtensionCacheDirectoryBusy(account_id));
361 382
362 // The cache directory for |account_id| has been deleted. The directory no 383 // The cache directory for |account_id| has been deleted. The directory no
363 // longer needs to be marked as busy. 384 // longer needs to be marked as busy.
364 busy_extension_cache_directories_.erase(account_id); 385 busy_extension_cache_directories_.erase(account_id);
365 386
366 // If another account with the same ID was created in the meantime, start its 387 // If another account with the same ID was created in the meantime, start its
367 // extension cache, creating a new cache directory. 388 // extension cache, creating a new cache directory.
368 StartExtensionCacheForAccountIfPresent(account_id); 389 StartExtensionCacheForAccountIfPresent(account_id);
369 } 390 }
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
410 // Reuse the existing broker if present. 431 // Reuse the existing broker if present.
411 broker.reset(broker_it->second); 432 broker.reset(broker_it->second);
412 old_policy_brokers.erase(broker_it); 433 old_policy_brokers.erase(broker_it);
413 broker_initialized = true; 434 broker_initialized = true;
414 } else { 435 } else {
415 scoped_ptr<DeviceLocalAccountPolicyStore> store( 436 scoped_ptr<DeviceLocalAccountPolicyStore> store(
416 new DeviceLocalAccountPolicyStore(it->account_id, 437 new DeviceLocalAccountPolicyStore(it->account_id,
417 session_manager_client_, 438 session_manager_client_,
418 device_settings_service_, 439 device_settings_service_,
419 store_background_task_runner_)); 440 store_background_task_runner_));
420 store->AddObserver(this);
421 scoped_refptr<DeviceLocalAccountExternalDataManager> 441 scoped_refptr<DeviceLocalAccountExternalDataManager>
422 external_data_manager = 442 external_data_manager =
423 external_data_service_->GetExternalDataManager(it->account_id, 443 external_data_service_->GetExternalDataManager(it->account_id,
424 store.get()); 444 store.get());
425 broker.reset(new DeviceLocalAccountPolicyBroker( 445 broker.reset(new DeviceLocalAccountPolicyBroker(
426 *it, 446 *it,
427 component_policy_cache_root_.Append(EncodeAccountId(it->account_id)), 447 component_policy_cache_root_.Append(
448 GetCacheSubdirectoryForAccountID(it->account_id)),
428 store.Pass(), 449 store.Pass(),
429 external_data_manager, 450 external_data_manager,
451 base::Bind(&DeviceLocalAccountPolicyService::NotifyPolicyUpdated,
452 base::Unretained(this),
453 it->user_id),
430 base::MessageLoopProxy::current())); 454 base::MessageLoopProxy::current()));
431 } 455 }
432 456
433 // Fire up the cloud connection for fetching policy for the account from 457 // Fire up the cloud connection for fetching policy for the account from
434 // the cloud if this is an enterprise-managed device. 458 // the cloud if this is an enterprise-managed device.
435 broker->ConnectIfPossible(device_settings_service_, 459 broker->ConnectIfPossible(device_settings_service_,
436 device_management_service_, 460 device_management_service_,
437 request_context_); 461 request_context_);
438 462
439 policy_brokers_[it->user_id] = broker.release(); 463 policy_brokers_[it->user_id] = broker.release();
440 if (!broker_initialized) { 464 if (!broker_initialized) {
441 // The broker must be initialized after it has been added to 465 // The broker must be initialized after it has been added to
442 // |policy_brokers_|. 466 // |policy_brokers_|.
443 policy_brokers_[it->user_id]->Initialize(); 467 policy_brokers_[it->user_id]->Initialize();
444 } 468 }
445 469
446 subdirectories_to_keep.insert(EncodeAccountId(it->account_id)); 470 subdirectories_to_keep.insert(
471 GetCacheSubdirectoryForAccountID(it->account_id));
447 } 472 }
448 473
449 if (orphan_cache_deletion_state_ == NOT_STARTED) { 474 if (orphan_extension_cache_deletion_state_ == NOT_STARTED) {
450 DCHECK(old_policy_brokers.empty()); 475 DCHECK(old_policy_brokers.empty());
451 DCHECK(busy_extension_cache_directories_.empty()); 476 DCHECK(busy_extension_cache_directories_.empty());
452 477
453 // If this method is running for the first time, no extension caches have 478 // If this method is running for the first time, no extension caches have
454 // been started yet. Take this opportunity to do a clean-up by removing 479 // been started yet. Take this opportunity to do a clean-up by removing
455 // orphaned cache directories not found in |subdirectories_to_keep| from the 480 // orphaned cache directories not found in |subdirectories_to_keep| from the
456 // cache directory. 481 // cache directory.
457 orphan_cache_deletion_state_ = IN_PROGRESS; 482 orphan_extension_cache_deletion_state_ = IN_PROGRESS;
458 483
459 base::FilePath cache_root_dir; 484 base::FilePath cache_root_dir;
460 CHECK(PathService::Get(chromeos::DIR_DEVICE_LOCAL_ACCOUNT_EXTENSIONS, 485 CHECK(PathService::Get(chromeos::DIR_DEVICE_LOCAL_ACCOUNT_EXTENSIONS,
461 &cache_root_dir)); 486 &cache_root_dir));
462 extension_cache_task_runner_->PostTaskAndReply( 487 extension_cache_task_runner_->PostTaskAndReply(
463 FROM_HERE, 488 FROM_HERE,
464 base::Bind( 489 base::Bind(
465 &DeleteOrphanedCaches, cache_root_dir, subdirectories_to_keep), 490 &DeleteOrphanedCaches, cache_root_dir, subdirectories_to_keep),
466 base::Bind( 491 base::Bind(
467 &DeviceLocalAccountPolicyService::OnOrphanedExtensionCachesDeleted, 492 &DeviceLocalAccountPolicyService::OnOrphanedExtensionCachesDeleted,
468 weak_factory_.GetWeakPtr())); 493 weak_factory_.GetWeakPtr()));
469 494
470 // Start the extension caches for all brokers. These belong to accounts in 495 // Start the extension caches for all brokers. These belong to accounts in
471 // |account_ids| and are not affected by the clean-up. 496 // |account_ids| and are not affected by the clean-up.
472 StartExtensionCachesIfPossible(); 497 StartExtensionCachesIfPossible();
473 } else { 498 } else {
474 // If this method has run before, obsolete brokers may exist. Shut down 499 // If this method has run before, obsolete brokers may exist. Shut down
475 // their extension caches and delete the brokers. 500 // their extension caches and delete the brokers.
476 DeleteBrokers(&old_policy_brokers); 501 DeleteBrokers(&old_policy_brokers);
477 502
478 if (orphan_cache_deletion_state_ == DONE) { 503 if (orphan_extension_cache_deletion_state_ == DONE) {
479 // If the initial clean-up of orphaned cache directories has been 504 // If the initial clean-up of orphaned cache directories has been
480 // complete, start any extension caches that are not running yet but can 505 // complete, start any extension caches that are not running yet but can
481 // be started now because their cache directories are not busy. 506 // be started now because their cache directories are not busy.
482 StartExtensionCachesIfPossible(); 507 StartExtensionCachesIfPossible();
483 } 508 }
484 } 509 }
485 510
486 // Purge the component policy caches of any accounts that have been removed. 511 // Purge the component policy caches of any accounts that have been removed.
487 // Do this only after any obsolete brokers have been destroyed. 512 // Do this only after any obsolete brokers have been destroyed.
488 // TODO(joaodasilva): for now this must be posted to the FILE thread, 513 // TODO(joaodasilva): for now this must be posted to the FILE thread,
489 // to avoid racing with the ComponentCloudPolicyStore. Use a task runner 514 // to avoid racing with the ComponentCloudPolicyStore. Use a task runner
490 // once that class supports another background thread too. 515 // once that class supports another background thread too.
491 content::BrowserThread::PostTask(content::BrowserThread::FILE, FROM_HERE, 516 content::BrowserThread::PostTask(content::BrowserThread::FILE, FROM_HERE,
492 base::Bind(&DeleteOrphanedCaches, 517 base::Bind(&DeleteOrphanedCaches,
493 component_policy_cache_root_, 518 component_policy_cache_root_,
494 subdirectories_to_keep)); 519 subdirectories_to_keep));
495 520
496 FOR_EACH_OBSERVER(Observer, observers_, OnDeviceLocalAccountsChanged()); 521 FOR_EACH_OBSERVER(Observer, observers_, OnDeviceLocalAccountsChanged());
497 } 522 }
498 523
499 void DeviceLocalAccountPolicyService::DeleteBrokers(PolicyBrokerMap* map) { 524 void DeviceLocalAccountPolicyService::DeleteBrokers(PolicyBrokerMap* map) {
500 for (PolicyBrokerMap::iterator it = map->begin(); it != map->end(); ++it) { 525 for (PolicyBrokerMap::iterator it = map->begin(); it != map->end(); ++it) {
501 it->second->core()->store()->RemoveObserver(this);
502 scoped_refptr<chromeos::DeviceLocalAccountExternalPolicyLoader> 526 scoped_refptr<chromeos::DeviceLocalAccountExternalPolicyLoader>
503 extension_loader = it->second->extension_loader(); 527 extension_loader = it->second->extension_loader();
504 if (extension_loader->IsCacheRunning()) { 528 if (extension_loader->IsCacheRunning()) {
505 DCHECK(!IsExtensionCacheDirectoryBusy(it->second->account_id())); 529 DCHECK(!IsExtensionCacheDirectoryBusy(it->second->account_id()));
506 busy_extension_cache_directories_.insert(it->second->account_id()); 530 busy_extension_cache_directories_.insert(it->second->account_id());
507 extension_loader->StopCache(base::Bind( 531 extension_loader->StopCache(base::Bind(
508 &DeviceLocalAccountPolicyService::OnObsoleteExtensionCacheShutdown, 532 &DeviceLocalAccountPolicyService::OnObsoleteExtensionCacheShutdown,
509 weak_factory_.GetWeakPtr(), 533 weak_factory_.GetWeakPtr(),
510 it->second->account_id())); 534 it->second->account_id()));
511 } 535 }
512 FOR_EACH_OBSERVER(Observer, observers_, OnBrokerShutdown(it->second)); 536
513 delete it->second; 537 delete it->second;
514 } 538 }
515 map->clear(); 539 map->clear();
516 } 540 }
517 541
518 DeviceLocalAccountPolicyBroker* 542 DeviceLocalAccountPolicyBroker*
519 DeviceLocalAccountPolicyService::GetBrokerForStore( 543 DeviceLocalAccountPolicyService::GetBrokerForStore(
520 CloudPolicyStore* store) { 544 CloudPolicyStore* store) {
521 for (PolicyBrokerMap::iterator it(policy_brokers_.begin()); 545 for (PolicyBrokerMap::iterator it(policy_brokers_.begin());
522 it != policy_brokers_.end(); ++it) { 546 it != policy_brokers_.end(); ++it) {
523 if (it->second->core()->store() == store) 547 if (it->second->core()->store() == store)
524 return it->second; 548 return it->second;
525 } 549 }
526 return NULL; 550 return NULL;
527 } 551 }
528 552
553 void DeviceLocalAccountPolicyService::NotifyPolicyUpdated(
554 const std::string& user_id) {
555 FOR_EACH_OBSERVER(Observer, observers_, OnPolicyUpdated(user_id));
556 }
557
529 } // namespace policy 558 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698