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