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