Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / remoting / host / policy_hack / policy_watcher.h
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_POLICY_WATCHER_H_
6 #define REMOTING_HOST_POLICY_HACK_POLICY_WATCHER_H_
7
8 #include "base/callback.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/values.h"
11 #include "components/policy/core/common/policy_service.h"
12
13 namespace base {
14 class SingleThreadTaskRunner;
15 class TimeDelta;
16 class WaitableEvent;
17 }  // namespace base
18
19 namespace remoting {
20 namespace policy_hack {
21
22 // Watches for changes to the managed remote access host policies.
23 // If StartWatching() has been called, then before this object can be deleted,
24 // StopWatching() have completed (the provided |done| event must be signaled).
25 class PolicyWatcher {
26  public:
27   // Called first with all policies, and subsequently with any changed policies.
28   typedef base::Callback<void(scoped_ptr<base::DictionaryValue>)>
29       PolicyCallback;
30
31   explicit PolicyWatcher(
32       scoped_refptr<base::SingleThreadTaskRunner> task_runner);
33   virtual ~PolicyWatcher();
34
35   // This guarantees that the |policy_callback| is called at least once with
36   // the current policies.  After that, |policy_callback| will be called
37   // whenever a change to any policy is detected. It will then be called only
38   // with the changed policies.
39   virtual void StartWatching(const PolicyCallback& policy_callback);
40
41   // Should be called after StartWatching() before the object is deleted. Calls
42   // should wait for |stopped_callback| to be called before deleting it.
43   virtual void StopWatching(const base::Closure& stopped_callback);
44
45   // Implemented by each platform.  |task_runner| should be an IO message loop.
46   // |policy_service| is currently only used on ChromeOS.  The caller must
47   // ensure that |policy_service| remains valid for the lifetime of
48   // PolicyWatcher.
49   static scoped_ptr<PolicyWatcher> Create(
50       policy::PolicyService* policy_service,
51       scoped_refptr<base::SingleThreadTaskRunner> task_runner);
52
53   // The name of the NAT traversal policy.
54   static const char kNatPolicyName[];
55
56   // The name of the policy for requiring 2-factor authentication.
57   static const char kHostRequireTwoFactorPolicyName[];
58
59   // The name of the host domain policy.
60   static const char kHostDomainPolicyName[];
61
62   // The name of the username policy. This policy is ignored on Windows.
63   // This policy is currently considered 'internal only' and so is not
64   // documented in policy_templates.json.
65   static const char kHostMatchUsernamePolicyName[];
66
67   // The name of the policy that controls the host talkgadget prefix.
68   static const char kHostTalkGadgetPrefixPolicyName[];
69
70   // The name of the policy for requiring curtain-mode.
71   static const char kHostRequireCurtainPolicyName[];
72
73   // The names of the policies for token authentication URLs.
74   static const char kHostTokenUrlPolicyName[];
75   static const char kHostTokenValidationUrlPolicyName[];
76   static const char kHostTokenValidationCertIssuerPolicyName[];
77
78   // The name of the policy for disabling PIN-less authentication.
79   static const char kHostAllowClientPairing[];
80
81   // The name of the policy for disabling gnubbyd forwarding.
82   static const char kHostAllowGnubbyAuthPolicyName[];
83
84   // The name of the policy for allowing use of relay servers.
85   static const char kRelayPolicyName[];
86
87   // The name of the policy that restricts the range of host UDP ports.
88   static const char kUdpPortRangePolicyName[];
89
90   // The name of the policy for overriding policies, for use in testing.
91   static const char kHostDebugOverridePoliciesName[];
92
93  protected:
94   virtual void StartWatchingInternal() = 0;
95   virtual void StopWatchingInternal() = 0;
96   virtual void Reload() = 0;
97
98   // Used to check if the class is on the right thread.
99   bool OnPolicyWatcherThread() const;
100
101   // Takes the policy dictionary from the OS specific store and extracts the
102   // relevant policies.
103   void UpdatePolicies(const base::DictionaryValue* new_policy);
104
105   // Used for time-based reloads in case something goes wrong with the
106   // notification system.
107   void ScheduleFallbackReloadTask();
108   void ScheduleReloadTask(const base::TimeDelta& delay);
109
110   // Returns a DictionaryValue containing the default values for each policy.
111   const base::DictionaryValue& Defaults() const;
112
113  private:
114   void StopWatchingOnPolicyWatcherThread();
115   scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
116
117   PolicyCallback policy_callback_;
118
119   scoped_ptr<base::DictionaryValue> old_policies_;
120   scoped_ptr<base::DictionaryValue> default_values_;
121   scoped_ptr<base::DictionaryValue> bad_type_values_;
122
123   // Allows us to cancel any inflight FileWatcher events or scheduled reloads.
124   base::WeakPtrFactory<PolicyWatcher> weak_factory_;
125 };
126
127 }  // namespace policy_hack
128 }  // namespace remoting
129
130 #endif  // REMOTING_HOST_POLICY_HACK_POLICY_WATCHER_H_