OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/policy/user_cloud_policy_store.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "chrome/browser/policy/proto/cloud_policy.pb.h" |
| 9 #include "chrome/browser/signin/signin_manager.h" |
| 10 #include "chrome/browser/signin/signin_manager_factory.h" |
| 11 |
| 12 using enterprise_management::PolicyData; |
| 13 |
| 14 namespace policy { |
| 15 |
| 16 UserCloudPolicyStore::UserCloudPolicyStore(Profile* profile) |
| 17 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
| 18 profile_(profile) { |
| 19 } |
| 20 |
| 21 UserCloudPolicyStore::~UserCloudPolicyStore() { |
| 22 } |
| 23 |
| 24 void UserCloudPolicyStore::Load() { |
| 25 // TODO(atwilson): Read policy from file. |
| 26 policy_.reset(); |
| 27 policy_map_.Clear(); |
| 28 NotifyStoreLoaded(); |
| 29 } |
| 30 |
| 31 void UserCloudPolicyStore::Store( |
| 32 const enterprise_management::PolicyFetchResponse& policy) { |
| 33 // Stop any pending requests to store policy, then validate the new policy |
| 34 // before storing it. |
| 35 weak_factory_.InvalidateWeakPtrs(); |
| 36 scoped_ptr<enterprise_management::PolicyFetchResponse> policy_copy( |
| 37 new enterprise_management::PolicyFetchResponse(policy)); |
| 38 Validate(policy_copy.Pass(), |
| 39 base::Bind(&UserCloudPolicyStore::StorePolicyAfterValidation, |
| 40 weak_factory_.GetWeakPtr())); |
| 41 } |
| 42 |
| 43 void UserCloudPolicyStore::Validate( |
| 44 scoped_ptr<enterprise_management::PolicyFetchResponse> policy, |
| 45 const UserCloudPolicyValidator::CompletionCallback& callback) { |
| 46 // Configure the validator. |
| 47 scoped_ptr<UserCloudPolicyValidator> validator = |
| 48 CreateValidator(policy.Pass(), callback); |
| 49 SigninManager* signin = SigninManagerFactory::GetForProfile(profile_); |
| 50 std::string username = signin->GetAuthenticatedUsername(); |
| 51 DCHECK(!username.empty()); |
| 52 validator->ValidateUsername(username); |
| 53 |
| 54 // Start validation. The Validator will free itself once validation is |
| 55 // complete. |
| 56 validator.release()->StartValidation(); |
| 57 } |
| 58 |
| 59 void UserCloudPolicyStore::StorePolicyAfterValidation( |
| 60 UserCloudPolicyValidator* validator) { |
| 61 validation_status_ = validator->status(); |
| 62 if (!validator->success()) { |
| 63 status_ = STATUS_VALIDATION_ERROR; |
| 64 NotifyStoreError(); |
| 65 return; |
| 66 } |
| 67 // TODO(atwilson): Write policy to file. |
| 68 InstallPolicy(validator->policy_data().Pass(), validator->payload().Pass()); |
| 69 status_ = STATUS_OK; |
| 70 NotifyStoreLoaded(); |
| 71 } |
| 72 |
| 73 // static |
| 74 scoped_ptr<CloudPolicyStore> CloudPolicyStore::CreateUserPolicyStore( |
| 75 Profile* profile) { |
| 76 return scoped_ptr<CloudPolicyStore>(new UserCloudPolicyStore(profile)); |
| 77 } |
| 78 |
| 79 } // namespace policy |
OLD | NEW |