| 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 "components/policy/core/common/cloud/cloud_policy_service.h" | 5 #include "components/policy/core/common/cloud/cloud_policy_service.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/logging.h" |
| 8 #include "policy/proto/device_management_backend.pb.h" | 9 #include "policy/proto/device_management_backend.pb.h" |
| 9 | 10 |
| 10 namespace em = enterprise_management; | 11 namespace em = enterprise_management; |
| 11 | 12 |
| 12 namespace policy { | 13 namespace policy { |
| 13 | 14 |
| 14 CloudPolicyService::CloudPolicyService(const std::string& policy_type, | 15 CloudPolicyService::CloudPolicyService(const std::string& policy_type, |
| 15 const std::string& settings_entity_id, | 16 const std::string& settings_entity_id, |
| 16 CloudPolicyClient* client, | 17 CloudPolicyClient* client, |
| 17 CloudPolicyStore* store) | 18 CloudPolicyStore* store) |
| 18 : policy_type_(policy_type), | 19 : policy_type_(policy_type), |
| 19 settings_entity_id_(settings_entity_id), | 20 settings_entity_id_(settings_entity_id), |
| 20 client_(client), | 21 client_(client), |
| 21 store_(store), | 22 store_(store), |
| 22 refresh_state_(REFRESH_NONE), | 23 refresh_state_(REFRESH_NONE), |
| 24 unregister_state_(UNREGISTER_NONE), |
| 23 initialization_complete_(false) { | 25 initialization_complete_(false) { |
| 24 client_->AddPolicyTypeToFetch(policy_type_, settings_entity_id_); | 26 client_->AddPolicyTypeToFetch(policy_type_, settings_entity_id_); |
| 25 client_->AddObserver(this); | 27 client_->AddObserver(this); |
| 26 store_->AddObserver(this); | 28 store_->AddObserver(this); |
| 27 | 29 |
| 28 // Make sure we initialize |client_| from the policy data that might be | 30 // Make sure we initialize |client_| from the policy data that might be |
| 29 // already present in |store_|. | 31 // already present in |store_|. |
| 30 OnStoreLoaded(store_); | 32 OnStoreLoaded(store_); |
| 31 } | 33 } |
| 32 | 34 |
| 33 CloudPolicyService::~CloudPolicyService() { | 35 CloudPolicyService::~CloudPolicyService() { |
| 34 client_->RemovePolicyTypeToFetch(policy_type_, settings_entity_id_); | 36 client_->RemovePolicyTypeToFetch(policy_type_, settings_entity_id_); |
| 35 client_->RemoveObserver(this); | 37 client_->RemoveObserver(this); |
| 36 store_->RemoveObserver(this); | 38 store_->RemoveObserver(this); |
| 37 } | 39 } |
| 38 | 40 |
| 39 std::string CloudPolicyService::ManagedBy() const { | 41 std::string CloudPolicyService::ManagedBy() const { |
| 40 const em::PolicyData* policy = store_->policy(); | 42 const em::PolicyData* policy = store_->policy(); |
| 41 if (policy) { | 43 if (policy) { |
| 42 std::string username = policy->username(); | 44 std::string username = policy->username(); |
| 43 std::size_t pos = username.find('@'); | 45 std::size_t pos = username.find('@'); |
| 44 if (pos != std::string::npos) | 46 if (pos != std::string::npos) |
| 45 return username.substr(pos + 1); | 47 return username.substr(pos + 1); |
| 46 } | 48 } |
| 47 return std::string(); | 49 return std::string(); |
| 48 } | 50 } |
| 49 | 51 |
| 50 void CloudPolicyService::RefreshPolicy(const RefreshPolicyCallback& callback) { | 52 void CloudPolicyService::RefreshPolicy(const RefreshPolicyCallback& callback) { |
| 51 // If the client is not registered, bail out. | 53 // If the client is not registered or is unregistering, bail out. |
| 52 if (!client_->is_registered()) { | 54 if (!client_->is_registered() || unregister_state_ != UNREGISTER_NONE) { |
| 53 callback.Run(false); | 55 callback.Run(false); |
| 54 return; | 56 return; |
| 55 } | 57 } |
| 56 | 58 |
| 57 // Else, trigger a refresh. | 59 // Else, trigger a refresh. |
| 58 refresh_callbacks_.push_back(callback); | 60 refresh_callbacks_.push_back(callback); |
| 59 refresh_state_ = REFRESH_POLICY_FETCH; | 61 refresh_state_ = REFRESH_POLICY_FETCH; |
| 60 client_->FetchPolicy(); | 62 client_->FetchPolicy(); |
| 61 } | 63 } |
| 62 | 64 |
| 65 void CloudPolicyService::Unregister(const UnregisterCallback& callback) { |
| 66 // Abort all pending refresh requests. |
| 67 if (refresh_state_ != REFRESH_NONE) |
| 68 RefreshCompleted(false); |
| 69 |
| 70 // Abort previous unregister request if any. |
| 71 if (unregister_state_ != UNREGISTER_NONE) |
| 72 UnregisterCompleted(false); |
| 73 |
| 74 unregister_callback_ = callback; |
| 75 unregister_state_ = UNREGISTER_PENDING; |
| 76 client_->Unregister(); |
| 77 } |
| 78 |
| 63 void CloudPolicyService::OnPolicyFetched(CloudPolicyClient* client) { | 79 void CloudPolicyService::OnPolicyFetched(CloudPolicyClient* client) { |
| 64 if (client_->status() != DM_STATUS_SUCCESS) { | 80 if (client_->status() != DM_STATUS_SUCCESS) { |
| 65 RefreshCompleted(false); | 81 RefreshCompleted(false); |
| 66 return; | 82 return; |
| 67 } | 83 } |
| 68 | 84 |
| 69 const em::PolicyFetchResponse* policy = | 85 const em::PolicyFetchResponse* policy = |
| 70 client_->GetPolicyFor(policy_type_, settings_entity_id_); | 86 client_->GetPolicyFor(policy_type_, settings_entity_id_); |
| 71 if (policy) { | 87 if (policy) { |
| 72 if (refresh_state_ != REFRESH_NONE) | 88 if (refresh_state_ != REFRESH_NONE) |
| 73 refresh_state_ = REFRESH_POLICY_STORE; | 89 refresh_state_ = REFRESH_POLICY_STORE; |
| 74 store_->Store(*policy, client->fetched_invalidation_version()); | 90 store_->Store(*policy, client->fetched_invalidation_version()); |
| 75 } else { | 91 } else { |
| 76 RefreshCompleted(false); | 92 RefreshCompleted(false); |
| 77 } | 93 } |
| 78 } | 94 } |
| 79 | 95 |
| 80 void CloudPolicyService::OnRegistrationStateChanged(CloudPolicyClient* client) { | 96 void CloudPolicyService::OnRegistrationStateChanged(CloudPolicyClient* client) { |
| 97 if (unregister_state_ == UNREGISTER_PENDING) |
| 98 UnregisterCompleted(true); |
| 81 } | 99 } |
| 82 | 100 |
| 83 void CloudPolicyService::OnClientError(CloudPolicyClient* client) { | 101 void CloudPolicyService::OnClientError(CloudPolicyClient* client) { |
| 84 if (refresh_state_ == REFRESH_POLICY_FETCH) | 102 if (refresh_state_ == REFRESH_POLICY_FETCH) |
| 85 RefreshCompleted(false); | 103 RefreshCompleted(false); |
| 104 if (unregister_state_ == UNREGISTER_PENDING) |
| 105 UnregisterCompleted(false); |
| 86 } | 106 } |
| 87 | 107 |
| 88 void CloudPolicyService::OnStoreLoaded(CloudPolicyStore* store) { | 108 void CloudPolicyService::OnStoreLoaded(CloudPolicyStore* store) { |
| 89 // Update the client with state from the store. | 109 // Update the client with state from the store. |
| 90 const em::PolicyData* policy(store_->policy()); | 110 const em::PolicyData* policy(store_->policy()); |
| 91 | 111 |
| 92 // Timestamp. | 112 // Timestamp. |
| 93 base::Time policy_timestamp; | 113 base::Time policy_timestamp; |
| 94 if (policy && policy->has_timestamp()) { | 114 if (policy && policy->has_timestamp()) { |
| 95 policy_timestamp = | 115 policy_timestamp = |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 callbacks.swap(refresh_callbacks_); | 165 callbacks.swap(refresh_callbacks_); |
| 146 refresh_state_ = REFRESH_NONE; | 166 refresh_state_ = REFRESH_NONE; |
| 147 | 167 |
| 148 for (std::vector<RefreshPolicyCallback>::iterator callback(callbacks.begin()); | 168 for (std::vector<RefreshPolicyCallback>::iterator callback(callbacks.begin()); |
| 149 callback != callbacks.end(); | 169 callback != callbacks.end(); |
| 150 ++callback) { | 170 ++callback) { |
| 151 callback->Run(success); | 171 callback->Run(success); |
| 152 } | 172 } |
| 153 } | 173 } |
| 154 | 174 |
| 175 void CloudPolicyService::UnregisterCompleted(bool success) { |
| 176 if (!success) |
| 177 LOG(ERROR) << "Unregister request failed."; |
| 178 |
| 179 unregister_state_ = UNREGISTER_NONE; |
| 180 unregister_callback_.Run(success); |
| 181 unregister_callback_ = UnregisterCallback(); // Reset. |
| 182 } |
| 183 |
| 155 void CloudPolicyService::AddObserver(Observer* observer) { | 184 void CloudPolicyService::AddObserver(Observer* observer) { |
| 156 observers_.AddObserver(observer); | 185 observers_.AddObserver(observer); |
| 157 } | 186 } |
| 158 | 187 |
| 159 void CloudPolicyService::RemoveObserver(Observer* observer) { | 188 void CloudPolicyService::RemoveObserver(Observer* observer) { |
| 160 observers_.RemoveObserver(observer); | 189 observers_.RemoveObserver(observer); |
| 161 } | 190 } |
| 162 | 191 |
| 163 } // namespace policy | 192 } // namespace policy |
| OLD | NEW |