1 // Copyright 2014 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 "components/policy/core/common/proxy_policy_provider.h" 6 7 #include "base/logging.h" 8 #include "base/memory/scoped_ptr.h" 9 #include "components/policy/core/common/policy_bundle.h" 10 11 namespace policy { 12 13 ProxyPolicyProvider::ProxyPolicyProvider() : delegate_(NULL) {} 14 15 ProxyPolicyProvider::~ProxyPolicyProvider() { 16 DCHECK(!delegate_); 17 } 18 19 void ProxyPolicyProvider::SetDelegate(ConfigurationPolicyProvider* delegate) { 20 if (delegate_) 21 delegate_->RemoveObserver(this); 22 delegate_ = delegate; 23 if (delegate_) { 24 delegate_->AddObserver(this); 25 OnUpdatePolicy(delegate_); 26 } else { 27 UpdatePolicy(scoped_ptr<PolicyBundle>(new PolicyBundle())); 28 } 29 } 30 31 void ProxyPolicyProvider::Shutdown() { 32 // Note: the delegate is not owned by the proxy provider, so this call is not 33 // forwarded. The same applies for the Init() call. 34 // Just drop the delegate without propagating updates here. 35 if (delegate_) { 36 delegate_->RemoveObserver(this); 37 delegate_ = NULL; 38 } 39 ConfigurationPolicyProvider::Shutdown(); 40 } 41 42 void ProxyPolicyProvider::RefreshPolicies() { 43 if (delegate_) { 44 delegate_->RefreshPolicies(); 45 } else { 46 // Subtle: if a RefreshPolicies() call comes after Shutdown() then the 47 // current bundle should be served instead. This also does the right thing 48 // if SetDelegate() was never called before. 49 scoped_ptr<PolicyBundle> bundle(new PolicyBundle()); 50 bundle->CopyFrom(policies()); 51 UpdatePolicy(bundle.Pass()); 52 } 53 } 54 55 void ProxyPolicyProvider::OnUpdatePolicy( 56 ConfigurationPolicyProvider* provider) { 57 DCHECK_EQ(delegate_, provider); 58 scoped_ptr<PolicyBundle> bundle(new PolicyBundle()); 59 bundle->CopyFrom(delegate_->policies()); 60 UpdatePolicy(bundle.Pass()); 61 } 62 63 } // namespace policy 64