Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / policy / core / common / proxy_policy_provider.cc
blob7a8919759c20fdbe9d0535a18f4df3e587518483
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.
5 #include "components/policy/core/common/proxy_policy_provider.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "components/policy/core/common/policy_bundle.h"
11 namespace policy {
13 ProxyPolicyProvider::ProxyPolicyProvider() : delegate_(NULL) {}
15 ProxyPolicyProvider::~ProxyPolicyProvider() {
16 DCHECK(!delegate_);
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()));
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;
39 ConfigurationPolicyProvider::Shutdown();
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());
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());
63 } // namespace policy