OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <stdio.h> | 5 #include <stdio.h> |
6 #include <string> | 6 #include <string> |
7 | 7 |
8 #include "base/at_exit.h" | 8 #include "base/at_exit.h" |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/cancelable_callback.h" | 10 #include "base/cancelable_callback.h" |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
16 #include "base/run_loop.h" | |
16 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
17 #include "base/strings/string_split.h" | 18 #include "base/strings/string_split.h" |
18 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
19 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
20 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
21 #include "base/time/time.h" | 22 #include "base/time/time.h" |
22 #include "components/wifi/wifi_service.h" | 23 #include "components/wifi/wifi_service.h" |
23 | 24 |
24 #if defined(OS_MACOSX) | 25 #if defined(OS_MACOSX) |
25 #include "base/mac/scoped_nsautorelease_pool.h" | 26 #include "base/mac/scoped_nsautorelease_pool.h" |
(...skipping 19 matching lines...) Expand all Loading... | |
45 bool ParseCommandLine(int argc, const char* argv[]); | 46 bool ParseCommandLine(int argc, const char* argv[]); |
46 | 47 |
47 void Start() {} | 48 void Start() {} |
48 void Finish(Result result) { | 49 void Finish(Result result) { |
49 DCHECK_NE(RESULT_PENDING, result); | 50 DCHECK_NE(RESULT_PENDING, result); |
50 result_ = result; | 51 result_ = result; |
51 if (base::MessageLoop::current()) | 52 if (base::MessageLoop::current()) |
52 base::MessageLoop::current()->Quit(); | 53 base::MessageLoop::current()->Quit(); |
53 } | 54 } |
54 | 55 |
56 void OnNetworksChanged( | |
57 const WiFiService::NetworkGuidList& network_guid_list) { | |
58 VLOG(0) << "Networks Changed: " << network_guid_list[0]; | |
59 base::DictionaryValue properties; | |
60 std::string error; | |
61 wifi_service_->GetProperties(network_guid_list[0], &properties, &error); | |
tbarzic
2014/04/23 20:53:56
quit loop if the target network connection status
mef
2014/04/23 21:41:23
It also gets notified about previously connected n
| |
62 VLOG(0) << error << ":\n" << properties; | |
63 } | |
64 | |
65 void OnNetworkListChanged( | |
66 const WiFiService::NetworkGuidList& network_guid_list) { | |
67 VLOG(0) << "Network List Changed: " << network_guid_list.size(); | |
68 } | |
69 | |
55 #if defined(OS_MACOSX) | 70 #if defined(OS_MACOSX) |
56 // Without this there will be a mem leak on osx. | 71 // Without this there will be a mem leak on osx. |
57 base::mac::ScopedNSAutoreleasePool scoped_pool_; | 72 base::mac::ScopedNSAutoreleasePool scoped_pool_; |
58 #endif | 73 #endif |
59 | 74 |
75 scoped_ptr<WiFiService> wifi_service_; | |
76 | |
60 // Need AtExitManager to support AsWeakPtr (in NetLog). | 77 // Need AtExitManager to support AsWeakPtr (in NetLog). |
61 base::AtExitManager exit_manager_; | 78 base::AtExitManager exit_manager_; |
62 | 79 |
63 Result result_; | 80 Result result_; |
64 }; | 81 }; |
65 | 82 |
66 WiFiTest::Result WiFiTest::Main(int argc, const char* argv[]) { | 83 WiFiTest::Result WiFiTest::Main(int argc, const char* argv[]) { |
67 if (!ParseCommandLine(argc, argv)) { | 84 if (!ParseCommandLine(argc, argv)) { |
68 VLOG(0) << "Usage: " << argv[0] << | 85 VLOG(0) << "Usage: " << argv[0] << |
69 " [--list]" | 86 " [--list]" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
104 #else | 121 #else |
105 network_guid = parsed_command_line.GetArgs()[0]; | 122 network_guid = parsed_command_line.GetArgs()[0]; |
106 #endif | 123 #endif |
107 } | 124 } |
108 | 125 |
109 #if defined(OS_WIN) | 126 #if defined(OS_WIN) |
110 if (parsed_command_line.HasSwitch("debug")) | 127 if (parsed_command_line.HasSwitch("debug")) |
111 MessageBoxA(NULL, __FUNCTION__, "Debug Me!", MB_OK); | 128 MessageBoxA(NULL, __FUNCTION__, "Debug Me!", MB_OK); |
112 #endif | 129 #endif |
113 | 130 |
114 #if defined(OS_WIN) || defined(OS_MACOSX) | 131 base::MessageLoopForIO loop; |
115 scoped_ptr<WiFiService> wifi_service(WiFiService::Create()); | |
116 #else | |
117 scoped_ptr<WiFiService> wifi_service(WiFiService::CreateForTest()); | |
118 #endif | |
119 | 132 |
120 wifi_service->Initialize(NULL); | 133 wifi_service_.reset(WiFiService::Create()); |
134 wifi_service_->Initialize(loop.message_loop_proxy()); | |
121 | 135 |
122 if (parsed_command_line.HasSwitch("list")) { | 136 if (parsed_command_line.HasSwitch("list")) { |
123 base::ListValue network_list; | 137 base::ListValue network_list; |
124 wifi_service->GetVisibleNetworks(std::string(), &network_list); | 138 wifi_service_->GetVisibleNetworks(std::string(), &network_list); |
125 VLOG(0) << network_list; | 139 VLOG(0) << network_list; |
126 return true; | 140 return true; |
127 } | 141 } |
128 | 142 |
129 if (parsed_command_line.HasSwitch("get_properties")) { | 143 if (parsed_command_line.HasSwitch("get_properties")) { |
130 if (network_guid.length() > 0) { | 144 if (network_guid.length() > 0) { |
131 base::DictionaryValue properties; | 145 base::DictionaryValue properties; |
132 std::string error; | 146 std::string error; |
133 wifi_service->GetProperties(network_guid, &properties, &error); | 147 wifi_service_->GetProperties(network_guid, &properties, &error); |
134 VLOG(0) << error << ":\n" << properties; | 148 VLOG(0) << error << ":\n" << properties; |
135 return true; | 149 return true; |
136 } | 150 } |
137 } | 151 } |
138 | 152 |
139 // Optional properties (frequency, password) to use for connect or create. | 153 // Optional properties (frequency, password) to use for connect or create. |
140 scoped_ptr<base::DictionaryValue> properties(new base::DictionaryValue()); | 154 scoped_ptr<base::DictionaryValue> properties(new base::DictionaryValue()); |
141 | 155 |
142 if (!frequency.empty()) { | 156 if (!frequency.empty()) { |
143 int value = 0; | 157 int value = 0; |
144 if (base::StringToInt(frequency, &value)) { | 158 if (base::StringToInt(frequency, &value)) { |
145 properties->SetInteger("WiFi.Frequency", value); | 159 properties->SetInteger("WiFi.Frequency", value); |
146 // fall through to connect. | 160 // fall through to connect. |
147 } | 161 } |
148 } | 162 } |
149 | 163 |
150 if (!password.empty()) | 164 if (!password.empty()) |
151 properties->SetString("WiFi.Passphrase", password); | 165 properties->SetString("WiFi.Passphrase", password); |
152 | 166 |
153 if (!security.empty()) | 167 if (!security.empty()) |
154 properties->SetString("WiFi.Security", security); | 168 properties->SetString("WiFi.Security", security); |
155 | 169 |
156 if (parsed_command_line.HasSwitch("create")) { | 170 if (parsed_command_line.HasSwitch("create")) { |
157 if (!network_guid.empty()) { | 171 if (!network_guid.empty()) { |
158 std::string error; | 172 std::string error; |
159 std::string new_network_guid; | 173 std::string new_network_guid; |
160 properties->SetString("WiFi.SSID", network_guid); | 174 properties->SetString("WiFi.SSID", network_guid); |
161 VLOG(0) << "Creating Network: " << *properties; | 175 VLOG(0) << "Creating Network: " << *properties; |
162 wifi_service->CreateNetwork(false, | 176 wifi_service_->CreateNetwork(false, |
163 properties.Pass(), | 177 properties.Pass(), |
tbarzic
2014/04/23 20:53:56
indent
mef
2014/04/23 21:41:23
Done.
| |
164 &new_network_guid, | 178 &new_network_guid, |
165 &error); | 179 &error); |
166 VLOG(0) << error << ":\n" << new_network_guid; | 180 VLOG(0) << error << ":\n" << new_network_guid; |
167 return true; | 181 return true; |
168 } | 182 } |
169 } | 183 } |
170 | 184 |
171 if (parsed_command_line.HasSwitch("connect")) { | 185 if (parsed_command_line.HasSwitch("connect")) { |
172 if (!network_guid.empty()) { | 186 if (!network_guid.empty()) { |
173 std::string error; | 187 std::string error; |
174 if (!properties->empty()) { | 188 if (!properties->empty()) { |
175 VLOG(0) << "Using connect properties: " << *properties; | 189 VLOG(0) << "Using connect properties: " << *properties; |
176 wifi_service->SetProperties(network_guid, | 190 wifi_service_->SetProperties(network_guid, |
177 properties.Pass(), | 191 properties.Pass(), |
178 &error); | 192 &error); |
179 } | 193 } |
180 wifi_service->StartConnect(network_guid, &error); | 194 |
195 wifi_service_->SetEventObservers( | |
196 loop.message_loop_proxy(), | |
197 base::Bind(&WiFiTest::OnNetworksChanged, | |
198 base::Unretained(this)), | |
199 base::Bind(&WiFiTest::OnNetworkListChanged, | |
200 base::Unretained(this))); | |
201 | |
202 wifi_service_->StartConnect(network_guid, &error); | |
181 VLOG(0) << error; | 203 VLOG(0) << error; |
204 base::MessageLoop::current()->Run(); | |
182 return true; | 205 return true; |
183 } | 206 } |
184 } | 207 } |
185 | 208 |
186 if (parsed_command_line.HasSwitch("disconnect")) { | 209 if (parsed_command_line.HasSwitch("disconnect")) { |
187 if (network_guid.length() > 0) { | 210 if (network_guid.length() > 0) { |
188 std::string error; | 211 std::string error; |
189 wifi_service->StartDisconnect(network_guid, &error); | 212 wifi_service_->StartDisconnect(network_guid, &error); |
190 VLOG(0) << error; | 213 VLOG(0) << error; |
191 return true; | 214 return true; |
192 } | 215 } |
193 } | 216 } |
194 | 217 |
195 if (parsed_command_line.HasSwitch("get_key")) { | 218 if (parsed_command_line.HasSwitch("get_key")) { |
196 if (network_guid.length() > 0) { | 219 if (network_guid.length() > 0) { |
197 std::string error; | 220 std::string error; |
198 std::string key_data; | 221 std::string key_data; |
199 wifi_service->GetKeyFromSystem(network_guid, | 222 wifi_service_->GetKeyFromSystem(network_guid, |
200 &key_data, | 223 &key_data, |
201 &error); | 224 &error); |
202 VLOG(0) << key_data << error; | 225 VLOG(0) << key_data << error; |
203 return true; | 226 return true; |
204 } | 227 } |
205 } | 228 } |
206 | 229 |
207 return false; | 230 return false; |
208 } | 231 } |
209 | 232 |
210 } // namespace wifi | 233 } // namespace wifi |
211 | 234 |
212 int main(int argc, const char* argv[]) { | 235 int main(int argc, const char* argv[]) { |
213 CommandLine::Init(argc, argv); | 236 CommandLine::Init(argc, argv); |
214 logging::LoggingSettings settings; | 237 logging::LoggingSettings settings; |
215 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; | 238 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; |
216 logging::InitLogging(settings); | 239 logging::InitLogging(settings); |
217 | 240 |
218 wifi::WiFiTest wifi_test; | 241 wifi::WiFiTest wifi_test; |
219 return wifi_test.Main(argc, argv); | 242 return wifi_test.Main(argc, argv); |
220 } | 243 } |
OLD | NEW |