Don't send a SHChangeNotify for creating an app icon when creating a shortcut.
[chromium-blink-merge.git] / remoting / test / access_token_fetcher.cc
blob4fa690e74a299542c514c75bc7d4c8474ba29a70
1 // Copyright 2015 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 #include "remoting/test/access_token_fetcher.h"
7 #include <string>
8 #include <vector>
10 #include "base/bind.h"
11 #include "base/logging.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "google_apis/gaia/gaia_constants.h"
15 #include "google_apis/google_api_keys.h"
16 #include "net/url_request/url_fetcher.h"
17 #include "remoting/base/url_request_context_getter.h"
19 namespace {
20 const int kMaxGetTokensRetries = 3;
21 const char kOauthRedirectUrl[] =
22 "https://chromoting-oauth.talkgadget."
23 "google.com/talkgadget/oauth/chrome-remote-desktop/dev";
24 } // namespace
26 namespace remoting {
27 namespace test {
29 AccessTokenFetcher::AccessTokenFetcher() {
30 oauth_client_info_ = {
31 google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING),
32 google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING),
33 kOauthRedirectUrl};
36 AccessTokenFetcher::~AccessTokenFetcher() {
39 void AccessTokenFetcher::GetAccessTokenFromAuthCode(
40 const std::string& auth_code,
41 const AccessTokenCallback& callback) {
42 DCHECK(!auth_code.empty());
43 DCHECK(!callback.is_null());
44 DCHECK(access_token_callback_.is_null());
46 DVLOG(2) << "Calling GetTokensFromAuthCode to exchange auth_code for token";
48 access_token_.clear();
49 refresh_token_.clear();
50 access_token_callback_ = callback;
52 // Create a new GaiaOAuthClient for each request to GAIA.
53 CreateNewGaiaOAuthClientInstance();
54 auth_client_->GetTokensFromAuthCode(
55 oauth_client_info_, auth_code, kMaxGetTokensRetries,
56 this); // GaiaOAuthClient::Delegate* delegate
59 void AccessTokenFetcher::GetAccessTokenFromRefreshToken(
60 const std::string& refresh_token,
61 const AccessTokenCallback& callback) {
62 DCHECK(!refresh_token.empty());
63 DCHECK(!callback.is_null());
64 DCHECK(access_token_callback_.is_null());
66 DVLOG(2) << "Calling RefreshToken to generate a new access token";
68 access_token_.clear();
69 refresh_token_ = refresh_token;
70 access_token_callback_ = callback;
72 // Create a new GaiaOAuthClient for each request to GAIA.
73 CreateNewGaiaOAuthClientInstance();
74 auth_client_->RefreshToken(oauth_client_info_, refresh_token_,
75 std::vector<std::string>(), // scopes
76 kMaxGetTokensRetries,
77 this); // GaiaOAuthClient::Delegate* delegate
80 void AccessTokenFetcher::CreateNewGaiaOAuthClientInstance() {
81 scoped_refptr<remoting::URLRequestContextGetter> request_context_getter;
82 request_context_getter = new remoting::URLRequestContextGetter(
83 base::ThreadTaskRunnerHandle::Get(), // network_runner
84 base::ThreadTaskRunnerHandle::Get()); // file_runner
86 auth_client_.reset(new gaia::GaiaOAuthClient(request_context_getter.get()));
89 void AccessTokenFetcher::OnGetTokensResponse(const std::string& refresh_token,
90 const std::string& access_token,
91 int expires_in_seconds) {
92 DVLOG(1) << "AccessTokenFetcher::OnGetTokensResponse() Called";
93 DVLOG(1) << "--refresh_token: " << refresh_token;
94 DVLOG(1) << "--access_token: " << access_token;
95 DVLOG(1) << "--expires_in_seconds: " << expires_in_seconds;
97 refresh_token_ = refresh_token;
98 access_token_ = access_token;
100 ValidateAccessToken();
103 void AccessTokenFetcher::OnRefreshTokenResponse(const std::string& access_token,
104 int expires_in_seconds) {
105 DVLOG(1) << "AccessTokenFetcher::OnRefreshTokenResponse() Called";
106 DVLOG(1) << "--access_token: " << access_token;
107 DVLOG(1) << "--expires_in_seconds: " << expires_in_seconds;
109 access_token_ = access_token;
111 ValidateAccessToken();
114 void AccessTokenFetcher::OnGetUserEmailResponse(const std::string& user_email) {
115 // This callback should not be called as we do not request the user's email.
116 NOTREACHED();
119 void AccessTokenFetcher::OnGetUserIdResponse(const std::string& user_id) {
120 // This callback should not be called as we do not request the user's id.
121 NOTREACHED();
124 void AccessTokenFetcher::OnGetUserInfoResponse(
125 scoped_ptr<base::DictionaryValue> user_info) {
126 // This callback should not be called as we do not request user info.
127 NOTREACHED();
130 void AccessTokenFetcher::OnGetTokenInfoResponse(
131 scoped_ptr<base::DictionaryValue> token_info) {
132 DVLOG(1) << "AccessTokenFetcher::OnGetTokenInfoResponse() Called";
134 std::string error_string;
135 std::string error_description;
137 // Check to see if the token_info we received had any errors,
138 // otherwise we will assume that it is valid for our purposes.
139 if (token_info->HasKey("error")) {
140 token_info->GetString("error", &error_string);
141 token_info->GetString("error_description", &error_description);
143 LOG(ERROR) << "OnGetTokenInfoResponse returned an error. "
144 << "error: " << error_string << ", "
145 << "description: " << error_description;
146 access_token_.clear();
147 refresh_token_.clear();
148 } else {
149 DVLOG(1) << "Access Token has been validated";
152 access_token_callback_.Run(access_token_, refresh_token_);
153 access_token_callback_.Reset();
156 void AccessTokenFetcher::OnOAuthError() {
157 LOG(ERROR) << "AccessTokenFetcher::OnOAuthError() Called";
159 access_token_.clear();
160 refresh_token_.clear();
162 access_token_callback_.Run(access_token_, refresh_token_);
163 access_token_callback_.Reset();
166 void AccessTokenFetcher::OnNetworkError(int response_code) {
167 LOG(ERROR) << "AccessTokenFetcher::OnNetworkError() Called";
168 LOG(ERROR) << "response code: " << response_code;
170 access_token_.clear();
171 refresh_token_.clear();
173 access_token_callback_.Run(access_token_, refresh_token_);
174 access_token_callback_.Reset();
177 void AccessTokenFetcher::ValidateAccessToken() {
178 DVLOG(2) << "Calling GetTokenInfo to validate access token";
180 // Create a new GaiaOAuthClient for each request to GAIA.
181 CreateNewGaiaOAuthClientInstance();
182 auth_client_->GetTokenInfo(access_token_, kMaxGetTokensRetries,
183 this); // GaiaOAuthClient::Delegate* delegate
186 } // namespace test
187 } // namespace remoting