Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(148)

Side by Side Diff: remoting/host/policy_hack/nat_policy.cc

Issue 10572005: Use SingleThreadTaskRunner instead of MessageLoopProxy in remoting/host. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Most of this code is copied from: 5 // Most of this code is copied from:
6 // src/chrome/browser/policy/asynchronous_policy_loader.{h,cc} 6 // src/chrome/browser/policy/asynchronous_policy_loader.{h,cc}
7 7
8 #include "remoting/host/policy_hack/nat_policy.h" 8 #include "remoting/host/policy_hack/nat_policy.h"
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/compiler_specific.h" 11 #include "base/compiler_specific.h"
12 #include "base/location.h" 12 #include "base/location.h"
13 #include "base/memory/weak_ptr.h" 13 #include "base/memory/weak_ptr.h"
14 #include "base/message_loop_proxy.h" 14 #include "base/single_thread_task_runner.h"
15 #include "base/synchronization/waitable_event.h" 15 #include "base/synchronization/waitable_event.h"
16 #include "base/time.h" 16 #include "base/time.h"
17 #include "base/values.h" 17 #include "base/values.h"
18 18
19 namespace remoting { 19 namespace remoting {
20 namespace policy_hack { 20 namespace policy_hack {
21 21
22 namespace { 22 namespace {
23 // The time interval for rechecking policy. This is our fallback in case the 23 // The time interval for rechecking policy. This is our fallback in case the
24 // delegate never reports a change to the ReloadObserver. 24 // delegate never reports a change to the ReloadObserver.
25 const int kFallbackReloadDelayMinutes = 15; 25 const int kFallbackReloadDelayMinutes = 15;
26 26
27 } // namespace 27 } // namespace
28 28
29 const char NatPolicy::kNatPolicyName[] = "RemoteAccessHostFirewallTraversal"; 29 const char NatPolicy::kNatPolicyName[] = "RemoteAccessHostFirewallTraversal";
30 30
31 NatPolicy::NatPolicy(base::MessageLoopProxy* message_loop_proxy) 31 NatPolicy::NatPolicy(scoped_refptr<base::SingleThreadTaskRunner> task_runner)
32 : message_loop_proxy_(message_loop_proxy), 32 : task_runner_(task_runner),
33 current_nat_enabled_state_(false), 33 current_nat_enabled_state_(false),
34 first_state_published_(false), 34 first_state_published_(false),
35 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { 35 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
36 } 36 }
37 37
38 NatPolicy::~NatPolicy() { 38 NatPolicy::~NatPolicy() {
39 } 39 }
40 40
41 void NatPolicy::StartWatching(const NatEnabledCallback& nat_enabled_cb) { 41 void NatPolicy::StartWatching(const NatEnabledCallback& nat_enabled_cb) {
42 if (!OnPolicyThread()) { 42 if (!OnPolicyThread()) {
43 message_loop_proxy_->PostTask(FROM_HERE, 43 task_runner_->PostTask(FROM_HERE,
44 base::Bind(&NatPolicy::StartWatching, 44 base::Bind(&NatPolicy::StartWatching,
45 base::Unretained(this), 45 base::Unretained(this),
46 nat_enabled_cb)); 46 nat_enabled_cb));
47 return; 47 return;
48 } 48 }
49 49
50 nat_enabled_cb_ = nat_enabled_cb; 50 nat_enabled_cb_ = nat_enabled_cb;
51 StartWatchingInternal(); 51 StartWatchingInternal();
52 } 52 }
53 53
54 void NatPolicy::StopWatching(base::WaitableEvent* done) { 54 void NatPolicy::StopWatching(base::WaitableEvent* done) {
55 if (!OnPolicyThread()) { 55 if (!OnPolicyThread()) {
56 message_loop_proxy_->PostTask(FROM_HERE, 56 task_runner_->PostTask(FROM_HERE,
57 base::Bind(&NatPolicy::StopWatching, 57 base::Bind(&NatPolicy::StopWatching,
58 base::Unretained(this), done)); 58 base::Unretained(this), done));
59 return; 59 return;
60 } 60 }
61 61
62 StopWatchingInternal(); 62 StopWatchingInternal();
63 weak_factory_.InvalidateWeakPtrs(); 63 weak_factory_.InvalidateWeakPtrs();
64 nat_enabled_cb_.Reset(); 64 nat_enabled_cb_.Reset();
65 65
66 done->Signal(); 66 done->Signal();
67 } 67 }
68 68
69 void NatPolicy::ScheduleFallbackReloadTask() { 69 void NatPolicy::ScheduleFallbackReloadTask() {
70 DCHECK(OnPolicyThread()); 70 DCHECK(OnPolicyThread());
71 ScheduleReloadTask( 71 ScheduleReloadTask(
72 base::TimeDelta::FromMinutes(kFallbackReloadDelayMinutes)); 72 base::TimeDelta::FromMinutes(kFallbackReloadDelayMinutes));
73 } 73 }
74 74
75 void NatPolicy::ScheduleReloadTask(const base::TimeDelta& delay) { 75 void NatPolicy::ScheduleReloadTask(const base::TimeDelta& delay) {
76 DCHECK(OnPolicyThread()); 76 DCHECK(OnPolicyThread());
77 message_loop_proxy_->PostDelayedTask( 77 task_runner_->PostDelayedTask(
78 FROM_HERE, 78 FROM_HERE,
79 base::Bind(&NatPolicy::Reload, weak_factory_.GetWeakPtr()), 79 base::Bind(&NatPolicy::Reload, weak_factory_.GetWeakPtr()),
80 delay); 80 delay);
81 } 81 }
82 82
83 bool NatPolicy::OnPolicyThread() const { 83 bool NatPolicy::OnPolicyThread() const {
84 return message_loop_proxy_->BelongsToCurrentThread(); 84 return task_runner_->BelongsToCurrentThread();
85 } 85 }
86 86
87 void NatPolicy::UpdateNatPolicy(base::DictionaryValue* new_policy) { 87 void NatPolicy::UpdateNatPolicy(base::DictionaryValue* new_policy) {
88 DCHECK(OnPolicyThread()); 88 DCHECK(OnPolicyThread());
89 bool new_nat_enabled_state = false; 89 bool new_nat_enabled_state = false;
90 if (!new_policy->HasKey(kNatPolicyName)) { 90 if (!new_policy->HasKey(kNatPolicyName)) {
91 // If unspecified, the default value of this policy is true. 91 // If unspecified, the default value of this policy is true.
92 new_nat_enabled_state = true; 92 new_nat_enabled_state = true;
93 } else { 93 } else {
94 // Otherwise, try to parse the value and only change from false if we get 94 // Otherwise, try to parse the value and only change from false if we get
95 // a successful read. 95 // a successful read.
96 base::Value* value; 96 base::Value* value;
97 if (new_policy->Get(kNatPolicyName, &value) && 97 if (new_policy->Get(kNatPolicyName, &value) &&
98 value->IsType(base::Value::TYPE_BOOLEAN)) { 98 value->IsType(base::Value::TYPE_BOOLEAN)) {
99 CHECK(value->GetAsBoolean(&new_nat_enabled_state)); 99 CHECK(value->GetAsBoolean(&new_nat_enabled_state));
100 } 100 }
101 } 101 }
102 102
103 if (!first_state_published_ || 103 if (!first_state_published_ ||
104 (new_nat_enabled_state != current_nat_enabled_state_)) { 104 (new_nat_enabled_state != current_nat_enabled_state_)) {
105 first_state_published_ = true; 105 first_state_published_ = true;
106 current_nat_enabled_state_ = new_nat_enabled_state; 106 current_nat_enabled_state_ = new_nat_enabled_state;
107 nat_enabled_cb_.Run(current_nat_enabled_state_); 107 nat_enabled_cb_.Run(current_nat_enabled_state_);
108 } 108 }
109 } 109 }
110 110
111 } // namespace policy_hack 111 } // namespace policy_hack
112 } // namespace remoting 112 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698