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

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

Issue 10804040: [Chromoting] Refactor the host policy watcher so that policies can easily be added. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix for Windows. Created 8 years, 5 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 various classes in 5 // Most of this code is copied from various classes in
6 // src/chrome/browser/policy. In particular, look at 6 // src/chrome/browser/policy. In particular, look at
7 // 7 //
8 // configuration_policy_provider_delegate_win.{h,cc} 8 // configuration_policy_provider_delegate_win.{h,cc}
9 // configuration_policy_loader_win.{h,cc} 9 // configuration_policy_loader_win.{h,cc}
10 // 10 //
11 // This is a reduction of the functionality in those classes. 11 // This is a reduction of the functionality in those classes.
12 12
13 #include "remoting/host/policy_hack/nat_policy.h" 13 #include "remoting/host/policy_hack/policy_watcher.h"
14 14
15 #include <userenv.h> 15 #include <userenv.h>
16 16
17 #include "base/compiler_specific.h" 17 #include "base/compiler_specific.h"
18 #include "base/memory/scoped_ptr.h" 18 #include "base/memory/scoped_ptr.h"
19 #include "base/message_loop_proxy.h" 19 #include "base/message_loop_proxy.h"
20 #include "base/string16.h" 20 #include "base/string16.h"
21 #include "base/synchronization/waitable_event.h" 21 #include "base/synchronization/waitable_event.h"
22 #include "base/utf_string_conversions.h" 22 #include "base/utf_string_conversions.h"
23 #include "base/values.h" 23 #include "base/values.h"
24 #include "base/win/object_watcher.h" 24 #include "base/win/object_watcher.h"
25 #include "base/win/registry.h" 25 #include "base/win/registry.h"
26 26
27 // userenv.dll is required for RegisterGPNotification(). 27 // userenv.dll is required for RegisterGPNotification().
28 #pragma comment(lib, "userenv.lib") 28 #pragma comment(lib, "userenv.lib")
29 29
30 using base::win::RegKey; 30 using base::win::RegKey;
31 31
32 namespace remoting { 32 namespace remoting {
33 namespace policy_hack { 33 namespace policy_hack {
34 34
35 namespace { 35 namespace {
36 36
37 const wchar_t kRegistrySubKey[] = L"SOFTWARE\\Policies\\Google\\Chrome"; 37 const wchar_t kRegistrySubKey[] = L"SOFTWARE\\Policies\\Google\\Chrome";
38 38
39 } // namespace 39 } // namespace
40 40
41 class NatPolicyWin : 41 class PolicyWatcherWin :
42 public NatPolicy, 42 public PolicyWatcher,
43 public base::win::ObjectWatcher::Delegate { 43 public base::win::ObjectWatcher::Delegate {
44 public: 44 public:
45 explicit NatPolicyWin(scoped_refptr<base::SingleThreadTaskRunner> task_runner) 45 explicit PolicyWatcherWin(
46 : NatPolicy(task_runner), 46 scoped_refptr<base::SingleThreadTaskRunner> task_runner)
47 : PolicyWatcher(task_runner),
47 user_policy_changed_event_(false, false), 48 user_policy_changed_event_(false, false),
48 machine_policy_changed_event_(false, false), 49 machine_policy_changed_event_(false, false),
49 user_policy_watcher_failed_(false), 50 user_policy_watcher_failed_(false),
50 machine_policy_watcher_failed_(false) { 51 machine_policy_watcher_failed_(false) {
51 } 52 }
52 53
53 virtual ~NatPolicyWin() { 54 virtual ~PolicyWatcherWin() {
54 } 55 }
55 56
56 virtual void StartWatchingInternal() OVERRIDE { 57 virtual void StartWatchingInternal() OVERRIDE {
57 DCHECK(OnPolicyThread()); 58 DCHECK(OnPolicyWatcherThread());
58 59
59 if (!RegisterGPNotification(user_policy_changed_event_.handle(), false)) { 60 if (!RegisterGPNotification(user_policy_changed_event_.handle(), false)) {
60 PLOG(WARNING) << "Failed to register user group policy notification"; 61 PLOG(WARNING) << "Failed to register user group policy notification";
61 user_policy_watcher_failed_ = true; 62 user_policy_watcher_failed_ = true;
62 } 63 }
63 64
64 if (!RegisterGPNotification(machine_policy_changed_event_.handle(), true)) { 65 if (!RegisterGPNotification(machine_policy_changed_event_.handle(), true)) {
65 PLOG(WARNING) << "Failed to register machine group policy notification."; 66 PLOG(WARNING) << "Failed to register machine group policy notification.";
66 machine_policy_watcher_failed_ = true; 67 machine_policy_watcher_failed_ = true;
67 } 68 }
68 69
69 Reload(); 70 Reload();
70 } 71 }
71 72
72 virtual void StopWatchingInternal() OVERRIDE { 73 virtual void StopWatchingInternal() OVERRIDE {
73 DCHECK(OnPolicyThread()); 74 DCHECK(OnPolicyWatcherThread());
74 75
75 if (!UnregisterGPNotification(user_policy_changed_event_.handle())) { 76 if (!UnregisterGPNotification(user_policy_changed_event_.handle())) {
76 PLOG(WARNING) << "Failed to unregister user group policy notification"; 77 PLOG(WARNING) << "Failed to unregister user group policy notification";
77 } 78 }
78 79
79 if (!UnregisterGPNotification(machine_policy_changed_event_.handle())) { 80 if (!UnregisterGPNotification(machine_policy_changed_event_.handle())) {
80 PLOG(WARNING) << 81 PLOG(WARNING) <<
81 "Failed to unregister machine group policy notification."; 82 "Failed to unregister machine group policy notification.";
82 } 83 }
83 84
84 user_policy_watcher_.StopWatching(); 85 user_policy_watcher_.StopWatching();
85 machine_policy_watcher_.StopWatching(); 86 machine_policy_watcher_.StopWatching();
86 } 87 }
87 88
88 private: 89 private:
89 // Updates the watchers and schedules the reload task if appropriate. 90 // Updates the watchers and schedules the reload task if appropriate.
90 void SetupWatches() { 91 void SetupWatches() {
91 DCHECK(OnPolicyThread()); 92 DCHECK(OnPolicyWatcherThread());
92 93
93 if (!user_policy_watcher_failed_ && 94 if (!user_policy_watcher_failed_ &&
94 !user_policy_watcher_.GetWatchedObject() && 95 !user_policy_watcher_.GetWatchedObject() &&
95 !user_policy_watcher_.StartWatching( 96 !user_policy_watcher_.StartWatching(
96 user_policy_changed_event_.handle(), this)) { 97 user_policy_changed_event_.handle(), this)) {
97 LOG(WARNING) << "Failed to start watch for user policy change event"; 98 LOG(WARNING) << "Failed to start watch for user policy change event";
98 user_policy_watcher_failed_ = true; 99 user_policy_watcher_failed_ = true;
99 } 100 }
100 101
101 if (!machine_policy_watcher_failed_ && 102 if (!machine_policy_watcher_failed_ &&
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 uint32 local_result = 0; 136 uint32 local_result = 0;
136 bool ret = GetRegistryPolicyInteger(value_name, &local_result); 137 bool ret = GetRegistryPolicyInteger(value_name, &local_result);
137 if (ret) 138 if (ret)
138 *result = local_result != 0; 139 *result = local_result != 0;
139 return ret; 140 return ret;
140 } 141 }
141 142
142 base::DictionaryValue* Load() { 143 base::DictionaryValue* Load() {
143 base::DictionaryValue* policy = new base::DictionaryValue(); 144 base::DictionaryValue* policy = new base::DictionaryValue();
144 145
145 bool bool_value; 146 for (int i = 0; i < kBooleanPolicyNamesNum; ++i) {
146 const string16 name(ASCIIToUTF16(kNatPolicyName)); 147 const char* policy_name = kBooleanPolicyNames[i];
147 if (GetRegistryPolicyBoolean(name, &bool_value)) { 148 bool bool_value;
148 policy->SetBoolean(kNatPolicyName, bool_value); 149 const string16 name(ASCIIToUTF16(policy_name));
150 if (GetRegistryPolicyBoolean(name, &bool_value)) {
151 policy->SetBoolean(policy_name, bool_value);
152 }
149 } 153 }
150 154
151 return policy; 155 return policy;
152 } 156 }
153 157
154 // Post a reload notification and update the watch machinery. 158 // Post a reload notification and update the watch machinery.
155 void Reload() { 159 void Reload() {
156 DCHECK(OnPolicyThread()); 160 DCHECK(OnPolicyWatcherThread());
157 SetupWatches(); 161 SetupWatches();
158 scoped_ptr<DictionaryValue> new_policy(Load()); 162 scoped_ptr<DictionaryValue> new_policy(Load());
159 UpdateNatPolicy(new_policy.get()); 163 UpdatePolicies(new_policy.get());
160 } 164 }
161 165
162 // ObjectWatcher::Delegate overrides: 166 // ObjectWatcher::Delegate overrides:
163 virtual void OnObjectSignaled(HANDLE object) { 167 virtual void OnObjectSignaled(HANDLE object) {
164 DCHECK(OnPolicyThread()); 168 DCHECK(OnPolicyWatcherThread());
165 DCHECK(object == user_policy_changed_event_.handle() || 169 DCHECK(object == user_policy_changed_event_.handle() ||
166 object == machine_policy_changed_event_.handle()) 170 object == machine_policy_changed_event_.handle())
167 << "unexpected object signaled policy reload, obj = " 171 << "unexpected object signaled policy reload, obj = "
168 << std::showbase << std::hex << object; 172 << std::showbase << std::hex << object;
169 Reload(); 173 Reload();
170 } 174 }
171 175
172 base::WaitableEvent user_policy_changed_event_; 176 base::WaitableEvent user_policy_changed_event_;
173 base::WaitableEvent machine_policy_changed_event_; 177 base::WaitableEvent machine_policy_changed_event_;
174 base::win::ObjectWatcher user_policy_watcher_; 178 base::win::ObjectWatcher user_policy_watcher_;
175 base::win::ObjectWatcher machine_policy_watcher_; 179 base::win::ObjectWatcher machine_policy_watcher_;
176 bool user_policy_watcher_failed_; 180 bool user_policy_watcher_failed_;
177 bool machine_policy_watcher_failed_; 181 bool machine_policy_watcher_failed_;
178 }; 182 };
179 183
180 NatPolicy* NatPolicy::Create( 184 PolicyWatcher* PolicyWatcher::Create(
181 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { 185 scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
182 return new NatPolicyWin(task_runner); 186 return new PolicyWatcherWin(task_runner);
183 } 187 }
184 188
185 } // namespace policy_hack 189 } // namespace policy_hack
186 } // namespace remoting 190 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/host/policy_hack/policy_watcher_unittest.cc ('k') | remoting/host/remoting_me2me_host.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698