| 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/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/files/file_enumerator.h" | 11 #include "base/files/file_enumerator.h" |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/message_loop/message_loop_proxy.h" | 15 #include "base/message_loop/message_loop_proxy.h" |
| 16 #include "base/path_service.h" | 16 #include "base/path_service.h" |
| 17 #include "base/sequenced_task_runner.h" | 17 #include "base/sequenced_task_runner.h" |
| 18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 19 #include "chrome/browser/chromeos/policy/device_local_account.h" | 19 #include "chrome/browser/chromeos/policy/device_local_account.h" |
| 20 #include "chrome/browser/chromeos/policy/device_local_account_external_data_serv
ice.h" | |
| 21 #include "chrome/browser/chromeos/policy/device_local_account_policy_store.h" | 20 #include "chrome/browser/chromeos/policy/device_local_account_policy_store.h" |
| 22 #include "chrome/browser/chromeos/settings/device_settings_service.h" | 21 #include "chrome/browser/chromeos/settings/device_settings_service.h" |
| 23 #include "chrome/browser/policy/cloud/cloud_policy_client.h" | 22 #include "chrome/browser/policy/cloud/cloud_policy_client.h" |
| 24 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" | 23 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" |
| 25 #include "chrome/browser/policy/cloud/cloud_policy_refresh_scheduler.h" | 24 #include "chrome/browser/policy/cloud/cloud_policy_refresh_scheduler.h" |
| 26 #include "chrome/browser/policy/cloud/device_management_service.h" | 25 #include "chrome/browser/policy/cloud/device_management_service.h" |
| 27 #include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h" | 26 #include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h" |
| 28 #include "chromeos/chromeos_paths.h" | 27 #include "chromeos/chromeos_paths.h" |
| 29 #include "chromeos/dbus/session_manager_client.h" | 28 #include "chromeos/dbus/session_manager_client.h" |
| 30 #include "chromeos/settings/cros_settings_names.h" | 29 #include "chromeos/settings/cros_settings_names.h" |
| 31 #include "chromeos/settings/cros_settings_provider.h" | 30 #include "chromeos/settings/cros_settings_provider.h" |
| 32 #include "net/url_request/url_request_context_getter.h" | |
| 33 #include "policy/policy_constants.h" | 31 #include "policy/policy_constants.h" |
| 34 | 32 |
| 35 namespace em = enterprise_management; | 33 namespace em = enterprise_management; |
| 36 | 34 |
| 37 namespace policy { | 35 namespace policy { |
| 38 | 36 |
| 39 namespace { | 37 namespace { |
| 40 | 38 |
| 41 // Creates and initializes a cloud policy client. Returns NULL if the device | 39 // Creates and initializes a cloud policy client. Returns NULL if the device |
| 42 // doesn't have credentials in device settings (i.e. is not | 40 // doesn't have credentials in device settings (i.e. is not |
| (...skipping 23 matching lines...) Expand all Loading... |
| 66 std::string GetCacheSubdirectoryForAccountID(const std::string& account_id) { | 64 std::string GetCacheSubdirectoryForAccountID(const std::string& account_id) { |
| 67 return base::HexEncode(account_id.c_str(), account_id.size()); | 65 return base::HexEncode(account_id.c_str(), account_id.size()); |
| 68 } | 66 } |
| 69 | 67 |
| 70 // Cleans up the cache directory by removing subdirectories that are not found | 68 // Cleans up the cache directory by removing subdirectories that are not found |
| 71 // in |subdirectories_to_keep|. Only caches whose cache directory is found in | 69 // in |subdirectories_to_keep|. Only caches whose cache directory is found in |
| 72 // |subdirectories_to_keep| may be running while the clean-up is in progress. | 70 // |subdirectories_to_keep| may be running while the clean-up is in progress. |
| 73 void DeleteOrphanedExtensionCaches( | 71 void DeleteOrphanedExtensionCaches( |
| 74 const std::set<std::string>& subdirectories_to_keep) { | 72 const std::set<std::string>& subdirectories_to_keep) { |
| 75 base::FilePath cache_root_dir; | 73 base::FilePath cache_root_dir; |
| 76 CHECK(PathService::Get(chromeos::DIR_DEVICE_LOCAL_ACCOUNT_EXTENSIONS, | 74 CHECK(PathService::Get(chromeos::DIR_DEVICE_LOCAL_ACCOUNT_CACHE, |
| 77 &cache_root_dir)); | 75 &cache_root_dir)); |
| 78 base::FileEnumerator enumerator(cache_root_dir, | 76 base::FileEnumerator enumerator(cache_root_dir, |
| 79 false, | 77 false, |
| 80 base::FileEnumerator::DIRECTORIES); | 78 base::FileEnumerator::DIRECTORIES); |
| 81 for (base::FilePath path = enumerator.Next(); !path.empty(); | 79 for (base::FilePath path = enumerator.Next(); !path.empty(); |
| 82 path = enumerator.Next()) { | 80 path = enumerator.Next()) { |
| 83 const std::string subdirectory(path.BaseName().MaybeAsASCII()); | 81 const std::string subdirectory(path.BaseName().MaybeAsASCII()); |
| 84 if (subdirectories_to_keep.find(subdirectory) == | 82 if (subdirectories_to_keep.find(subdirectory) == |
| 85 subdirectories_to_keep.end()) { | 83 subdirectories_to_keep.end()) { |
| 86 base::DeleteFile(path, true); | 84 base::DeleteFile(path, true); |
| 87 } | 85 } |
| 88 } | 86 } |
| 89 } | 87 } |
| 90 | 88 |
| 91 // Removes the subdirectory belonging to |account_id_to_delete| from the cache | 89 // Removes the subdirectory belonging to |account_id_to_delete| from the cache |
| 92 // directory. No cache belonging to |account_id_to_delete| may be running while | 90 // directory. No cache belonging to |account_id_to_delete| may be running while |
| 93 // the removal is in progress. | 91 // the removal is in progress. |
| 94 void DeleteObsoleteExtensionCache(const std::string& account_id_to_delete) { | 92 void DeleteObsoleteExtensionCache(const std::string& account_id_to_delete) { |
| 95 base::FilePath cache_root_dir; | 93 base::FilePath cache_root_dir; |
| 96 CHECK(PathService::Get(chromeos::DIR_DEVICE_LOCAL_ACCOUNT_EXTENSIONS, | 94 CHECK(PathService::Get(chromeos::DIR_DEVICE_LOCAL_ACCOUNT_CACHE, |
| 97 &cache_root_dir)); | 95 &cache_root_dir)); |
| 98 const base::FilePath path = cache_root_dir | 96 const base::FilePath path = cache_root_dir |
| 99 .Append(GetCacheSubdirectoryForAccountID(account_id_to_delete)); | 97 .Append(GetCacheSubdirectoryForAccountID(account_id_to_delete)); |
| 100 if (base::DirectoryExists(path)) | 98 if (base::DirectoryExists(path)) |
| 101 base::DeleteFile(path, true); | 99 base::DeleteFile(path, true); |
| 102 } | 100 } |
| 103 | 101 |
| 104 } // namespace | 102 } // namespace |
| 105 | 103 |
| 106 DeviceLocalAccountPolicyBroker::DeviceLocalAccountPolicyBroker( | 104 DeviceLocalAccountPolicyBroker::DeviceLocalAccountPolicyBroker( |
| 107 const DeviceLocalAccount& account, | 105 const DeviceLocalAccount& account, |
| 108 scoped_ptr<DeviceLocalAccountPolicyStore> store, | 106 scoped_ptr<DeviceLocalAccountPolicyStore> store, |
| 109 scoped_refptr<DeviceLocalAccountExternalDataManager> external_data_manager, | |
| 110 const scoped_refptr<base::SequencedTaskRunner>& task_runner) | 107 const scoped_refptr<base::SequencedTaskRunner>& task_runner) |
| 111 : account_id_(account.account_id), | 108 : account_id_(account.account_id), |
| 112 user_id_(account.user_id), | 109 user_id_(account.user_id), |
| 113 store_(store.Pass()), | 110 store_(store.Pass()), |
| 114 external_data_manager_(external_data_manager), | |
| 115 core_(PolicyNamespaceKey(dm_protocol::kChromePublicAccountPolicyType, | 111 core_(PolicyNamespaceKey(dm_protocol::kChromePublicAccountPolicyType, |
| 116 store_->account_id()), | 112 store_->account_id()), |
| 117 store_.get(), | 113 store_.get(), |
| 118 task_runner) { | 114 task_runner) { |
| 119 base::FilePath cache_root_dir; | 115 base::FilePath cache_root_dir; |
| 120 CHECK(PathService::Get(chromeos::DIR_DEVICE_LOCAL_ACCOUNT_EXTENSIONS, | 116 CHECK(PathService::Get(chromeos::DIR_DEVICE_LOCAL_ACCOUNT_CACHE, |
| 121 &cache_root_dir)); | 117 &cache_root_dir)); |
| 122 extension_loader_ = new chromeos::DeviceLocalAccountExternalPolicyLoader( | 118 extension_loader_ = new chromeos::DeviceLocalAccountExternalPolicyLoader( |
| 123 store_.get(), | 119 store_.get(), |
| 124 cache_root_dir.Append( | 120 cache_root_dir.Append( |
| 125 GetCacheSubdirectoryForAccountID(account.account_id))); | 121 GetCacheSubdirectoryForAccountID(account.account_id))); |
| 126 } | 122 } |
| 127 | 123 |
| 128 DeviceLocalAccountPolicyBroker::~DeviceLocalAccountPolicyBroker() { | 124 DeviceLocalAccountPolicyBroker::~DeviceLocalAccountPolicyBroker() { |
| 129 external_data_manager_->SetPolicyStore(NULL); | |
| 130 external_data_manager_->Disconnect(); | |
| 131 } | 125 } |
| 132 | 126 |
| 133 void DeviceLocalAccountPolicyBroker::Initialize() { | 127 void DeviceLocalAccountPolicyBroker::Initialize() { |
| 134 store_->Load(); | 128 store_->Load(); |
| 135 } | 129 } |
| 136 | 130 |
| 137 void DeviceLocalAccountPolicyBroker::ConnectIfPossible( | 131 void DeviceLocalAccountPolicyBroker::ConnectIfPossible( |
| 138 chromeos::DeviceSettingsService* device_settings_service, | 132 chromeos::DeviceSettingsService* device_settings_service, |
| 139 DeviceManagementService* device_management_service, | 133 DeviceManagementService* device_management_service) { |
| 140 scoped_refptr<net::URLRequestContextGetter> request_context) { | |
| 141 if (core_.client()) | 134 if (core_.client()) |
| 142 return; | 135 return; |
| 143 | 136 |
| 144 scoped_ptr<CloudPolicyClient> client(CreateClient(device_settings_service, | 137 scoped_ptr<CloudPolicyClient> client(CreateClient(device_settings_service, |
| 145 device_management_service)); | 138 device_management_service)); |
| 146 if (!client) | 139 if (!client) |
| 147 return; | 140 return; |
| 148 | 141 |
| 149 core_.Connect(client.Pass()); | 142 core_.Connect(client.Pass()); |
| 150 external_data_manager_->Connect(request_context); | |
| 151 core_.StartRefreshScheduler(); | 143 core_.StartRefreshScheduler(); |
| 152 UpdateRefreshDelay(); | 144 UpdateRefreshDelay(); |
| 153 } | 145 } |
| 154 | 146 |
| 147 void DeviceLocalAccountPolicyBroker::Disconnect() { |
| 148 core_.Disconnect(); |
| 149 } |
| 150 |
| 155 void DeviceLocalAccountPolicyBroker::UpdateRefreshDelay() { | 151 void DeviceLocalAccountPolicyBroker::UpdateRefreshDelay() { |
| 156 if (core_.refresh_scheduler()) { | 152 if (core_.refresh_scheduler()) { |
| 157 const Value* policy_value = | 153 const Value* policy_value = |
| 158 store_->policy_map().GetValue(key::kPolicyRefreshRate); | 154 store_->policy_map().GetValue(key::kPolicyRefreshRate); |
| 159 int delay = 0; | 155 int delay = 0; |
| 160 if (policy_value && policy_value->GetAsInteger(&delay)) | 156 if (policy_value && policy_value->GetAsInteger(&delay)) |
| 161 core_.refresh_scheduler()->SetRefreshDelay(delay); | 157 core_.refresh_scheduler()->SetRefreshDelay(delay); |
| 162 } | 158 } |
| 163 } | 159 } |
| 164 | 160 |
| 165 std::string DeviceLocalAccountPolicyBroker::GetDisplayName() const { | 161 std::string DeviceLocalAccountPolicyBroker::GetDisplayName() const { |
| 166 std::string display_name; | 162 std::string display_name; |
| 167 const base::Value* display_name_value = | 163 const base::Value* display_name_value = |
| 168 store_->policy_map().GetValue(policy::key::kUserDisplayName); | 164 store_->policy_map().GetValue(policy::key::kUserDisplayName); |
| 169 if (display_name_value) | 165 if (display_name_value) |
| 170 display_name_value->GetAsString(&display_name); | 166 display_name_value->GetAsString(&display_name); |
| 171 return display_name; | 167 return display_name; |
| 172 } | 168 } |
| 173 | 169 |
| 174 DeviceLocalAccountPolicyService::DeviceLocalAccountPolicyService( | 170 DeviceLocalAccountPolicyService::DeviceLocalAccountPolicyService( |
| 175 chromeos::SessionManagerClient* session_manager_client, | 171 chromeos::SessionManagerClient* session_manager_client, |
| 176 chromeos::DeviceSettingsService* device_settings_service, | 172 chromeos::DeviceSettingsService* device_settings_service, |
| 177 chromeos::CrosSettings* cros_settings, | 173 chromeos::CrosSettings* cros_settings, |
| 178 scoped_refptr<base::SequencedTaskRunner> store_background_task_runner, | 174 scoped_refptr<base::SequencedTaskRunner> store_background_task_runner, |
| 179 scoped_refptr<base::SequencedTaskRunner> extension_cache_task_runner, | 175 scoped_refptr<base::SequencedTaskRunner> extension_cache_task_runner) |
| 180 scoped_refptr<base::SequencedTaskRunner> | |
| 181 external_data_service_backend_task_runner, | |
| 182 scoped_refptr<base::SequencedTaskRunner> io_task_runner, | |
| 183 scoped_refptr<net::URLRequestContextGetter> request_context) | |
| 184 : session_manager_client_(session_manager_client), | 176 : session_manager_client_(session_manager_client), |
| 185 device_settings_service_(device_settings_service), | 177 device_settings_service_(device_settings_service), |
| 186 cros_settings_(cros_settings), | 178 cros_settings_(cros_settings), |
| 187 device_management_service_(NULL), | 179 device_management_service_(NULL), |
| 188 waiting_for_cros_settings_(false), | 180 waiting_for_cros_settings_(false), |
| 189 orphan_cache_deletion_state_(NOT_STARTED), | 181 orphan_cache_deletion_state_(NOT_STARTED), |
| 190 store_background_task_runner_(store_background_task_runner), | 182 store_background_task_runner_(store_background_task_runner), |
| 191 extension_cache_task_runner_(extension_cache_task_runner), | 183 extension_cache_task_runner_(extension_cache_task_runner), |
| 192 request_context_(request_context), | |
| 193 local_accounts_subscription_(cros_settings_->AddSettingsObserver( | 184 local_accounts_subscription_(cros_settings_->AddSettingsObserver( |
| 194 chromeos::kAccountsPrefDeviceLocalAccounts, | 185 chromeos::kAccountsPrefDeviceLocalAccounts, |
| 195 base::Bind(&DeviceLocalAccountPolicyService:: | 186 base::Bind(&DeviceLocalAccountPolicyService:: |
| 196 UpdateAccountListIfNonePending, | 187 UpdateAccountListIfNonePending, |
| 197 base::Unretained(this)))), | 188 base::Unretained(this)))), |
| 198 weak_factory_(this) { | 189 weak_factory_(this) { |
| 199 external_data_service_.reset(new DeviceLocalAccountExternalDataService( | |
| 200 this, | |
| 201 external_data_service_backend_task_runner, | |
| 202 io_task_runner)); | |
| 203 UpdateAccountList(); | 190 UpdateAccountList(); |
| 204 } | 191 } |
| 205 | 192 |
| 206 DeviceLocalAccountPolicyService::~DeviceLocalAccountPolicyService() { | 193 DeviceLocalAccountPolicyService::~DeviceLocalAccountPolicyService() { |
| 207 DCHECK(!request_context_); | |
| 208 DCHECK(policy_brokers_.empty()); | |
| 209 } | |
| 210 | |
| 211 void DeviceLocalAccountPolicyService::Shutdown() { | |
| 212 device_management_service_ = NULL; | |
| 213 request_context_ = NULL; | |
| 214 DeleteBrokers(&policy_brokers_); | 194 DeleteBrokers(&policy_brokers_); |
| 215 } | 195 } |
| 216 | 196 |
| 217 void DeviceLocalAccountPolicyService::Connect( | 197 void DeviceLocalAccountPolicyService::Connect( |
| 218 DeviceManagementService* device_management_service) { | 198 DeviceManagementService* device_management_service) { |
| 219 DCHECK(!device_management_service_); | 199 DCHECK(!device_management_service_); |
| 220 device_management_service_ = device_management_service; | 200 device_management_service_ = device_management_service; |
| 221 | 201 |
| 222 // Connect the brokers. | 202 // Connect the brokers. |
| 223 for (PolicyBrokerMap::iterator it(policy_brokers_.begin()); | 203 for (PolicyBrokerMap::iterator it(policy_brokers_.begin()); |
| 224 it != policy_brokers_.end(); ++it) { | 204 it != policy_brokers_.end(); ++it) { |
| 225 it->second->ConnectIfPossible(device_settings_service_, | 205 it->second->ConnectIfPossible(device_settings_service_, |
| 226 device_management_service_, | 206 device_management_service_); |
| 227 request_context_); | 207 } |
| 208 } |
| 209 |
| 210 void DeviceLocalAccountPolicyService::Disconnect() { |
| 211 DCHECK(device_management_service_); |
| 212 device_management_service_ = NULL; |
| 213 |
| 214 // Disconnect the brokers. |
| 215 for (PolicyBrokerMap::iterator it(policy_brokers_.begin()); |
| 216 it != policy_brokers_.end(); ++it) { |
| 217 it->second->Disconnect(); |
| 228 } | 218 } |
| 229 } | 219 } |
| 230 | 220 |
| 231 DeviceLocalAccountPolicyBroker* | 221 DeviceLocalAccountPolicyBroker* |
| 232 DeviceLocalAccountPolicyService::GetBrokerForUser( | 222 DeviceLocalAccountPolicyService::GetBrokerForUser( |
| 233 const std::string& user_id) { | 223 const std::string& user_id) { |
| 234 PolicyBrokerMap::iterator entry = policy_brokers_.find(user_id); | 224 PolicyBrokerMap::iterator entry = policy_brokers_.find(user_id); |
| 235 if (entry == policy_brokers_.end()) | 225 if (entry == policy_brokers_.end()) |
| 236 return NULL; | 226 return NULL; |
| 237 | 227 |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 390 broker.reset(broker_it->second); | 380 broker.reset(broker_it->second); |
| 391 old_policy_brokers.erase(broker_it); | 381 old_policy_brokers.erase(broker_it); |
| 392 broker_initialized = true; | 382 broker_initialized = true; |
| 393 } else { | 383 } else { |
| 394 scoped_ptr<DeviceLocalAccountPolicyStore> store( | 384 scoped_ptr<DeviceLocalAccountPolicyStore> store( |
| 395 new DeviceLocalAccountPolicyStore(it->account_id, | 385 new DeviceLocalAccountPolicyStore(it->account_id, |
| 396 session_manager_client_, | 386 session_manager_client_, |
| 397 device_settings_service_, | 387 device_settings_service_, |
| 398 store_background_task_runner_)); | 388 store_background_task_runner_)); |
| 399 store->AddObserver(this); | 389 store->AddObserver(this); |
| 400 scoped_refptr<DeviceLocalAccountExternalDataManager> | |
| 401 external_data_manager = | |
| 402 external_data_service_->GetExternalDataManager(it->account_id, | |
| 403 store.get()); | |
| 404 broker.reset(new DeviceLocalAccountPolicyBroker( | 390 broker.reset(new DeviceLocalAccountPolicyBroker( |
| 405 *it, | 391 *it, |
| 406 store.Pass(), | 392 store.Pass(), |
| 407 external_data_manager, | |
| 408 base::MessageLoopProxy::current())); | 393 base::MessageLoopProxy::current())); |
| 409 } | 394 } |
| 410 | 395 |
| 411 // Fire up the cloud connection for fetching policy for the account from | 396 // Fire up the cloud connection for fetching policy for the account from |
| 412 // the cloud if this is an enterprise-managed device. | 397 // the cloud if this is an enterprise-managed device. |
| 413 broker->ConnectIfPossible(device_settings_service_, | 398 broker->ConnectIfPossible(device_settings_service_, |
| 414 device_management_service_, | 399 device_management_service_); |
| 415 request_context_); | |
| 416 | 400 |
| 417 policy_brokers_[it->user_id] = broker.release(); | 401 policy_brokers_[it->user_id] = broker.release(); |
| 418 if (!broker_initialized) { | 402 if (!broker_initialized) { |
| 419 // The broker must be initialized after it has been added to | 403 // The broker must be initialized after it has been added to |
| 420 // |policy_brokers_|. | 404 // |policy_brokers_|. |
| 421 policy_brokers_[it->user_id]->Initialize(); | 405 policy_brokers_[it->user_id]->Initialize(); |
| 422 } | 406 } |
| 423 | 407 |
| 424 if (orphan_cache_deletion_state_ == NOT_STARTED) { | 408 if (orphan_cache_deletion_state_ == NOT_STARTED) { |
| 425 subdirectories_to_keep.insert( | 409 subdirectories_to_keep.insert( |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 491 CloudPolicyStore* store) { | 475 CloudPolicyStore* store) { |
| 492 for (PolicyBrokerMap::iterator it(policy_brokers_.begin()); | 476 for (PolicyBrokerMap::iterator it(policy_brokers_.begin()); |
| 493 it != policy_brokers_.end(); ++it) { | 477 it != policy_brokers_.end(); ++it) { |
| 494 if (it->second->core()->store() == store) | 478 if (it->second->core()->store() == store) |
| 495 return it->second; | 479 return it->second; |
| 496 } | 480 } |
| 497 return NULL; | 481 return NULL; |
| 498 } | 482 } |
| 499 | 483 |
| 500 } // namespace policy | 484 } // namespace policy |
| OLD | NEW |