Home | History | Annotate | Download | only in setup
      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 #ifndef REMOTING_HOST_HOST_STARTER
      6 #define REMOTING_HOST_HOST_STARTER
      7 
      8 #include <string>
      9 
     10 #include "base/callback.h"
     11 #include "base/memory/ref_counted.h"
     12 #include "google_apis/gaia/gaia_oauth_client.h"
     13 #include "remoting/base/rsa_key_pair.h"
     14 #include "remoting/base/url_request_context.h"
     15 #include "remoting/host/setup/daemon_controller.h"
     16 #include "remoting/host/setup/service_client.h"
     17 
     18 namespace remoting {
     19 
     20 // A helper class that registers and starts a host.
     21 class HostStarter : public gaia::GaiaOAuthClient::Delegate,
     22                     public remoting::ServiceClient::Delegate {
     23  public:
     24   enum Result {
     25     START_COMPLETE,
     26     NETWORK_ERROR,
     27     OAUTH_ERROR,
     28     START_ERROR,
     29   };
     30 
     31   typedef base::Callback<void(Result)> CompletionCallback;
     32 
     33   virtual ~HostStarter();
     34 
     35   // Creates a HostStarter.
     36   static scoped_ptr<HostStarter> Create(
     37       const std::string& chromoting_hosts_url,
     38       net::URLRequestContextGetter* url_request_context_getter);
     39 
     40   // Registers a new host with the Chromoting service, and starts it.
     41   // |auth_code| must be a valid OAuth2 authorization code, typically acquired
     42   // from a browser. This method uses that code to get an OAuth2 refresh token.
     43   void StartHost(const std::string& host_name,
     44                  const std::string& host_pin,
     45                  bool consent_to_data_collection,
     46                  const std::string& auth_code,
     47                  const std::string& redirect_url,
     48                  CompletionCallback on_done);
     49 
     50   // gaia::GaiaOAuthClient::Delegate
     51   virtual void OnGetTokensResponse(const std::string& refresh_token,
     52                                    const std::string& access_token,
     53                                    int expires_in_seconds) OVERRIDE;
     54   virtual void OnRefreshTokenResponse(const std::string& access_token,
     55                                       int expires_in_seconds) OVERRIDE;
     56   virtual void OnGetUserEmailResponse(const std::string& user_email) OVERRIDE;
     57 
     58   // remoting::ServiceClient::Delegate
     59   virtual void OnHostRegistered(const std::string& authorization_code) OVERRIDE;
     60   virtual void OnHostUnregistered() OVERRIDE;
     61 
     62   // TODO(sergeyu): Following methods are members of all three delegate
     63   // interfaces implemented in this class. Fix ServiceClient and
     64   // GaiaUserEmailFetcher so that Delegate interfaces do not overlap (ideally
     65   // they should be changed to use Callback<>).
     66   virtual void OnOAuthError() OVERRIDE;
     67   virtual void OnNetworkError(int response_code) OVERRIDE;
     68 
     69  private:
     70   HostStarter(scoped_ptr<gaia::GaiaOAuthClient> oauth_client,
     71               scoped_ptr<remoting::ServiceClient> service_client,
     72               scoped_refptr<remoting::DaemonController> daemon_controller);
     73 
     74   void StartHostProcess();
     75 
     76   void OnHostStarted(DaemonController::AsyncResult result);
     77 
     78   scoped_ptr<gaia::GaiaOAuthClient> oauth_client_;
     79   scoped_ptr<remoting::ServiceClient> service_client_;
     80   scoped_refptr<remoting::DaemonController> daemon_controller_;
     81   gaia::OAuthClientInfo oauth_client_info_;
     82   std::string host_name_;
     83   std::string host_pin_;
     84   bool consent_to_data_collection_;
     85   CompletionCallback on_done_;
     86   scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
     87   std::string refresh_token_;
     88   std::string access_token_;
     89   std::string host_owner_;
     90   std::string xmpp_login_;
     91   scoped_refptr<remoting::RsaKeyPair> key_pair_;
     92   std::string host_id_;
     93   bool use_service_account_;
     94 
     95   // True if the host was not started and unregistration was requested. If this
     96   // is set and a network/OAuth error occurs during unregistration, this will
     97   // be logged, but the error will still be reported as START_ERROR.
     98   bool unregistering_host_;
     99 
    100   base::WeakPtr<HostStarter> weak_ptr_;
    101   base::WeakPtrFactory<HostStarter> weak_ptr_factory_;
    102 
    103   DISALLOW_COPY_AND_ASSIGN(HostStarter);
    104 };
    105 
    106 }  // namespace remoting
    107 
    108 #endif  // REMOTING_HOST_HOST_STARTER
    109