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 | |
Sergey Ulanov
2012/10/15 18:25:30
add comment to describe what this file is for.
simonmorris
2012/10/15 20:49:55
Done.
| |
5 #include <iostream> | |
6 #include <stdio.h> | |
7 #include <termios.h> | |
8 | |
9 #include "base/at_exit.h" | |
10 #include "base/command_line.h" | |
11 #include "base/run_loop.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 using remoting::HostStarter; | |
19 | |
20 // True if the host could not be started. | |
21 bool fail = true; | |
Sergey Ulanov
2012/10/15 18:25:30
maybe store exit code for consistency with our Me2
simonmorris
2012/10/15 20:49:55
I'm not sure an exit code would be useful.
Added
| |
22 | |
23 // The main message loop. | |
24 MessageLoop* message_loop = NULL; | |
Sergey Ulanov
2012/10/15 18:25:30
g_message_loop
Or maybe put everything in a class
simonmorris
2012/10/15 20:49:55
Added a g_ prefix.
| |
25 | |
26 // Lets us hide the PIN that a user types. | |
27 void SetEcho(bool echo) { | |
28 termios term; | |
29 tcgetattr(STDIN_FILENO, &term); | |
30 if (echo) { | |
31 term.c_lflag |= ECHO; | |
32 } else { | |
33 term.c_lflag &= ~ECHO; | |
34 } | |
35 tcsetattr(STDIN_FILENO, TCSANOW, &term); | |
36 } | |
37 | |
38 // Called when the HostStarter has finished. | |
39 void OnDone(HostStarter::Result result) { | |
40 if (MessageLoop::current() != message_loop) { | |
41 message_loop->PostTask(FROM_HERE, base::Bind(&OnDone, result)); | |
42 return; | |
43 } | |
44 switch (result) { | |
45 case HostStarter::START_IN_PROGRESS: | |
46 fprintf(stderr, "Internal error: START_IN_PROGRESS.\n"); | |
47 break; | |
48 case HostStarter::START_COMPLETE: | |
49 fail = false; | |
50 break; | |
51 case HostStarter::NETWORK_ERROR: | |
52 fprintf(stderr, "Couldn't start host: network error.\n"); | |
53 break; | |
54 case HostStarter::OAUTH_ERROR: | |
55 fprintf(stderr, "Couldn't start host: OAuth error.\n"); | |
56 break; | |
57 case HostStarter::START_ERROR: | |
58 fprintf(stderr, "Couldn't start host.\n"); | |
59 break; | |
60 } | |
61 | |
62 message_loop->QuitNow(); | |
63 } | |
64 | |
65 int main(int argc, char** argv) { | |
66 // google_apis::GetOAuth2ClientID/Secret need a static CommandLine. | |
67 CommandLine::Init(argc, argv); | |
68 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
69 | |
70 std::string host_name = command_line->GetSwitchValueASCII("name"); | |
71 std::string host_pin = command_line->GetSwitchValueASCII("pin"); | |
72 std::string auth_code = command_line->GetSwitchValueASCII("code"); | |
73 | |
74 if (host_name.empty() || auth_code.empty()) { | |
Sergey Ulanov
2012/10/15 18:25:30
Why hostname is a required parameter?
Sergey Ulanov
2012/10/15 18:25:30
It may not always be secure to pass authentication
simonmorris
2012/10/15 20:49:55
For simplicity. The host has to have a name.
If t
simonmorris
2012/10/15 20:49:55
Done.
| |
75 std::cerr << "Usage: " << argv[0] | |
Sergey Ulanov
2012/10/15 18:25:30
Here and below - style guide discourages usage of
simonmorris
2012/10/15 20:49:55
Done.
| |
76 << " -name=<hostname> -code=<auth-code> [-pin=<PIN>]\n"; | |
Sergey Ulanov
2012/10/15 18:25:30
I think it should be --name, --code and --pin
simonmorris
2012/10/15 20:49:55
Done.
| |
77 return 1; | |
78 } | |
79 | |
80 if (host_pin.empty()) { | |
81 std::cout << "Enter a six-digit PIN: "; | |
82 SetEcho(false); | |
83 std::cin >> host_pin; | |
84 SetEcho(true); | |
85 std::cout << "\n"; | |
86 std::string host_pin_confirm; | |
87 std::cout << "Enter the same PIN again: "; | |
88 SetEcho(false); | |
89 std::cin >> host_pin_confirm; | |
90 SetEcho(true); | |
91 std::cout << "\n"; | |
92 if (host_pin != host_pin_confirm) { | |
93 std::cerr << "You entered different PINs.\n"; | |
Sergey Ulanov
2012/10/15 18:25:30
better UX would be to ask the PIN again instead of
simonmorris
2012/10/15 20:49:55
Done.
| |
94 return 1; | |
95 } | |
96 } | |
97 | |
98 if (!remoting::IsPinValid(host_pin)) { | |
99 std::cerr << "Please use a PIN consisting of at least six digits.\n"; | |
100 return 1; | |
101 } | |
102 | |
103 // This object instance is required by Chrome code (for example, | |
104 // FilePath, LazyInstance, MessageLoop). | |
105 base::AtExitManager exit_manager; | |
106 | |
107 // Provide message loops and threads for the URLRequestContextGetter. | |
108 message_loop = new MessageLoop(MessageLoop::TYPE_UI); | |
Sergey Ulanov
2012/10/15 18:25:30
This object is never freed. I think it's more comm
simonmorris
2012/10/15 20:49:55
Done.
| |
109 base::Thread io_thread("IO thread"); | |
110 base::Thread::Options io_thread_options(MessageLoop::TYPE_IO, 0); | |
111 io_thread.StartWithOptions(io_thread_options); | |
112 | |
113 // The scope ensures that the URLRequestContextGetter and HostStarter | |
114 // are destroyed before the IO thread is stopped. | |
Sergey Ulanov
2012/10/15 18:25:30
nit: Alternatively you could just reset url_reque
simonmorris
2012/10/15 20:49:55
Done.
| |
115 { | |
116 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter( | |
117 new remoting::URLRequestContextGetter( | |
118 message_loop->message_loop_proxy(), | |
119 io_thread.message_loop_proxy())); | |
120 | |
121 // Start the host. | |
122 scoped_ptr<HostStarter> host_starter( | |
123 HostStarter::Create(url_request_context_getter)); | |
124 host_starter->StartHost(host_name, host_pin, true, auth_code, | |
125 base::Bind(&OnDone)); | |
126 | |
127 // Run the message loop until the StartHost completion callback. | |
128 base::RunLoop run_loop; | |
129 run_loop.Run(); | |
130 } | |
131 | |
132 io_thread.Stop(); | |
133 | |
134 return fail ? 1 : 0; | |
135 } | |
OLD | NEW |