Make sure webrtc::VideoSource is released when WebRtcVideoTrackAdapter is destroyed.
[chromium-blink-merge.git] / components / wifi / wifi_test.cc
blobf34e0c0afc31e5cfd5308ca897380c76dfae6e20
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/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_key]"
88 " [--get_properties]"
89 " [--create]"
90 " [--connect]"
91 " [--disconnect]"
92 " [--network_guid=<network_guid>]"
93 " [--frequency=0|2400|5000]"
94 " [--security=none|WEP-PSK|WPA-PSK|WPA2-PSK]"
95 " [--password=<wifi_password>]"
96 " [<network_guid>]\n";
97 return RESULT_WRONG_USAGE;
100 base::MessageLoopForIO loop;
101 result_ = RESULT_PENDING;
103 return result_;
106 bool WiFiTest::ParseCommandLine(int argc, const char* argv[]) {
107 CommandLine::Init(argc, argv);
108 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
109 std::string network_guid =
110 parsed_command_line.GetSwitchValueASCII("network_guid");
111 std::string frequency =
112 parsed_command_line.GetSwitchValueASCII("frequency");
113 std::string password =
114 parsed_command_line.GetSwitchValueASCII("password");
115 std::string security =
116 parsed_command_line.GetSwitchValueASCII("security");
118 if (parsed_command_line.GetArgs().size() == 1) {
119 #if defined(OS_WIN)
120 network_guid = base::UTF16ToASCII(parsed_command_line.GetArgs()[0]);
121 #else
122 network_guid = parsed_command_line.GetArgs()[0];
123 #endif
126 #if defined(OS_WIN)
127 if (parsed_command_line.HasSwitch("debug"))
128 MessageBoxA(NULL, __FUNCTION__, "Debug Me!", MB_OK);
129 #endif
131 base::MessageLoopForIO loop;
133 wifi_service_.reset(WiFiService::Create());
134 wifi_service_->Initialize(loop.message_loop_proxy());
136 if (parsed_command_line.HasSwitch("list")) {
137 base::ListValue network_list;
138 wifi_service_->GetVisibleNetworks(std::string(), &network_list);
139 VLOG(0) << network_list;
140 return true;
143 if (parsed_command_line.HasSwitch("get_properties")) {
144 if (network_guid.length() > 0) {
145 base::DictionaryValue properties;
146 std::string error;
147 wifi_service_->GetProperties(network_guid, &properties, &error);
148 VLOG(0) << error << ":\n" << properties;
149 return true;
153 // Optional properties (frequency, password) to use for connect or create.
154 scoped_ptr<base::DictionaryValue> properties(new base::DictionaryValue());
156 if (!frequency.empty()) {
157 int value = 0;
158 if (base::StringToInt(frequency, &value)) {
159 properties->SetInteger("WiFi.Frequency", value);
160 // fall through to connect.
164 if (!password.empty())
165 properties->SetString("WiFi.Passphrase", password);
167 if (!security.empty())
168 properties->SetString("WiFi.Security", security);
170 if (parsed_command_line.HasSwitch("create")) {
171 if (!network_guid.empty()) {
172 std::string error;
173 std::string new_network_guid;
174 properties->SetString("WiFi.SSID", network_guid);
175 VLOG(0) << "Creating Network: " << *properties;
176 wifi_service_->CreateNetwork(
177 false, properties.Pass(), &new_network_guid, &error);
178 VLOG(0) << error << ":\n" << new_network_guid;
179 return true;
183 if (parsed_command_line.HasSwitch("connect")) {
184 if (!network_guid.empty()) {
185 std::string error;
186 if (!properties->empty()) {
187 VLOG(0) << "Using connect properties: " << *properties;
188 wifi_service_->SetProperties(network_guid, properties.Pass(), &error);
191 wifi_service_->SetEventObservers(
192 loop.message_loop_proxy(),
193 base::Bind(&WiFiTest::OnNetworksChanged, base::Unretained(this)),
194 base::Bind(&WiFiTest::OnNetworkListChanged, base::Unretained(this)));
196 wifi_service_->StartConnect(network_guid, &error);
197 VLOG(0) << error;
198 if (error.empty())
199 base::MessageLoop::current()->Run();
200 return true;
204 if (parsed_command_line.HasSwitch("disconnect")) {
205 if (network_guid.length() > 0) {
206 std::string error;
207 wifi_service_->StartDisconnect(network_guid, &error);
208 VLOG(0) << error;
209 return true;
213 if (parsed_command_line.HasSwitch("get_key")) {
214 if (network_guid.length() > 0) {
215 std::string error;
216 std::string key_data;
217 wifi_service_->GetKeyFromSystem(network_guid, &key_data, &error);
218 VLOG(0) << key_data << error;
219 return true;
223 return false;
226 } // namespace wifi
228 int main(int argc, const char* argv[]) {
229 CommandLine::Init(argc, argv);
230 logging::LoggingSettings settings;
231 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
232 logging::InitLogging(settings);
234 wifi::WiFiTest wifi_test;
235 return wifi_test.Main(argc, argv);