Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(519)

Side by Side Diff: chrome/browser/ui/desktop_ios_promotion/sms_service.cc

Issue 2646823002: D2MIOS: Add plumbing to query verified number and send SMS from growth server. (Closed)
Patch Set: Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2017 The Chromium Authors. All rights reserved.
msramek 2017/01/23 21:13:56 nit: We don't use (c) anymore. s/\(c\)//g in all f
justincohen 2017/01/25 22:52:38 Done.
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 DesktopIOSPromotion::SMSService::Request,
msramek 2017/01/23 21:13:57 WebHistoryService:Request is public. Couldn't we j
justincohen 2017/01/25 22:52:38 Wouldn't we need to reimplement that virtuals? Ad
msramek 2017/01/27 16:38:57 Ah, right. I think it would make sense to have a s
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 DesktopIOSPromotion::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 DesktopIOSPromotion::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 DesktopIOSPromotion::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 namespace DesktopIOSPromotion {
220
221 SMSService::Request::Request() {}
222
223 SMSService::Request::~Request() {}
224
225 SMSService::SMSService(
226 OAuth2TokenService* token_service,
227 SigninManagerBase* signin_manager,
228 const scoped_refptr<net::URLRequestContextGetter>& request_context)
229 : token_service_(token_service),
230 signin_manager_(signin_manager),
231 request_context_(request_context),
232 weak_ptr_factory_(this) {}
233
234 SMSService::~SMSService() {}
235
236 void SMSService::AddObserver(SMSServiceObserver* observer) {
237 observer_list_.AddObserver(observer);
238 }
239
240 void SMSService::RemoveObserver(SMSServiceObserver* observer) {
241 observer_list_.RemoveObserver(observer);
242 }
243
244 SMSService::Request* SMSService::CreateRequest(
245 const GURL& url,
246 const CompletionCallback& callback) {
247 return new RequestImpl(token_service_, signin_manager_, request_context_, url,
248 callback);
249 }
250
251 void SMSService::QueryPhoneNumber(const PhoneNumberCallback& callback) {
252 CompletionCallback completion_callback =
253 base::Bind(&SMSService::QueryPhoneNumberCompletionCallback,
254 weak_ptr_factory_.GetWeakPtr(), callback);
255
256 GURL url(kDesktopIOSPromotionQueryPhoneNumber);
257 Request* request = CreateRequest(url, completion_callback);
258 request->SetPostData("{}");
msramek 2017/01/23 21:13:56 Is this a placeholder? In that case please add a T
justincohen 2017/01/25 22:52:38 added a comment.
259 pending_requests_[request] = base::WrapUnique(request);
260 request->Start();
261 }
262
263 void SMSService::SendSMS(std::string promo_id,
264 const SMSService::PhoneNumberCallback& callback) {
265 CompletionCallback completion_callback = base::Bind(
266 &SMSService::SendSMSCallback, weak_ptr_factory_.GetWeakPtr(), callback);
267 GURL url(kDesktopIOSPromotionSendSMS);
268 Request* request = CreateRequest(url, completion_callback);
269 request->SetPostData(
270 base::StringPrintf(kSendSMSPromoFormat, promo_id.c_str()));
271 pending_requests_[request] = base::WrapUnique(request);
272 request->Start();
273 }
274
275 void SMSService::QueryPhoneNumberCompletionCallback(
276 const SMSService::PhoneNumberCallback& callback,
277 SMSService::Request* request,
278 bool success) {
279 std::unique_ptr<Request> request_ptr = std::move(pending_requests_[request]);
280 pending_requests_.erase(request);
281
282 std::string phone_number;
283 std::unique_ptr<base::Value> value =
284 base::JSONReader::Read(request->GetResponseBody());
285 if (value.get() && value.get()->IsType(base::Value::Type::DICTIONARY)) {
msramek 2017/01/23 21:13:56 This looks complex enough to deserve a test.
justincohen 2017/01/27 05:12:00 Done.
286 const base::DictionaryValue* dictionary;
287 if (value->GetAsDictionary(&dictionary)) {
288 const base::ListValue* number_list;
289 if (dictionary->GetList("phoneNumber", &number_list)) {
290 const base::DictionaryValue* sub_dictionary;
291 // For now only handle the first number.
292 if (number_list->GetSize() > 0 &&
293 number_list->GetDictionary(0, &sub_dictionary)) {
294 sub_dictionary->GetString("phoneNumber", &phone_number);
295 }
296 }
297 }
298 }
299 callback.Run(request, success, phone_number);
300 }
301
302 void SMSService::SendSMSCallback(
303 const SMSService::PhoneNumberCallback& callback,
304 SMSService::Request* request,
305 bool success) {
306 std::unique_ptr<Request> request_ptr = std::move(pending_requests_[request]);
307 pending_requests_.erase(request);
308
309 std::string phone_number;
310 std::unique_ptr<base::Value> value =
311 base::JSONReader::Read(request->GetResponseBody());
312 if (value.get() && value.get()->IsType(base::Value::Type::DICTIONARY)) {
313 const base::DictionaryValue* dictionary;
314 if (value->GetAsDictionary(&dictionary)) {
315 dictionary->GetString("phoneNumber", &phone_number);
316 }
317 }
318 callback.Run(request, success, phone_number);
319 }
320
321 } // namespace DesktopIOSPromotion
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698