safe_browsing_token_fetcher_impl.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. #ifndef WEBLAYER_BROWSER_SAFE_BROWSING_SAFE_BROWSING_TOKEN_FETCHER_IMPL_H_
  5. #define WEBLAYER_BROWSER_SAFE_BROWSING_SAFE_BROWSING_TOKEN_FETCHER_IMPL_H_
  6. #include <memory>
  7. #include <set>
  8. #include "base/memory/weak_ptr.h"
  9. #include "components/safe_browsing/core/browser/safe_browsing_token_fetch_tracker.h"
  10. #include "components/safe_browsing/core/browser/safe_browsing_token_fetcher.h"
  11. namespace weblayer {
  12. class GoogleAccountAccessTokenFetchDelegate;
  13. // This class fetches access tokens for Safe Browsing via a
  14. // GoogleAccountAccessTokenFetcherDelegate.
  15. class SafeBrowsingTokenFetcherImpl
  16. : public safe_browsing::SafeBrowsingTokenFetcher {
  17. public:
  18. using AccessTokenFetchDelegateGetter =
  19. base::RepeatingCallback<GoogleAccountAccessTokenFetchDelegate*()>;
  20. // Create a SafeBrowsingTokenFetcherImpl that makes access token requests via
  21. // the object returned by |delegate_getter|. |delegate_getter| may return
  22. // null, in which case this object will return the empty string for access
  23. // token requests. This object will not cache the pointer returned by
  24. // |delegate_getter| but will instead invoke it on every access token request,
  25. // as that object might change over time.
  26. // NOTE: In production the getter is
  27. // ProfileImpl::access_token_fetcher_delegate(); this level of indirection is
  28. // present to support unittests.
  29. explicit SafeBrowsingTokenFetcherImpl(
  30. const AccessTokenFetchDelegateGetter& delegate_getter);
  31. ~SafeBrowsingTokenFetcherImpl() override;
  32. // SafeBrowsingTokenFetcher:
  33. void Start(Callback callback) override;
  34. void OnInvalidAccessToken(const std::string& invalid_access_token) override;
  35. private:
  36. void OnTokenFetched(int request_id, const std::string& access_token);
  37. void OnTokenTimeout(int request_id);
  38. AccessTokenFetchDelegateGetter delegate_getter_;
  39. safe_browsing::SafeBrowsingTokenFetchTracker token_fetch_tracker_;
  40. // IDs of outstanding client access token requests being tracked by
  41. // |token_fetch_tracker_|.
  42. std::set<int> request_ids_;
  43. base::WeakPtrFactory<SafeBrowsingTokenFetcherImpl> weak_ptr_factory_{this};
  44. };
  45. } // namespace weblayer
  46. #endif // WEBLAYER_BROWSER_SAFE_BROWSING_SAFE_BROWSING_TOKEN_FETCHER_IMPL_H_