drivefs_auth.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2019 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 ASH_COMPONENTS_DRIVEFS_DRIVEFS_AUTH_H_
  5. #define ASH_COMPONENTS_DRIVEFS_DRIVEFS_AUTH_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "ash/components/drivefs/mojom/drivefs.mojom.h"
  10. #include "base/component_export.h"
  11. #include "base/time/clock.h"
  12. #include "base/time/time.h"
  13. #include "base/timer/timer.h"
  14. #include "third_party/abseil-cpp/absl/types/optional.h"
  15. class AccountId;
  16. class GoogleServiceAuthError;
  17. namespace network {
  18. class SharedURLLoaderFactory;
  19. } // namespace network
  20. namespace signin {
  21. struct AccessTokenInfo;
  22. class IdentityManager;
  23. class PrimaryAccountAccessTokenFetcher;
  24. } // namespace signin
  25. namespace drivefs {
  26. class COMPONENT_EXPORT(DRIVEFS) DriveFsAuth {
  27. public:
  28. class Delegate {
  29. public:
  30. Delegate() = default;
  31. Delegate(const Delegate&) = delete;
  32. Delegate& operator=(const Delegate&) = delete;
  33. virtual ~Delegate() = default;
  34. virtual scoped_refptr<network::SharedURLLoaderFactory>
  35. GetURLLoaderFactory() = 0;
  36. virtual signin::IdentityManager* GetIdentityManager() = 0;
  37. virtual const AccountId& GetAccountId() = 0;
  38. virtual std::string GetObfuscatedAccountId() = 0;
  39. virtual bool IsMetricsCollectionEnabled() = 0;
  40. };
  41. DriveFsAuth(const base::Clock* clock,
  42. const base::FilePath& profile_path,
  43. std::unique_ptr<base::OneShotTimer> timer,
  44. Delegate* delegate);
  45. DriveFsAuth(const DriveFsAuth&) = delete;
  46. DriveFsAuth& operator=(const DriveFsAuth&) = delete;
  47. virtual ~DriveFsAuth();
  48. const base::FilePath& GetProfilePath() const { return profile_path_; }
  49. const AccountId& GetAccountId() { return delegate_->GetAccountId(); }
  50. std::string GetObfuscatedAccountId() {
  51. return delegate_->GetObfuscatedAccountId();
  52. }
  53. bool IsMetricsCollectionEnabled() {
  54. return delegate_->IsMetricsCollectionEnabled();
  55. }
  56. absl::optional<std::string> GetCachedAccessToken();
  57. virtual void GetAccessToken(
  58. bool use_cached,
  59. mojom::DriveFsDelegate::GetAccessTokenCallback callback);
  60. private:
  61. void GotChromeAccessToken(GoogleServiceAuthError error,
  62. signin::AccessTokenInfo access_token_info);
  63. const std::string& GetOrResetCachedToken(bool use_cached);
  64. void UpdateCachedToken(const std::string& token, base::Time expiry);
  65. void AuthTimeout();
  66. SEQUENCE_CHECKER(sequence_checker_);
  67. const base::Clock* const clock_;
  68. const base::FilePath profile_path_;
  69. const std::unique_ptr<base::OneShotTimer> timer_;
  70. Delegate* const delegate_;
  71. std::unique_ptr<signin::PrimaryAccountAccessTokenFetcher>
  72. access_token_fetcher_;
  73. // Pending callback for an in-flight GetAccessToken request.
  74. mojom::DriveFsDelegate::GetAccessTokenCallback get_access_token_callback_;
  75. std::string last_token_;
  76. base::Time last_token_expiry_;
  77. };
  78. } // namespace drivefs
  79. #endif // ASH_COMPONENTS_DRIVEFS_DRIVEFS_AUTH_H_