summaryrefslogtreecommitdiff
path: root/components/policy/core/common/schema_registry_tracking_policy_provider.cc
blob: 5f15b33fda1b58eb879ddf483affa97df7718fc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/policy/core/common/schema_registry_tracking_policy_provider.h"

#include <utility>

#include "components/policy/core/common/schema_map.h"
#include "components/policy/core/common/schema_registry.h"

namespace policy {

SchemaRegistryTrackingPolicyProvider::SchemaRegistryTrackingPolicyProvider(
    ConfigurationPolicyProvider* delegate)
    : delegate_(delegate), state_(WAITING_FOR_REGISTRY_READY) {
  delegate_->AddObserver(this);
  // Serve the initial |delegate_| policies.
  OnUpdatePolicy(delegate_);
}

SchemaRegistryTrackingPolicyProvider::~SchemaRegistryTrackingPolicyProvider() {
  delegate_->RemoveObserver(this);
}

void SchemaRegistryTrackingPolicyProvider::Init(SchemaRegistry* registry) {
  ConfigurationPolicyProvider::Init(registry);
  if (registry->IsReady())
    OnSchemaRegistryReady();
}

bool SchemaRegistryTrackingPolicyProvider::IsInitializationComplete(
    PolicyDomain domain) const {
  if (domain == POLICY_DOMAIN_CHROME)
    return delegate_->IsInitializationComplete(domain);
  // This provider keeps its own state for all the other domains.
  return state_ == READY;
}

void SchemaRegistryTrackingPolicyProvider::RefreshPolicies() {
  delegate_->RefreshPolicies();
}

void SchemaRegistryTrackingPolicyProvider::OnSchemaRegistryReady() {
  DCHECK_EQ(WAITING_FOR_REGISTRY_READY, state_);
  // This provider's registry is ready, meaning that it has all the initial
  // components schemas; the delegate's registry should also see them now,
  // since it's tracking the former.
  // Asking the delegate to RefreshPolicies now means that the next
  // OnUpdatePolicy from the delegate will have the initial policy for
  // components.
  if (!schema_map()->HasComponents()) {
    // If there are no component registered for this provider then there's no
    // need to reload.
    state_ = READY;
    OnUpdatePolicy(delegate_);
    return;
  }

  state_ = WAITING_FOR_REFRESH;
  RefreshPolicies();
}

void SchemaRegistryTrackingPolicyProvider::OnSchemaRegistryUpdated(
    bool has_new_schemas) {
  if (state_ != READY)
    return;
  if (has_new_schemas) {
    RefreshPolicies();
  } else {
    // Remove the policies that were being served for the component that have
    // been removed. This is important so that update notifications are also
    // sent in case those component are reinstalled during the current session.
    OnUpdatePolicy(delegate_);
  }
}

void SchemaRegistryTrackingPolicyProvider::OnUpdatePolicy(
    ConfigurationPolicyProvider* provider) {
  DCHECK_EQ(delegate_, provider);

  if (state_ == WAITING_FOR_REFRESH)
    state_ = READY;

  std::unique_ptr<PolicyBundle> bundle(new PolicyBundle());
  if (state_ == READY) {
    bundle->CopyFrom(delegate_->policies());
    schema_map()->FilterBundle(bundle.get());
  } else {
    // Always pass on the Chrome policy, even if the components are not ready
    // yet.
    const PolicyNamespace chrome_ns(POLICY_DOMAIN_CHROME, "");
    bundle->Get(chrome_ns).CopyFrom(delegate_->policies().Get(chrome_ns));
  }

  UpdatePolicy(std::move(bundle));
}

}  // namespace policy