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 <termios.h> | |
7 | |
8 #include "base/at_exit.h" | |
9 #include "base/command_line.h" | |
10 #include "base/run_loop.h" | |
11 #include "base/stringprintf.h" | |
12 #include "base/threading/thread.h" | |
13 #include "net/url_request/url_request_context_getter.h" | |
14 #include "remoting/host/setup/host_starter.h" | |
15 #include "remoting/host/setup/pin_validator.h" | |
16 #include "remoting/host/url_request_context.h" | |
17 | |
18 // A simple command-line app that registers and starts a host. | |
19 | |
20 using remoting::HostStarter; | |
21 | |
22 // True if the host could not be started. | |
23 bool g_fail = true; | |
Sergey Ulanov
2012/10/16 00:06:52
better to replace it with "g_started = false;". It
simonmorris
2012/10/16 00:38:53
Done.
| |
24 | |
25 // The main message loop. | |
26 MessageLoop* g_message_loop = NULL; | |
27 | |
28 // Lets us hide the PIN that a user types. | |
29 void SetEcho(bool echo) { | |
30 termios term; | |
31 tcgetattr(STDIN_FILENO, &term); | |
32 if (echo) { | |
33 term.c_lflag |= ECHO; | |
34 } else { | |
35 term.c_lflag &= ~ECHO; | |
36 } | |
37 tcsetattr(STDIN_FILENO, TCSANOW, &term); | |
38 } | |
39 | |
40 // Reads a newline-terminated string from stdin. | |
41 std::string ReadString(bool no_echo) { | |
42 if (no_echo) | |
43 SetEcho(false); | |
44 const int kMaxLen = 1024; | |
45 std::string str(kMaxLen, 0); | |
46 std::string format = base::StringPrintf("%%%d[^\n]%%*c", kMaxLen - 1); | |
47 int result = scanf(format.c_str(), &str[0]); | |
Sergey Ulanov
2012/10/16 00:06:52
Why not just use fgets() here, instead of scanf?
A
simonmorris
2012/10/16 00:38:53
Done.
| |
48 if (no_echo) { | |
49 printf("\n"); | |
50 SetEcho(true); | |
51 } | |
52 if (result < 1) | |
53 return ""; | |
54 str.resize(strlen(&str[0])); | |
55 return str; | |
56 } | |
57 | |
58 // Called when the HostStarter has finished. | |
59 void OnDone(HostStarter::Result result) { | |
60 if (MessageLoop::current() != g_message_loop) { | |
61 g_message_loop->PostTask(FROM_HERE, base::Bind(&OnDone, result)); | |
62 return; | |
63 } | |
64 switch (result) { | |
65 case HostStarter::START_IN_PROGRESS: | |
66 fprintf(stderr, "Internal error: START_IN_PROGRESS.\n"); | |
67 break; | |
68 case HostStarter::START_COMPLETE: | |
69 g_fail = false; | |
70 break; | |
71 case HostStarter::NETWORK_ERROR: | |
72 fprintf(stderr, "Couldn't start host: network error.\n"); | |
73 break; | |
74 case HostStarter::OAUTH_ERROR: | |
75 fprintf(stderr, "Couldn't start host: OAuth error.\n"); | |
76 break; | |
77 case HostStarter::START_ERROR: | |
78 fprintf(stderr, "Couldn't start host.\n"); | |
79 break; | |
80 } | |
81 | |
82 g_message_loop->QuitNow(); | |
83 } | |
84 | |
85 int main(int argc, char** argv) { | |
86 // google_apis::GetOAuth2ClientID/Secret need a static CommandLine. | |
87 CommandLine::Init(argc, argv); | |
88 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
89 | |
90 std::string host_name = command_line->GetSwitchValueASCII("name"); | |
91 std::string host_pin = command_line->GetSwitchValueASCII("pin"); | |
92 std::string auth_code = command_line->GetSwitchValueASCII("code"); | |
93 | |
94 if (host_name.empty()) { | |
95 fprintf(stderr, | |
96 "Usage: %s --name=<hostname> [--code=<auth-code>] [--pin=<PIN>]\n", | |
97 argv[0]); | |
98 return 1; | |
99 } | |
100 | |
101 if (host_pin.empty()) { | |
102 while (true) { | |
103 fprintf(stdout, "Enter a six-digit PIN: "); | |
Sergey Ulanov
2012/10/16 00:06:52
I think you also need to fflush(stdout) in case st
simonmorris
2012/10/16 00:38:53
Done.
| |
104 host_pin = ReadString(true); | |
105 if (!remoting::IsPinValid(host_pin)) { | |
106 fprintf(stdout, | |
107 "Please use a PIN consisting of at least six digits.\n"); | |
108 continue; | |
109 } | |
110 std::string host_pin_confirm; | |
111 fprintf(stdout, "Enter the same PIN again: "); | |
Sergey Ulanov
2012/10/16 00:06:52
fflush(stdout)
simonmorris
2012/10/16 00:38:53
Done.
| |
112 host_pin_confirm = ReadString(true); | |
113 if (host_pin != host_pin_confirm) { | |
114 fprintf(stdout, "You entered different PINs.\n"); | |
115 continue; | |
116 } | |
117 break; | |
118 } | |
119 } else { | |
120 if (!remoting::IsPinValid(host_pin)) { | |
121 fprintf(stderr, "Please use a PIN consisting of at least six digits.\n"); | |
122 return 1; | |
123 } | |
124 } | |
125 | |
126 if (auth_code.empty()) { | |
127 fprintf(stdout, "Enter an authorization code: "); | |
Sergey Ulanov
2012/10/16 00:06:52
fflush(stdout)
simonmorris
2012/10/16 00:38:53
Done.
| |
128 auth_code = ReadString(true); | |
129 } | |
130 | |
131 // This object instance is required by Chrome code (for example, | |
132 // FilePath, LazyInstance, MessageLoop). | |
133 base::AtExitManager exit_manager; | |
134 | |
135 // Provide message loops and threads for the URLRequestContextGetter. | |
136 MessageLoop message_loop(MessageLoop::TYPE_UI); | |
Sergey Ulanov
2012/10/16 00:06:52
Why do you need to specify TYPE_UI here? If you ne
simonmorris
2012/10/16 00:38:53
Done.
| |
137 g_message_loop = &message_loop; | |
138 base::Thread io_thread("IO thread"); | |
139 base::Thread::Options io_thread_options(MessageLoop::TYPE_IO, 0); | |
140 io_thread.StartWithOptions(io_thread_options); | |
141 | |
142 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter( | |
143 new remoting::URLRequestContextGetter( | |
144 g_message_loop->message_loop_proxy(), | |
145 io_thread.message_loop_proxy())); | |
146 | |
147 // Start the host. | |
148 scoped_ptr<HostStarter> host_starter( | |
149 HostStarter::Create(url_request_context_getter)); | |
150 host_starter->StartHost(host_name, host_pin, true, auth_code, | |
151 base::Bind(&OnDone)); | |
152 | |
153 // Run the message loop until the StartHost completion callback. | |
154 base::RunLoop run_loop; | |
155 run_loop.Run(); | |
156 | |
157 g_message_loop = NULL; | |
158 | |
159 // Destroy the HostStarter and URLRequestContextGetter before stopping the | |
160 // IO thread. | |
161 host_starter.reset(); | |
162 url_request_context_getter = NULL; | |
163 | |
164 io_thread.Stop(); | |
165 | |
166 return g_fail ? 1 : 0; | |
167 } | |
OLD | NEW |