Chromium Code Reviews| Index: chrome/browser/policy/user_cloud_policy_store.cc |
| diff --git a/chrome/browser/policy/user_cloud_policy_store.cc b/chrome/browser/policy/user_cloud_policy_store.cc |
| index 9817828acf22f3efc308df7eb13308945f38c3d0..079a7691119be1cf57df07cbf1e421ce66cc2f59 100644 |
| --- a/chrome/browser/policy/user_cloud_policy_store.cc |
| +++ b/chrome/browser/policy/user_cloud_policy_store.cc |
| @@ -5,43 +5,117 @@ |
| #include "chrome/browser/policy/user_cloud_policy_store.h" |
| #include "base/bind.h" |
| +#include "base/file_path.h" |
| #include "chrome/browser/policy/proto/cloud_policy.pb.h" |
| +#include "chrome/browser/policy/proto/device_management_backend.pb.h" |
| +#include "chrome/browser/policy/proto/device_management_local.pb.h" |
| +#include "chrome/browser/profiles/profile.h" |
| #include "chrome/browser/signin/signin_manager.h" |
| #include "chrome/browser/signin/signin_manager_factory.h" |
| -using enterprise_management::PolicyData; |
| +namespace em = enterprise_management; |
| + |
| +namespace { |
| +// Subdirectory in the user's profile for storing user policies. |
| +const FilePath::CharType kPolicyDir[] = FILE_PATH_LITERAL("Policy"); |
| +// File in the above directory for storing user policy data. |
| +const FilePath::CharType kPolicyCacheFile[] = FILE_PATH_LITERAL("User Policy"); |
| +} // namespace |
| namespace policy { |
| -UserCloudPolicyStore::UserCloudPolicyStore(Profile* profile) |
| +UserCloudPolicyStore::UserCloudPolicyStore(Profile* profile, |
| + const FilePath& path) |
| : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
| profile_(profile) { |
| + |
|
Mattias Nissler (ping if slow)
2012/08/20 15:29:30
no newline here I think?
Andrew T Wilson (Slow)
2012/08/21 23:18:19
Done.
|
| + policy_cache_ = new UserPolicyDiskCache(weak_factory_.GetWeakPtr(), path); |
| } |
| UserCloudPolicyStore::~UserCloudPolicyStore() { |
| } |
| void UserCloudPolicyStore::Load() { |
| - // TODO(atwilson): Read policy from file. |
| - policy_.reset(); |
| - policy_map_.Clear(); |
| + DVLOG(1) << "Initiating policy load from disk"; |
| + policy_cache_->Load(); |
| +} |
| + |
| +void UserCloudPolicyStore::OnDiskCacheLoaded( |
| + UserPolicyDiskCache::LoadResult result, |
| + const em::CachedCloudPolicyResponse& policy) { |
| + // Map the disk cache result to a CloudPolicyStore status code. |
| + switch (result) { |
| + case UserPolicyDiskCache::LOAD_RESULT_SUCCESS: |
| + // The stored policy data should contain cloud policy. |
| + DVLOG(1) << "Policy data loaded from disk - starting validation"; |
| + DCHECK(policy.has_cloud_policy()); |
| + if (policy.has_cloud_policy()) { |
| + status_ = STATUS_OK; |
| + // Validate the policy loaded from disk. |
| + scoped_ptr<em::PolicyFetchResponse> cloud_policy; |
| + cloud_policy.reset( |
| + new em::PolicyFetchResponse(policy.cloud_policy())); |
| + Validate(cloud_policy.Pass(), |
| + base::Bind( |
| + &UserCloudPolicyStore::InstallLoadedPolicyAfterValidation, |
| + weak_factory_.GetWeakPtr())); |
| + } else { |
| + // No cloud policy to validate - this shouldn't be possible, so treat |
| + // like an error. |
| + status_ = STATUS_LOAD_ERROR; |
| + NotifyStoreError(); |
| + } |
| + break; |
| + case UserPolicyDiskCache::LOAD_RESULT_NOT_FOUND: |
| + // Treat a non-existent file as STATUS_OK, because it's not actually an |
| + // error - it just means no policy has been downloaded yet. |
| + status_ = STATUS_OK; |
| + NotifyStoreLoaded(); |
| + DVLOG(1) << "No policy found on disk"; |
| + break; |
| + case UserPolicyDiskCache::LOAD_RESULT_PARSE_ERROR: |
| + case UserPolicyDiskCache::LOAD_RESULT_READ_ERROR: |
| + DLOG(WARNING) << "Error reading policy from disk"; |
| + status_ = STATUS_LOAD_ERROR; |
| + NotifyStoreError(); |
| + break; |
| + default: |
| + NOTREACHED(); |
| + } |
| +} |
| + |
| +void UserCloudPolicyStore::InstallLoadedPolicyAfterValidation( |
| + UserCloudPolicyValidator* validator) { |
| + validation_status_ = validator->status(); |
| + if (!validator->success()) { |
| + DVLOG(1) << "Validation failed: status=" << validation_status_; |
| + status_ = STATUS_VALIDATION_ERROR; |
| + NotifyStoreError(); |
| + return; |
| + } |
| + |
| + DVLOG(1) << "Validation succeeded - installing policy with dm_token: " << |
| + validator->policy_data()->request_token(); |
| + DVLOG(1) << "Device ID: " << validator->policy_data()->device_id(); |
| + |
| + InstallPolicy(validator->policy_data().Pass(), validator->payload().Pass()); |
| + status_ = STATUS_OK; |
| NotifyStoreLoaded(); |
| } |
| -void UserCloudPolicyStore::Store( |
| - const enterprise_management::PolicyFetchResponse& policy) { |
| + |
|
Mattias Nissler (ping if slow)
2012/08/20 15:29:30
remove extra blank line
Andrew T Wilson (Slow)
2012/08/21 23:18:19
Done.
|
| +void UserCloudPolicyStore::Store(const em::PolicyFetchResponse& policy) { |
| // Stop any pending requests to store policy, then validate the new policy |
| // before storing it. |
| - weak_factory_.InvalidateWeakPtrs(); |
|
Andrew T Wilson (Slow)
2012/08/17 06:26:13
Per my email, note that I no longer call Invalidat
Mattias Nissler (ping if slow)
2012/08/20 15:29:30
As pointed out on the mail thread, it's actually r
|
| - scoped_ptr<enterprise_management::PolicyFetchResponse> policy_copy( |
| - new enterprise_management::PolicyFetchResponse(policy)); |
| + scoped_ptr<em::PolicyFetchResponse> policy_copy( |
| + new em::PolicyFetchResponse(policy)); |
| Validate(policy_copy.Pass(), |
| base::Bind(&UserCloudPolicyStore::StorePolicyAfterValidation, |
| weak_factory_.GetWeakPtr())); |
| } |
| void UserCloudPolicyStore::Validate( |
| - scoped_ptr<enterprise_management::PolicyFetchResponse> policy, |
| + scoped_ptr<em::PolicyFetchResponse> policy, |
| const UserCloudPolicyValidator::CompletionCallback& callback) { |
| // Configure the validator. |
| scoped_ptr<UserCloudPolicyValidator> validator = |
| @@ -60,11 +134,18 @@ void UserCloudPolicyStore::StorePolicyAfterValidation( |
| UserCloudPolicyValidator* validator) { |
| validation_status_ = validator->status(); |
| if (!validator->success()) { |
| + DVLOG(1) << "Validation failed: status=" << validation_status_; |
| status_ = STATUS_VALIDATION_ERROR; |
| NotifyStoreError(); |
| return; |
| } |
| - // TODO(atwilson): Write policy to file. |
| + |
| + DVLOG(1) << "Policy validation succeeded - storing to disk."; |
|
Mattias Nissler (ping if slow)
2012/08/20 15:29:30
might just replace the two DVLOGs with a single on
Andrew T Wilson (Slow)
2012/08/21 23:18:19
Done.
|
| + // Persist the validated policy. |
| + em::CachedCloudPolicyResponse policy_response; |
| + const em::PolicyFetchResponse& resp = *validator->policy(); |
| + *(policy_response.mutable_cloud_policy()) = resp; |
| + policy_cache_->Store(policy_response); |
| InstallPolicy(validator->policy_data().Pass(), validator->payload().Pass()); |
| status_ = STATUS_OK; |
| NotifyStoreLoaded(); |
| @@ -73,7 +154,10 @@ void UserCloudPolicyStore::StorePolicyAfterValidation( |
| // static |
| scoped_ptr<CloudPolicyStore> CloudPolicyStore::CreateUserPolicyStore( |
| Profile* profile) { |
| - return scoped_ptr<CloudPolicyStore>(new UserCloudPolicyStore(profile)); |
| + FilePath path = profile->GetPath(); |
| + path = path.Append(kPolicyDir); |
| + path = path.Append(kPolicyCacheFile); |
| + return scoped_ptr<CloudPolicyStore>(new UserCloudPolicyStore(profile, path)); |
| } |
| } // namespace policy |