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 various classes in | |
6 // src/chrome/browser/policy. In particular, look at | |
7 // | |
8 // configuration_policy_provider_delegate_win.{h,cc} | |
9 // configuration_policy_loader_win.{h,cc} | |
10 // | |
11 // This is a reduction of the functionality in those classes. | |
12 | |
13 #include "remoting/host/policy_hack/nat_policy.h" | |
14 | |
15 #include <userenv.h> | |
16 | |
17 #include "base/compiler_specific.h" | |
18 #include "base/memory/scoped_ptr.h" | |
19 #include "base/message_loop_proxy.h" | |
20 #include "base/string16.h" | |
21 #include "base/synchronization/waitable_event.h" | |
22 #include "base/utf_string_conversions.h" | |
23 #include "base/values.h" | |
24 #include "base/win/object_watcher.h" | |
25 #include "base/win/registry.h" | |
26 | |
27 // userenv.dll is required for RegisterGPNotification(). | |
28 #pragma comment(lib, "userenv.lib") | |
29 | |
30 using base::win::RegKey; | |
31 | |
32 namespace remoting { | |
33 namespace policy_hack { | |
34 | |
35 namespace { | |
36 | |
37 const wchar_t kRegistrySubKey[] = L"SOFTWARE\\Policies\\Google\\Chrome"; | |
38 | |
39 } // namespace | |
40 | |
41 class NatPolicyWin : | |
42 public NatPolicy, | |
43 public base::win::ObjectWatcher::Delegate { | |
44 public: | |
45 explicit NatPolicyWin(scoped_refptr<base::SingleThreadTaskRunner> task_runner) | |
46 : NatPolicy(task_runner), | |
47 user_policy_changed_event_(false, false), | |
48 machine_policy_changed_event_(false, false), | |
49 user_policy_watcher_failed_(false), | |
50 machine_policy_watcher_failed_(false) { | |
51 } | |
52 | |
53 virtual ~NatPolicyWin() { | |
54 } | |
55 | |
56 virtual void StartWatchingInternal() OVERRIDE { | |
57 DCHECK(OnPolicyThread()); | |
58 | |
59 if (!RegisterGPNotification(user_policy_changed_event_.handle(), false)) { | |
60 PLOG(WARNING) << "Failed to register user group policy notification"; | |
61 user_policy_watcher_failed_ = true; | |
62 } | |
63 | |
64 if (!RegisterGPNotification(machine_policy_changed_event_.handle(), true)) { | |
65 PLOG(WARNING) << "Failed to register machine group policy notification."; | |
66 machine_policy_watcher_failed_ = true; | |
67 } | |
68 | |
69 Reload(); | |
70 } | |
71 | |
72 virtual void StopWatchingInternal() OVERRIDE { | |
73 DCHECK(OnPolicyThread()); | |
74 | |
75 if (!UnregisterGPNotification(user_policy_changed_event_.handle())) { | |
76 PLOG(WARNING) << "Failed to unregister user group policy notification"; | |
77 } | |
78 | |
79 if (!UnregisterGPNotification(machine_policy_changed_event_.handle())) { | |
80 PLOG(WARNING) << | |
81 "Failed to unregister machine group policy notification."; | |
82 } | |
83 | |
84 user_policy_watcher_.StopWatching(); | |
85 machine_policy_watcher_.StopWatching(); | |
86 } | |
87 | |
88 private: | |
89 // Updates the watchers and schedules the reload task if appropriate. | |
90 void SetupWatches() { | |
91 DCHECK(OnPolicyThread()); | |
92 | |
93 if (!user_policy_watcher_failed_ && | |
94 !user_policy_watcher_.GetWatchedObject() && | |
95 !user_policy_watcher_.StartWatching( | |
96 user_policy_changed_event_.handle(), this)) { | |
97 LOG(WARNING) << "Failed to start watch for user policy change event"; | |
98 user_policy_watcher_failed_ = true; | |
99 } | |
100 | |
101 if (!machine_policy_watcher_failed_ && | |
102 !machine_policy_watcher_.GetWatchedObject() && | |
103 !machine_policy_watcher_.StartWatching( | |
104 machine_policy_changed_event_.handle(), this)) { | |
105 LOG(WARNING) << "Failed to start watch for machine policy change event"; | |
106 machine_policy_watcher_failed_ = true; | |
107 } | |
108 | |
109 if (user_policy_watcher_failed_ || machine_policy_watcher_failed_) { | |
110 ScheduleFallbackReloadTask(); | |
111 } | |
112 } | |
113 | |
114 bool GetRegistryPolicyInteger(const string16& value_name, | |
115 uint32* result) const { | |
116 DWORD value = 0; | |
117 RegKey policy_key(HKEY_LOCAL_MACHINE, kRegistrySubKey, KEY_READ); | |
118 if (policy_key.ReadValueDW(value_name.c_str(), &value) == ERROR_SUCCESS) { | |
119 *result = value; | |
120 return true; | |
121 } | |
122 | |
123 if (policy_key.Open(HKEY_CURRENT_USER, kRegistrySubKey, KEY_READ) == | |
124 ERROR_SUCCESS) { | |
125 if (policy_key.ReadValueDW(value_name.c_str(), &value) == ERROR_SUCCESS) { | |
126 *result = value; | |
127 return true; | |
128 } | |
129 } | |
130 return false; | |
131 } | |
132 | |
133 bool GetRegistryPolicyBoolean(const string16& value_name, | |
134 bool* result) const { | |
135 uint32 local_result = 0; | |
136 bool ret = GetRegistryPolicyInteger(value_name, &local_result); | |
137 if (ret) | |
138 *result = local_result != 0; | |
139 return ret; | |
140 } | |
141 | |
142 base::DictionaryValue* Load() { | |
143 base::DictionaryValue* policy = new base::DictionaryValue(); | |
144 | |
145 bool bool_value; | |
146 const string16 name(ASCIIToUTF16(kNatPolicyName)); | |
147 if (GetRegistryPolicyBoolean(name, &bool_value)) { | |
148 policy->SetBoolean(kNatPolicyName, bool_value); | |
149 } | |
150 | |
151 return policy; | |
152 } | |
153 | |
154 // Post a reload notification and update the watch machinery. | |
155 void Reload() { | |
156 DCHECK(OnPolicyThread()); | |
157 SetupWatches(); | |
158 scoped_ptr<DictionaryValue> new_policy(Load()); | |
159 UpdateNatPolicy(new_policy.get()); | |
160 } | |
161 | |
162 // ObjectWatcher::Delegate overrides: | |
163 virtual void OnObjectSignaled(HANDLE object) { | |
164 DCHECK(OnPolicyThread()); | |
165 DCHECK(object == user_policy_changed_event_.handle() || | |
166 object == machine_policy_changed_event_.handle()) | |
167 << "unexpected object signaled policy reload, obj = " | |
168 << std::showbase << std::hex << object; | |
169 Reload(); | |
170 } | |
171 | |
172 base::WaitableEvent user_policy_changed_event_; | |
173 base::WaitableEvent machine_policy_changed_event_; | |
174 base::win::ObjectWatcher user_policy_watcher_; | |
175 base::win::ObjectWatcher machine_policy_watcher_; | |
176 bool user_policy_watcher_failed_; | |
177 bool machine_policy_watcher_failed_; | |
178 }; | |
179 | |
180 NatPolicy* NatPolicy::Create( | |
181 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { | |
182 return new NatPolicyWin(task_runner); | |
183 } | |
184 | |
185 } // namespace policy_hack | |
186 } // namespace remoting | |
OLD | NEW |