| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stdio.h> | 5 #include <stdio.h> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "base/at_exit.h" | 8 #include "base/at_exit.h" |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/cancelable_callback.h" | 10 #include "base/cancelable_callback.h" |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 16 #include "base/run_loop.h" |
| 16 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/strings/string_split.h" | 18 #include "base/strings/string_split.h" |
| 18 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
| 19 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
| 20 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
| 21 #include "base/time/time.h" | 22 #include "base/time/time.h" |
| 22 #include "components/wifi/wifi_service.h" | 23 #include "components/wifi/wifi_service.h" |
| 23 | 24 |
| 24 #if defined(OS_MACOSX) | 25 #if defined(OS_MACOSX) |
| 25 #include "base/mac/scoped_nsautorelease_pool.h" | 26 #include "base/mac/scoped_nsautorelease_pool.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 45 bool ParseCommandLine(int argc, const char* argv[]); | 46 bool ParseCommandLine(int argc, const char* argv[]); |
| 46 | 47 |
| 47 void Start() {} | 48 void Start() {} |
| 48 void Finish(Result result) { | 49 void Finish(Result result) { |
| 49 DCHECK_NE(RESULT_PENDING, result); | 50 DCHECK_NE(RESULT_PENDING, result); |
| 50 result_ = result; | 51 result_ = result; |
| 51 if (base::MessageLoop::current()) | 52 if (base::MessageLoop::current()) |
| 52 base::MessageLoop::current()->Quit(); | 53 base::MessageLoop::current()->Quit(); |
| 53 } | 54 } |
| 54 | 55 |
| 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; |
| 63 } |
| 64 |
| 65 void OnNetworkListChanged( |
| 66 const WiFiService::NetworkGuidList& network_guid_list) { |
| 67 VLOG(0) << "Network List Changed: " << network_guid_list.size(); |
| 68 } |
| 69 |
| 55 #if defined(OS_MACOSX) | 70 #if defined(OS_MACOSX) |
| 56 // Without this there will be a mem leak on osx. | 71 // Without this there will be a mem leak on osx. |
| 57 base::mac::ScopedNSAutoreleasePool scoped_pool_; | 72 base::mac::ScopedNSAutoreleasePool scoped_pool_; |
| 58 #endif | 73 #endif |
| 59 | 74 |
| 75 scoped_ptr<WiFiService> wifi_service_; |
| 76 |
| 60 // Need AtExitManager to support AsWeakPtr (in NetLog). | 77 // Need AtExitManager to support AsWeakPtr (in NetLog). |
| 61 base::AtExitManager exit_manager_; | 78 base::AtExitManager exit_manager_; |
| 62 | 79 |
| 63 Result result_; | 80 Result result_; |
| 64 }; | 81 }; |
| 65 | 82 |
| 66 WiFiTest::Result WiFiTest::Main(int argc, const char* argv[]) { | 83 WiFiTest::Result WiFiTest::Main(int argc, const char* argv[]) { |
| 67 if (!ParseCommandLine(argc, argv)) { | 84 if (!ParseCommandLine(argc, argv)) { |
| 68 VLOG(0) << "Usage: " << argv[0] << | 85 VLOG(0) << "Usage: " << argv[0] << |
| 69 " [--list]" | 86 " [--list]" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 #else | 121 #else |
| 105 network_guid = parsed_command_line.GetArgs()[0]; | 122 network_guid = parsed_command_line.GetArgs()[0]; |
| 106 #endif | 123 #endif |
| 107 } | 124 } |
| 108 | 125 |
| 109 #if defined(OS_WIN) | 126 #if defined(OS_WIN) |
| 110 if (parsed_command_line.HasSwitch("debug")) | 127 if (parsed_command_line.HasSwitch("debug")) |
| 111 MessageBoxA(NULL, __FUNCTION__, "Debug Me!", MB_OK); | 128 MessageBoxA(NULL, __FUNCTION__, "Debug Me!", MB_OK); |
| 112 #endif | 129 #endif |
| 113 | 130 |
| 114 #if defined(OS_WIN) || defined(OS_MACOSX) | 131 base::MessageLoopForIO loop; |
| 115 scoped_ptr<WiFiService> wifi_service(WiFiService::Create()); | |
| 116 #else | |
| 117 scoped_ptr<WiFiService> wifi_service(WiFiService::CreateForTest()); | |
| 118 #endif | |
| 119 | 132 |
| 120 wifi_service->Initialize(NULL); | 133 wifi_service_.reset(WiFiService::Create()); |
| 134 wifi_service_->Initialize(loop.message_loop_proxy()); |
| 121 | 135 |
| 122 if (parsed_command_line.HasSwitch("list")) { | 136 if (parsed_command_line.HasSwitch("list")) { |
| 123 base::ListValue network_list; | 137 base::ListValue network_list; |
| 124 wifi_service->GetVisibleNetworks(std::string(), &network_list); | 138 wifi_service_->GetVisibleNetworks(std::string(), &network_list); |
| 125 VLOG(0) << network_list; | 139 VLOG(0) << network_list; |
| 126 return true; | 140 return true; |
| 127 } | 141 } |
| 128 | 142 |
| 129 if (parsed_command_line.HasSwitch("get_properties")) { | 143 if (parsed_command_line.HasSwitch("get_properties")) { |
| 130 if (network_guid.length() > 0) { | 144 if (network_guid.length() > 0) { |
| 131 base::DictionaryValue properties; | 145 base::DictionaryValue properties; |
| 132 std::string error; | 146 std::string error; |
| 133 wifi_service->GetProperties(network_guid, &properties, &error); | 147 wifi_service_->GetProperties(network_guid, &properties, &error); |
| 134 VLOG(0) << error << ":\n" << properties; | 148 VLOG(0) << error << ":\n" << properties; |
| 135 return true; | 149 return true; |
| 136 } | 150 } |
| 137 } | 151 } |
| 138 | 152 |
| 139 // Optional properties (frequency, password) to use for connect or create. | 153 // Optional properties (frequency, password) to use for connect or create. |
| 140 scoped_ptr<base::DictionaryValue> properties(new base::DictionaryValue()); | 154 scoped_ptr<base::DictionaryValue> properties(new base::DictionaryValue()); |
| 141 | 155 |
| 142 if (!frequency.empty()) { | 156 if (!frequency.empty()) { |
| 143 int value = 0; | 157 int value = 0; |
| 144 if (base::StringToInt(frequency, &value)) { | 158 if (base::StringToInt(frequency, &value)) { |
| 145 properties->SetInteger("WiFi.Frequency", value); | 159 properties->SetInteger("WiFi.Frequency", value); |
| 146 // fall through to connect. | 160 // fall through to connect. |
| 147 } | 161 } |
| 148 } | 162 } |
| 149 | 163 |
| 150 if (!password.empty()) | 164 if (!password.empty()) |
| 151 properties->SetString("WiFi.Passphrase", password); | 165 properties->SetString("WiFi.Passphrase", password); |
| 152 | 166 |
| 153 if (!security.empty()) | 167 if (!security.empty()) |
| 154 properties->SetString("WiFi.Security", security); | 168 properties->SetString("WiFi.Security", security); |
| 155 | 169 |
| 156 if (parsed_command_line.HasSwitch("create")) { | 170 if (parsed_command_line.HasSwitch("create")) { |
| 157 if (!network_guid.empty()) { | 171 if (!network_guid.empty()) { |
| 158 std::string error; | 172 std::string error; |
| 159 std::string new_network_guid; | 173 std::string new_network_guid; |
| 160 properties->SetString("WiFi.SSID", network_guid); | 174 properties->SetString("WiFi.SSID", network_guid); |
| 161 VLOG(0) << "Creating Network: " << *properties; | 175 VLOG(0) << "Creating Network: " << *properties; |
| 162 wifi_service->CreateNetwork(false, | 176 wifi_service_->CreateNetwork( |
| 163 properties.Pass(), | 177 false, properties.Pass(), &new_network_guid, &error); |
| 164 &new_network_guid, | |
| 165 &error); | |
| 166 VLOG(0) << error << ":\n" << new_network_guid; | 178 VLOG(0) << error << ":\n" << new_network_guid; |
| 167 return true; | 179 return true; |
| 168 } | 180 } |
| 169 } | 181 } |
| 170 | 182 |
| 171 if (parsed_command_line.HasSwitch("connect")) { | 183 if (parsed_command_line.HasSwitch("connect")) { |
| 172 if (!network_guid.empty()) { | 184 if (!network_guid.empty()) { |
| 173 std::string error; | 185 std::string error; |
| 174 if (!properties->empty()) { | 186 if (!properties->empty()) { |
| 175 VLOG(0) << "Using connect properties: " << *properties; | 187 VLOG(0) << "Using connect properties: " << *properties; |
| 176 wifi_service->SetProperties(network_guid, | 188 wifi_service_->SetProperties(network_guid, properties.Pass(), &error); |
| 177 properties.Pass(), | |
| 178 &error); | |
| 179 } | 189 } |
| 180 wifi_service->StartConnect(network_guid, &error); | 190 |
| 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))); |
| 195 |
| 196 wifi_service_->StartConnect(network_guid, &error); |
| 181 VLOG(0) << error; | 197 VLOG(0) << error; |
| 198 base::MessageLoop::current()->Run(); |
| 182 return true; | 199 return true; |
| 183 } | 200 } |
| 184 } | 201 } |
| 185 | 202 |
| 186 if (parsed_command_line.HasSwitch("disconnect")) { | 203 if (parsed_command_line.HasSwitch("disconnect")) { |
| 187 if (network_guid.length() > 0) { | 204 if (network_guid.length() > 0) { |
| 188 std::string error; | 205 std::string error; |
| 189 wifi_service->StartDisconnect(network_guid, &error); | 206 wifi_service_->StartDisconnect(network_guid, &error); |
| 190 VLOG(0) << error; | 207 VLOG(0) << error; |
| 191 return true; | 208 return true; |
| 192 } | 209 } |
| 193 } | 210 } |
| 194 | 211 |
| 195 if (parsed_command_line.HasSwitch("get_key")) { | 212 if (parsed_command_line.HasSwitch("get_key")) { |
| 196 if (network_guid.length() > 0) { | 213 if (network_guid.length() > 0) { |
| 197 std::string error; | 214 std::string error; |
| 198 std::string key_data; | 215 std::string key_data; |
| 199 wifi_service->GetKeyFromSystem(network_guid, | 216 wifi_service_->GetKeyFromSystem(network_guid, &key_data, &error); |
| 200 &key_data, | |
| 201 &error); | |
| 202 VLOG(0) << key_data << error; | 217 VLOG(0) << key_data << error; |
| 203 return true; | 218 return true; |
| 204 } | 219 } |
| 205 } | 220 } |
| 206 | 221 |
| 207 return false; | 222 return false; |
| 208 } | 223 } |
| 209 | 224 |
| 210 } // namespace wifi | 225 } // namespace wifi |
| 211 | 226 |
| 212 int main(int argc, const char* argv[]) { | 227 int main(int argc, const char* argv[]) { |
| 213 CommandLine::Init(argc, argv); | 228 CommandLine::Init(argc, argv); |
| 214 logging::LoggingSettings settings; | 229 logging::LoggingSettings settings; |
| 215 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; | 230 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; |
| 216 logging::InitLogging(settings); | 231 logging::InitLogging(settings); |
| 217 | 232 |
| 218 wifi::WiFiTest wifi_test; | 233 wifi::WiFiTest wifi_test; |
| 219 return wifi_test.Main(argc, argv); | 234 return wifi_test.Main(argc, argv); |
| 220 } | 235 } |
| OLD | NEW |