OLD | NEW |
(Empty) | |
| 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 // Most of this code is copied from: |
| 6 // src/chrome/browser/policy/asynchronous_policy_loader.{h,cc} |
| 7 |
| 8 #include "remoting/host/policy_hack/policy_watcher.h" |
| 9 |
| 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/synchronization/waitable_event.h" |
| 16 #include "base/time.h" |
| 17 #include "base/values.h" |
| 18 |
| 19 namespace remoting { |
| 20 namespace policy_hack { |
| 21 |
| 22 namespace { |
| 23 // The time interval for rechecking policy. This is our fallback in case the |
| 24 // delegate never reports a change to the ReloadObserver. |
| 25 const int kFallbackReloadDelayMinutes = 15; |
| 26 |
| 27 // Gets a boolean from a dictionary, or returns a default value if the boolean |
| 28 // couldn't be read. |
| 29 bool GetBooleanOrDefault(const base::DictionaryValue* dict, const char* key, |
| 30 bool default_if_value_missing, |
| 31 bool default_if_value_not_boolean) { |
| 32 if (!dict->HasKey(key)) { |
| 33 return default_if_value_missing; |
| 34 } |
| 35 base::Value* value; |
| 36 if (dict->Get(key, &value) && value->IsType(base::Value::TYPE_BOOLEAN)) { |
| 37 bool bool_value; |
| 38 CHECK(value->GetAsBoolean(&bool_value)); |
| 39 return bool_value; |
| 40 } |
| 41 return default_if_value_not_boolean; |
| 42 } |
| 43 |
| 44 // Copies a boolean from one dictionary to another, using a default value |
| 45 // if the boolean couldn't be read from the first dictionary. |
| 46 void CopyBooleanOrDefault(base::DictionaryValue* to, |
| 47 const base::DictionaryValue* from, const char* key, |
| 48 bool default_if_value_missing, |
| 49 bool default_if_value_not_boolean) { |
| 50 to->Set(key, base::Value::CreateBooleanValue( |
| 51 GetBooleanOrDefault(from, key, default_if_value_missing, |
| 52 default_if_value_not_boolean))); |
| 53 } |
| 54 |
| 55 // Copies all policy values from one dictionary to another, using default values |
| 56 // when necessary. |
| 57 scoped_ptr<base::DictionaryValue> AddDefaultValuesWhenNecessary( |
| 58 const base::DictionaryValue* from) { |
| 59 scoped_ptr<base::DictionaryValue> to(new base::DictionaryValue()); |
| 60 CopyBooleanOrDefault(to.get(), from, |
| 61 PolicyWatcher::kNatPolicyName, true, false); |
| 62 return to.Pass(); |
| 63 } |
| 64 |
| 65 } // namespace |
| 66 |
| 67 const char PolicyWatcher::kNatPolicyName[] = |
| 68 "RemoteAccessHostFirewallTraversal"; |
| 69 |
| 70 const char* const PolicyWatcher::kBooleanPolicyNames[] = |
| 71 { PolicyWatcher::kNatPolicyName }; |
| 72 |
| 73 const int PolicyWatcher::kBooleanPolicyNamesNum = |
| 74 arraysize(kBooleanPolicyNames); |
| 75 |
| 76 PolicyWatcher::PolicyWatcher( |
| 77 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 78 : task_runner_(task_runner), |
| 79 old_policies_(new base::DictionaryValue()), |
| 80 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 81 } |
| 82 |
| 83 PolicyWatcher::~PolicyWatcher() { |
| 84 } |
| 85 |
| 86 void PolicyWatcher::StartWatching(const PolicyCallback& policy_callback) { |
| 87 if (!OnPolicyWatcherThread()) { |
| 88 task_runner_->PostTask(FROM_HERE, |
| 89 base::Bind(&PolicyWatcher::StartWatching, |
| 90 base::Unretained(this), |
| 91 policy_callback)); |
| 92 return; |
| 93 } |
| 94 |
| 95 policy_callback_ = policy_callback; |
| 96 StartWatchingInternal(); |
| 97 } |
| 98 |
| 99 void PolicyWatcher::StopWatching(base::WaitableEvent* done) { |
| 100 if (!OnPolicyWatcherThread()) { |
| 101 task_runner_->PostTask(FROM_HERE, |
| 102 base::Bind(&PolicyWatcher::StopWatching, |
| 103 base::Unretained(this), done)); |
| 104 return; |
| 105 } |
| 106 |
| 107 StopWatchingInternal(); |
| 108 weak_factory_.InvalidateWeakPtrs(); |
| 109 policy_callback_.Reset(); |
| 110 |
| 111 done->Signal(); |
| 112 } |
| 113 |
| 114 void PolicyWatcher::ScheduleFallbackReloadTask() { |
| 115 DCHECK(OnPolicyWatcherThread()); |
| 116 ScheduleReloadTask( |
| 117 base::TimeDelta::FromMinutes(kFallbackReloadDelayMinutes)); |
| 118 } |
| 119 |
| 120 void PolicyWatcher::ScheduleReloadTask(const base::TimeDelta& delay) { |
| 121 DCHECK(OnPolicyWatcherThread()); |
| 122 task_runner_->PostDelayedTask( |
| 123 FROM_HERE, |
| 124 base::Bind(&PolicyWatcher::Reload, weak_factory_.GetWeakPtr()), |
| 125 delay); |
| 126 } |
| 127 |
| 128 bool PolicyWatcher::OnPolicyWatcherThread() const { |
| 129 return task_runner_->BelongsToCurrentThread(); |
| 130 } |
| 131 |
| 132 void PolicyWatcher::UpdatePolicies( |
| 133 const base::DictionaryValue* new_policies_raw) { |
| 134 DCHECK(OnPolicyWatcherThread()); |
| 135 |
| 136 // Use default values for any missing policies. |
| 137 scoped_ptr<base::DictionaryValue> new_policies = |
| 138 AddDefaultValuesWhenNecessary(new_policies_raw); |
| 139 |
| 140 // Find the changed policies. |
| 141 scoped_ptr<base::DictionaryValue> changed_policies( |
| 142 new base::DictionaryValue()); |
| 143 base::DictionaryValue::Iterator iter(*new_policies); |
| 144 while (iter.HasNext()) { |
| 145 base::Value* old_policy; |
| 146 if (!(old_policies_->Get(iter.key(), &old_policy) && |
| 147 old_policy->Equals(&iter.value()))) { |
| 148 changed_policies->Set(iter.key(), iter.value().DeepCopy()); |
| 149 } |
| 150 iter.Advance(); |
| 151 } |
| 152 |
| 153 // Save the new policies. |
| 154 old_policies_.swap(new_policies); |
| 155 |
| 156 // Notify our client of the changed policies. |
| 157 if (!changed_policies->empty()) { |
| 158 policy_callback_.Run(changed_policies.Pass()); |
| 159 } |
| 160 } |
| 161 |
| 162 } // namespace policy_hack |
| 163 } // namespace remoting |
OLD | NEW |