Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/policy/cloud_policy_service.h" | |
| 6 | |
| 7 #include "base/callback.h" | |
| 8 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | |
| 9 | |
| 10 namespace em = enterprise_management; | |
| 11 | |
| 12 namespace policy { | |
| 13 | |
| 14 CloudPolicyService::CloudPolicyService(scoped_ptr<CloudPolicyClient> client, | |
| 15 scoped_ptr<CloudPolicyStore> store) | |
| 16 : client_(client.Pass()), | |
| 17 store_(store.Pass()) { | |
| 18 client_->AddObserver(this); | |
| 19 store_->AddObserver(this); | |
| 20 } | |
| 21 | |
| 22 CloudPolicyService::~CloudPolicyService() { | |
| 23 client_->RemoveObserver(this); | |
| 24 store_->RemoveObserver(this); | |
| 25 } | |
| 26 | |
| 27 std::string CloudPolicyService::ManagedBy() const { | |
| 28 std::string username = store_->policy().username(); | |
| 29 std::size_t pos = username.find('@'); | |
| 30 if (pos != std::string::npos) | |
| 31 return username.substr(pos + 1); | |
| 32 return std::string(); | |
| 33 } | |
| 34 | |
| 35 void CloudPolicyService::RefreshPolicy(const base::Closure& callback) { | |
| 36 // If the client is not registered, bail out. | |
| 37 if (!client_->is_registered()) { | |
| 38 callback.Run(); | |
| 39 return; | |
| 40 } | |
| 41 | |
| 42 // Else, trigger a refresh. | |
| 43 refresh_callbacks_.push_back(callback); | |
| 44 refresh_state_ = REFRESH_POLICY_FETCH; | |
| 45 client_->FetchPolicy(); | |
| 46 } | |
| 47 | |
| 48 void CloudPolicyService::OnPolicyFetched(CloudPolicyClient* client) { | |
| 49 if (client_->status() != DM_STATUS_SUCCESS) { | |
| 50 RefreshCompleted(); | |
| 51 return; | |
| 52 } | |
| 53 | |
| 54 const em::PolicyFetchResponse* policy = client_->policy(); | |
| 55 if (policy) { | |
| 56 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.
| |
| 57 if (refresh_state_ != REFRESH_NONE) | |
| 58 refresh_state_ = REFRESH_POLICY_STORE; | |
| 59 } else { | |
| 60 RefreshCompleted(); | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 void CloudPolicyService::OnRegistrationStateChanged(CloudPolicyClient* client) { | |
| 65 } | |
| 66 | |
| 67 void CloudPolicyService::OnClientError(CloudPolicyClient* client) { | |
| 68 if (refresh_state_ == REFRESH_POLICY_FETCH) | |
| 69 RefreshCompleted(); | |
| 70 } | |
| 71 | |
| 72 void CloudPolicyService::OnPolicyLoaded(CloudPolicyStore* store) { | |
| 73 if (refresh_state_ == REFRESH_POLICY_STORE) | |
| 74 RefreshCompleted(); | |
| 75 } | |
| 76 | |
| 77 void CloudPolicyService::OnStoreError(CloudPolicyStore* store) { | |
| 78 if (refresh_state_ == REFRESH_POLICY_STORE) | |
| 79 RefreshCompleted(); | |
| 80 } | |
| 81 | |
| 82 void CloudPolicyService::RefreshCompleted() { | |
| 83 for (std::vector<base::Closure>::iterator callback( | |
| 84 refresh_callbacks_.begin()); | |
| 85 callback != refresh_callbacks_.end(); | |
| 86 ++callback) { | |
| 87 callback->Run(); | |
| 88 } | |
| 89 refresh_callbacks_.clear(); | |
| 90 refresh_state_ = REFRESH_NONE; | |
| 91 } | |
| 92 | |
| 93 } // namespace policy | |
| OLD | NEW |