gaia_access_token_fetcher.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2020 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. #include "google_apis/gaia/gaia_access_token_fetcher.h"
  5. #include <string>
  6. #include "base/memory/ptr_util.h"
  7. #include "base/metrics/histogram.h"
  8. #include "base/metrics/histogram_functions.h"
  9. #include "base/metrics/histogram_macros.h"
  10. #include "google_apis/gaia/gaia_urls.h"
  11. #include "services/network/public/cpp/shared_url_loader_factory.h"
  12. // static
  13. const char GaiaAccessTokenFetcher::kOAuth2NetResponseCodeHistogramName[] =
  14. "Gaia.ResponseCodesForOAuth2AccessToken";
  15. // static
  16. const char GaiaAccessTokenFetcher::kOAuth2ResponseHistogramName[] =
  17. "Gaia.ResponseForOAuth2AccessToken";
  18. // static
  19. std::unique_ptr<GaiaAccessTokenFetcher>
  20. GaiaAccessTokenFetcher::CreateExchangeRefreshTokenForAccessTokenInstance(
  21. OAuth2AccessTokenConsumer* consumer,
  22. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
  23. const std::string& refresh_token) {
  24. // Using `new` to access a non-public constructor.
  25. return base::WrapUnique(new GaiaAccessTokenFetcher(
  26. consumer, url_loader_factory, refresh_token, std::string()));
  27. }
  28. // static
  29. std::unique_ptr<GaiaAccessTokenFetcher>
  30. GaiaAccessTokenFetcher::CreateExchangeAuthCodeForRefeshTokenInstance(
  31. OAuth2AccessTokenConsumer* consumer,
  32. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
  33. const std::string& auth_code) {
  34. // Using `new` to access a non-public constructor.
  35. return base::WrapUnique(new GaiaAccessTokenFetcher(
  36. consumer, url_loader_factory, std::string(), auth_code));
  37. }
  38. GaiaAccessTokenFetcher::GaiaAccessTokenFetcher(
  39. OAuth2AccessTokenConsumer* consumer,
  40. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
  41. const std::string& refresh_token,
  42. const std::string& auth_code)
  43. : OAuth2AccessTokenFetcherImpl(consumer,
  44. url_loader_factory,
  45. refresh_token,
  46. auth_code) {}
  47. GaiaAccessTokenFetcher::~GaiaAccessTokenFetcher() = default;
  48. void GaiaAccessTokenFetcher::RecordResponseCodeUma(int error_value) const {
  49. base::UmaHistogramSparse(kOAuth2NetResponseCodeHistogramName, error_value);
  50. }
  51. void GaiaAccessTokenFetcher::RecordOAuth2Response(
  52. OAuth2Response response) const {
  53. base::UmaHistogramEnumeration(kOAuth2ResponseHistogramName, response);
  54. }
  55. GURL GaiaAccessTokenFetcher::GetAccessTokenURL() const {
  56. return GaiaUrls::GetInstance()->oauth2_token_url();
  57. }
  58. net::NetworkTrafficAnnotationTag
  59. GaiaAccessTokenFetcher::GetTrafficAnnotationTag() const {
  60. return net::DefineNetworkTrafficAnnotation("oauth2_access_token_fetcher", R"(
  61. semantics {
  62. sender: "OAuth 2.0 Access Token Fetcher"
  63. description:
  64. "This request is used by the Token Service to fetch an OAuth 2.0 "
  65. "access token for a known Google account."
  66. trigger:
  67. "This request can be triggered at any moment when any service "
  68. "requests an OAuth 2.0 access token from the Token Service."
  69. data:
  70. "Chrome OAuth 2.0 client id and secret, the set of OAuth 2.0 "
  71. "scopes and the OAuth 2.0 refresh token."
  72. destination: GOOGLE_OWNED_SERVICE
  73. }
  74. policy {
  75. cookies_allowed: NO
  76. setting:
  77. "This feature cannot be disabled in settings, but if user signs "
  78. "out of Chrome, this request would not be made."
  79. chrome_policy {
  80. SigninAllowed {
  81. policy_options {mode: MANDATORY}
  82. SigninAllowed: false
  83. }
  84. }
  85. })");
  86. }