[email protected] | ddaf98a | 2012-02-16 00:13:56 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | c992780 | 2011-08-15 23:54:57 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | f992585f | 2011-08-17 22:07:55 | [diff] [blame] | 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 | |
[email protected] | 000d1f6 | 2012-07-24 01:56:54 | [diff] [blame] | 13 | #include "remoting/host/policy_hack/policy_watcher.h" |
[email protected] | c992780 | 2011-08-15 23:54:57 | [diff] [blame] | 14 | |
[email protected] | f992585f | 2011-08-17 22:07:55 | [diff] [blame] | 15 | #include <userenv.h> |
| 16 | |
[email protected] | c992780 | 2011-08-15 23:54:57 | [diff] [blame] | 17 | #include "base/compiler_specific.h" |
[email protected] | 7f8f556 | 2011-09-18 20:59:06 | [diff] [blame] | 18 | #include "base/memory/scoped_ptr.h" |
[email protected] | c992780 | 2011-08-15 23:54:57 | [diff] [blame] | 19 | #include "base/message_loop_proxy.h" |
[email protected] | 7f8f556 | 2011-09-18 20:59:06 | [diff] [blame] | 20 | #include "base/string16.h" |
[email protected] | c992780 | 2011-08-15 23:54:57 | [diff] [blame] | 21 | #include "base/synchronization/waitable_event.h" |
[email protected] | 7f8f556 | 2011-09-18 20:59:06 | [diff] [blame] | 22 | #include "base/utf_string_conversions.h" |
[email protected] | f992585f | 2011-08-17 22:07:55 | [diff] [blame] | 23 | #include "base/values.h" |
| 24 | #include "base/win/object_watcher.h" |
[email protected] | 7f8f556 | 2011-09-18 20:59:06 | [diff] [blame] | 25 | #include "base/win/registry.h" |
[email protected] | f992585f | 2011-08-17 22:07:55 | [diff] [blame] | 26 | |
| 27 | // userenv.dll is required for RegisterGPNotification(). |
| 28 | #pragma comment(lib, "userenv.lib") |
| 29 | |
| 30 | using base::win::RegKey; |
[email protected] | c992780 | 2011-08-15 23:54:57 | [diff] [blame] | 31 | |
| 32 | namespace remoting { |
| 33 | namespace policy_hack { |
| 34 | |
[email protected] | f992585f | 2011-08-17 22:07:55 | [diff] [blame] | 35 | namespace { |
| 36 | |
[email protected] | f992585f | 2011-08-17 22:07:55 | [diff] [blame] | 37 | const wchar_t kRegistrySubKey[] = L"SOFTWARE\\Policies\\Google\\Chrome"; |
[email protected] | f992585f | 2011-08-17 22:07:55 | [diff] [blame] | 38 | |
| 39 | } // namespace |
| 40 | |
[email protected] | 000d1f6 | 2012-07-24 01:56:54 | [diff] [blame] | 41 | class PolicyWatcherWin : |
| 42 | public PolicyWatcher, |
[email protected] | f992585f | 2011-08-17 22:07:55 | [diff] [blame] | 43 | public base::win::ObjectWatcher::Delegate { |
[email protected] | c992780 | 2011-08-15 23:54:57 | [diff] [blame] | 44 | public: |
[email protected] | 000d1f6 | 2012-07-24 01:56:54 | [diff] [blame] | 45 | explicit PolicyWatcherWin( |
| 46 | scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 47 | : PolicyWatcher(task_runner), |
[email protected] | f992585f | 2011-08-17 22:07:55 | [diff] [blame] | 48 | user_policy_changed_event_(false, false), |
| 49 | machine_policy_changed_event_(false, false), |
| 50 | user_policy_watcher_failed_(false), |
| 51 | machine_policy_watcher_failed_(false) { |
[email protected] | c992780 | 2011-08-15 23:54:57 | [diff] [blame] | 52 | } |
| 53 | |
[email protected] | 000d1f6 | 2012-07-24 01:56:54 | [diff] [blame] | 54 | virtual ~PolicyWatcherWin() { |
[email protected] | c992780 | 2011-08-15 23:54:57 | [diff] [blame] | 55 | } |
| 56 | |
[email protected] | f992585f | 2011-08-17 22:07:55 | [diff] [blame] | 57 | virtual void StartWatchingInternal() OVERRIDE { |
[email protected] | 000d1f6 | 2012-07-24 01:56:54 | [diff] [blame] | 58 | DCHECK(OnPolicyWatcherThread()); |
[email protected] | f992585f | 2011-08-17 22:07:55 | [diff] [blame] | 59 | |
| 60 | if (!RegisterGPNotification(user_policy_changed_event_.handle(), false)) { |
| 61 | PLOG(WARNING) << "Failed to register user group policy notification"; |
| 62 | user_policy_watcher_failed_ = true; |
| 63 | } |
| 64 | |
| 65 | if (!RegisterGPNotification(machine_policy_changed_event_.handle(), true)) { |
| 66 | PLOG(WARNING) << "Failed to register machine group policy notification."; |
| 67 | machine_policy_watcher_failed_ = true; |
| 68 | } |
| 69 | |
| 70 | Reload(); |
[email protected] | c992780 | 2011-08-15 23:54:57 | [diff] [blame] | 71 | } |
| 72 | |
[email protected] | f992585f | 2011-08-17 22:07:55 | [diff] [blame] | 73 | virtual void StopWatchingInternal() OVERRIDE { |
[email protected] | 000d1f6 | 2012-07-24 01:56:54 | [diff] [blame] | 74 | DCHECK(OnPolicyWatcherThread()); |
[email protected] | f992585f | 2011-08-17 22:07:55 | [diff] [blame] | 75 | |
| 76 | if (!UnregisterGPNotification(user_policy_changed_event_.handle())) { |
| 77 | PLOG(WARNING) << "Failed to unregister user group policy notification"; |
| 78 | } |
| 79 | |
| 80 | if (!UnregisterGPNotification(machine_policy_changed_event_.handle())) { |
| 81 | PLOG(WARNING) << |
| 82 | "Failed to unregister machine group policy notification."; |
| 83 | } |
| 84 | |
| 85 | user_policy_watcher_.StopWatching(); |
| 86 | machine_policy_watcher_.StopWatching(); |
[email protected] | c992780 | 2011-08-15 23:54:57 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | private: |
[email protected] | f992585f | 2011-08-17 22:07:55 | [diff] [blame] | 90 | // Updates the watchers and schedules the reload task if appropriate. |
| 91 | void SetupWatches() { |
[email protected] | 000d1f6 | 2012-07-24 01:56:54 | [diff] [blame] | 92 | DCHECK(OnPolicyWatcherThread()); |
[email protected] | f992585f | 2011-08-17 22:07:55 | [diff] [blame] | 93 | |
| 94 | if (!user_policy_watcher_failed_ && |
| 95 | !user_policy_watcher_.GetWatchedObject() && |
| 96 | !user_policy_watcher_.StartWatching( |
| 97 | user_policy_changed_event_.handle(), this)) { |
| 98 | LOG(WARNING) << "Failed to start watch for user policy change event"; |
| 99 | user_policy_watcher_failed_ = true; |
| 100 | } |
| 101 | |
| 102 | if (!machine_policy_watcher_failed_ && |
| 103 | !machine_policy_watcher_.GetWatchedObject() && |
| 104 | !machine_policy_watcher_.StartWatching( |
| 105 | machine_policy_changed_event_.handle(), this)) { |
| 106 | LOG(WARNING) << "Failed to start watch for machine policy change event"; |
| 107 | machine_policy_watcher_failed_ = true; |
| 108 | } |
| 109 | |
| 110 | if (user_policy_watcher_failed_ || machine_policy_watcher_failed_) { |
| 111 | ScheduleFallbackReloadTask(); |
| 112 | } |
| 113 | } |
| 114 | |
[email protected] | 5693a021 | 2012-07-31 15:05:22 | [diff] [blame] | 115 | bool GetRegistryPolicyString(const std::string& value_name, |
| 116 | std::string* result) const { |
[email protected] | a11dbe9b | 2012-08-07 01:32:58 | [diff] [blame^] | 117 | // presubmit: allow wstring |
[email protected] | 5693a021 | 2012-07-31 15:05:22 | [diff] [blame] | 118 | std::wstring value_name_wide = UTF8ToWide(value_name); |
[email protected] | a11dbe9b | 2012-08-07 01:32:58 | [diff] [blame^] | 119 | // presubmit: allow wstring |
[email protected] | 5693a021 | 2012-07-31 15:05:22 | [diff] [blame] | 120 | std::wstring value; |
| 121 | RegKey policy_key(HKEY_LOCAL_MACHINE, kRegistrySubKey, KEY_READ); |
| 122 | if (policy_key.ReadValue(value_name_wide.c_str(), &value) == |
| 123 | ERROR_SUCCESS) { |
| 124 | *result = WideToUTF8(value); |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | if (policy_key.Open(HKEY_CURRENT_USER, kRegistrySubKey, KEY_READ) == |
| 129 | ERROR_SUCCESS) { |
| 130 | if (policy_key.ReadValue(value_name_wide.c_str(), &value) == |
| 131 | ERROR_SUCCESS) { |
| 132 | *result = WideToUTF8(value); |
| 133 | return true; |
| 134 | } |
| 135 | } |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | bool GetRegistryPolicyInteger(const std::string& value_name, |
[email protected] | f992585f | 2011-08-17 22:07:55 | [diff] [blame] | 140 | uint32* result) const { |
[email protected] | a11dbe9b | 2012-08-07 01:32:58 | [diff] [blame^] | 141 | // presubmit: allow wstring |
[email protected] | 5693a021 | 2012-07-31 15:05:22 | [diff] [blame] | 142 | std::wstring value_name_wide = UTF8ToWide(value_name); |
[email protected] | f992585f | 2011-08-17 22:07:55 | [diff] [blame] | 143 | DWORD value = 0; |
| 144 | RegKey policy_key(HKEY_LOCAL_MACHINE, kRegistrySubKey, KEY_READ); |
[email protected] | 5693a021 | 2012-07-31 15:05:22 | [diff] [blame] | 145 | if (policy_key.ReadValueDW(value_name_wide.c_str(), &value) == |
| 146 | ERROR_SUCCESS) { |
[email protected] | f992585f | 2011-08-17 22:07:55 | [diff] [blame] | 147 | *result = value; |
| 148 | return true; |
| 149 | } |
| 150 | |
| 151 | if (policy_key.Open(HKEY_CURRENT_USER, kRegistrySubKey, KEY_READ) == |
| 152 | ERROR_SUCCESS) { |
[email protected] | 5693a021 | 2012-07-31 15:05:22 | [diff] [blame] | 153 | if (policy_key.ReadValueDW(value_name_wide.c_str(), &value) == |
| 154 | ERROR_SUCCESS) { |
[email protected] | f992585f | 2011-08-17 22:07:55 | [diff] [blame] | 155 | *result = value; |
| 156 | return true; |
| 157 | } |
| 158 | } |
| 159 | return false; |
| 160 | } |
| 161 | |
[email protected] | 5693a021 | 2012-07-31 15:05:22 | [diff] [blame] | 162 | bool GetRegistryPolicyBoolean(const std::string& value_name, |
[email protected] | f992585f | 2011-08-17 22:07:55 | [diff] [blame] | 163 | bool* result) const { |
| 164 | uint32 local_result = 0; |
| 165 | bool ret = GetRegistryPolicyInteger(value_name, &local_result); |
| 166 | if (ret) |
| 167 | *result = local_result != 0; |
| 168 | return ret; |
| 169 | } |
| 170 | |
| 171 | base::DictionaryValue* Load() { |
| 172 | base::DictionaryValue* policy = new base::DictionaryValue(); |
| 173 | |
[email protected] | 000d1f6 | 2012-07-24 01:56:54 | [diff] [blame] | 174 | for (int i = 0; i < kBooleanPolicyNamesNum; ++i) { |
| 175 | const char* policy_name = kBooleanPolicyNames[i]; |
| 176 | bool bool_value; |
[email protected] | 5693a021 | 2012-07-31 15:05:22 | [diff] [blame] | 177 | if (GetRegistryPolicyBoolean(policy_name, &bool_value)) { |
[email protected] | 000d1f6 | 2012-07-24 01:56:54 | [diff] [blame] | 178 | policy->SetBoolean(policy_name, bool_value); |
| 179 | } |
[email protected] | f992585f | 2011-08-17 22:07:55 | [diff] [blame] | 180 | } |
[email protected] | 5693a021 | 2012-07-31 15:05:22 | [diff] [blame] | 181 | for (int i = 0; i < kStringPolicyNamesNum; ++i) { |
| 182 | const char* policy_name = kStringPolicyNames[i]; |
| 183 | std::string string_value; |
| 184 | if (GetRegistryPolicyString(policy_name, &string_value)) { |
| 185 | policy->SetString(policy_name, string_value); |
| 186 | } |
| 187 | } |
[email protected] | f992585f | 2011-08-17 22:07:55 | [diff] [blame] | 188 | return policy; |
| 189 | } |
| 190 | |
| 191 | // Post a reload notification and update the watch machinery. |
| 192 | void Reload() { |
[email protected] | 000d1f6 | 2012-07-24 01:56:54 | [diff] [blame] | 193 | DCHECK(OnPolicyWatcherThread()); |
[email protected] | f992585f | 2011-08-17 22:07:55 | [diff] [blame] | 194 | SetupWatches(); |
| 195 | scoped_ptr<DictionaryValue> new_policy(Load()); |
[email protected] | 000d1f6 | 2012-07-24 01:56:54 | [diff] [blame] | 196 | UpdatePolicies(new_policy.get()); |
[email protected] | f992585f | 2011-08-17 22:07:55 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | // ObjectWatcher::Delegate overrides: |
| 200 | virtual void OnObjectSignaled(HANDLE object) { |
[email protected] | 000d1f6 | 2012-07-24 01:56:54 | [diff] [blame] | 201 | DCHECK(OnPolicyWatcherThread()); |
[email protected] | f992585f | 2011-08-17 22:07:55 | [diff] [blame] | 202 | DCHECK(object == user_policy_changed_event_.handle() || |
| 203 | object == machine_policy_changed_event_.handle()) |
| 204 | << "unexpected object signaled policy reload, obj = " |
| 205 | << std::showbase << std::hex << object; |
| 206 | Reload(); |
| 207 | } |
| 208 | |
| 209 | base::WaitableEvent user_policy_changed_event_; |
| 210 | base::WaitableEvent machine_policy_changed_event_; |
| 211 | base::win::ObjectWatcher user_policy_watcher_; |
| 212 | base::win::ObjectWatcher machine_policy_watcher_; |
| 213 | bool user_policy_watcher_failed_; |
| 214 | bool machine_policy_watcher_failed_; |
[email protected] | c992780 | 2011-08-15 23:54:57 | [diff] [blame] | 215 | }; |
| 216 | |
[email protected] | 000d1f6 | 2012-07-24 01:56:54 | [diff] [blame] | 217 | PolicyWatcher* PolicyWatcher::Create( |
[email protected] | 3c8cfbe7 | 2012-07-03 00:24:15 | [diff] [blame] | 218 | scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
[email protected] | 000d1f6 | 2012-07-24 01:56:54 | [diff] [blame] | 219 | return new PolicyWatcherWin(task_runner); |
[email protected] | c992780 | 2011-08-15 23:54:57 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | } // namespace policy_hack |
| 223 | } // namespace remoting |