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