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