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 was started successfully. |
| 23 bool g_started = false; |
| 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 char* result = fgets(&str[0], kMaxLen, stdin); |
| 47 if (no_echo) { |
| 48 printf("\n"); |
| 49 SetEcho(true); |
| 50 } |
| 51 if (!result) |
| 52 return ""; |
| 53 size_t newline_index = str.find('\n'); |
| 54 if (newline_index != std::string::npos) |
| 55 str[newline_index] = '\0'; |
| 56 str.resize(strlen(&str[0])); |
| 57 return str; |
| 58 } |
| 59 |
| 60 // Called when the HostStarter has finished. |
| 61 void OnDone(HostStarter::Result result) { |
| 62 if (MessageLoop::current() != g_message_loop) { |
| 63 g_message_loop->PostTask(FROM_HERE, base::Bind(&OnDone, result)); |
| 64 return; |
| 65 } |
| 66 switch (result) { |
| 67 case HostStarter::START_IN_PROGRESS: |
| 68 fprintf(stderr, "Internal error: START_IN_PROGRESS.\n"); |
| 69 break; |
| 70 case HostStarter::START_COMPLETE: |
| 71 g_started = true; |
| 72 break; |
| 73 case HostStarter::NETWORK_ERROR: |
| 74 fprintf(stderr, "Couldn't start host: network error.\n"); |
| 75 break; |
| 76 case HostStarter::OAUTH_ERROR: |
| 77 fprintf(stderr, "Couldn't start host: OAuth error.\n"); |
| 78 break; |
| 79 case HostStarter::START_ERROR: |
| 80 fprintf(stderr, "Couldn't start host.\n"); |
| 81 break; |
| 82 } |
| 83 |
| 84 g_message_loop->QuitNow(); |
| 85 } |
| 86 |
| 87 int main(int argc, char** argv) { |
| 88 // google_apis::GetOAuth2ClientID/Secret need a static CommandLine. |
| 89 CommandLine::Init(argc, argv); |
| 90 const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 91 |
| 92 std::string host_name = command_line->GetSwitchValueASCII("name"); |
| 93 std::string host_pin = command_line->GetSwitchValueASCII("pin"); |
| 94 std::string auth_code = command_line->GetSwitchValueASCII("code"); |
| 95 |
| 96 if (host_name.empty()) { |
| 97 fprintf(stderr, |
| 98 "Usage: %s --name=<hostname> [--code=<auth-code>] [--pin=<PIN>]\n", |
| 99 argv[0]); |
| 100 return 1; |
| 101 } |
| 102 |
| 103 if (host_pin.empty()) { |
| 104 while (true) { |
| 105 fprintf(stdout, "Enter a six-digit PIN: "); |
| 106 fflush(stdout); |
| 107 host_pin = ReadString(true); |
| 108 if (!remoting::IsPinValid(host_pin)) { |
| 109 fprintf(stdout, |
| 110 "Please use a PIN consisting of at least six digits.\n"); |
| 111 fflush(stdout); |
| 112 continue; |
| 113 } |
| 114 std::string host_pin_confirm; |
| 115 fprintf(stdout, "Enter the same PIN again: "); |
| 116 fflush(stdout); |
| 117 host_pin_confirm = ReadString(true); |
| 118 if (host_pin != host_pin_confirm) { |
| 119 fprintf(stdout, "You entered different PINs.\n"); |
| 120 fflush(stdout); |
| 121 continue; |
| 122 } |
| 123 break; |
| 124 } |
| 125 } else { |
| 126 if (!remoting::IsPinValid(host_pin)) { |
| 127 fprintf(stderr, "Please use a PIN consisting of at least six digits.\n"); |
| 128 return 1; |
| 129 } |
| 130 } |
| 131 |
| 132 if (auth_code.empty()) { |
| 133 fprintf(stdout, "Enter an authorization code: "); |
| 134 fflush(stdout); |
| 135 auth_code = ReadString(true); |
| 136 } |
| 137 |
| 138 // This object instance is required by Chrome code (for example, |
| 139 // FilePath, LazyInstance, MessageLoop). |
| 140 base::AtExitManager exit_manager; |
| 141 |
| 142 // Provide message loops and threads for the URLRequestContextGetter. |
| 143 MessageLoop message_loop; |
| 144 g_message_loop = &message_loop; |
| 145 base::Thread io_thread("IO thread"); |
| 146 base::Thread::Options io_thread_options(MessageLoop::TYPE_IO, 0); |
| 147 io_thread.StartWithOptions(io_thread_options); |
| 148 |
| 149 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter( |
| 150 new remoting::URLRequestContextGetter( |
| 151 g_message_loop->message_loop_proxy(), |
| 152 io_thread.message_loop_proxy())); |
| 153 |
| 154 // Start the host. |
| 155 scoped_ptr<HostStarter> host_starter( |
| 156 HostStarter::Create(url_request_context_getter)); |
| 157 host_starter->StartHost(host_name, host_pin, true, auth_code, |
| 158 base::Bind(&OnDone)); |
| 159 |
| 160 // Run the message loop until the StartHost completion callback. |
| 161 base::RunLoop run_loop; |
| 162 run_loop.Run(); |
| 163 |
| 164 g_message_loop = NULL; |
| 165 |
| 166 // Destroy the HostStarter and URLRequestContextGetter before stopping the |
| 167 // IO thread. |
| 168 host_starter.reset(); |
| 169 url_request_context_getter = NULL; |
| 170 |
| 171 io_thread.Stop(); |
| 172 |
| 173 return g_started ? 0 : 1; |
| 174 } |
OLD | NEW |