safe_browsing_token_fetcher_impl.cc 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2021 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 "weblayer/browser/safe_browsing/safe_browsing_token_fetcher_impl.h"
  5. #include "base/bind.h"
  6. #include "base/memory/weak_ptr.h"
  7. #include "google_apis/gaia/gaia_constants.h"
  8. #include "weblayer/public/google_account_access_token_fetch_delegate.h"
  9. namespace weblayer {
  10. SafeBrowsingTokenFetcherImpl::SafeBrowsingTokenFetcherImpl(
  11. const AccessTokenFetchDelegateGetter& delegate_getter)
  12. : delegate_getter_(delegate_getter) {}
  13. SafeBrowsingTokenFetcherImpl::~SafeBrowsingTokenFetcherImpl() = default;
  14. void SafeBrowsingTokenFetcherImpl::Start(Callback callback) {
  15. auto* delegate = delegate_getter_.Run();
  16. if (!delegate) {
  17. std::move(callback).Run("");
  18. return;
  19. }
  20. // NOTE: When a token fetch timeout occurs |token_fetch_tracker_| will invoke
  21. // the client callback, which may end up synchronously destroying this object
  22. // before this object's own callback is invoked. Hence we bind our own
  23. // callback via a WeakPtr.
  24. const int request_id = token_fetch_tracker_.StartTrackingTokenFetch(
  25. std::move(callback),
  26. base::BindOnce(&SafeBrowsingTokenFetcherImpl::OnTokenTimeout,
  27. weak_ptr_factory_.GetWeakPtr()));
  28. request_ids_.insert(request_id);
  29. // In contrast, this object does *not* have a determined lifetime relationship
  30. // with |delegate|.
  31. delegate->FetchAccessToken(
  32. {GaiaConstants::kChromeSafeBrowsingOAuth2Scope},
  33. base::BindOnce(&SafeBrowsingTokenFetcherImpl::OnTokenFetched,
  34. weak_ptr_factory_.GetWeakPtr(), request_id));
  35. }
  36. void SafeBrowsingTokenFetcherImpl::OnInvalidAccessToken(
  37. const std::string& invalid_access_token) {
  38. auto* delegate = delegate_getter_.Run();
  39. if (!delegate)
  40. return;
  41. delegate->OnAccessTokenIdentifiedAsInvalid(
  42. {GaiaConstants::kChromeSafeBrowsingOAuth2Scope}, invalid_access_token);
  43. }
  44. void SafeBrowsingTokenFetcherImpl::OnTokenFetched(
  45. int request_id,
  46. const std::string& access_token) {
  47. if (!request_ids_.count(request_id)) {
  48. // The request timed out before the delegate responded; nothing to do.
  49. return;
  50. }
  51. request_ids_.erase(request_id);
  52. token_fetch_tracker_.OnTokenFetchComplete(request_id, access_token);
  53. // NOTE: Calling SafeBrowsingTokenFetchTracker::OnTokenFetchComplete might
  54. // have resulted in the synchronous destruction of this object, so there is
  55. // nothing safe to do here but return.
  56. }
  57. void SafeBrowsingTokenFetcherImpl::OnTokenTimeout(int request_id) {
  58. DCHECK(request_ids_.count(request_id));
  59. request_ids_.erase(request_id);
  60. }
  61. } // namespace weblayer