OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 #include "chrome/browser/ui/desktop_ios_promotion/sms_service.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/json/json_reader.h" |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/metrics/histogram_macros.h" |
| 11 #include "base/optional.h" |
| 12 #include "base/strings/stringprintf.h" |
| 13 #include "components/signin/core/browser/signin_manager.h" |
| 14 #include "google_apis/gaia/gaia_urls.h" |
| 15 #include "google_apis/gaia/oauth2_token_service.h" |
| 16 #include "net/base/load_flags.h" |
| 17 #include "net/base/url_util.h" |
| 18 #include "net/http/http_status_code.h" |
| 19 #include "net/http/http_util.h" |
| 20 #include "net/url_request/url_fetcher.h" |
| 21 #include "net/url_request/url_fetcher_delegate.h" |
| 22 #include "net/url_request/url_request_context_getter.h" |
| 23 |
| 24 namespace { |
| 25 |
| 26 const char kDesktopIOSPromotionOAuthScope[] = |
| 27 "https://www.googleapis.com/auth/mobile_user_preferences"; |
| 28 |
| 29 const char kDesktopIOSPromotionQueryPhoneNumber[] = |
| 30 "https://growth-pa.googleapis.com/v1/get_verified_phone_numbers"; |
| 31 |
| 32 const char kDesktopIOSPromotionSendSMS[] = |
| 33 "https://growth-pa.googleapis.com/v1/send_sms"; |
| 34 |
| 35 const char kPostDataMimeType[] = "application/json"; |
| 36 |
| 37 const char kSendSMSPromoFormat[] = "{promo_id:%s}"; |
| 38 |
| 39 // The maximum number of retries for the URLFetcher requests. |
| 40 const size_t kMaxRetries = 1; |
| 41 |
| 42 class RequestImpl : public SMSService::Request, |
| 43 private OAuth2TokenService::Consumer, |
| 44 private net::URLFetcherDelegate { |
| 45 public: |
| 46 ~RequestImpl() override {} |
| 47 |
| 48 // Returns the response code received from the server, which will only be |
| 49 // valid if the request succeeded. |
| 50 int GetResponseCode() override { return response_code_; } |
| 51 |
| 52 // Returns the contents of the response body received from the server. |
| 53 const std::string& GetResponseBody() override { return response_body_; } |
| 54 |
| 55 bool IsPending() override { return is_pending_; } |
| 56 |
| 57 private: |
| 58 friend class ::SMSService; |
| 59 |
| 60 RequestImpl( |
| 61 OAuth2TokenService* token_service, |
| 62 SigninManagerBase* signin_manager, |
| 63 const scoped_refptr<net::URLRequestContextGetter>& request_context, |
| 64 const GURL& url, |
| 65 const SMSService::CompletionCallback& callback) |
| 66 : OAuth2TokenService::Consumer("desktop_ios_promotion"), |
| 67 token_service_(token_service), |
| 68 signin_manager_(signin_manager), |
| 69 request_context_(request_context), |
| 70 url_(url), |
| 71 post_data_mime_type_(kPostDataMimeType), |
| 72 response_code_(0), |
| 73 auth_retry_count_(0), |
| 74 callback_(callback), |
| 75 is_pending_(false) { |
| 76 DCHECK(token_service_); |
| 77 DCHECK(signin_manager_); |
| 78 DCHECK(request_context_); |
| 79 } |
| 80 |
| 81 void Start() override { |
| 82 OAuth2TokenService::ScopeSet oauth_scopes; |
| 83 oauth_scopes.insert(kDesktopIOSPromotionOAuthScope); |
| 84 token_request_ = token_service_->StartRequest( |
| 85 signin_manager_->GetAuthenticatedAccountId(), oauth_scopes, this); |
| 86 is_pending_ = true; |
| 87 } |
| 88 |
| 89 // content::URLFetcherDelegate interface. |
| 90 void OnURLFetchComplete(const net::URLFetcher* source) override { |
| 91 DCHECK_EQ(source, url_fetcher_.get()); |
| 92 response_code_ = url_fetcher_->GetResponseCode(); |
| 93 |
| 94 UMA_HISTOGRAM_CUSTOM_ENUMERATION( |
| 95 "DesktopIOSPromotion.OAuthTokenResponseCode", |
| 96 net::HttpUtil::MapStatusCodeForHistogram(response_code_), |
| 97 net::HttpUtil::GetStatusCodesForHistogram()); |
| 98 |
| 99 // If the response code indicates that the token might not be valid, |
| 100 // invalidate the token and try again. |
| 101 if (response_code_ == net::HTTP_UNAUTHORIZED && ++auth_retry_count_ <= 1) { |
| 102 OAuth2TokenService::ScopeSet oauth_scopes; |
| 103 oauth_scopes.insert(kDesktopIOSPromotionOAuthScope); |
| 104 token_service_->InvalidateAccessToken( |
| 105 signin_manager_->GetAuthenticatedAccountId(), oauth_scopes, |
| 106 access_token_); |
| 107 |
| 108 access_token_.clear(); |
| 109 Start(); |
| 110 return; |
| 111 } |
| 112 url_fetcher_->GetResponseAsString(&response_body_); |
| 113 url_fetcher_.reset(); |
| 114 is_pending_ = false; |
| 115 callback_.Run(this, response_code_ == net::HTTP_OK); |
| 116 // It is valid for the callback to delete |this|, so do not access any |
| 117 // members below here. |
| 118 } |
| 119 |
| 120 // OAuth2TokenService::Consumer interface. |
| 121 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
| 122 const std::string& access_token, |
| 123 const base::Time& expiration_time) override { |
| 124 token_request_.reset(); |
| 125 DCHECK(!access_token.empty()); |
| 126 access_token_ = access_token; |
| 127 |
| 128 UMA_HISTOGRAM_BOOLEAN("DesktopIOSPromotion.OAuthTokenCompletion", true); |
| 129 |
| 130 // Got an access token -- start the actual API request. |
| 131 url_fetcher_ = CreateUrlFetcher(access_token); |
| 132 url_fetcher_->Start(); |
| 133 } |
| 134 |
| 135 void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
| 136 const GoogleServiceAuthError& error) override { |
| 137 token_request_.reset(); |
| 138 is_pending_ = false; |
| 139 |
| 140 UMA_HISTOGRAM_BOOLEAN("DesktopIOSPromotion.OAuthTokenCompletion", false); |
| 141 |
| 142 callback_.Run(this, false); |
| 143 // It is valid for the callback to delete |this|, so do not access any |
| 144 // members below here. |
| 145 } |
| 146 |
| 147 // Helper for creating a new URLFetcher for the API request. |
| 148 std::unique_ptr<net::URLFetcher> CreateUrlFetcher( |
| 149 const std::string& access_token) { |
| 150 net::URLFetcher::RequestType request_type = |
| 151 post_data_ ? net::URLFetcher::POST : net::URLFetcher::GET; |
| 152 std::unique_ptr<net::URLFetcher> fetcher = |
| 153 net::URLFetcher::Create(url_, request_type, this); |
| 154 fetcher->SetRequestContext(request_context_.get()); |
| 155 fetcher->SetMaxRetriesOn5xx(kMaxRetries); |
| 156 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 157 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 158 fetcher->AddExtraRequestHeader("Authorization: Bearer " + access_token); |
| 159 fetcher->AddExtraRequestHeader( |
| 160 "X-Developer-Key: " + |
| 161 GaiaUrls::GetInstance()->oauth2_chrome_client_id()); |
| 162 |
| 163 if (post_data_) |
| 164 fetcher->SetUploadData(post_data_mime_type_, post_data_.value()); |
| 165 return fetcher; |
| 166 } |
| 167 |
| 168 void SetPostData(const std::string& post_data) override { |
| 169 SetPostDataAndType(post_data, kPostDataMimeType); |
| 170 } |
| 171 |
| 172 void SetPostDataAndType(const std::string& post_data, |
| 173 const std::string& mime_type) override { |
| 174 post_data_ = post_data; |
| 175 post_data_mime_type_ = mime_type; |
| 176 } |
| 177 |
| 178 OAuth2TokenService* token_service_; |
| 179 SigninManagerBase* signin_manager_; |
| 180 scoped_refptr<net::URLRequestContextGetter> request_context_; |
| 181 |
| 182 // The URL of the API endpoint. |
| 183 GURL url_; |
| 184 |
| 185 // POST data to be sent with the request (may be empty). |
| 186 base::Optional<std::string> post_data_; |
| 187 |
| 188 // MIME type of the post requests. Defaults to text/plain. |
| 189 std::string post_data_mime_type_; |
| 190 |
| 191 // The OAuth2 access token request. |
| 192 std::unique_ptr<OAuth2TokenService::Request> token_request_; |
| 193 |
| 194 // The current OAuth2 access token. |
| 195 std::string access_token_; |
| 196 |
| 197 // Handles the actual API requests after the OAuth token is acquired. |
| 198 std::unique_ptr<net::URLFetcher> url_fetcher_; |
| 199 |
| 200 // Holds the response code received from the server. |
| 201 int response_code_; |
| 202 |
| 203 // Holds the response body received from the server. |
| 204 std::string response_body_; |
| 205 |
| 206 // The number of times this request has already been retried due to |
| 207 // authorization problems. |
| 208 int auth_retry_count_; |
| 209 |
| 210 // The callback to execute when the query is complete. |
| 211 SMSService::CompletionCallback callback_; |
| 212 |
| 213 // True if the request was started and has not yet completed, otherwise false. |
| 214 bool is_pending_; |
| 215 }; |
| 216 |
| 217 } // namespace |
| 218 |
| 219 SMSService::Request::Request() {} |
| 220 |
| 221 SMSService::Request::~Request() {} |
| 222 |
| 223 SMSService::SMSService( |
| 224 OAuth2TokenService* token_service, |
| 225 SigninManagerBase* signin_manager, |
| 226 const scoped_refptr<net::URLRequestContextGetter>& request_context) |
| 227 : token_service_(token_service), |
| 228 signin_manager_(signin_manager), |
| 229 request_context_(request_context), |
| 230 weak_ptr_factory_(this) {} |
| 231 |
| 232 SMSService::~SMSService() {} |
| 233 |
| 234 SMSService::Request* SMSService::CreateRequest( |
| 235 const GURL& url, |
| 236 const CompletionCallback& callback) { |
| 237 return new RequestImpl(token_service_, signin_manager_, request_context_, url, |
| 238 callback); |
| 239 } |
| 240 |
| 241 void SMSService::QueryPhoneNumber(const PhoneNumberCallback& callback) { |
| 242 CompletionCallback completion_callback = |
| 243 base::Bind(&SMSService::QueryPhoneNumberCompletionCallback, |
| 244 weak_ptr_factory_.GetWeakPtr(), callback); |
| 245 |
| 246 GURL url(kDesktopIOSPromotionQueryPhoneNumber); |
| 247 Request* request = CreateRequest(url, completion_callback); |
| 248 // This placeholder is required by the API. |
| 249 request->SetPostData("{}"); |
| 250 pending_requests_[request] = base::WrapUnique(request); |
| 251 request->Start(); |
| 252 } |
| 253 |
| 254 void SMSService::SendSMS(const std::string& promo_id, |
| 255 const SMSService::PhoneNumberCallback& callback) { |
| 256 CompletionCallback completion_callback = base::Bind( |
| 257 &SMSService::SendSMSCallback, weak_ptr_factory_.GetWeakPtr(), callback); |
| 258 GURL url(kDesktopIOSPromotionSendSMS); |
| 259 Request* request = CreateRequest(url, completion_callback); |
| 260 request->SetPostData( |
| 261 base::StringPrintf(kSendSMSPromoFormat, promo_id.c_str())); |
| 262 pending_requests_[request] = base::WrapUnique(request); |
| 263 request->Start(); |
| 264 } |
| 265 |
| 266 void SMSService::QueryPhoneNumberCompletionCallback( |
| 267 const SMSService::PhoneNumberCallback& callback, |
| 268 SMSService::Request* request, |
| 269 bool success) { |
| 270 std::unique_ptr<Request> request_ptr = std::move(pending_requests_[request]); |
| 271 pending_requests_.erase(request); |
| 272 |
| 273 std::string phone_number; |
| 274 bool has_number = false; |
| 275 std::unique_ptr<base::Value> value = |
| 276 base::JSONReader::Read(request->GetResponseBody()); |
| 277 if (value.get() && value.get()->IsType(base::Value::Type::DICTIONARY)) { |
| 278 const base::DictionaryValue* dictionary; |
| 279 if (value->GetAsDictionary(&dictionary)) { |
| 280 const base::ListValue* number_list; |
| 281 if (dictionary->GetList("phoneNumber", &number_list)) { |
| 282 const base::DictionaryValue* sub_dictionary; |
| 283 // For now only handle the first number. |
| 284 if (number_list->GetSize() > 0 && |
| 285 number_list->GetDictionary(0, &sub_dictionary)) { |
| 286 if (sub_dictionary->GetString("phoneNumber", &phone_number)) |
| 287 has_number = true; |
| 288 } |
| 289 } |
| 290 } |
| 291 } |
| 292 callback.Run(request, success && has_number, phone_number); |
| 293 } |
| 294 |
| 295 void SMSService::SendSMSCallback( |
| 296 const SMSService::PhoneNumberCallback& callback, |
| 297 SMSService::Request* request, |
| 298 bool success) { |
| 299 std::unique_ptr<Request> request_ptr = std::move(pending_requests_[request]); |
| 300 pending_requests_.erase(request); |
| 301 |
| 302 std::string phone_number; |
| 303 bool has_number = false; |
| 304 std::unique_ptr<base::Value> value = |
| 305 base::JSONReader::Read(request->GetResponseBody()); |
| 306 if (value.get() && value.get()->IsType(base::Value::Type::DICTIONARY)) { |
| 307 const base::DictionaryValue* dictionary; |
| 308 if (value->GetAsDictionary(&dictionary)) { |
| 309 if (dictionary->GetString("phoneNumber", &phone_number)) |
| 310 has_number = true; |
| 311 } |
| 312 } |
| 313 callback.Run(request, success && has_number, phone_number); |
| 314 } |
OLD | NEW |