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 <memory> | 10 #include <memory> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/stl_util.h" | 15 #include "base/memory/ptr_util.h" |
16 #include "base/strings/string_tokenizer.h" | 16 #include "base/strings/string_tokenizer.h" |
17 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
18 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
19 #include "base/threading/thread_restrictions.h" | 19 #include "base/threading/thread_restrictions.h" |
20 #include "base/win/registry.h" | 20 #include "base/win/registry.h" |
21 #include "base/win/scoped_handle.h" | 21 #include "base/win/scoped_handle.h" |
22 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
23 #include "net/proxy/proxy_config.h" | 23 #include "net/proxy/proxy_config.h" |
24 | 24 |
25 namespace net { | 25 namespace net { |
(...skipping 13 matching lines...) Expand all Loading... |
39 | 39 |
40 } // namespace | 40 } // namespace |
41 | 41 |
42 ProxyConfigServiceWin::ProxyConfigServiceWin() | 42 ProxyConfigServiceWin::ProxyConfigServiceWin() |
43 : PollingProxyConfigService( | 43 : PollingProxyConfigService( |
44 base::TimeDelta::FromSeconds(kPollIntervalSec), | 44 base::TimeDelta::FromSeconds(kPollIntervalSec), |
45 &ProxyConfigServiceWin::GetCurrentProxyConfig) { | 45 &ProxyConfigServiceWin::GetCurrentProxyConfig) { |
46 } | 46 } |
47 | 47 |
48 ProxyConfigServiceWin::~ProxyConfigServiceWin() { | 48 ProxyConfigServiceWin::~ProxyConfigServiceWin() { |
49 // The registry functions below will end up going to disk. Do this on another | 49 // The registry functions below will end up going to disk. TODO: Do this on |
50 // thread to avoid slowing the IO thread. http://crbug.com/61453 | 50 // another thread to avoid slowing the IO thread. http://crbug.com/61453 |
51 base::ThreadRestrictions::ScopedAllowIO allow_io; | 51 base::ThreadRestrictions::ScopedAllowIO allow_io; |
52 base::STLDeleteElements(&keys_to_watch_); | 52 keys_to_watch_.clear(); |
53 } | 53 } |
54 | 54 |
55 void ProxyConfigServiceWin::AddObserver(Observer* observer) { | 55 void ProxyConfigServiceWin::AddObserver(Observer* observer) { |
56 // Lazily-initialize our registry watcher. | 56 // Lazily-initialize our registry watcher. |
57 StartWatchingRegistryForChanges(); | 57 StartWatchingRegistryForChanges(); |
58 | 58 |
59 // Let the super-class do its work now. | 59 // Let the super-class do its work now. |
60 PollingProxyConfigService::AddObserver(observer); | 60 PollingProxyConfigService::AddObserver(observer); |
61 } | 61 } |
62 | 62 |
(...skipping 26 matching lines...) Expand all Loading... |
89 L"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"); | 89 L"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"); |
90 | 90 |
91 AddKeyToWatchList( | 91 AddKeyToWatchList( |
92 HKEY_LOCAL_MACHINE, | 92 HKEY_LOCAL_MACHINE, |
93 L"SOFTWARE\\Policies\\Microsoft\\Windows\\CurrentVersion\\" | 93 L"SOFTWARE\\Policies\\Microsoft\\Windows\\CurrentVersion\\" |
94 L"Internet Settings"); | 94 L"Internet Settings"); |
95 } | 95 } |
96 | 96 |
97 bool ProxyConfigServiceWin::AddKeyToWatchList(HKEY rootkey, | 97 bool ProxyConfigServiceWin::AddKeyToWatchList(HKEY rootkey, |
98 const wchar_t* subkey) { | 98 const wchar_t* subkey) { |
99 std::unique_ptr<base::win::RegKey> key(new base::win::RegKey); | 99 std::unique_ptr<base::win::RegKey> key = |
| 100 base::MakeUnique<base::win::RegKey>(); |
100 if (key->Create(rootkey, subkey, KEY_NOTIFY) != ERROR_SUCCESS) | 101 if (key->Create(rootkey, subkey, KEY_NOTIFY) != ERROR_SUCCESS) |
101 return false; | 102 return false; |
102 | 103 |
103 if (!key->StartWatching(base::Bind(&ProxyConfigServiceWin::OnObjectSignaled, | 104 if (!key->StartWatching(base::Bind(&ProxyConfigServiceWin::OnObjectSignaled, |
104 base::Unretained(this), | 105 base::Unretained(this), |
105 base::Unretained(key.get())))) { | 106 base::Unretained(key.get())))) { |
106 return false; | 107 return false; |
107 } | 108 } |
108 | 109 |
109 keys_to_watch_.push_back(key.release()); | 110 keys_to_watch_.push_back(std::move(key)); |
110 return true; | 111 return true; |
111 } | 112 } |
112 | 113 |
113 void ProxyConfigServiceWin::OnObjectSignaled(base::win::RegKey* key) { | 114 void ProxyConfigServiceWin::OnObjectSignaled(base::win::RegKey* key) { |
114 // Figure out which registry key signalled this change. | 115 // Figure out which registry key signalled this change. |
115 RegKeyList::iterator it = | 116 auto it = std::find_if(keys_to_watch_.begin(), keys_to_watch_.end(), |
116 std::find(keys_to_watch_.begin(), keys_to_watch_.end(), key); | 117 [key](const std::unique_ptr<base::win::RegKey>& ptr) { |
| 118 return ptr.get() == key; |
| 119 }); |
117 DCHECK(it != keys_to_watch_.end()); | 120 DCHECK(it != keys_to_watch_.end()); |
118 | 121 |
119 // Keep watching the registry key. | 122 // Keep watching the registry key. |
120 if (!key->StartWatching(base::Bind(&ProxyConfigServiceWin::OnObjectSignaled, | 123 if (!key->StartWatching(base::Bind(&ProxyConfigServiceWin::OnObjectSignaled, |
121 base::Unretained(this), | 124 base::Unretained(this), |
122 base::Unretained(key)))) { | 125 base::Unretained(key)))) { |
123 delete *it; | |
124 keys_to_watch_.erase(it); | 126 keys_to_watch_.erase(it); |
125 } | 127 } |
126 | 128 |
127 // Have the PollingProxyConfigService test for changes. | 129 // Have the PollingProxyConfigService test for changes. |
128 CheckForChangesNow(); | 130 CheckForChangesNow(); |
129 } | 131 } |
130 | 132 |
131 // static | 133 // static |
132 void ProxyConfigServiceWin::GetCurrentProxyConfig(ProxyConfig* config) { | 134 void ProxyConfigServiceWin::GetCurrentProxyConfig(ProxyConfig* config) { |
133 WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ie_config = {0}; | 135 WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ie_config = {0}; |
(...skipping 28 matching lines...) Expand all Loading... |
162 std::string bypass_url_domain = proxy_server_bypass_list.token(); | 164 std::string bypass_url_domain = proxy_server_bypass_list.token(); |
163 config->proxy_rules().bypass_rules.AddRuleFromString(bypass_url_domain); | 165 config->proxy_rules().bypass_rules.AddRuleFromString(bypass_url_domain); |
164 } | 166 } |
165 } | 167 } |
166 if (ie_config.lpszAutoConfigUrl) | 168 if (ie_config.lpszAutoConfigUrl) |
167 config->set_pac_url(GURL(ie_config.lpszAutoConfigUrl)); | 169 config->set_pac_url(GURL(ie_config.lpszAutoConfigUrl)); |
168 config->set_source(PROXY_CONFIG_SOURCE_SYSTEM); | 170 config->set_source(PROXY_CONFIG_SOURCE_SYSTEM); |
169 } | 171 } |
170 | 172 |
171 } // namespace net | 173 } // namespace net |
OLD | NEW |