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/nat_policy.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 } // namespace | |
28 | |
29 const char NatPolicy::kNatPolicyName[] = "RemoteAccessHostFirewallTraversal"; | |
30 | |
31 NatPolicy::NatPolicy(scoped_refptr<base::SingleThreadTaskRunner> task_runner) | |
32 : task_runner_(task_runner), | |
33 current_nat_enabled_state_(false), | |
34 first_state_published_(false), | |
35 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | |
36 } | |
37 | |
38 NatPolicy::~NatPolicy() { | |
39 } | |
40 | |
41 void NatPolicy::StartWatching(const NatEnabledCallback& nat_enabled_cb) { | |
42 if (!OnPolicyThread()) { | |
43 task_runner_->PostTask(FROM_HERE, | |
44 base::Bind(&NatPolicy::StartWatching, | |
45 base::Unretained(this), | |
46 nat_enabled_cb)); | |
47 return; | |
48 } | |
49 | |
50 nat_enabled_cb_ = nat_enabled_cb; | |
51 StartWatchingInternal(); | |
52 } | |
53 | |
54 void NatPolicy::StopWatching(base::WaitableEvent* done) { | |
55 if (!OnPolicyThread()) { | |
56 task_runner_->PostTask(FROM_HERE, | |
57 base::Bind(&NatPolicy::StopWatching, | |
58 base::Unretained(this), done)); | |
59 return; | |
60 } | |
61 | |
62 StopWatchingInternal(); | |
63 weak_factory_.InvalidateWeakPtrs(); | |
64 nat_enabled_cb_.Reset(); | |
65 | |
66 done->Signal(); | |
67 } | |
68 | |
69 void NatPolicy::ScheduleFallbackReloadTask() { | |
70 DCHECK(OnPolicyThread()); | |
71 ScheduleReloadTask( | |
72 base::TimeDelta::FromMinutes(kFallbackReloadDelayMinutes)); | |
73 } | |
74 | |
75 void NatPolicy::ScheduleReloadTask(const base::TimeDelta& delay) { | |
76 DCHECK(OnPolicyThread()); | |
77 task_runner_->PostDelayedTask( | |
78 FROM_HERE, | |
79 base::Bind(&NatPolicy::Reload, weak_factory_.GetWeakPtr()), | |
80 delay); | |
81 } | |
82 | |
83 bool NatPolicy::OnPolicyThread() const { | |
84 return task_runner_->BelongsToCurrentThread(); | |
85 } | |
86 | |
87 void NatPolicy::UpdateNatPolicy(base::DictionaryValue* new_policy) { | |
88 DCHECK(OnPolicyThread()); | |
89 bool new_nat_enabled_state = false; | |
90 if (!new_policy->HasKey(kNatPolicyName)) { | |
91 // If unspecified, the default value of this policy is true. | |
92 new_nat_enabled_state = true; | |
93 } else { | |
94 // Otherwise, try to parse the value and only change from false if we get | |
95 // a successful read. | |
96 base::Value* value; | |
97 if (new_policy->Get(kNatPolicyName, &value) && | |
98 value->IsType(base::Value::TYPE_BOOLEAN)) { | |
99 CHECK(value->GetAsBoolean(&new_nat_enabled_state)); | |
100 } | |
101 } | |
102 | |
103 if (!first_state_published_ || | |
104 (new_nat_enabled_state != current_nat_enabled_state_)) { | |
105 first_state_published_ = true; | |
106 current_nat_enabled_state_ = new_nat_enabled_state; | |
107 nat_enabled_cb_.Run(current_nat_enabled_state_); | |
108 } | |
109 } | |
110 | |
111 } // namespace policy_hack | |
112 } // namespace remoting | |
OLD | NEW |