Chromium Code Reviews| 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/cloud_policy_service.h" | |
| 6 | |
| 7 #include "base/callback.h" | |
| 8 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | |
| 9 | |
| 10 namespace em = enterprise_management; | |
| 11 | |
| 12 namespace policy { | |
| 13 | |
| 14 CloudPolicyService::CloudPolicyService(scoped_ptr<CloudPolicyClient> client, | |
| 15 scoped_ptr<CloudPolicyStore> store) | |
| 16 : client_(client.Pass()), | |
| 17 store_(store.Pass()) { | |
|
Joao da Silva
2012/05/22 22:01:44
initialize refresh_state_(REFRESH_NONE). The valgr
Mattias Nissler (ping if slow)
2012/05/24 10:12:25
Done.
| |
| 18 client_->AddObserver(this); | |
| 19 store_->AddObserver(this); | |
| 20 } | |
| 21 | |
| 22 CloudPolicyService::~CloudPolicyService() { | |
| 23 client_->RemoveObserver(this); | |
| 24 store_->RemoveObserver(this); | |
| 25 } | |
| 26 | |
| 27 std::string CloudPolicyService::ManagedBy() const { | |
| 28 const em::PolicyData* policy = store_->policy(); | |
| 29 if (policy) { | |
| 30 std::string username = policy->username(); | |
| 31 std::size_t pos = username.find('@'); | |
| 32 if (pos != std::string::npos) | |
| 33 return username.substr(pos + 1); | |
| 34 } | |
| 35 return std::string(); | |
| 36 } | |
| 37 | |
| 38 void CloudPolicyService::RefreshPolicy(const base::Closure& callback) { | |
| 39 // If the client is not registered, bail out. | |
| 40 if (!client_->is_registered()) { | |
| 41 callback.Run(); | |
| 42 return; | |
| 43 } | |
| 44 | |
| 45 // Else, trigger a refresh. | |
| 46 refresh_callbacks_.push_back(callback); | |
| 47 refresh_state_ = REFRESH_POLICY_FETCH; | |
| 48 client_->FetchPolicy(); | |
| 49 } | |
| 50 | |
| 51 void CloudPolicyService::OnPolicyFetched(CloudPolicyClient* client) { | |
| 52 if (client_->status() != DM_STATUS_SUCCESS) { | |
| 53 RefreshCompleted(); | |
| 54 return; | |
| 55 } | |
| 56 | |
| 57 const em::PolicyFetchResponse* policy = client_->policy(); | |
| 58 if (policy) { | |
| 59 if (refresh_state_ != REFRESH_NONE) | |
| 60 refresh_state_ = REFRESH_POLICY_STORE; | |
| 61 store_->Store(*policy); | |
| 62 } else { | |
| 63 RefreshCompleted(); | |
| 64 } | |
| 65 } | |
| 66 | |
| 67 void CloudPolicyService::OnRegistrationStateChanged(CloudPolicyClient* client) { | |
| 68 } | |
| 69 | |
| 70 void CloudPolicyService::OnClientError(CloudPolicyClient* client) { | |
| 71 if (refresh_state_ == REFRESH_POLICY_FETCH) | |
| 72 RefreshCompleted(); | |
| 73 } | |
| 74 | |
| 75 void CloudPolicyService::OnStoreLoaded(CloudPolicyStore* store) { | |
| 76 // Update the client with state from the store. | |
| 77 const em::PolicyData* policy(store_->policy()); | |
| 78 | |
| 79 // Timestamp. | |
| 80 base::Time policy_timestamp; | |
| 81 if (policy && policy->has_timestamp()) { | |
| 82 policy_timestamp = | |
| 83 base::Time::UnixEpoch() + | |
| 84 base::TimeDelta::FromMilliseconds(policy->timestamp()); | |
| 85 } | |
| 86 client_->set_last_policy_timestamp(policy_timestamp); | |
| 87 | |
| 88 // Public key version. | |
| 89 CloudPolicyClient::PublicKeyVersion public_key_version; | |
| 90 if (policy && policy->has_public_key_version()) { | |
| 91 public_key_version = | |
| 92 CloudPolicyClient::PublicKeyVersion(policy->public_key_version()); | |
| 93 } | |
| 94 client_->set_public_key_version(public_key_version); | |
| 95 | |
| 96 // Whether to submit the machine ID. | |
| 97 bool submit_machine_id = false; | |
| 98 if (policy && policy->has_valid_serial_number_missing()) | |
| 99 submit_machine_id = policy->valid_serial_number_missing(); | |
| 100 client_->set_submit_machine_id(submit_machine_id); | |
| 101 | |
| 102 // Finally, set up registration if necessary. | |
| 103 if (policy && policy->has_request_token() && policy->has_device_id() && | |
| 104 !client_->is_registered()) { | |
| 105 client_->SetupRegistration(policy->request_token(), | |
| 106 policy->device_id()); | |
| 107 } | |
| 108 | |
| 109 if (refresh_state_ == REFRESH_POLICY_STORE) | |
| 110 RefreshCompleted(); | |
| 111 } | |
| 112 | |
| 113 void CloudPolicyService::OnStoreError(CloudPolicyStore* store) { | |
| 114 if (refresh_state_ == REFRESH_POLICY_STORE) | |
| 115 RefreshCompleted(); | |
| 116 } | |
| 117 | |
| 118 void CloudPolicyService::RefreshCompleted() { | |
| 119 // Clear state and |refresh_callbacks_| before actually invoking them, s.t. | |
| 120 // triggering new policy fetches behaves as expected. | |
| 121 std::vector<base::Closure> callbacks; | |
| 122 callbacks.swap(refresh_callbacks_); | |
| 123 refresh_state_ = REFRESH_NONE; | |
| 124 | |
| 125 for (std::vector<base::Closure>::iterator callback(callbacks.begin()); | |
| 126 callback != callbacks.end(); | |
| 127 ++callback) { | |
| 128 callback->Run(); | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 } // namespace policy | |
| OLD | NEW |