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/cloud_policy_core.h" 6 7 #include "base/bind.h" 8 #include "base/bind_helpers.h" 9 #include "base/message_loop/message_loop.h" 10 #include "base/prefs/pref_service.h" 11 #include "chrome/browser/policy/cloud/cloud_policy_client.h" 12 #include "chrome/browser/policy/cloud/cloud_policy_refresh_scheduler.h" 13 #include "chrome/browser/policy/cloud/cloud_policy_service.h" 14 #include "chrome/browser/policy/cloud/cloud_policy_store.h" 15 16 namespace policy { 17 18 CloudPolicyCore::CloudPolicyCore(const PolicyNamespaceKey& key, 19 CloudPolicyStore* store) 20 : policy_ns_key_(key), 21 store_(store) {} 22 23 CloudPolicyCore::~CloudPolicyCore() {} 24 25 void CloudPolicyCore::Connect(scoped_ptr<CloudPolicyClient> client) { 26 CHECK(!client_); 27 CHECK(client); 28 client_ = client.Pass(); 29 service_.reset(new CloudPolicyService(policy_ns_key_, client_.get(), store_)); 30 } 31 32 void CloudPolicyCore::Disconnect() { 33 refresh_delay_.reset(); 34 refresh_scheduler_.reset(); 35 service_.reset(); 36 client_.reset(); 37 } 38 39 void CloudPolicyCore::RefreshSoon() { 40 if (refresh_scheduler_) 41 refresh_scheduler_->RefreshSoon(); 42 } 43 44 void CloudPolicyCore::StartRefreshScheduler() { 45 if (!refresh_scheduler_) { 46 refresh_scheduler_.reset( 47 new CloudPolicyRefreshScheduler( 48 client_.get(), store_, 49 base::MessageLoop::current()->message_loop_proxy())); 50 UpdateRefreshDelayFromPref(); 51 } 52 } 53 54 void CloudPolicyCore::TrackRefreshDelayPref( 55 PrefService* pref_service, 56 const std::string& refresh_pref_name) { 57 refresh_delay_.reset(new IntegerPrefMember()); 58 refresh_delay_->Init( 59 refresh_pref_name.c_str(), pref_service, 60 base::Bind(&CloudPolicyCore::UpdateRefreshDelayFromPref, 61 base::Unretained(this))); 62 UpdateRefreshDelayFromPref(); 63 } 64 65 void CloudPolicyCore::UpdateRefreshDelayFromPref() { 66 if (refresh_scheduler_ && refresh_delay_) 67 refresh_scheduler_->SetRefreshDelay(refresh_delay_->GetValue()); 68 } 69 70 } // namespace policy 71