| 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 "chrome/browser/policy/cloud_policy_service.h" | 5 #include "chrome/browser/policy/cloud_policy_service.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | 8 #include "chrome/browser/policy/proto/device_management_backend.pb.h" |
| 9 | 9 |
| 10 namespace em = enterprise_management; | 10 namespace em = enterprise_management; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 const em::PolicyData* policy = store_->policy(); | 34 const em::PolicyData* policy = store_->policy(); |
| 35 if (policy) { | 35 if (policy) { |
| 36 std::string username = policy->username(); | 36 std::string username = policy->username(); |
| 37 std::size_t pos = username.find('@'); | 37 std::size_t pos = username.find('@'); |
| 38 if (pos != std::string::npos) | 38 if (pos != std::string::npos) |
| 39 return username.substr(pos + 1); | 39 return username.substr(pos + 1); |
| 40 } | 40 } |
| 41 return std::string(); | 41 return std::string(); |
| 42 } | 42 } |
| 43 | 43 |
| 44 void CloudPolicyService::RefreshPolicy(const base::Closure& callback) { | 44 void CloudPolicyService::RefreshPolicy(const RefreshPolicyCallback& callback) { |
| 45 // If the client is not registered, bail out. | 45 // If the client is not registered, bail out. |
| 46 if (!client_->is_registered()) { | 46 if (!client_->is_registered()) { |
| 47 callback.Run(); | 47 callback.Run(false); |
| 48 return; | 48 return; |
| 49 } | 49 } |
| 50 | 50 |
| 51 // Else, trigger a refresh. | 51 // Else, trigger a refresh. |
| 52 refresh_callbacks_.push_back(callback); | 52 refresh_callbacks_.push_back(callback); |
| 53 refresh_state_ = REFRESH_POLICY_FETCH; | 53 refresh_state_ = REFRESH_POLICY_FETCH; |
| 54 client_->FetchPolicy(); | 54 client_->FetchPolicy(); |
| 55 } | 55 } |
| 56 | 56 |
| 57 void CloudPolicyService::OnPolicyFetched(CloudPolicyClient* client) { | 57 void CloudPolicyService::OnPolicyFetched(CloudPolicyClient* client) { |
| 58 if (client_->status() != DM_STATUS_SUCCESS) { | 58 if (client_->status() != DM_STATUS_SUCCESS) { |
| 59 RefreshCompleted(); | 59 RefreshCompleted(false); |
| 60 return; | 60 return; |
| 61 } | 61 } |
| 62 | 62 |
| 63 const em::PolicyFetchResponse* policy = client_->policy(); | 63 const em::PolicyFetchResponse* policy = client_->policy(); |
| 64 if (policy) { | 64 if (policy) { |
| 65 if (refresh_state_ != REFRESH_NONE) | 65 if (refresh_state_ != REFRESH_NONE) |
| 66 refresh_state_ = REFRESH_POLICY_STORE; | 66 refresh_state_ = REFRESH_POLICY_STORE; |
| 67 store_->Store(*policy); | 67 store_->Store(*policy); |
| 68 } else { | 68 } else { |
| 69 RefreshCompleted(); | 69 RefreshCompleted(false); |
| 70 } | 70 } |
| 71 } | 71 } |
| 72 | 72 |
| 73 void CloudPolicyService::OnRegistrationStateChanged(CloudPolicyClient* client) { | 73 void CloudPolicyService::OnRegistrationStateChanged(CloudPolicyClient* client) { |
| 74 } | 74 } |
| 75 | 75 |
| 76 void CloudPolicyService::OnClientError(CloudPolicyClient* client) { | 76 void CloudPolicyService::OnClientError(CloudPolicyClient* client) { |
| 77 if (refresh_state_ == REFRESH_POLICY_FETCH) | 77 if (refresh_state_ == REFRESH_POLICY_FETCH) |
| 78 RefreshCompleted(); | 78 RefreshCompleted(false); |
| 79 } | 79 } |
| 80 | 80 |
| 81 void CloudPolicyService::OnStoreLoaded(CloudPolicyStore* store) { | 81 void CloudPolicyService::OnStoreLoaded(CloudPolicyStore* store) { |
| 82 // Update the client with state from the store. | 82 // Update the client with state from the store. |
| 83 const em::PolicyData* policy(store_->policy()); | 83 const em::PolicyData* policy(store_->policy()); |
| 84 | 84 |
| 85 // Timestamp. | 85 // Timestamp. |
| 86 base::Time policy_timestamp; | 86 base::Time policy_timestamp; |
| 87 if (policy && policy->has_timestamp()) { | 87 if (policy && policy->has_timestamp()) { |
| 88 policy_timestamp = | 88 policy_timestamp = |
| (...skipping 17 matching lines...) Expand all Loading... |
| 106 // Finally, set up registration if necessary. | 106 // Finally, set up registration if necessary. |
| 107 if (policy && policy->has_request_token() && policy->has_device_id() && | 107 if (policy && policy->has_request_token() && policy->has_device_id() && |
| 108 !client_->is_registered()) { | 108 !client_->is_registered()) { |
| 109 DVLOG(1) << "Setting up registration with request token: " | 109 DVLOG(1) << "Setting up registration with request token: " |
| 110 << policy->request_token(); | 110 << policy->request_token(); |
| 111 client_->SetupRegistration(policy->request_token(), | 111 client_->SetupRegistration(policy->request_token(), |
| 112 policy->device_id()); | 112 policy->device_id()); |
| 113 } | 113 } |
| 114 | 114 |
| 115 if (refresh_state_ == REFRESH_POLICY_STORE) | 115 if (refresh_state_ == REFRESH_POLICY_STORE) |
| 116 RefreshCompleted(); | 116 RefreshCompleted(true); |
| 117 | 117 |
| 118 CheckInitializationCompleted(); | 118 CheckInitializationCompleted(); |
| 119 } | 119 } |
| 120 | 120 |
| 121 void CloudPolicyService::OnStoreError(CloudPolicyStore* store) { | 121 void CloudPolicyService::OnStoreError(CloudPolicyStore* store) { |
| 122 if (refresh_state_ == REFRESH_POLICY_STORE) | 122 if (refresh_state_ == REFRESH_POLICY_STORE) |
| 123 RefreshCompleted(); | 123 RefreshCompleted(false); |
| 124 CheckInitializationCompleted(); | 124 CheckInitializationCompleted(); |
| 125 } | 125 } |
| 126 | 126 |
| 127 void CloudPolicyService::CheckInitializationCompleted() { | 127 void CloudPolicyService::CheckInitializationCompleted() { |
| 128 if (!IsInitializationComplete() && store_->is_initialized()) { | 128 if (!IsInitializationComplete() && store_->is_initialized()) { |
| 129 initialization_complete_ = true; | 129 initialization_complete_ = true; |
| 130 FOR_EACH_OBSERVER(Observer, observers_, OnInitializationCompleted(this)); | 130 FOR_EACH_OBSERVER(Observer, observers_, OnInitializationCompleted(this)); |
| 131 } | 131 } |
| 132 } | 132 } |
| 133 | 133 |
| 134 void CloudPolicyService::RefreshCompleted() { | 134 void CloudPolicyService::RefreshCompleted(bool success) { |
| 135 // Clear state and |refresh_callbacks_| before actually invoking them, s.t. | 135 // Clear state and |refresh_callbacks_| before actually invoking them, s.t. |
| 136 // triggering new policy fetches behaves as expected. | 136 // triggering new policy fetches behaves as expected. |
| 137 std::vector<base::Closure> callbacks; | 137 std::vector<RefreshPolicyCallback> callbacks; |
| 138 callbacks.swap(refresh_callbacks_); | 138 callbacks.swap(refresh_callbacks_); |
| 139 refresh_state_ = REFRESH_NONE; | 139 refresh_state_ = REFRESH_NONE; |
| 140 | 140 |
| 141 for (std::vector<base::Closure>::iterator callback(callbacks.begin()); | 141 for (std::vector<RefreshPolicyCallback>::iterator callback(callbacks.begin()); |
| 142 callback != callbacks.end(); | 142 callback != callbacks.end(); |
| 143 ++callback) { | 143 ++callback) { |
| 144 callback->Run(); | 144 callback->Run(success); |
| 145 } | 145 } |
| 146 } | 146 } |
| 147 | 147 |
| 148 void CloudPolicyService::AddObserver(Observer* observer) { | 148 void CloudPolicyService::AddObserver(Observer* observer) { |
| 149 observers_.AddObserver(observer); | 149 observers_.AddObserver(observer); |
| 150 } | 150 } |
| 151 | 151 |
| 152 void CloudPolicyService::RemoveObserver(Observer* observer) { | 152 void CloudPolicyService::RemoveObserver(Observer* observer) { |
| 153 observers_.RemoveObserver(observer); | 153 observers_.RemoveObserver(observer); |
| 154 } | 154 } |
| 155 | 155 |
| 156 } // namespace policy | 156 } // namespace policy |
| OLD | NEW |