Chromium Code Reviews| 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/policy/user_cloud_policy_store.h" | 5 #include "chrome/browser/policy/user_cloud_policy_store.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/file_path.h" | |
| 8 #include "chrome/browser/policy/proto/cloud_policy.pb.h" | 9 #include "chrome/browser/policy/proto/cloud_policy.pb.h" |
| 10 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | |
| 11 #include "chrome/browser/policy/proto/device_management_local.pb.h" | |
| 12 #include "chrome/browser/profiles/profile.h" | |
| 9 #include "chrome/browser/signin/signin_manager.h" | 13 #include "chrome/browser/signin/signin_manager.h" |
| 10 #include "chrome/browser/signin/signin_manager_factory.h" | 14 #include "chrome/browser/signin/signin_manager_factory.h" |
| 11 | 15 |
| 12 using enterprise_management::PolicyData; | 16 namespace em = enterprise_management; |
| 17 | |
| 18 namespace { | |
| 19 // Subdirectory in the user's profile for storing user policies. | |
| 20 const FilePath::CharType kPolicyDir[] = FILE_PATH_LITERAL("Policy"); | |
| 21 // File in the above directory for storing user policy data. | |
| 22 const FilePath::CharType kPolicyCacheFile[] = FILE_PATH_LITERAL("User Policy"); | |
| 23 } // namespace | |
| 13 | 24 |
| 14 namespace policy { | 25 namespace policy { |
| 15 | 26 |
| 16 UserCloudPolicyStore::UserCloudPolicyStore(Profile* profile) | 27 UserCloudPolicyStore::UserCloudPolicyStore(Profile* profile, |
| 28 const FilePath& path) | |
| 17 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), | 29 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
| 18 profile_(profile) { | 30 profile_(profile) { |
| 31 | |
|
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.
| |
| 32 policy_cache_ = new UserPolicyDiskCache(weak_factory_.GetWeakPtr(), path); | |
| 19 } | 33 } |
| 20 | 34 |
| 21 UserCloudPolicyStore::~UserCloudPolicyStore() { | 35 UserCloudPolicyStore::~UserCloudPolicyStore() { |
| 22 } | 36 } |
| 23 | 37 |
| 24 void UserCloudPolicyStore::Load() { | 38 void UserCloudPolicyStore::Load() { |
| 25 // TODO(atwilson): Read policy from file. | 39 DVLOG(1) << "Initiating policy load from disk"; |
| 26 policy_.reset(); | 40 policy_cache_->Load(); |
| 27 policy_map_.Clear(); | 41 } |
| 42 | |
| 43 void UserCloudPolicyStore::OnDiskCacheLoaded( | |
| 44 UserPolicyDiskCache::LoadResult result, | |
| 45 const em::CachedCloudPolicyResponse& policy) { | |
| 46 // Map the disk cache result to a CloudPolicyStore status code. | |
| 47 switch (result) { | |
| 48 case UserPolicyDiskCache::LOAD_RESULT_SUCCESS: | |
| 49 // The stored policy data should contain cloud policy. | |
| 50 DVLOG(1) << "Policy data loaded from disk - starting validation"; | |
| 51 DCHECK(policy.has_cloud_policy()); | |
| 52 if (policy.has_cloud_policy()) { | |
| 53 status_ = STATUS_OK; | |
| 54 // Validate the policy loaded from disk. | |
| 55 scoped_ptr<em::PolicyFetchResponse> cloud_policy; | |
| 56 cloud_policy.reset( | |
| 57 new em::PolicyFetchResponse(policy.cloud_policy())); | |
| 58 Validate(cloud_policy.Pass(), | |
| 59 base::Bind( | |
| 60 &UserCloudPolicyStore::InstallLoadedPolicyAfterValidation, | |
| 61 weak_factory_.GetWeakPtr())); | |
| 62 } else { | |
| 63 // No cloud policy to validate - this shouldn't be possible, so treat | |
| 64 // like an error. | |
| 65 status_ = STATUS_LOAD_ERROR; | |
| 66 NotifyStoreError(); | |
| 67 } | |
| 68 break; | |
| 69 case UserPolicyDiskCache::LOAD_RESULT_NOT_FOUND: | |
| 70 // Treat a non-existent file as STATUS_OK, because it's not actually an | |
| 71 // error - it just means no policy has been downloaded yet. | |
| 72 status_ = STATUS_OK; | |
| 73 NotifyStoreLoaded(); | |
| 74 DVLOG(1) << "No policy found on disk"; | |
| 75 break; | |
| 76 case UserPolicyDiskCache::LOAD_RESULT_PARSE_ERROR: | |
| 77 case UserPolicyDiskCache::LOAD_RESULT_READ_ERROR: | |
| 78 DLOG(WARNING) << "Error reading policy from disk"; | |
| 79 status_ = STATUS_LOAD_ERROR; | |
| 80 NotifyStoreError(); | |
| 81 break; | |
| 82 default: | |
| 83 NOTREACHED(); | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 void UserCloudPolicyStore::InstallLoadedPolicyAfterValidation( | |
| 88 UserCloudPolicyValidator* validator) { | |
| 89 validation_status_ = validator->status(); | |
| 90 if (!validator->success()) { | |
| 91 DVLOG(1) << "Validation failed: status=" << validation_status_; | |
| 92 status_ = STATUS_VALIDATION_ERROR; | |
| 93 NotifyStoreError(); | |
| 94 return; | |
| 95 } | |
| 96 | |
| 97 DVLOG(1) << "Validation succeeded - installing policy with dm_token: " << | |
| 98 validator->policy_data()->request_token(); | |
| 99 DVLOG(1) << "Device ID: " << validator->policy_data()->device_id(); | |
| 100 | |
| 101 InstallPolicy(validator->policy_data().Pass(), validator->payload().Pass()); | |
| 102 status_ = STATUS_OK; | |
| 28 NotifyStoreLoaded(); | 103 NotifyStoreLoaded(); |
| 29 } | 104 } |
| 30 | 105 |
| 31 void UserCloudPolicyStore::Store( | 106 |
|
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.
| |
| 32 const enterprise_management::PolicyFetchResponse& policy) { | 107 void UserCloudPolicyStore::Store(const em::PolicyFetchResponse& policy) { |
| 33 // Stop any pending requests to store policy, then validate the new policy | 108 // Stop any pending requests to store policy, then validate the new policy |
| 34 // before storing it. | 109 // before storing it. |
| 35 weak_factory_.InvalidateWeakPtrs(); | 110 scoped_ptr<em::PolicyFetchResponse> policy_copy( |
|
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
| |
| 36 scoped_ptr<enterprise_management::PolicyFetchResponse> policy_copy( | 111 new em::PolicyFetchResponse(policy)); |
| 37 new enterprise_management::PolicyFetchResponse(policy)); | |
| 38 Validate(policy_copy.Pass(), | 112 Validate(policy_copy.Pass(), |
| 39 base::Bind(&UserCloudPolicyStore::StorePolicyAfterValidation, | 113 base::Bind(&UserCloudPolicyStore::StorePolicyAfterValidation, |
| 40 weak_factory_.GetWeakPtr())); | 114 weak_factory_.GetWeakPtr())); |
| 41 } | 115 } |
| 42 | 116 |
| 43 void UserCloudPolicyStore::Validate( | 117 void UserCloudPolicyStore::Validate( |
| 44 scoped_ptr<enterprise_management::PolicyFetchResponse> policy, | 118 scoped_ptr<em::PolicyFetchResponse> policy, |
| 45 const UserCloudPolicyValidator::CompletionCallback& callback) { | 119 const UserCloudPolicyValidator::CompletionCallback& callback) { |
| 46 // Configure the validator. | 120 // Configure the validator. |
| 47 scoped_ptr<UserCloudPolicyValidator> validator = | 121 scoped_ptr<UserCloudPolicyValidator> validator = |
| 48 CreateValidator(policy.Pass(), callback); | 122 CreateValidator(policy.Pass(), callback); |
| 49 SigninManager* signin = SigninManagerFactory::GetForProfile(profile_); | 123 SigninManager* signin = SigninManagerFactory::GetForProfile(profile_); |
| 50 std::string username = signin->GetAuthenticatedUsername(); | 124 std::string username = signin->GetAuthenticatedUsername(); |
| 51 DCHECK(!username.empty()); | 125 DCHECK(!username.empty()); |
| 52 validator->ValidateUsername(username); | 126 validator->ValidateUsername(username); |
| 53 | 127 |
| 54 // Start validation. The Validator will free itself once validation is | 128 // Start validation. The Validator will free itself once validation is |
| 55 // complete. | 129 // complete. |
| 56 validator.release()->StartValidation(); | 130 validator.release()->StartValidation(); |
| 57 } | 131 } |
| 58 | 132 |
| 59 void UserCloudPolicyStore::StorePolicyAfterValidation( | 133 void UserCloudPolicyStore::StorePolicyAfterValidation( |
| 60 UserCloudPolicyValidator* validator) { | 134 UserCloudPolicyValidator* validator) { |
| 61 validation_status_ = validator->status(); | 135 validation_status_ = validator->status(); |
| 62 if (!validator->success()) { | 136 if (!validator->success()) { |
| 137 DVLOG(1) << "Validation failed: status=" << validation_status_; | |
| 63 status_ = STATUS_VALIDATION_ERROR; | 138 status_ = STATUS_VALIDATION_ERROR; |
| 64 NotifyStoreError(); | 139 NotifyStoreError(); |
| 65 return; | 140 return; |
| 66 } | 141 } |
| 67 // TODO(atwilson): Write policy to file. | 142 |
| 143 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.
| |
| 144 // Persist the validated policy. | |
| 145 em::CachedCloudPolicyResponse policy_response; | |
| 146 const em::PolicyFetchResponse& resp = *validator->policy(); | |
| 147 *(policy_response.mutable_cloud_policy()) = resp; | |
| 148 policy_cache_->Store(policy_response); | |
| 68 InstallPolicy(validator->policy_data().Pass(), validator->payload().Pass()); | 149 InstallPolicy(validator->policy_data().Pass(), validator->payload().Pass()); |
| 69 status_ = STATUS_OK; | 150 status_ = STATUS_OK; |
| 70 NotifyStoreLoaded(); | 151 NotifyStoreLoaded(); |
| 71 } | 152 } |
| 72 | 153 |
| 73 // static | 154 // static |
| 74 scoped_ptr<CloudPolicyStore> CloudPolicyStore::CreateUserPolicyStore( | 155 scoped_ptr<CloudPolicyStore> CloudPolicyStore::CreateUserPolicyStore( |
| 75 Profile* profile) { | 156 Profile* profile) { |
| 76 return scoped_ptr<CloudPolicyStore>(new UserCloudPolicyStore(profile)); | 157 FilePath path = profile->GetPath(); |
| 158 path = path.Append(kPolicyDir); | |
| 159 path = path.Append(kPolicyCacheFile); | |
| 160 return scoped_ptr<CloudPolicyStore>(new UserCloudPolicyStore(profile, path)); | |
| 77 } | 161 } |
| 78 | 162 |
| 79 } // namespace policy | 163 } // namespace policy |
| OLD | NEW |