Chromium Code Reviews| Index: chrome/browser/policy/cloud_policy_service.cc |
| diff --git a/chrome/browser/policy/cloud_policy_service.cc b/chrome/browser/policy/cloud_policy_service.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..322c227791517c54d1e8f88dd75e8d04cb57613f |
| --- /dev/null |
| +++ b/chrome/browser/policy/cloud_policy_service.cc |
| @@ -0,0 +1,93 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/policy/cloud_policy_service.h" |
| + |
| +#include "base/callback.h" |
| +#include "chrome/browser/policy/proto/device_management_backend.pb.h" |
| + |
| +namespace em = enterprise_management; |
| + |
| +namespace policy { |
| + |
| +CloudPolicyService::CloudPolicyService(scoped_ptr<CloudPolicyClient> client, |
| + scoped_ptr<CloudPolicyStore> store) |
| + : client_(client.Pass()), |
| + store_(store.Pass()) { |
| + client_->AddObserver(this); |
| + store_->AddObserver(this); |
| +} |
| + |
| +CloudPolicyService::~CloudPolicyService() { |
| + client_->RemoveObserver(this); |
| + store_->RemoveObserver(this); |
| +} |
| + |
| +std::string CloudPolicyService::ManagedBy() const { |
| + std::string username = store_->policy().username(); |
| + std::size_t pos = username.find('@'); |
| + if (pos != std::string::npos) |
| + return username.substr(pos + 1); |
| + return std::string(); |
| +} |
| + |
| +void CloudPolicyService::RefreshPolicy(const base::Closure& callback) { |
| + // If the client is not registered, bail out. |
| + if (!client_->is_registered()) { |
| + callback.Run(); |
| + return; |
| + } |
| + |
| + // Else, trigger a refresh. |
| + refresh_callbacks_.push_back(callback); |
| + refresh_state_ = REFRESH_POLICY_FETCH; |
| + client_->FetchPolicy(); |
| +} |
| + |
| +void CloudPolicyService::OnPolicyFetched(CloudPolicyClient* client) { |
| + if (client_->status() != DM_STATUS_SUCCESS) { |
| + RefreshCompleted(); |
| + return; |
| + } |
| + |
| + const em::PolicyFetchResponse* policy = client_->policy(); |
| + if (policy) { |
| + store_->Store(*policy); |
|
Joao da Silva
2012/04/17 00:30:58
I'd move this line after the refresh_state_ update
Mattias Nissler (ping if slow)
2012/05/22 14:14:26
Good catch. Done.
|
| + if (refresh_state_ != REFRESH_NONE) |
| + refresh_state_ = REFRESH_POLICY_STORE; |
| + } else { |
| + RefreshCompleted(); |
| + } |
| +} |
| + |
| +void CloudPolicyService::OnRegistrationStateChanged(CloudPolicyClient* client) { |
| +} |
| + |
| +void CloudPolicyService::OnClientError(CloudPolicyClient* client) { |
| + if (refresh_state_ == REFRESH_POLICY_FETCH) |
| + RefreshCompleted(); |
| +} |
| + |
| +void CloudPolicyService::OnPolicyLoaded(CloudPolicyStore* store) { |
| + if (refresh_state_ == REFRESH_POLICY_STORE) |
| + RefreshCompleted(); |
| +} |
| + |
| +void CloudPolicyService::OnStoreError(CloudPolicyStore* store) { |
| + if (refresh_state_ == REFRESH_POLICY_STORE) |
| + RefreshCompleted(); |
| +} |
| + |
| +void CloudPolicyService::RefreshCompleted() { |
| + for (std::vector<base::Closure>::iterator callback( |
| + refresh_callbacks_.begin()); |
| + callback != refresh_callbacks_.end(); |
| + ++callback) { |
| + callback->Run(); |
| + } |
| + refresh_callbacks_.clear(); |
| + refresh_state_ = REFRESH_NONE; |
| +} |
| + |
| +} // namespace policy |