Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(721)

Side by Side Diff: chrome/utility/wifi/wifi_test.cc

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

Powered by Google App Engine
This is Rietveld 408576698