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..0a0b6ea5ca4b8013fed85c86a44a7a5bc16e9356 |
--- /dev/null |
+++ b/remoting/host/setup/start_host.cc |
@@ -0,0 +1,135 @@ |
+// 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. |
+ |
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.
|
+#include <iostream> |
+#include <stdio.h> |
+#include <termios.h> |
+ |
+#include "base/at_exit.h" |
+#include "base/command_line.h" |
+#include "base/run_loop.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" |
+ |
+using remoting::HostStarter; |
+ |
+// True if the host could not be started. |
+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
|
+ |
+// The main message loop. |
+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.
|
+ |
+// 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); |
+} |
+ |
+// Called when the HostStarter has finished. |
+void OnDone(HostStarter::Result result) { |
+ if (MessageLoop::current() != message_loop) { |
+ 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: |
+ 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; |
+ } |
+ |
+ 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() || 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.
|
+ 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.
|
+ << " -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.
|
+ return 1; |
+ } |
+ |
+ if (host_pin.empty()) { |
+ std::cout << "Enter a six-digit PIN: "; |
+ SetEcho(false); |
+ std::cin >> host_pin; |
+ SetEcho(true); |
+ std::cout << "\n"; |
+ std::string host_pin_confirm; |
+ std::cout << "Enter the same PIN again: "; |
+ SetEcho(false); |
+ std::cin >> host_pin_confirm; |
+ SetEcho(true); |
+ std::cout << "\n"; |
+ if (host_pin != host_pin_confirm) { |
+ 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.
|
+ return 1; |
+ } |
+ } |
+ |
+ if (!remoting::IsPinValid(host_pin)) { |
+ std::cerr << "Please use a PIN consisting of at least six digits.\n"; |
+ return 1; |
+ } |
+ |
+ // 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. |
+ 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.
|
+ base::Thread io_thread("IO thread"); |
+ base::Thread::Options io_thread_options(MessageLoop::TYPE_IO, 0); |
+ io_thread.StartWithOptions(io_thread_options); |
+ |
+ // The scope ensures that the URLRequestContextGetter and HostStarter |
+ // 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.
|
+ { |
+ scoped_refptr<net::URLRequestContextGetter> url_request_context_getter( |
+ new remoting::URLRequestContextGetter( |
+ 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(); |
+ } |
+ |
+ io_thread.Stop(); |
+ |
+ return fail ? 1 : 0; |
+} |