Vectorize website settings icons in omnibox
[chromium-blink-merge.git] / components / wifi / wifi_test.cc
blob490de32c31a9594c81810d90de687118c697b602
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
3 // found in the LICENSE file.
5 #include <stdio.h>
6 #include <string>
8 #include "base/at_exit.h"
9 #include "base/bind.h"
10 #include "base/cancelable_callback.h"
11 #include "base/command_line.h"
12 #include "base/files/file_util.h"
13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/run_loop.h"
17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/string_split.h"
19 #include "base/strings/string_util.h"
20 #include "base/strings/stringprintf.h"
21 #include "base/strings/utf_string_conversions.h"
22 #include "base/time/time.h"
23 #include "components/wifi/wifi_service.h"
25 #if defined(OS_MACOSX)
26 #include "base/mac/scoped_nsautorelease_pool.h"
27 #endif
29 namespace wifi {
31 class WiFiTest {
32 public:
33 WiFiTest() {}
34 ~WiFiTest() {}
36 enum Result {
37 RESULT_ERROR = -2,
38 RESULT_WRONG_USAGE = -1,
39 RESULT_OK = 0,
40 RESULT_PENDING = 1,
43 Result Main(int argc, const char* argv[]);
45 private:
46 bool ParseCommandLine(int argc, const char* argv[]);
48 void Start() {}
49 void Finish(Result result) {
50 DCHECK_NE(RESULT_PENDING, result);
51 result_ = result;
52 if (base::MessageLoop::current())
53 base::MessageLoop::current()->Quit();
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);
62 VLOG(0) << error << ":\n" << properties;
65 void OnNetworkListChanged(
66 const WiFiService::NetworkGuidList& network_guid_list) {
67 VLOG(0) << "Network List Changed: " << network_guid_list.size();
70 #if defined(OS_MACOSX)
71 // Without this there will be a mem leak on osx.
72 base::mac::ScopedNSAutoreleasePool scoped_pool_;
73 #endif
75 scoped_ptr<WiFiService> wifi_service_;
77 // Need AtExitManager to support AsWeakPtr (in NetLog).
78 base::AtExitManager exit_manager_;
80 Result result_;
83 WiFiTest::Result WiFiTest::Main(int argc, const char* argv[]) {
84 if (!ParseCommandLine(argc, argv)) {
85 VLOG(0) << "Usage: " << argv[0]
86 << " [--list]"
87 " [--get_connected_ssid]"
88 " [--get_key]"
89 " [--get_properties]"
90 " [--create]"
91 " [--connect]"
92 " [--disconnect]"
93 " [--scan]"
94 " [--network_guid=<network_guid>]"
95 " [--frequency=0|2400|5000]"
96 " [--security=none|WEP-PSK|WPA-PSK|WPA2-PSK]"
97 " [--password=<wifi_password>]"
98 " [<network_guid>]\n";
99 return RESULT_WRONG_USAGE;
102 base::MessageLoopForIO loop;
103 result_ = RESULT_PENDING;
105 return result_;
108 bool WiFiTest::ParseCommandLine(int argc, const char* argv[]) {
109 base::CommandLine::Init(argc, argv);
110 const base::CommandLine& parsed_command_line =
111 *base::CommandLine::ForCurrentProcess();
112 std::string network_guid =
113 parsed_command_line.GetSwitchValueASCII("network_guid");
114 std::string frequency =
115 parsed_command_line.GetSwitchValueASCII("frequency");
116 std::string password =
117 parsed_command_line.GetSwitchValueASCII("password");
118 std::string security =
119 parsed_command_line.GetSwitchValueASCII("security");
121 if (parsed_command_line.GetArgs().size() == 1) {
122 #if defined(OS_WIN)
123 network_guid = base::UTF16ToASCII(parsed_command_line.GetArgs()[0]);
124 #else
125 network_guid = parsed_command_line.GetArgs()[0];
126 #endif
129 #if defined(OS_WIN)
130 if (parsed_command_line.HasSwitch("debug"))
131 MessageBoxA(NULL, __FUNCTION__, "Debug Me!", MB_OK);
132 #endif
134 base::MessageLoopForIO loop;
136 wifi_service_.reset(WiFiService::Create());
137 wifi_service_->Initialize(loop.task_runner());
139 if (parsed_command_line.HasSwitch("list")) {
140 base::ListValue network_list;
141 wifi_service_->GetVisibleNetworks(std::string(), &network_list, true);
142 VLOG(0) << network_list;
143 return true;
146 if (parsed_command_line.HasSwitch("get_properties")) {
147 if (network_guid.length() > 0) {
148 base::DictionaryValue properties;
149 std::string error;
150 wifi_service_->GetProperties(network_guid, &properties, &error);
151 VLOG(0) << error << ":\n" << properties;
152 return true;
156 // Optional properties (frequency, password) to use for connect or create.
157 scoped_ptr<base::DictionaryValue> properties(new base::DictionaryValue());
159 if (!frequency.empty()) {
160 int value = 0;
161 if (base::StringToInt(frequency, &value)) {
162 properties->SetInteger("WiFi.Frequency", value);
163 // fall through to connect.
167 if (!password.empty())
168 properties->SetString("WiFi.Passphrase", password);
170 if (!security.empty())
171 properties->SetString("WiFi.Security", security);
173 if (parsed_command_line.HasSwitch("create")) {
174 if (!network_guid.empty()) {
175 std::string error;
176 std::string new_network_guid;
177 properties->SetString("WiFi.SSID", network_guid);
178 VLOG(0) << "Creating Network: " << *properties;
179 wifi_service_->CreateNetwork(
180 false, properties.Pass(), &new_network_guid, &error);
181 VLOG(0) << error << ":\n" << new_network_guid;
182 return true;
186 if (parsed_command_line.HasSwitch("connect")) {
187 if (!network_guid.empty()) {
188 std::string error;
189 if (!properties->empty()) {
190 VLOG(0) << "Using connect properties: " << *properties;
191 wifi_service_->SetProperties(network_guid, properties.Pass(), &error);
194 wifi_service_->SetEventObservers(
195 loop.task_runner(),
196 base::Bind(&WiFiTest::OnNetworksChanged, base::Unretained(this)),
197 base::Bind(&WiFiTest::OnNetworkListChanged, base::Unretained(this)));
199 wifi_service_->StartConnect(network_guid, &error);
200 VLOG(0) << error;
201 if (error.empty())
202 base::MessageLoop::current()->Run();
203 return true;
207 if (parsed_command_line.HasSwitch("disconnect")) {
208 if (network_guid.length() > 0) {
209 std::string error;
210 wifi_service_->StartDisconnect(network_guid, &error);
211 VLOG(0) << error;
212 return true;
216 if (parsed_command_line.HasSwitch("get_key")) {
217 if (network_guid.length() > 0) {
218 std::string error;
219 std::string key_data;
220 wifi_service_->GetKeyFromSystem(network_guid, &key_data, &error);
221 VLOG(0) << key_data << error;
222 return true;
226 if (parsed_command_line.HasSwitch("scan")) {
227 wifi_service_->SetEventObservers(
228 loop.task_runner(),
229 base::Bind(&WiFiTest::OnNetworksChanged, base::Unretained(this)),
230 base::Bind(&WiFiTest::OnNetworkListChanged, base::Unretained(this)));
231 wifi_service_->RequestNetworkScan();
232 base::MessageLoop::current()->Run();
233 return true;
236 if (parsed_command_line.HasSwitch("get_connected_ssid")) {
237 std::string ssid;
238 std::string error;
239 wifi_service_->GetConnectedNetworkSSID(&ssid, &error);
240 if (error.length() > 0)
241 VLOG(0) << error;
242 else
243 VLOG(0) << "Network SSID: " << ssid;
244 return true;
247 return false;
250 } // namespace wifi
252 int main(int argc, const char* argv[]) {
253 base::CommandLine::Init(argc, argv);
254 logging::LoggingSettings settings;
255 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
256 logging::InitLogging(settings);
258 wifi::WiFiTest wifi_test;
259 return wifi_test.Main(argc, argv);