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 #ifndef REMOTING_HOST_POLICY_HACK_NAT_POLICY_H_ | |
6 #define REMOTING_HOST_POLICY_HACK_NAT_POLICY_H_ | |
7 | |
8 #include "base/callback.h" | |
9 #include "base/memory/weak_ptr.h" | |
10 | |
11 namespace base { | |
12 class DictionaryValue; | |
13 class SingleThreadTaskRunner; | |
14 class TimeDelta; | |
15 class WaitableEvent; | |
16 } // namespace base | |
17 | |
18 namespace remoting { | |
19 namespace policy_hack { | |
20 | |
21 // Watches for changes to the managed remote access host NAT policies. | |
22 // If StartWatching() has been called, then before this object can be deleted, | |
23 // StopWatching() have completed (the provided |done| event must be signaled). | |
24 class NatPolicy { | |
25 public: | |
26 // Called with the current status of whether or not NAT traversal is enabled. | |
27 typedef base::Callback<void(bool)> NatEnabledCallback; | |
28 | |
29 explicit NatPolicy(scoped_refptr<base::SingleThreadTaskRunner> task_runner); | |
30 virtual ~NatPolicy(); | |
31 | |
32 // This guarantees that the |nat_enabled_cb| is called at least once with | |
33 // the current policy. After that, |nat_enabled_cb| will be called whenever | |
34 // a change to the nat policy is detected. | |
35 virtual void StartWatching(const NatEnabledCallback& nat_enabled_cb); | |
36 | |
37 // Should be called after StartWatching() before the object is deleted. Calls | |
38 // just wait for |done| to be signaled before deleting the object. | |
39 virtual void StopWatching(base::WaitableEvent* done); | |
40 | |
41 // Implemented by each platform. This message loop should be an IO message | |
42 // loop. | |
43 static NatPolicy* Create( | |
44 scoped_refptr<base::SingleThreadTaskRunner> task_runner); | |
45 | |
46 protected: | |
47 virtual void StartWatchingInternal() = 0; | |
48 virtual void StopWatchingInternal() = 0; | |
49 virtual void Reload() = 0; | |
50 | |
51 // Used to check if the class is on the right thread. | |
52 bool OnPolicyThread() const; | |
53 | |
54 // Takes the policy dictionary from the OS specific store and extracts the | |
55 // NAT traversal setting. | |
56 void UpdateNatPolicy(base::DictionaryValue* new_policy); | |
57 | |
58 // Used for time-based reloads in case something goes wrong with the | |
59 // notification system. | |
60 void ScheduleFallbackReloadTask(); | |
61 void ScheduleReloadTask(const base::TimeDelta& delay); | |
62 | |
63 static const char kNatPolicyName[]; | |
64 | |
65 private: | |
66 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
67 | |
68 NatEnabledCallback nat_enabled_cb_; | |
69 bool current_nat_enabled_state_; | |
70 bool first_state_published_; | |
71 | |
72 // Allows us to cancel any inflight FileWatcher events or scheduled reloads. | |
73 base::WeakPtrFactory<NatPolicy> weak_factory_; | |
74 }; | |
75 | |
76 } // namespace policy_hack | |
77 } // namespace remoting | |
78 | |
79 #endif // REMOTING_HOST_POLICY_HACK_NAT_POLICY_H_ | |
OLD | NEW |