OLD | NEW |
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 Loading... |
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); |
| 142 |
| 143 // Unblock the |schema_registry_| so that the |component_policy_service_| |
| 144 // starts using it. |
| 145 schema_registry_.SetReady(POLICY_DOMAIN_CHROME); |
| 146 schema_registry_.SetReady(POLICY_DOMAIN_EXTENSIONS); |
134 } | 147 } |
135 | 148 |
136 DeviceLocalAccountPolicyBroker::~DeviceLocalAccountPolicyBroker() { | 149 DeviceLocalAccountPolicyBroker::~DeviceLocalAccountPolicyBroker() { |
| 150 store_->RemoveObserver(this); |
137 external_data_manager_->SetPolicyStore(NULL); | 151 external_data_manager_->SetPolicyStore(NULL); |
138 external_data_manager_->Disconnect(); | 152 external_data_manager_->Disconnect(); |
139 } | 153 } |
140 | 154 |
141 void DeviceLocalAccountPolicyBroker::Initialize() { | 155 void DeviceLocalAccountPolicyBroker::Initialize() { |
142 store_->Load(); | 156 store_->Load(); |
143 } | 157 } |
144 | 158 |
145 void DeviceLocalAccountPolicyBroker::ConnectIfPossible( | 159 void DeviceLocalAccountPolicyBroker::ConnectIfPossible( |
146 chromeos::DeviceSettingsService* device_settings_service, | 160 chromeos::DeviceSettingsService* device_settings_service, |
147 DeviceManagementService* device_management_service, | 161 DeviceManagementService* device_management_service, |
148 scoped_refptr<net::URLRequestContextGetter> request_context) { | 162 scoped_refptr<net::URLRequestContextGetter> request_context) { |
149 if (core_.client()) | 163 if (core_.client()) |
150 return; | 164 return; |
151 | 165 |
152 scoped_ptr<CloudPolicyClient> client(CreateClient(device_settings_service, | 166 scoped_ptr<CloudPolicyClient> client(CreateClient(device_settings_service, |
153 device_management_service, | 167 device_management_service, |
154 request_context)); | 168 request_context)); |
155 if (!client) | 169 if (!client) |
156 return; | 170 return; |
157 | 171 |
158 core_.Connect(client.Pass()); | 172 core_.Connect(client.Pass()); |
159 external_data_manager_->Connect(request_context); | 173 external_data_manager_->Connect(request_context); |
160 core_.StartRefreshScheduler(); | 174 core_.StartRefreshScheduler(); |
161 UpdateRefreshDelay(); | 175 UpdateRefreshDelay(); |
| 176 CreateComponentCloudPolicyService(request_context); |
162 } | 177 } |
163 | 178 |
164 void DeviceLocalAccountPolicyBroker::UpdateRefreshDelay() { | 179 void DeviceLocalAccountPolicyBroker::UpdateRefreshDelay() { |
165 if (core_.refresh_scheduler()) { | 180 if (core_.refresh_scheduler()) { |
166 const base::Value* policy_value = | 181 const base::Value* policy_value = |
167 store_->policy_map().GetValue(key::kPolicyRefreshRate); | 182 store_->policy_map().GetValue(key::kPolicyRefreshRate); |
168 int delay = 0; | 183 int delay = 0; |
169 if (policy_value && policy_value->GetAsInteger(&delay)) | 184 if (policy_value && policy_value->GetAsInteger(&delay)) |
170 core_.refresh_scheduler()->SetRefreshDelay(delay); | 185 core_.refresh_scheduler()->SetRefreshDelay(delay); |
171 } | 186 } |
172 } | 187 } |
173 | 188 |
174 std::string DeviceLocalAccountPolicyBroker::GetDisplayName() const { | 189 std::string DeviceLocalAccountPolicyBroker::GetDisplayName() const { |
175 std::string display_name; | 190 std::string display_name; |
176 const base::Value* display_name_value = | 191 const base::Value* display_name_value = |
177 store_->policy_map().GetValue(policy::key::kUserDisplayName); | 192 store_->policy_map().GetValue(policy::key::kUserDisplayName); |
178 if (display_name_value) | 193 if (display_name_value) |
179 display_name_value->GetAsString(&display_name); | 194 display_name_value->GetAsString(&display_name); |
180 return display_name; | 195 return display_name; |
181 } | 196 } |
182 | 197 |
183 base::FilePath DeviceLocalAccountPolicyBroker::GetComponentPolicyCachePath() | 198 void DeviceLocalAccountPolicyBroker::OnStoreLoaded(CloudPolicyStore* store) { |
184 const { | 199 UpdateRefreshDelay(); |
185 return component_policy_cache_path_; | 200 policy_update_callback_.Run(); |
| 201 } |
| 202 |
| 203 void DeviceLocalAccountPolicyBroker::OnStoreError(CloudPolicyStore* store) { |
| 204 policy_update_callback_.Run(); |
| 205 } |
| 206 |
| 207 void DeviceLocalAccountPolicyBroker::OnComponentCloudPolicyUpdated() { |
| 208 policy_update_callback_.Run(); |
| 209 } |
| 210 |
| 211 void DeviceLocalAccountPolicyBroker::CreateComponentCloudPolicyService( |
| 212 const scoped_refptr<net::URLRequestContextGetter>& request_context) { |
| 213 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 214 switches::kDisableComponentCloudPolicy)) { |
| 215 // Disabled via the command line. |
| 216 return; |
| 217 } |
| 218 |
| 219 scoped_ptr<ResourceCache> resource_cache( |
| 220 new ResourceCache(component_policy_cache_path_, |
| 221 content::BrowserThread::GetMessageLoopProxyForThread( |
| 222 content::BrowserThread::FILE))); |
| 223 |
| 224 component_policy_service_.reset(new ComponentCloudPolicyService( |
| 225 this, |
| 226 &schema_registry_, |
| 227 core(), |
| 228 resource_cache.Pass(), |
| 229 request_context, |
| 230 content::BrowserThread::GetMessageLoopProxyForThread( |
| 231 content::BrowserThread::FILE), |
| 232 content::BrowserThread::GetMessageLoopProxyForThread( |
| 233 content::BrowserThread::IO))); |
186 } | 234 } |
187 | 235 |
188 DeviceLocalAccountPolicyService::DeviceLocalAccountPolicyService( | 236 DeviceLocalAccountPolicyService::DeviceLocalAccountPolicyService( |
189 chromeos::SessionManagerClient* session_manager_client, | 237 chromeos::SessionManagerClient* session_manager_client, |
190 chromeos::DeviceSettingsService* device_settings_service, | 238 chromeos::DeviceSettingsService* device_settings_service, |
191 chromeos::CrosSettings* cros_settings, | 239 chromeos::CrosSettings* cros_settings, |
192 scoped_refptr<base::SequencedTaskRunner> store_background_task_runner, | 240 scoped_refptr<base::SequencedTaskRunner> store_background_task_runner, |
193 scoped_refptr<base::SequencedTaskRunner> extension_cache_task_runner, | 241 scoped_refptr<base::SequencedTaskRunner> extension_cache_task_runner, |
194 scoped_refptr<base::SequencedTaskRunner> | 242 scoped_refptr<base::SequencedTaskRunner> |
195 external_data_service_backend_task_runner, | 243 external_data_service_backend_task_runner, |
196 scoped_refptr<base::SequencedTaskRunner> io_task_runner, | 244 scoped_refptr<base::SequencedTaskRunner> io_task_runner, |
197 scoped_refptr<net::URLRequestContextGetter> request_context) | 245 scoped_refptr<net::URLRequestContextGetter> request_context) |
198 : session_manager_client_(session_manager_client), | 246 : session_manager_client_(session_manager_client), |
199 device_settings_service_(device_settings_service), | 247 device_settings_service_(device_settings_service), |
200 cros_settings_(cros_settings), | 248 cros_settings_(cros_settings), |
201 device_management_service_(NULL), | 249 device_management_service_(NULL), |
202 waiting_for_cros_settings_(false), | 250 waiting_for_cros_settings_(false), |
203 orphan_cache_deletion_state_(NOT_STARTED), | 251 orphan_extension_cache_deletion_state_(NOT_STARTED), |
204 store_background_task_runner_(store_background_task_runner), | 252 store_background_task_runner_(store_background_task_runner), |
205 extension_cache_task_runner_(extension_cache_task_runner), | 253 extension_cache_task_runner_(extension_cache_task_runner), |
206 request_context_(request_context), | 254 request_context_(request_context), |
207 local_accounts_subscription_(cros_settings_->AddSettingsObserver( | 255 local_accounts_subscription_(cros_settings_->AddSettingsObserver( |
208 chromeos::kAccountsPrefDeviceLocalAccounts, | 256 chromeos::kAccountsPrefDeviceLocalAccounts, |
209 base::Bind(&DeviceLocalAccountPolicyService:: | 257 base::Bind(&DeviceLocalAccountPolicyService:: |
210 UpdateAccountListIfNonePending, | 258 UpdateAccountListIfNonePending, |
211 base::Unretained(this)))), | 259 base::Unretained(this)))), |
212 weak_factory_(this) { | 260 weak_factory_(this) { |
213 CHECK(PathService::Get(chromeos::DIR_DEVICE_LOCAL_ACCOUNT_COMPONENT_POLICY, | 261 CHECK(PathService::Get(chromeos::DIR_DEVICE_LOCAL_ACCOUNT_COMPONENT_POLICY, |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 | 301 |
254 return entry->second; | 302 return entry->second; |
255 } | 303 } |
256 | 304 |
257 bool DeviceLocalAccountPolicyService::IsPolicyAvailableForUser( | 305 bool DeviceLocalAccountPolicyService::IsPolicyAvailableForUser( |
258 const std::string& user_id) { | 306 const std::string& user_id) { |
259 DeviceLocalAccountPolicyBroker* broker = GetBrokerForUser(user_id); | 307 DeviceLocalAccountPolicyBroker* broker = GetBrokerForUser(user_id); |
260 return broker && broker->core()->store()->is_managed(); | 308 return broker && broker->core()->store()->is_managed(); |
261 } | 309 } |
262 | 310 |
263 scoped_refptr<net::URLRequestContextGetter> | |
264 DeviceLocalAccountPolicyService::request_context() const { | |
265 return request_context_; | |
266 } | |
267 | |
268 void DeviceLocalAccountPolicyService::AddObserver(Observer* observer) { | 311 void DeviceLocalAccountPolicyService::AddObserver(Observer* observer) { |
269 observers_.AddObserver(observer); | 312 observers_.AddObserver(observer); |
270 } | 313 } |
271 | 314 |
272 void DeviceLocalAccountPolicyService::RemoveObserver(Observer* observer) { | 315 void DeviceLocalAccountPolicyService::RemoveObserver(Observer* observer) { |
273 observers_.RemoveObserver(observer); | 316 observers_.RemoveObserver(observer); |
274 } | 317 } |
275 | 318 |
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( | 319 bool DeviceLocalAccountPolicyService::IsExtensionCacheDirectoryBusy( |
294 const std::string& account_id) { | 320 const std::string& account_id) { |
295 return busy_extension_cache_directories_.find(account_id) != | 321 return busy_extension_cache_directories_.find(account_id) != |
296 busy_extension_cache_directories_.end(); | 322 busy_extension_cache_directories_.end(); |
297 } | 323 } |
298 | 324 |
299 void DeviceLocalAccountPolicyService::StartExtensionCachesIfPossible() { | 325 void DeviceLocalAccountPolicyService::StartExtensionCachesIfPossible() { |
300 for (PolicyBrokerMap::iterator it = policy_brokers_.begin(); | 326 for (PolicyBrokerMap::iterator it = policy_brokers_.begin(); |
301 it != policy_brokers_.end(); ++it) { | 327 it != policy_brokers_.end(); ++it) { |
302 if (!it->second->extension_loader()->IsCacheRunning() && | 328 if (!it->second->extension_loader()->IsCacheRunning() && |
(...skipping 10 matching lines...) Expand all Loading... |
313 if (it->second->account_id() == account_id) { | 339 if (it->second->account_id() == account_id) { |
314 DCHECK(!it->second->extension_loader()->IsCacheRunning()); | 340 DCHECK(!it->second->extension_loader()->IsCacheRunning()); |
315 it->second->extension_loader()->StartCache(extension_cache_task_runner_); | 341 it->second->extension_loader()->StartCache(extension_cache_task_runner_); |
316 return true; | 342 return true; |
317 } | 343 } |
318 } | 344 } |
319 return false; | 345 return false; |
320 } | 346 } |
321 | 347 |
322 void DeviceLocalAccountPolicyService::OnOrphanedExtensionCachesDeleted() { | 348 void DeviceLocalAccountPolicyService::OnOrphanedExtensionCachesDeleted() { |
323 DCHECK_EQ(IN_PROGRESS, orphan_cache_deletion_state_); | 349 DCHECK_EQ(IN_PROGRESS, orphan_extension_cache_deletion_state_); |
324 | 350 |
325 orphan_cache_deletion_state_ = DONE; | 351 orphan_extension_cache_deletion_state_ = DONE; |
326 StartExtensionCachesIfPossible(); | 352 StartExtensionCachesIfPossible(); |
327 } | 353 } |
328 | 354 |
329 void DeviceLocalAccountPolicyService::OnObsoleteExtensionCacheShutdown( | 355 void DeviceLocalAccountPolicyService::OnObsoleteExtensionCacheShutdown( |
330 const std::string& account_id) { | 356 const std::string& account_id) { |
331 DCHECK_NE(NOT_STARTED, orphan_cache_deletion_state_); | 357 DCHECK_NE(NOT_STARTED, orphan_extension_cache_deletion_state_); |
332 DCHECK(IsExtensionCacheDirectoryBusy(account_id)); | 358 DCHECK(IsExtensionCacheDirectoryBusy(account_id)); |
333 | 359 |
334 // The account with |account_id| was deleted and the broker for it has shut | 360 // The account with |account_id| was deleted and the broker for it has shut |
335 // down completely. | 361 // down completely. |
336 | 362 |
337 if (StartExtensionCacheForAccountIfPresent(account_id)) { | 363 if (StartExtensionCacheForAccountIfPresent(account_id)) { |
338 // If another account with the same ID was created in the meantime, its | 364 // 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 | 365 // extension cache is started, reusing the cache directory. The directory no |
340 // longer needs to be marked as busy in this case. | 366 // longer needs to be marked as busy in this case. |
341 busy_extension_cache_directories_.erase(account_id); | 367 busy_extension_cache_directories_.erase(account_id); |
342 return; | 368 return; |
343 } | 369 } |
344 | 370 |
345 // If no account with |account_id| exists anymore, the cache directory should | 371 // 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 | 372 // be removed. The directory must stay marked as busy while the removal is in |
347 // progress. | 373 // progress. |
348 extension_cache_task_runner_->PostTaskAndReply( | 374 extension_cache_task_runner_->PostTaskAndReply( |
349 FROM_HERE, | 375 FROM_HERE, |
350 base::Bind(&DeleteObsoleteExtensionCache, account_id), | 376 base::Bind(&DeleteObsoleteExtensionCache, account_id), |
351 base::Bind(&DeviceLocalAccountPolicyService:: | 377 base::Bind(&DeviceLocalAccountPolicyService:: |
352 OnObsoleteExtensionCacheDeleted, | 378 OnObsoleteExtensionCacheDeleted, |
353 weak_factory_.GetWeakPtr(), | 379 weak_factory_.GetWeakPtr(), |
354 account_id)); | 380 account_id)); |
355 } | 381 } |
356 | 382 |
357 void DeviceLocalAccountPolicyService::OnObsoleteExtensionCacheDeleted( | 383 void DeviceLocalAccountPolicyService::OnObsoleteExtensionCacheDeleted( |
358 const std::string& account_id) { | 384 const std::string& account_id) { |
359 DCHECK_EQ(DONE, orphan_cache_deletion_state_); | 385 DCHECK_EQ(DONE, orphan_extension_cache_deletion_state_); |
360 DCHECK(IsExtensionCacheDirectoryBusy(account_id)); | 386 DCHECK(IsExtensionCacheDirectoryBusy(account_id)); |
361 | 387 |
362 // The cache directory for |account_id| has been deleted. The directory no | 388 // The cache directory for |account_id| has been deleted. The directory no |
363 // longer needs to be marked as busy. | 389 // longer needs to be marked as busy. |
364 busy_extension_cache_directories_.erase(account_id); | 390 busy_extension_cache_directories_.erase(account_id); |
365 | 391 |
366 // If another account with the same ID was created in the meantime, start its | 392 // If another account with the same ID was created in the meantime, start its |
367 // extension cache, creating a new cache directory. | 393 // extension cache, creating a new cache directory. |
368 StartExtensionCacheForAccountIfPresent(account_id); | 394 StartExtensionCacheForAccountIfPresent(account_id); |
369 } | 395 } |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 // Reuse the existing broker if present. | 436 // Reuse the existing broker if present. |
411 broker.reset(broker_it->second); | 437 broker.reset(broker_it->second); |
412 old_policy_brokers.erase(broker_it); | 438 old_policy_brokers.erase(broker_it); |
413 broker_initialized = true; | 439 broker_initialized = true; |
414 } else { | 440 } else { |
415 scoped_ptr<DeviceLocalAccountPolicyStore> store( | 441 scoped_ptr<DeviceLocalAccountPolicyStore> store( |
416 new DeviceLocalAccountPolicyStore(it->account_id, | 442 new DeviceLocalAccountPolicyStore(it->account_id, |
417 session_manager_client_, | 443 session_manager_client_, |
418 device_settings_service_, | 444 device_settings_service_, |
419 store_background_task_runner_)); | 445 store_background_task_runner_)); |
420 store->AddObserver(this); | |
421 scoped_refptr<DeviceLocalAccountExternalDataManager> | 446 scoped_refptr<DeviceLocalAccountExternalDataManager> |
422 external_data_manager = | 447 external_data_manager = |
423 external_data_service_->GetExternalDataManager(it->account_id, | 448 external_data_service_->GetExternalDataManager(it->account_id, |
424 store.get()); | 449 store.get()); |
425 broker.reset(new DeviceLocalAccountPolicyBroker( | 450 broker.reset(new DeviceLocalAccountPolicyBroker( |
426 *it, | 451 *it, |
427 component_policy_cache_root_.Append(EncodeAccountId(it->account_id)), | 452 component_policy_cache_root_.Append( |
| 453 GetCacheSubdirectoryForAccountID(it->account_id)), |
428 store.Pass(), | 454 store.Pass(), |
429 external_data_manager, | 455 external_data_manager, |
| 456 base::Bind(&DeviceLocalAccountPolicyService::NotifyPolicyUpdated, |
| 457 base::Unretained(this), |
| 458 it->user_id), |
430 base::MessageLoopProxy::current())); | 459 base::MessageLoopProxy::current())); |
431 } | 460 } |
432 | 461 |
433 // Fire up the cloud connection for fetching policy for the account from | 462 // Fire up the cloud connection for fetching policy for the account from |
434 // the cloud if this is an enterprise-managed device. | 463 // the cloud if this is an enterprise-managed device. |
435 broker->ConnectIfPossible(device_settings_service_, | 464 broker->ConnectIfPossible(device_settings_service_, |
436 device_management_service_, | 465 device_management_service_, |
437 request_context_); | 466 request_context_); |
438 | 467 |
439 policy_brokers_[it->user_id] = broker.release(); | 468 policy_brokers_[it->user_id] = broker.release(); |
440 if (!broker_initialized) { | 469 if (!broker_initialized) { |
441 // The broker must be initialized after it has been added to | 470 // The broker must be initialized after it has been added to |
442 // |policy_brokers_|. | 471 // |policy_brokers_|. |
443 policy_brokers_[it->user_id]->Initialize(); | 472 policy_brokers_[it->user_id]->Initialize(); |
444 } | 473 } |
445 | 474 |
446 subdirectories_to_keep.insert(EncodeAccountId(it->account_id)); | 475 subdirectories_to_keep.insert( |
| 476 GetCacheSubdirectoryForAccountID(it->account_id)); |
447 } | 477 } |
448 | 478 |
449 if (orphan_cache_deletion_state_ == NOT_STARTED) { | 479 if (orphan_extension_cache_deletion_state_ == NOT_STARTED) { |
450 DCHECK(old_policy_brokers.empty()); | 480 DCHECK(old_policy_brokers.empty()); |
451 DCHECK(busy_extension_cache_directories_.empty()); | 481 DCHECK(busy_extension_cache_directories_.empty()); |
452 | 482 |
453 // If this method is running for the first time, no extension caches have | 483 // 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 | 484 // 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 | 485 // orphaned cache directories not found in |subdirectories_to_keep| from the |
456 // cache directory. | 486 // cache directory. |
457 orphan_cache_deletion_state_ = IN_PROGRESS; | 487 orphan_extension_cache_deletion_state_ = IN_PROGRESS; |
458 | 488 |
459 base::FilePath cache_root_dir; | 489 base::FilePath cache_root_dir; |
460 CHECK(PathService::Get(chromeos::DIR_DEVICE_LOCAL_ACCOUNT_EXTENSIONS, | 490 CHECK(PathService::Get(chromeos::DIR_DEVICE_LOCAL_ACCOUNT_EXTENSIONS, |
461 &cache_root_dir)); | 491 &cache_root_dir)); |
462 extension_cache_task_runner_->PostTaskAndReply( | 492 extension_cache_task_runner_->PostTaskAndReply( |
463 FROM_HERE, | 493 FROM_HERE, |
464 base::Bind( | 494 base::Bind( |
465 &DeleteOrphanedCaches, cache_root_dir, subdirectories_to_keep), | 495 &DeleteOrphanedCaches, cache_root_dir, subdirectories_to_keep), |
466 base::Bind( | 496 base::Bind( |
467 &DeviceLocalAccountPolicyService::OnOrphanedExtensionCachesDeleted, | 497 &DeviceLocalAccountPolicyService::OnOrphanedExtensionCachesDeleted, |
468 weak_factory_.GetWeakPtr())); | 498 weak_factory_.GetWeakPtr())); |
469 | 499 |
470 // Start the extension caches for all brokers. These belong to accounts in | 500 // Start the extension caches for all brokers. These belong to accounts in |
471 // |account_ids| and are not affected by the clean-up. | 501 // |account_ids| and are not affected by the clean-up. |
472 StartExtensionCachesIfPossible(); | 502 StartExtensionCachesIfPossible(); |
473 } else { | 503 } else { |
474 // If this method has run before, obsolete brokers may exist. Shut down | 504 // If this method has run before, obsolete brokers may exist. Shut down |
475 // their extension caches and delete the brokers. | 505 // their extension caches and delete the brokers. |
476 DeleteBrokers(&old_policy_brokers); | 506 DeleteBrokers(&old_policy_brokers); |
477 | 507 |
478 if (orphan_cache_deletion_state_ == DONE) { | 508 if (orphan_extension_cache_deletion_state_ == DONE) { |
479 // If the initial clean-up of orphaned cache directories has been | 509 // If the initial clean-up of orphaned cache directories has been |
480 // complete, start any extension caches that are not running yet but can | 510 // complete, start any extension caches that are not running yet but can |
481 // be started now because their cache directories are not busy. | 511 // be started now because their cache directories are not busy. |
482 StartExtensionCachesIfPossible(); | 512 StartExtensionCachesIfPossible(); |
483 } | 513 } |
484 } | 514 } |
485 | 515 |
486 // Purge the component policy caches of any accounts that have been removed. | 516 // Purge the component policy caches of any accounts that have been removed. |
487 // Do this only after any obsolete brokers have been destroyed. | 517 // Do this only after any obsolete brokers have been destroyed. |
488 // TODO(joaodasilva): for now this must be posted to the FILE thread, | 518 // TODO(joaodasilva): for now this must be posted to the FILE thread, |
489 // to avoid racing with the ComponentCloudPolicyStore. Use a task runner | 519 // to avoid racing with the ComponentCloudPolicyStore. Use a task runner |
490 // once that class supports another background thread too. | 520 // once that class supports another background thread too. |
491 content::BrowserThread::PostTask(content::BrowserThread::FILE, FROM_HERE, | 521 content::BrowserThread::PostTask(content::BrowserThread::FILE, FROM_HERE, |
492 base::Bind(&DeleteOrphanedCaches, | 522 base::Bind(&DeleteOrphanedCaches, |
493 component_policy_cache_root_, | 523 component_policy_cache_root_, |
494 subdirectories_to_keep)); | 524 subdirectories_to_keep)); |
495 | 525 |
496 FOR_EACH_OBSERVER(Observer, observers_, OnDeviceLocalAccountsChanged()); | 526 FOR_EACH_OBSERVER(Observer, observers_, OnDeviceLocalAccountsChanged()); |
497 } | 527 } |
498 | 528 |
499 void DeviceLocalAccountPolicyService::DeleteBrokers(PolicyBrokerMap* map) { | 529 void DeviceLocalAccountPolicyService::DeleteBrokers(PolicyBrokerMap* map) { |
500 for (PolicyBrokerMap::iterator it = map->begin(); it != map->end(); ++it) { | 530 for (PolicyBrokerMap::iterator it = map->begin(); it != map->end(); ++it) { |
501 it->second->core()->store()->RemoveObserver(this); | |
502 scoped_refptr<chromeos::DeviceLocalAccountExternalPolicyLoader> | 531 scoped_refptr<chromeos::DeviceLocalAccountExternalPolicyLoader> |
503 extension_loader = it->second->extension_loader(); | 532 extension_loader = it->second->extension_loader(); |
504 if (extension_loader->IsCacheRunning()) { | 533 if (extension_loader->IsCacheRunning()) { |
505 DCHECK(!IsExtensionCacheDirectoryBusy(it->second->account_id())); | 534 DCHECK(!IsExtensionCacheDirectoryBusy(it->second->account_id())); |
506 busy_extension_cache_directories_.insert(it->second->account_id()); | 535 busy_extension_cache_directories_.insert(it->second->account_id()); |
507 extension_loader->StopCache(base::Bind( | 536 extension_loader->StopCache(base::Bind( |
508 &DeviceLocalAccountPolicyService::OnObsoleteExtensionCacheShutdown, | 537 &DeviceLocalAccountPolicyService::OnObsoleteExtensionCacheShutdown, |
509 weak_factory_.GetWeakPtr(), | 538 weak_factory_.GetWeakPtr(), |
510 it->second->account_id())); | 539 it->second->account_id())); |
511 } | 540 } |
512 FOR_EACH_OBSERVER(Observer, observers_, OnBrokerShutdown(it->second)); | 541 |
513 delete it->second; | 542 delete it->second; |
514 } | 543 } |
515 map->clear(); | 544 map->clear(); |
516 } | 545 } |
517 | 546 |
518 DeviceLocalAccountPolicyBroker* | 547 DeviceLocalAccountPolicyBroker* |
519 DeviceLocalAccountPolicyService::GetBrokerForStore( | 548 DeviceLocalAccountPolicyService::GetBrokerForStore( |
520 CloudPolicyStore* store) { | 549 CloudPolicyStore* store) { |
521 for (PolicyBrokerMap::iterator it(policy_brokers_.begin()); | 550 for (PolicyBrokerMap::iterator it(policy_brokers_.begin()); |
522 it != policy_brokers_.end(); ++it) { | 551 it != policy_brokers_.end(); ++it) { |
523 if (it->second->core()->store() == store) | 552 if (it->second->core()->store() == store) |
524 return it->second; | 553 return it->second; |
525 } | 554 } |
526 return NULL; | 555 return NULL; |
527 } | 556 } |
528 | 557 |
| 558 void DeviceLocalAccountPolicyService::NotifyPolicyUpdated( |
| 559 const std::string& user_id) { |
| 560 FOR_EACH_OBSERVER(Observer, observers_, OnPolicyUpdated(user_id)); |
| 561 } |
| 562 |
529 } // namespace policy | 563 } // namespace policy |
OLD | NEW |