Chromium Code Reviews| Index: remoting/host/setup/start_host.cc |
| diff --git a/remoting/host/setup/start_host.cc b/remoting/host/setup/start_host.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d739c27686c41fdbedd9eaae68ec6d546e997c6e |
| --- /dev/null |
| +++ b/remoting/host/setup/start_host.cc |
| @@ -0,0 +1,167 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <stdio.h> |
| +#include <termios.h> |
| + |
| +#include "base/at_exit.h" |
| +#include "base/command_line.h" |
| +#include "base/run_loop.h" |
| +#include "base/stringprintf.h" |
| +#include "base/threading/thread.h" |
| +#include "net/url_request/url_request_context_getter.h" |
| +#include "remoting/host/setup/host_starter.h" |
| +#include "remoting/host/setup/pin_validator.h" |
| +#include "remoting/host/url_request_context.h" |
| + |
| +// A simple command-line app that registers and starts a host. |
| + |
| +using remoting::HostStarter; |
| + |
| +// True if the host could not be started. |
| +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.
|
| + |
| +// The main message loop. |
| +MessageLoop* g_message_loop = NULL; |
| + |
| +// Lets us hide the PIN that a user types. |
| +void SetEcho(bool echo) { |
| + termios term; |
| + tcgetattr(STDIN_FILENO, &term); |
| + if (echo) { |
| + term.c_lflag |= ECHO; |
| + } else { |
| + term.c_lflag &= ~ECHO; |
| + } |
| + tcsetattr(STDIN_FILENO, TCSANOW, &term); |
| +} |
| + |
| +// Reads a newline-terminated string from stdin. |
| +std::string ReadString(bool no_echo) { |
| + if (no_echo) |
| + SetEcho(false); |
| + const int kMaxLen = 1024; |
| + std::string str(kMaxLen, 0); |
| + std::string format = base::StringPrintf("%%%d[^\n]%%*c", kMaxLen - 1); |
| + 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.
|
| + if (no_echo) { |
| + printf("\n"); |
| + SetEcho(true); |
| + } |
| + if (result < 1) |
| + return ""; |
| + str.resize(strlen(&str[0])); |
| + return str; |
| +} |
| + |
| +// Called when the HostStarter has finished. |
| +void OnDone(HostStarter::Result result) { |
| + if (MessageLoop::current() != g_message_loop) { |
| + g_message_loop->PostTask(FROM_HERE, base::Bind(&OnDone, result)); |
| + return; |
| + } |
| + switch (result) { |
| + case HostStarter::START_IN_PROGRESS: |
| + fprintf(stderr, "Internal error: START_IN_PROGRESS.\n"); |
| + break; |
| + case HostStarter::START_COMPLETE: |
| + g_fail = false; |
| + break; |
| + case HostStarter::NETWORK_ERROR: |
| + fprintf(stderr, "Couldn't start host: network error.\n"); |
| + break; |
| + case HostStarter::OAUTH_ERROR: |
| + fprintf(stderr, "Couldn't start host: OAuth error.\n"); |
| + break; |
| + case HostStarter::START_ERROR: |
| + fprintf(stderr, "Couldn't start host.\n"); |
| + break; |
| + } |
| + |
| + g_message_loop->QuitNow(); |
| +} |
| + |
| +int main(int argc, char** argv) { |
| + // google_apis::GetOAuth2ClientID/Secret need a static CommandLine. |
| + CommandLine::Init(argc, argv); |
| + const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| + |
| + std::string host_name = command_line->GetSwitchValueASCII("name"); |
| + std::string host_pin = command_line->GetSwitchValueASCII("pin"); |
| + std::string auth_code = command_line->GetSwitchValueASCII("code"); |
| + |
| + if (host_name.empty()) { |
| + fprintf(stderr, |
| + "Usage: %s --name=<hostname> [--code=<auth-code>] [--pin=<PIN>]\n", |
| + argv[0]); |
| + return 1; |
| + } |
| + |
| + if (host_pin.empty()) { |
| + while (true) { |
| + 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.
|
| + host_pin = ReadString(true); |
| + if (!remoting::IsPinValid(host_pin)) { |
| + fprintf(stdout, |
| + "Please use a PIN consisting of at least six digits.\n"); |
| + continue; |
| + } |
| + std::string host_pin_confirm; |
| + 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.
|
| + host_pin_confirm = ReadString(true); |
| + if (host_pin != host_pin_confirm) { |
| + fprintf(stdout, "You entered different PINs.\n"); |
| + continue; |
| + } |
| + break; |
| + } |
| + } else { |
| + if (!remoting::IsPinValid(host_pin)) { |
| + fprintf(stderr, "Please use a PIN consisting of at least six digits.\n"); |
| + return 1; |
| + } |
| + } |
| + |
| + if (auth_code.empty()) { |
| + fprintf(stdout, "Enter an authorization code: "); |
|
Sergey Ulanov
2012/10/16 00:06:52
fflush(stdout)
simonmorris
2012/10/16 00:38:53
Done.
|
| + auth_code = ReadString(true); |
| + } |
| + |
| + // This object instance is required by Chrome code (for example, |
| + // FilePath, LazyInstance, MessageLoop). |
| + base::AtExitManager exit_manager; |
| + |
| + // Provide message loops and threads for the URLRequestContextGetter. |
| + 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.
|
| + g_message_loop = &message_loop; |
| + base::Thread io_thread("IO thread"); |
| + base::Thread::Options io_thread_options(MessageLoop::TYPE_IO, 0); |
| + io_thread.StartWithOptions(io_thread_options); |
| + |
| + scoped_refptr<net::URLRequestContextGetter> url_request_context_getter( |
| + new remoting::URLRequestContextGetter( |
| + g_message_loop->message_loop_proxy(), |
| + io_thread.message_loop_proxy())); |
| + |
| + // Start the host. |
| + scoped_ptr<HostStarter> host_starter( |
| + HostStarter::Create(url_request_context_getter)); |
| + host_starter->StartHost(host_name, host_pin, true, auth_code, |
| + base::Bind(&OnDone)); |
| + |
| + // Run the message loop until the StartHost completion callback. |
| + base::RunLoop run_loop; |
| + run_loop.Run(); |
| + |
| + g_message_loop = NULL; |
| + |
| + // Destroy the HostStarter and URLRequestContextGetter before stopping the |
| + // IO thread. |
| + host_starter.reset(); |
| + url_request_context_getter = NULL; |
| + |
| + io_thread.Stop(); |
| + |
| + return g_fail ? 1 : 0; |
| +} |