OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include <stdio.h> |
| 6 #include <iostream> |
| 7 #include <string> |
| 8 |
| 9 #include "base/at_exit.h" |
| 10 #include "base/bind.h" |
| 11 #include "base/cancelable_callback.h" |
| 12 #include "base/command_line.h" |
| 13 #include "base/file_util.h" |
| 14 #include "base/logging.h" |
| 15 #if defined(OS_MACOSX) |
| 16 #include "base/mac/scoped_nsautorelease_pool.h" |
| 17 #endif |
| 18 #include "base/memory/scoped_ptr.h" |
| 19 #include "base/message_loop/message_loop.h" |
| 20 #include "base/strings/string_number_conversions.h" |
| 21 #include "base/strings/string_split.h" |
| 22 #include "base/strings/string_util.h" |
| 23 #include "base/strings/stringprintf.h" |
| 24 #include "base/time/time.h" |
| 25 #include "chrome/utility/wifi/wifi_service.h" |
| 26 |
| 27 namespace wifi { |
| 28 |
| 29 class WiFiTest { |
| 30 public: |
| 31 WiFiTest() {} |
| 32 ~WiFiTest() {} |
| 33 |
| 34 enum Result { |
| 35 RESULT_ERROR = -2, |
| 36 RESULT_WRONG_USAGE = -1, |
| 37 RESULT_OK = 0, |
| 38 RESULT_PENDING = 1, |
| 39 }; |
| 40 |
| 41 Result Main(int argc, const char* argv[]); |
| 42 |
| 43 private: |
| 44 bool ParseCommandLine(int argc, const char* argv[]); |
| 45 |
| 46 void Start() {} |
| 47 void Finish(Result result) { |
| 48 DCHECK_NE(RESULT_PENDING, result); |
| 49 result_ = result; |
| 50 if (base::MessageLoop::current()) |
| 51 base::MessageLoop::current()->Quit(); |
| 52 } |
| 53 |
| 54 void OnError(const std::string& error_name, |
| 55 scoped_ptr<base::DictionaryValue> error_data) { |
| 56 std::cout << __FUNCTION__ << ":" << error_name; |
| 57 Finish(RESULT_ERROR); |
| 58 } |
| 59 |
| 60 void OnStringResult(const std::string& network_guid) { |
| 61 std::cout << __FUNCTION__ << ":\n" << network_guid; |
| 62 Finish(RESULT_OK); |
| 63 } |
| 64 |
| 65 void OnNetworkProperties(const std::string& network_guid, |
| 66 const WiFiService::NetworkProperties& properties) { |
| 67 std::cout << __FUNCTION__ << ":\n" << *properties.ToValue(false).release(); |
| 68 Finish(RESULT_OK); |
| 69 } |
| 70 |
| 71 void OnGetVisibleNetworks(const WiFiService::NetworkList& network_list) { |
| 72 std::cout << __FUNCTION__ << ":\n"; |
| 73 for (WiFiService::NetworkList::const_iterator it = network_list.begin(); |
| 74 it != network_list.end(); |
| 75 ++it) { |
| 76 std::cout << *it->ToValue(false).release() << "\n"; |
| 77 } |
| 78 Finish(RESULT_OK); |
| 79 } |
| 80 |
| 81 #if defined(OS_MACOSX) |
| 82 // Without this there will be a mem leak on osx. |
| 83 base::mac::ScopedNSAutoreleasePool scoped_pool_; |
| 84 #endif |
| 85 |
| 86 // Need AtExitManager to support AsWeakPtr (in NetLog). |
| 87 base::AtExitManager exit_manager_; |
| 88 |
| 89 Result result_; |
| 90 }; |
| 91 |
| 92 WiFiTest::Result WiFiTest::Main(int argc, const char* argv[]) { |
| 93 if (!ParseCommandLine(argc, argv)) { |
| 94 fprintf(stderr, |
| 95 "usage: %s [--list]" |
| 96 " [--get_properties]" |
| 97 " [--connect]" |
| 98 " [--disconnect]" |
| 99 " [--network_guid=<network_guid>]" |
| 100 " [<network_guid>]\n", |
| 101 argv[0]); |
| 102 return RESULT_WRONG_USAGE; |
| 103 } |
| 104 |
| 105 base::MessageLoopForIO loop; |
| 106 result_ = RESULT_PENDING; |
| 107 Start(); |
| 108 |
| 109 return result_; |
| 110 } |
| 111 |
| 112 bool WiFiTest::ParseCommandLine(int argc, const char* argv[]) { |
| 113 CommandLine::Init(argc, argv); |
| 114 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess(); |
| 115 std::string network_guid = |
| 116 parsed_command_line.GetSwitchValueASCII("network_guid"); |
| 117 |
| 118 if (parsed_command_line.GetArgs().size() == 1) { |
| 119 #if defined(OS_WIN) |
| 120 network_guid = WideToASCII(parsed_command_line.GetArgs()[0]); |
| 121 #else |
| 122 network_guid = parsed_command_line.GetArgs()[0]; |
| 123 #endif |
| 124 } |
| 125 |
| 126 #if defined(OS_WIN) |
| 127 if (parsed_command_line.HasSwitch("debug")) |
| 128 MessageBoxA(NULL, __FUNCTION__, "Debug Me!", MB_OK); |
| 129 #endif |
| 130 |
| 131 #if defined(OS_WIN) |
| 132 scoped_ptr<WiFiService> wifi_service(WiFiService::CreateService()); |
| 133 #else |
| 134 scoped_ptr<WiFiService> wifi_service(WiFiService::CreateServiceMock()); |
| 135 #endif |
| 136 |
| 137 if (parsed_command_line.HasSwitch("list")) { |
| 138 wifi_service->GetVisibleNetworks( |
| 139 base::Bind(&WiFiTest::OnGetVisibleNetworks, base::Unretained(this)), |
| 140 base::Bind(&WiFiTest::OnError, base::Unretained(this))); |
| 141 return true; |
| 142 } |
| 143 |
| 144 if (parsed_command_line.HasSwitch("get_properties")) { |
| 145 if (network_guid.length() > 0) { |
| 146 wifi_service->GetProperties( |
| 147 network_guid, |
| 148 base::Bind(&WiFiTest::OnNetworkProperties, base::Unretained(this)), |
| 149 base::Bind(&WiFiTest::OnError, base::Unretained(this))); |
| 150 |
| 151 return true; |
| 152 } |
| 153 } |
| 154 |
| 155 if (parsed_command_line.HasSwitch("connect")) { |
| 156 if (network_guid.length() > 0) { |
| 157 wifi_service->StartConnect( |
| 158 network_guid, |
| 159 base::Bind(&WiFiTest::OnStringResult, base::Unretained(this)), |
| 160 base::Bind(&WiFiTest::OnError, base::Unretained(this))); |
| 161 |
| 162 return true; |
| 163 } |
| 164 } |
| 165 |
| 166 if (parsed_command_line.HasSwitch("disconnect")) { |
| 167 if (network_guid.length() > 0) { |
| 168 wifi_service->StartDisconnect( |
| 169 network_guid, |
| 170 base::Bind(&WiFiTest::OnStringResult, base::Unretained(this)), |
| 171 base::Bind(&WiFiTest::OnError, base::Unretained(this))); |
| 172 |
| 173 return true; |
| 174 } |
| 175 } |
| 176 |
| 177 return false; |
| 178 } |
| 179 |
| 180 } // namespace wifi |
| 181 |
| 182 int main(int argc, const char* argv[]) { |
| 183 wifi::WiFiTest wifi_test; |
| 184 return wifi_test.Main(argc, argv); |
| 185 } |
OLD | NEW |