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