Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / signin / core / browser / refresh_token_annotation_request.h
blobb0c14eed0feac7b9fd89cb43ad64073d397ff012
1 // Copyright 2014 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 COMPONENTS_SIGNIN_CORE_BROWSER_REFRESH_TOKEN_ANNOTATION_REQUEST_H_
6 #define COMPONENTS_SIGNIN_CORE_BROWSER_REFRESH_TOKEN_ANNOTATION_REQUEST_H_
8 #include "base/callback.h"
9 #include "base/threading/non_thread_safe.h"
10 #include "google_apis/gaia/oauth2_api_call_flow.h"
11 #include "google_apis/gaia/oauth2_token_service.h"
13 class PrefService;
14 class SigninClient;
16 // RefreshTokenAnnotationRequest sends request to IssueToken endpoint with
17 // device_id to backfill device info for refresh tokens issued pre-M38. It is
18 // important to keep server QPS low therefore this request is sent on average
19 // once per 10 days per profile.
20 // This code shold be removed once majority of refresh tokens are updated.
21 class RefreshTokenAnnotationRequest : public base::NonThreadSafe,
22 public OAuth2TokenService::Consumer,
23 public OAuth2ApiCallFlow {
24 public:
25 ~RefreshTokenAnnotationRequest() override;
27 // Checks if it's time to send IssueToken request. If it is then schedule
28 // delay for next request and start request flow.
30 // If request is started then return value is scoped_ptr to request. Callback
31 // will be posted on current thread when request flow is finished one way or
32 // another. Callback's purpose is to delete request after it is done.
34 // If request is deleted before flow is complete all internal requests will be
35 // cancelled and callback will not be posted.
37 // If SendIfNeeded returns nullptr then callback will not be posted.
38 static scoped_ptr<RefreshTokenAnnotationRequest> SendIfNeeded(
39 PrefService* pref_service,
40 OAuth2TokenService* token_service,
41 SigninClient* signin_client,
42 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter,
43 const std::string& account_id,
44 const base::Closure& request_callback);
46 // OAuth2TokenService::Consumer implementation.
47 void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
48 const std::string& access_token,
49 const base::Time& expiration_time) override;
50 void OnGetTokenFailure(const OAuth2TokenService::Request* request,
51 const GoogleServiceAuthError& error) override;
53 // OAuth2ApiCallFlow implementation.
54 GURL CreateApiCallUrl() override;
55 std::string CreateApiCallBody() override;
56 void ProcessApiCallSuccess(const net::URLFetcher* source) override;
57 void ProcessApiCallFailure(const net::URLFetcher* source) override;
59 private:
60 FRIEND_TEST_ALL_PREFIXES(RefreshTokenAnnotationRequestTest, ShouldSendNow);
61 FRIEND_TEST_ALL_PREFIXES(RefreshTokenAnnotationRequestTest,
62 CreateApiCallBody);
64 RefreshTokenAnnotationRequest(
65 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter,
66 const std::string& product_version,
67 const std::string& device_id,
68 const std::string& client_id,
69 const base::Closure& request_callback);
71 static bool ShouldSendNow(PrefService* pref_service);
72 void RequestAccessToken(OAuth2TokenService* token_service,
73 const std::string& account_id);
75 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
76 std::string product_version_;
77 std::string device_id_;
78 std::string client_id_;
79 base::Closure request_callback_;
81 scoped_ptr<OAuth2TokenService::Request> access_token_request_;
83 DISALLOW_COPY_AND_ASSIGN(RefreshTokenAnnotationRequest);
86 #endif // COMPONENTS_SIGNIN_CORE_BROWSER_REFRESH_TOKEN_ANNOTATION_REQUEST_H_