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> entry(new base::win::RegKey); |
eroman
2014/10/10 21:34:35
rename entry --> key
| |
129 if (!entry->CreateRegKey(rootkey, subkey)) | 102 if (entry->Create(rootkey, subkey, KEY_NOTIFY) != ERROR_SUCCESS) |
130 return false; | 103 return false; |
131 | 104 |
132 if (!entry->StartWatching(this)) | 105 if (!entry->StartWatching(base::Bind(&ProxyConfigServiceWin::OnObjectSignaled, |
106 base::Unretained(this), | |
107 base::Unretained(entry.get())))) { | |
133 return false; | 108 return false; |
109 } | |
134 | 110 |
135 keys_to_watch_.push_back(entry.release()); | 111 keys_to_watch_.push_back(entry.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 KeyEntryList::iterator it; |
eroman
2014/10/10 21:41:58
Looks like the pre-existing code is bad and doesn'
rvargas (doing something else)
2014/10/10 22:22:05
dos it on the loop
eroman
2014/10/10 22:35:12
You are right about the loop part; brain fail on m
| |
147 for (it = keys_to_watch_.begin(); it != keys_to_watch_.end(); ++it) { | 123 for (it = keys_to_watch_.begin(); it != keys_to_watch_.end(); ++it) { |
148 if ((*it)->watch_event() == object) | 124 if ((*it) == key) |
149 break; | 125 break; |
150 } | 126 } |
151 | 127 |
152 DCHECK(it != keys_to_watch_.end()); | 128 DCHECK(it != keys_to_watch_.end()); |
153 | 129 |
154 // Keep watching the registry key. | 130 // Keep watching the registry key. |
155 if (!(*it)->StartWatching(this)) | 131 if (!key->StartWatching(base::Bind(&ProxyConfigServiceWin::OnObjectSignaled, |
132 base::Unretained(this), | |
133 base::Unretained(key)))) { | |
156 keys_to_watch_.erase(it); | 134 keys_to_watch_.erase(it); |
eroman
2014/10/10 21:34:35
Looks like the pre-existing code is bad, and is le
rvargas (doing something else)
2014/10/10 22:22:05
Done.
| |
135 } | |
157 | 136 |
158 // Have the PollingProxyConfigService test for changes. | 137 // Have the PollingProxyConfigService test for changes. |
159 CheckForChangesNow(); | 138 CheckForChangesNow(); |
160 } | 139 } |
161 | 140 |
162 // static | 141 // static |
163 void ProxyConfigServiceWin::GetCurrentProxyConfig(ProxyConfig* config) { | 142 void ProxyConfigServiceWin::GetCurrentProxyConfig(ProxyConfig* config) { |
164 WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ie_config = {0}; | 143 WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ie_config = {0}; |
165 if (!WinHttpGetIEProxyConfigForCurrentUser(&ie_config)) { | 144 if (!WinHttpGetIEProxyConfigForCurrentUser(&ie_config)) { |
166 LOG(ERROR) << "WinHttpGetIEProxyConfigForCurrentUser failed: " << | 145 LOG(ERROR) << "WinHttpGetIEProxyConfigForCurrentUser failed: " << |
(...skipping 26 matching lines...) Expand all Loading... | |
193 std::string bypass_url_domain = proxy_server_bypass_list.token(); | 172 std::string bypass_url_domain = proxy_server_bypass_list.token(); |
194 config->proxy_rules().bypass_rules.AddRuleFromString(bypass_url_domain); | 173 config->proxy_rules().bypass_rules.AddRuleFromString(bypass_url_domain); |
195 } | 174 } |
196 } | 175 } |
197 if (ie_config.lpszAutoConfigUrl) | 176 if (ie_config.lpszAutoConfigUrl) |
198 config->set_pac_url(GURL(ie_config.lpszAutoConfigUrl)); | 177 config->set_pac_url(GURL(ie_config.lpszAutoConfigUrl)); |
199 config->set_source(PROXY_CONFIG_SOURCE_SYSTEM); | 178 config->set_source(PROXY_CONFIG_SOURCE_SYSTEM); |
200 } | 179 } |
201 | 180 |
202 } // namespace net | 181 } // namespace net |
OLD | NEW |