OLD | NEW |
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 #include "net/proxy/proxy_config_service_win.h" | 5 #include "net/proxy/proxy_config_service_win.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 #include <winhttp.h> | 8 #include <winhttp.h> |
9 | 9 |
10 #include "base/bind.h" | |
11 #include "base/bind_helpers.h" | |
12 #include "base/logging.h" | 10 #include "base/logging.h" |
13 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
14 #include "base/profiler/scoped_profile.h" | 12 #include "base/profiler/scoped_profile.h" |
15 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
16 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
17 #include "base/strings/string_tokenizer.h" | 15 #include "base/strings/string_tokenizer.h" |
18 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
19 #include "base/threading/thread_restrictions.h" | 17 #include "base/threading/thread_restrictions.h" |
20 #include "base/win/registry.h" | 18 #include "base/win/registry.h" |
21 #include "base/win/scoped_handle.h" | |
22 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
23 #include "net/proxy/proxy_config.h" | 20 #include "net/proxy/proxy_config.h" |
24 | 21 |
25 #pragma comment(lib, "winhttp.lib") | 22 #pragma comment(lib, "winhttp.lib") |
26 | 23 |
27 namespace net { | 24 namespace net { |
28 | 25 |
29 namespace { | 26 namespace { |
30 | 27 |
31 const int kPollIntervalSec = 10; | 28 const int kPollIntervalSec = 10; |
32 | 29 |
33 void FreeIEConfig(WINHTTP_CURRENT_USER_IE_PROXY_CONFIG* ie_config) { | 30 void FreeIEConfig(WINHTTP_CURRENT_USER_IE_PROXY_CONFIG* ie_config) { |
34 if (ie_config->lpszAutoConfigUrl) | 31 if (ie_config->lpszAutoConfigUrl) |
35 GlobalFree(ie_config->lpszAutoConfigUrl); | 32 GlobalFree(ie_config->lpszAutoConfigUrl); |
36 if (ie_config->lpszProxy) | 33 if (ie_config->lpszProxy) |
37 GlobalFree(ie_config->lpszProxy); | 34 GlobalFree(ie_config->lpszProxy); |
38 if (ie_config->lpszProxyBypass) | 35 if (ie_config->lpszProxyBypass) |
39 GlobalFree(ie_config->lpszProxyBypass); | 36 GlobalFree(ie_config->lpszProxyBypass); |
40 } | 37 } |
41 | 38 |
42 } // namespace | 39 } // namespace |
43 | 40 |
| 41 // RegKey and ObjectWatcher pair. |
| 42 class ProxyConfigServiceWin::KeyEntry { |
| 43 public: |
| 44 bool StartWatching(base::win::ObjectWatcher::Delegate* delegate) { |
| 45 // Try to create a watch event for the registry key (which watches the |
| 46 // sibling tree as well). |
| 47 if (key_.StartWatching() != ERROR_SUCCESS) |
| 48 return false; |
| 49 |
| 50 // Now setup an ObjectWatcher for this event, so we get OnObjectSignaled() |
| 51 // invoked on this message loop once it is signalled. |
| 52 if (!watcher_.StartWatching(key_.watch_event(), delegate)) |
| 53 return false; |
| 54 |
| 55 return true; |
| 56 } |
| 57 |
| 58 bool CreateRegKey(HKEY rootkey, const wchar_t* subkey) { |
| 59 return key_.Create(rootkey, subkey, KEY_NOTIFY) == ERROR_SUCCESS; |
| 60 } |
| 61 |
| 62 HANDLE watch_event() const { |
| 63 return key_.watch_event(); |
| 64 } |
| 65 |
| 66 private: |
| 67 base::win::RegKey key_; |
| 68 base::win::ObjectWatcher watcher_; |
| 69 }; |
| 70 |
44 ProxyConfigServiceWin::ProxyConfigServiceWin() | 71 ProxyConfigServiceWin::ProxyConfigServiceWin() |
45 : PollingProxyConfigService( | 72 : PollingProxyConfigService( |
46 base::TimeDelta::FromSeconds(kPollIntervalSec), | 73 base::TimeDelta::FromSeconds(kPollIntervalSec), |
47 &ProxyConfigServiceWin::GetCurrentProxyConfig) { | 74 &ProxyConfigServiceWin::GetCurrentProxyConfig) { |
48 } | 75 } |
49 | 76 |
50 ProxyConfigServiceWin::~ProxyConfigServiceWin() { | 77 ProxyConfigServiceWin::~ProxyConfigServiceWin() { |
51 // The registry functions below will end up going to disk. Do this on another | 78 // The registry functions below will end up going to disk. Do this on another |
52 // thread to avoid slowing the IO thread. http://crbug.com/61453 | 79 // thread to avoid slowing the IO thread. http://crbug.com/61453 |
53 base::ThreadRestrictions::ScopedAllowIO allow_io; | 80 base::ThreadRestrictions::ScopedAllowIO allow_io; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 L"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"); | 118 L"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"); |
92 | 119 |
93 AddKeyToWatchList( | 120 AddKeyToWatchList( |
94 HKEY_LOCAL_MACHINE, | 121 HKEY_LOCAL_MACHINE, |
95 L"SOFTWARE\\Policies\\Microsoft\\Windows\\CurrentVersion\\" | 122 L"SOFTWARE\\Policies\\Microsoft\\Windows\\CurrentVersion\\" |
96 L"Internet Settings"); | 123 L"Internet Settings"); |
97 } | 124 } |
98 | 125 |
99 bool ProxyConfigServiceWin::AddKeyToWatchList(HKEY rootkey, | 126 bool ProxyConfigServiceWin::AddKeyToWatchList(HKEY rootkey, |
100 const wchar_t* subkey) { | 127 const wchar_t* subkey) { |
101 scoped_ptr<base::win::RegKey> key(new base::win::RegKey); | 128 scoped_ptr<KeyEntry> entry(new KeyEntry); |
102 if (key->Create(rootkey, subkey, KEY_NOTIFY) != ERROR_SUCCESS) | 129 if (!entry->CreateRegKey(rootkey, subkey)) |
103 return false; | 130 return false; |
104 | 131 |
105 if (!key->StartWatching(base::Bind(&ProxyConfigServiceWin::OnObjectSignaled, | 132 if (!entry->StartWatching(this)) |
106 base::Unretained(this), | |
107 base::Unretained(key.get())))) { | |
108 return false; | 133 return false; |
109 } | |
110 | 134 |
111 keys_to_watch_.push_back(key.release()); | 135 keys_to_watch_.push_back(entry.release()); |
112 return true; | 136 return true; |
113 } | 137 } |
114 | 138 |
115 void ProxyConfigServiceWin::OnObjectSignaled(base::win::RegKey* key) { | 139 void ProxyConfigServiceWin::OnObjectSignaled(HANDLE object) { |
116 // TODO(vadimt): Remove ScopedProfile below once crbug.com/418183 is fixed. | 140 // TODO(vadimt): Remove ScopedProfile below once crbug.com/418183 is fixed. |
117 tracked_objects::ScopedProfile tracking_profile( | 141 tracked_objects::ScopedProfile tracking_profile( |
118 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 142 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
119 "ProxyConfigServiceWin_OnObjectSignaled")); | 143 "ProxyConfigServiceWin_OnObjectSignaled")); |
120 | 144 |
121 // Figure out which registry key signalled this change. | 145 // Figure out which registry key signalled this change. |
122 RegKeyList::iterator it = | 146 KeyEntryList::iterator it; |
123 std::find(keys_to_watch_.begin(), keys_to_watch_.end(), key); | 147 for (it = keys_to_watch_.begin(); it != keys_to_watch_.end(); ++it) { |
| 148 if ((*it)->watch_event() == object) |
| 149 break; |
| 150 } |
| 151 |
124 DCHECK(it != keys_to_watch_.end()); | 152 DCHECK(it != keys_to_watch_.end()); |
125 | 153 |
126 // Keep watching the registry key. | 154 // Keep watching the registry key. |
127 if (!key->StartWatching(base::Bind(&ProxyConfigServiceWin::OnObjectSignaled, | 155 if (!(*it)->StartWatching(this)) |
128 base::Unretained(this), | |
129 base::Unretained(key)))) { | |
130 delete *it; | |
131 keys_to_watch_.erase(it); | 156 keys_to_watch_.erase(it); |
132 } | |
133 | 157 |
134 // Have the PollingProxyConfigService test for changes. | 158 // Have the PollingProxyConfigService test for changes. |
135 CheckForChangesNow(); | 159 CheckForChangesNow(); |
136 } | 160 } |
137 | 161 |
138 // static | 162 // static |
139 void ProxyConfigServiceWin::GetCurrentProxyConfig(ProxyConfig* config) { | 163 void ProxyConfigServiceWin::GetCurrentProxyConfig(ProxyConfig* config) { |
140 WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ie_config = {0}; | 164 WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ie_config = {0}; |
141 if (!WinHttpGetIEProxyConfigForCurrentUser(&ie_config)) { | 165 if (!WinHttpGetIEProxyConfigForCurrentUser(&ie_config)) { |
142 LOG(ERROR) << "WinHttpGetIEProxyConfigForCurrentUser failed: " << | 166 LOG(ERROR) << "WinHttpGetIEProxyConfigForCurrentUser failed: " << |
(...skipping 26 matching lines...) Expand all Loading... |
169 std::string bypass_url_domain = proxy_server_bypass_list.token(); | 193 std::string bypass_url_domain = proxy_server_bypass_list.token(); |
170 config->proxy_rules().bypass_rules.AddRuleFromString(bypass_url_domain); | 194 config->proxy_rules().bypass_rules.AddRuleFromString(bypass_url_domain); |
171 } | 195 } |
172 } | 196 } |
173 if (ie_config.lpszAutoConfigUrl) | 197 if (ie_config.lpszAutoConfigUrl) |
174 config->set_pac_url(GURL(ie_config.lpszAutoConfigUrl)); | 198 config->set_pac_url(GURL(ie_config.lpszAutoConfigUrl)); |
175 config->set_source(PROXY_CONFIG_SOURCE_SYSTEM); | 199 config->set_source(PROXY_CONFIG_SOURCE_SYSTEM); |
176 } | 200 } |
177 | 201 |
178 } // namespace net | 202 } // namespace net |
OLD | NEW |