OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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_ERROR); |
| 64 } |
| 65 |
| 66 void OnDictionaryResult(const std::string& network_guid, |
| 67 const base::DictionaryValue& dictionary) { |
| 68 std::cout << __FUNCTION__ << ":\n" << network_guid << dictionary; |
| 69 Finish(RESULT_ERROR); |
| 70 } |
| 71 |
| 72 void OnNetworkProperties(const std::string& network_guid, |
| 73 const WiFiService::NetworkProperties& properties) { |
| 74 std::cout << __FUNCTION__ << ":\n" << *properties.ToValue(false).release(); |
| 75 Finish(RESULT_OK); |
| 76 } |
| 77 |
| 78 void OnGetVisibleNetworks(const WiFiService::NetworkList& network_list) { |
| 79 std::cout << __FUNCTION__ << ":\n"; |
| 80 for (WiFiService::NetworkList::const_iterator it = network_list.begin(); |
| 81 it != network_list.end(); |
| 82 ++it) { |
| 83 std::cout << *it->ToValue(false).release() << "\n"; |
| 84 } |
| 85 } |
| 86 |
| 87 #if defined(OS_MACOSX) |
| 88 // Without this there will be a mem leak on osx. |
| 89 base::mac::ScopedNSAutoreleasePool scoped_pool_; |
| 90 #endif |
| 91 |
| 92 // Need AtExitManager to support AsWeakPtr (in NetLog). |
| 93 base::AtExitManager exit_manager_; |
| 94 |
| 95 Result result_; |
| 96 }; |
| 97 |
| 98 WiFiTest::Result WiFiTest::Main(int argc, const char* argv[]) { |
| 99 if (!ParseCommandLine(argc, argv)) { |
| 100 fprintf(stderr, |
| 101 "usage: %s [--list]" |
| 102 " [--get_properties]" |
| 103 " [--connect]" |
| 104 " [--disconnect]" |
| 105 " [--network_guid=<network_guid>]" |
| 106 " [<network_guid>]\n", |
| 107 argv[0]); |
| 108 return RESULT_WRONG_USAGE; |
| 109 } |
| 110 |
| 111 base::MessageLoopForIO loop; |
| 112 result_ = RESULT_PENDING; |
| 113 Start(); |
| 114 // if (result_ == RESULT_PENDING) |
| 115 // base::MessageLoop::current()->Run(); |
| 116 |
| 117 return result_; |
| 118 } |
| 119 |
| 120 bool WiFiTest::ParseCommandLine(int argc, const char* argv[]) { |
| 121 CommandLine::Init(argc, argv); |
| 122 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess(); |
| 123 std::string network_guid = |
| 124 parsed_command_line.GetSwitchValueASCII("network_guid"); |
| 125 |
| 126 if (parsed_command_line.GetArgs().size() == 1) { |
| 127 #if defined(OS_WIN) |
| 128 network_guid = WideToASCII(parsed_command_line.GetArgs()[0]); |
| 129 #else |
| 130 network_guid = parsed_command_line.GetArgs()[0]; |
| 131 #endif |
| 132 } |
| 133 |
| 134 #if defined(OS_WIN) |
| 135 if (parsed_command_line.HasSwitch("debug")) |
| 136 MessageBoxA(NULL, __FUNCTION__, "Debug Me!", MB_OK); |
| 137 #endif |
| 138 |
| 139 #if defined(OS_WIN) |
| 140 scoped_ptr<WiFiService> wifi_service(WiFiService::CreateService()); |
| 141 #else |
| 142 scoped_ptr<WiFiService> wifi_service(WiFiService::CreateServiceMock()); |
| 143 #endif |
| 144 |
| 145 if (parsed_command_line.HasSwitch("list")) { |
| 146 wifi_service->GetVisibleNetworks( |
| 147 base::Bind(&WiFiTest::OnGetVisibleNetworks, base::Unretained(this)), |
| 148 base::Bind(&WiFiTest::OnError, base::Unretained(this))); |
| 149 return true; |
| 150 } |
| 151 |
| 152 if (parsed_command_line.HasSwitch("get_properties")) { |
| 153 if (network_guid.length() > 0) { |
| 154 wifi_service->GetProperties( |
| 155 network_guid, |
| 156 base::Bind(&WiFiTest::OnNetworkProperties, base::Unretained(this)), |
| 157 base::Bind(&WiFiTest::OnError, base::Unretained(this))); |
| 158 |
| 159 return true; |
| 160 } |
| 161 } |
| 162 |
| 163 if (parsed_command_line.HasSwitch("connect")) { |
| 164 if (network_guid.length() > 0) { |
| 165 wifi_service->StartConnect( |
| 166 network_guid, |
| 167 base::Bind(&WiFiTest::OnStringResult, base::Unretained(this)), |
| 168 base::Bind(&WiFiTest::OnError, base::Unretained(this))); |
| 169 |
| 170 return true; |
| 171 } |
| 172 } |
| 173 |
| 174 if (parsed_command_line.HasSwitch("disconnect")) { |
| 175 if (network_guid.length() > 0) { |
| 176 wifi_service->StartDisconnect( |
| 177 network_guid, |
| 178 base::Bind(&WiFiTest::OnStringResult, base::Unretained(this)), |
| 179 base::Bind(&WiFiTest::OnError, base::Unretained(this))); |
| 180 |
| 181 return true; |
| 182 } |
| 183 } |
| 184 |
| 185 return false; |
| 186 } |
| 187 |
| 188 } // namespace wifi |
| 189 |
| 190 int main(int argc, const char* argv[]) { |
| 191 wifi::WiFiTest wifi_test; |
| 192 return wifi_test.Main(argc, argv); |
| 193 } |
OLD | NEW |