| 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_manager.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 9 #include "base/logging.h" |
| 10 #include "chrome/browser/policy/cloud_policy_refresh_scheduler.h" |
| 11 #include "chrome/browser/policy/cloud_policy_service.h" |
| 12 #include "chrome/browser/policy/policy_bundle.h" |
| 13 #include "chrome/browser/policy/policy_map.h" |
| 14 |
| 15 namespace policy { |
| 16 |
| 17 CloudPolicyManager::CloudPolicyManager(scoped_ptr<CloudPolicyStore> store) |
| 18 : store_(store.Pass()), |
| 19 waiting_for_policy_refresh_(false) { |
| 20 store_->AddObserver(this); |
| 21 store_->Load(); |
| 22 } |
| 23 |
| 24 CloudPolicyManager::~CloudPolicyManager() { |
| 25 ShutdownService(); |
| 26 store_->RemoveObserver(this); |
| 27 } |
| 28 |
| 29 bool CloudPolicyManager::IsInitializationComplete() const { |
| 30 return store_->is_initialized(); |
| 31 } |
| 32 |
| 33 void CloudPolicyManager::RefreshPolicies() { |
| 34 if (service_.get()) { |
| 35 waiting_for_policy_refresh_ = true; |
| 36 service_->RefreshPolicy( |
| 37 base::Bind(&CloudPolicyManager::OnRefreshComplete, |
| 38 base::Unretained(this))); |
| 39 } else { |
| 40 OnRefreshComplete(); |
| 41 } |
| 42 } |
| 43 |
| 44 void CloudPolicyManager::OnStoreLoaded(CloudPolicyStore* store) { |
| 45 DCHECK_EQ(store_.get(), store); |
| 46 CheckAndPublishPolicy(); |
| 47 } |
| 48 |
| 49 void CloudPolicyManager::OnStoreError(CloudPolicyStore* store) { |
| 50 DCHECK_EQ(store_.get(), store); |
| 51 // No action required, the old policy is still valid. |
| 52 } |
| 53 |
| 54 void CloudPolicyManager::InitializeService( |
| 55 scoped_ptr<CloudPolicyClient> client) { |
| 56 CHECK(!client_.get()); |
| 57 CHECK(client.get()); |
| 58 client_ = client.Pass(); |
| 59 service_.reset(new CloudPolicyService(client_.get(), store_.get())); |
| 60 } |
| 61 |
| 62 void CloudPolicyManager::ShutdownService() { |
| 63 refresh_scheduler_.reset(); |
| 64 service_.reset(); |
| 65 client_.reset(); |
| 66 } |
| 67 |
| 68 void CloudPolicyManager::StartRefreshScheduler( |
| 69 PrefService* local_state, |
| 70 const std::string& refresh_rate_pref) { |
| 71 if (!refresh_scheduler_.get()) { |
| 72 refresh_scheduler_.reset( |
| 73 new CloudPolicyRefreshScheduler( |
| 74 client_.get(), store_.get(), local_state, refresh_rate_pref, |
| 75 MessageLoop::current()->message_loop_proxy())); |
| 76 } |
| 77 } |
| 78 |
| 79 void CloudPolicyManager::CheckAndPublishPolicy() { |
| 80 if (IsInitializationComplete() && !waiting_for_policy_refresh_) { |
| 81 scoped_ptr<PolicyBundle> bundle(new PolicyBundle()); |
| 82 bundle->Get(POLICY_DOMAIN_CHROME, std::string()).CopyFrom( |
| 83 store_->policy_map()); |
| 84 UpdatePolicy(bundle.Pass()); |
| 85 } |
| 86 } |
| 87 |
| 88 void CloudPolicyManager::OnRefreshComplete() { |
| 89 waiting_for_policy_refresh_ = false; |
| 90 CheckAndPublishPolicy(); |
| 91 } |
| 92 |
| 93 } // namespace policy |
| OLD | NEW |