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 "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 PolicyNamespaceKey& policy_ns_key, | 15 CloudPolicyService::CloudPolicyService(const PolicyNamespaceKey& policy_ns_key, |
| 15 CloudPolicyClient* client, | 16 CloudPolicyClient* client, |
| 16 CloudPolicyStore* store) | 17 CloudPolicyStore* store) |
| 17 : policy_ns_key_(policy_ns_key), | 18 : policy_ns_key_(policy_ns_key), |
| 18 client_(client), | 19 client_(client), |
| 19 store_(store), | 20 store_(store), |
| 20 refresh_state_(REFRESH_NONE), | 21 refresh_state_(REFRESH_NONE), |
| 22 unregister_state_(UNREGISTER_NONE), | |
| 21 initialization_complete_(false) { | 23 initialization_complete_(false) { |
| 22 client_->AddNamespaceToFetch(policy_ns_key_); | 24 client_->AddNamespaceToFetch(policy_ns_key_); |
| 23 client_->AddObserver(this); | 25 client_->AddObserver(this); |
| 24 store_->AddObserver(this); | 26 store_->AddObserver(this); |
| 25 | 27 |
| 26 // 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 |
| 27 // already present in |store_|. | 29 // already present in |store_|. |
| 28 OnStoreLoaded(store_); | 30 OnStoreLoaded(store_); |
| 29 } | 31 } |
| 30 | 32 |
| 31 CloudPolicyService::~CloudPolicyService() { | 33 CloudPolicyService::~CloudPolicyService() { |
| 32 client_->RemoveNamespaceToFetch(policy_ns_key_); | 34 client_->RemoveNamespaceToFetch(policy_ns_key_); |
| 33 client_->RemoveObserver(this); | 35 client_->RemoveObserver(this); |
| 34 store_->RemoveObserver(this); | 36 store_->RemoveObserver(this); |
| 35 } | 37 } |
| 36 | 38 |
| 37 std::string CloudPolicyService::ManagedBy() const { | 39 std::string CloudPolicyService::ManagedBy() const { |
| 38 const em::PolicyData* policy = store_->policy(); | 40 const em::PolicyData* policy = store_->policy(); |
| 39 if (policy) { | 41 if (policy) { |
| 40 std::string username = policy->username(); | 42 std::string username = policy->username(); |
| 41 std::size_t pos = username.find('@'); | 43 std::size_t pos = username.find('@'); |
| 42 if (pos != std::string::npos) | 44 if (pos != std::string::npos) |
| 43 return username.substr(pos + 1); | 45 return username.substr(pos + 1); |
| 44 } | 46 } |
| 45 return std::string(); | 47 return std::string(); |
| 46 } | 48 } |
| 47 | 49 |
| 48 void CloudPolicyService::RefreshPolicy(const RefreshPolicyCallback& callback) { | 50 void CloudPolicyService::RefreshPolicy(const RefreshPolicyCallback& callback) { |
| 49 // If the client is not registered, bail out. | 51 // If the client is not registered or is unregistering, bail out. |
| 50 if (!client_->is_registered()) { | 52 if (!client_->is_registered() || unregister_state_ != UNREGISTER_NONE) { |
| 51 callback.Run(false); | 53 callback.Run(false); |
| 52 return; | 54 return; |
| 53 } | 55 } |
| 54 | 56 |
| 55 // Else, trigger a refresh. | 57 // Else, trigger a refresh. |
| 56 refresh_callbacks_.push_back(callback); | 58 refresh_callbacks_.push_back(callback); |
| 57 refresh_state_ = REFRESH_POLICY_FETCH; | 59 refresh_state_ = REFRESH_POLICY_FETCH; |
| 58 client_->FetchPolicy(); | 60 client_->FetchPolicy(); |
| 59 } | 61 } |
| 60 | 62 |
| 63 void CloudPolicyService::Unregister(const UnregisterCallback& callback) { | |
| 64 // Abort all pending refresh requests. | |
| 65 if (refresh_state_ != REFRESH_NONE) { | |
| 66 RefreshCompleted(false); | |
| 67 } | |
| 68 | |
| 69 unregister_callback_ = callback; | |
|
bartfab (slow)
2014/11/28 13:25:18
What if there already is an unregistration in prog
davidyu
2014/12/01 17:05:23
Done.
| |
| 70 unregister_state_ = UNREGISTER_PENDING; | |
| 71 client_->Unregister(); | |
| 72 } | |
| 73 | |
| 61 void CloudPolicyService::OnPolicyFetched(CloudPolicyClient* client) { | 74 void CloudPolicyService::OnPolicyFetched(CloudPolicyClient* client) { |
| 62 if (client_->status() != DM_STATUS_SUCCESS) { | 75 if (client_->status() != DM_STATUS_SUCCESS) { |
| 63 RefreshCompleted(false); | 76 RefreshCompleted(false); |
| 64 return; | 77 return; |
| 65 } | 78 } |
| 66 | 79 |
| 67 const em::PolicyFetchResponse* policy = client_->GetPolicyFor(policy_ns_key_); | 80 const em::PolicyFetchResponse* policy = client_->GetPolicyFor(policy_ns_key_); |
| 68 if (policy) { | 81 if (policy) { |
| 69 if (refresh_state_ != REFRESH_NONE) | 82 if (refresh_state_ != REFRESH_NONE) |
| 70 refresh_state_ = REFRESH_POLICY_STORE; | 83 refresh_state_ = REFRESH_POLICY_STORE; |
| 71 store_->Store(*policy, client->fetched_invalidation_version()); | 84 store_->Store(*policy, client->fetched_invalidation_version()); |
| 72 } else { | 85 } else { |
| 73 RefreshCompleted(false); | 86 RefreshCompleted(false); |
| 74 } | 87 } |
| 75 } | 88 } |
| 76 | 89 |
| 77 void CloudPolicyService::OnRegistrationStateChanged(CloudPolicyClient* client) { | 90 void CloudPolicyService::OnRegistrationStateChanged(CloudPolicyClient* client) { |
| 91 if (unregister_state_ == UNREGISTER_PENDING) | |
| 92 UnregisterCompleted(true); | |
| 78 } | 93 } |
| 79 | 94 |
| 80 void CloudPolicyService::OnClientError(CloudPolicyClient* client) { | 95 void CloudPolicyService::OnClientError(CloudPolicyClient* client) { |
| 81 if (refresh_state_ == REFRESH_POLICY_FETCH) | 96 if (refresh_state_ == REFRESH_POLICY_FETCH) |
| 82 RefreshCompleted(false); | 97 RefreshCompleted(false); |
| 98 if (unregister_state_ == UNREGISTER_PENDING) | |
| 99 UnregisterCompleted(false); | |
| 83 } | 100 } |
| 84 | 101 |
| 85 void CloudPolicyService::OnStoreLoaded(CloudPolicyStore* store) { | 102 void CloudPolicyService::OnStoreLoaded(CloudPolicyStore* store) { |
| 86 // Update the client with state from the store. | 103 // Update the client with state from the store. |
| 87 const em::PolicyData* policy(store_->policy()); | 104 const em::PolicyData* policy(store_->policy()); |
| 88 | 105 |
| 89 // Timestamp. | 106 // Timestamp. |
| 90 base::Time policy_timestamp; | 107 base::Time policy_timestamp; |
| 91 if (policy && policy->has_timestamp()) { | 108 if (policy && policy->has_timestamp()) { |
| 92 policy_timestamp = | 109 policy_timestamp = |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 142 callbacks.swap(refresh_callbacks_); | 159 callbacks.swap(refresh_callbacks_); |
| 143 refresh_state_ = REFRESH_NONE; | 160 refresh_state_ = REFRESH_NONE; |
| 144 | 161 |
| 145 for (std::vector<RefreshPolicyCallback>::iterator callback(callbacks.begin()); | 162 for (std::vector<RefreshPolicyCallback>::iterator callback(callbacks.begin()); |
| 146 callback != callbacks.end(); | 163 callback != callbacks.end(); |
| 147 ++callback) { | 164 ++callback) { |
| 148 callback->Run(success); | 165 callback->Run(success); |
| 149 } | 166 } |
| 150 } | 167 } |
| 151 | 168 |
| 169 void CloudPolicyService::UnregisterCompleted(bool success) { | |
| 170 if (!success) | |
| 171 LOG(ERROR) << "Unregister request failed."; | |
| 172 | |
| 173 unregister_state_ = UNREGISTER_NONE; | |
| 174 unregister_callback_.Run(success); | |
|
bartfab (slow)
2014/11/28 13:25:18
Nit: Reset the callback after running it. This wil
davidyu
2014/12/01 17:05:23
Done.
| |
| 175 } | |
| 176 | |
| 152 void CloudPolicyService::AddObserver(Observer* observer) { | 177 void CloudPolicyService::AddObserver(Observer* observer) { |
| 153 observers_.AddObserver(observer); | 178 observers_.AddObserver(observer); |
| 154 } | 179 } |
| 155 | 180 |
| 156 void CloudPolicyService::RemoveObserver(Observer* observer) { | 181 void CloudPolicyService::RemoveObserver(Observer* observer) { |
| 157 observers_.RemoveObserver(observer); | 182 observers_.RemoveObserver(observer); |
| 158 } | 183 } |
| 159 | 184 |
| 160 } // namespace policy | 185 } // namespace policy |
| OLD | NEW |