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