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.
5 // Most of this code is copied from:
6 // src/chrome/browser/policy/asynchronous_policy_loader.{h,cc}
8 #include "remoting/host/policy_hack/policy_watcher.h"
10 #include "base/bind.h"
11 #include "base/compiler_specific.h"
12 #include "base/location.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/single_thread_task_runner.h"
15 #include "base/time/time.h"
16 #include "base/values.h"
17 #include "remoting/host/dns_blackhole_checker.h"
20 #include "base/json/json_reader.h"
24 namespace policy_hack
{
28 // The time interval for rechecking policy. This is our fallback in case the
29 // delegate never reports a change to the ReloadObserver.
30 const int kFallbackReloadDelayMinutes
= 15;
32 // Copies all policy values from one dictionary to another, using values from
33 // |default| if they are not set in |from|, or values from |bad_type_values| if
34 // the value in |from| has the wrong type.
35 scoped_ptr
<base::DictionaryValue
> CopyGoodValuesAndAddDefaults(
36 const base::DictionaryValue
* from
,
37 const base::DictionaryValue
* default_values
,
38 const base::DictionaryValue
* bad_type_values
) {
39 scoped_ptr
<base::DictionaryValue
> to(default_values
->DeepCopy());
40 for (base::DictionaryValue::Iterator
i(*default_values
);
41 !i
.IsAtEnd(); i
.Advance()) {
43 const base::Value
* value
= NULL
;
45 // If the policy isn't in |from|, use the default.
46 if (!from
->Get(i
.key(), &value
)) {
50 // If the policy is the wrong type, use the value from |bad_type_values|.
51 if (!value
->IsType(i
.value().GetType())) {
52 CHECK(bad_type_values
->Get(i
.key(), &value
));
55 to
->Set(i
.key(), value
->DeepCopy());
59 // Replace values with those specified in DebugOverridePolicies, if present.
60 std::string policy_overrides
;
61 if (from
->GetString(PolicyWatcher::kHostDebugOverridePoliciesName
,
63 scoped_ptr
<base::Value
> value(base::JSONReader::Read(policy_overrides
));
64 const base::DictionaryValue
* override_values
;
65 if (value
&& value
->GetAsDictionary(&override_values
)) {
66 to
->MergeDictionary(override_values
);
69 #endif // defined(NDEBUG)
76 const char PolicyWatcher::kNatPolicyName
[] =
77 "RemoteAccessHostFirewallTraversal";
79 const char PolicyWatcher::kHostRequireTwoFactorPolicyName
[] =
80 "RemoteAccessHostRequireTwoFactor";
82 const char PolicyWatcher::kHostDomainPolicyName
[] =
83 "RemoteAccessHostDomain";
85 const char PolicyWatcher::kHostMatchUsernamePolicyName
[] =
86 "RemoteAccessHostMatchUsername";
88 const char PolicyWatcher::kHostTalkGadgetPrefixPolicyName
[] =
89 "RemoteAccessHostTalkGadgetPrefix";
91 const char PolicyWatcher::kHostRequireCurtainPolicyName
[] =
92 "RemoteAccessHostRequireCurtain";
94 const char PolicyWatcher::kHostTokenUrlPolicyName
[] =
95 "RemoteAccessHostTokenUrl";
97 const char PolicyWatcher::kHostTokenValidationUrlPolicyName
[] =
98 "RemoteAccessHostTokenValidationUrl";
100 const char PolicyWatcher::kHostTokenValidationCertIssuerPolicyName
[] =
101 "RemoteAccessHostTokenValidationCertificateIssuer";
103 const char PolicyWatcher::kHostAllowClientPairing
[] =
104 "RemoteAccessHostAllowClientPairing";
106 const char PolicyWatcher::kHostAllowGnubbyAuthPolicyName
[] =
107 "RemoteAccessHostAllowGnubbyAuth";
109 const char PolicyWatcher::kRelayPolicyName
[] =
110 "RemoteAccessHostAllowRelayedConnection";
112 const char PolicyWatcher::kUdpPortRangePolicyName
[] =
113 "RemoteAccessHostUdpPortRange";
115 const char PolicyWatcher::kHostDebugOverridePoliciesName
[] =
116 "RemoteAccessHostDebugOverridePolicies";
118 PolicyWatcher::PolicyWatcher(
119 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
)
120 : task_runner_(task_runner
),
121 old_policies_(new base::DictionaryValue()),
122 default_values_(new base::DictionaryValue()),
123 weak_factory_(this) {
124 // Initialize the default values for each policy.
125 default_values_
->SetBoolean(kNatPolicyName
, true);
126 default_values_
->SetBoolean(kHostRequireTwoFactorPolicyName
, false);
127 default_values_
->SetBoolean(kHostRequireCurtainPolicyName
, false);
128 default_values_
->SetBoolean(kHostMatchUsernamePolicyName
, false);
129 default_values_
->SetString(kHostDomainPolicyName
, std::string());
130 default_values_
->SetString(kHostTalkGadgetPrefixPolicyName
,
131 kDefaultHostTalkGadgetPrefix
);
132 default_values_
->SetString(kHostTokenUrlPolicyName
, std::string());
133 default_values_
->SetString(kHostTokenValidationUrlPolicyName
, std::string());
134 default_values_
->SetString(kHostTokenValidationCertIssuerPolicyName
,
136 default_values_
->SetBoolean(kHostAllowClientPairing
, true);
137 default_values_
->SetBoolean(kHostAllowGnubbyAuthPolicyName
, true);
138 default_values_
->SetBoolean(kRelayPolicyName
, true);
139 default_values_
->SetString(kUdpPortRangePolicyName
, "");
141 default_values_
->SetString(kHostDebugOverridePoliciesName
, std::string());
144 // Initialize the fall-back values to use for unreadable policies.
145 // For most policies these match the defaults.
146 bad_type_values_
.reset(default_values_
->DeepCopy());
147 bad_type_values_
->SetBoolean(kNatPolicyName
, false);
148 bad_type_values_
->SetBoolean(kRelayPolicyName
, false);
151 PolicyWatcher::~PolicyWatcher() {
154 void PolicyWatcher::StartWatching(const PolicyCallback
& policy_callback
) {
155 if (!OnPolicyWatcherThread()) {
156 task_runner_
->PostTask(FROM_HERE
,
157 base::Bind(&PolicyWatcher::StartWatching
,
158 base::Unretained(this),
163 policy_callback_
= policy_callback
;
164 StartWatchingInternal();
167 void PolicyWatcher::StopWatching(const base::Closure
& stopped_callback
) {
168 task_runner_
->PostTaskAndReply(
169 FROM_HERE
, base::Bind(&PolicyWatcher::StopWatchingOnPolicyWatcherThread
,
170 base::Unretained(this)),
174 void PolicyWatcher::StopWatchingOnPolicyWatcherThread() {
175 StopWatchingInternal();
176 weak_factory_
.InvalidateWeakPtrs();
177 policy_callback_
.Reset();
180 void PolicyWatcher::ScheduleFallbackReloadTask() {
181 DCHECK(OnPolicyWatcherThread());
183 base::TimeDelta::FromMinutes(kFallbackReloadDelayMinutes
));
186 void PolicyWatcher::ScheduleReloadTask(const base::TimeDelta
& delay
) {
187 DCHECK(OnPolicyWatcherThread());
188 task_runner_
->PostDelayedTask(
190 base::Bind(&PolicyWatcher::Reload
, weak_factory_
.GetWeakPtr()),
194 const base::DictionaryValue
& PolicyWatcher::Defaults() const {
195 return *default_values_
;
198 bool PolicyWatcher::OnPolicyWatcherThread() const {
199 return task_runner_
->BelongsToCurrentThread();
202 void PolicyWatcher::UpdatePolicies(
203 const base::DictionaryValue
* new_policies_raw
) {
204 DCHECK(OnPolicyWatcherThread());
206 // Use default values for any missing policies.
207 scoped_ptr
<base::DictionaryValue
> new_policies
=
208 CopyGoodValuesAndAddDefaults(
209 new_policies_raw
, default_values_
.get(), bad_type_values_
.get());
211 // Find the changed policies.
212 scoped_ptr
<base::DictionaryValue
> changed_policies(
213 new base::DictionaryValue());
214 base::DictionaryValue::Iterator
iter(*new_policies
);
215 while (!iter
.IsAtEnd()) {
216 base::Value
* old_policy
;
217 if (!(old_policies_
->Get(iter
.key(), &old_policy
) &&
218 old_policy
->Equals(&iter
.value()))) {
219 changed_policies
->Set(iter
.key(), iter
.value().DeepCopy());
224 // Save the new policies.
225 old_policies_
.swap(new_policies
);
227 // Notify our client of the changed policies.
228 if (!changed_policies
->empty()) {
229 policy_callback_
.Run(changed_policies
.Pass());
233 } // namespace policy_hack
234 } // namespace remoting