File: glic_user_status_fetcher.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (101 lines) | stat: -rw-r--r-- 4,153 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_GLIC_GLIC_USER_STATUS_FETCHER_H_
#define CHROME_BROWSER_GLIC_GLIC_USER_STATUS_FETCHER_H_

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/time/time.h"
#include "base/timer/wall_clock_timer.h"
#include "chrome/browser/glic/glic_user_status_code.h"
#include "chrome/browser/profiles/profile.h"
#include "components/signin/public/base/gaia_id_hash.h"
#include "google_apis/common/request_sender.h"
#include "google_apis/gaia/google_service_auth_error.h"

namespace glic {

// This class, GlicUserStatusFetcher, is responsible for asynchronously fetching
// the Glic user status from a Google-owned API. The response is processed in a
// callback and determines if the current signed-in primary account user is able
// to see Glic UI surfaces(e.g. tab strip button).
//
// Ownership:
// This class is instantiate as a member of the `GlicEnabling` object if the
// feature is enabled.
//
// Request Schedule:
// The fetching of the user status is scheduled as follows:
// - Upon construction: An initial request is made if enough time has passed
//   since the last successful update (based on `kGlicUserStatusRequestDelay`
//   feature flag).
// - Upon primary account sign-in: When a primary account is signed in (or when
//   Chrome launches with a signed-in primary account), a request is initiated.
// - When refresh token becomes available: If a request is attempted
//   but the refresh token is not yet available, the fetch is retried when the
//   refresh token is updated.
// - Periodically: Subsequent requests are scheduled to occur every
//   `kGlicUserStatusRequestDelay` (currently 23 hours) after a successful
//   response.
//
// Error Handling:
// If the server responds with an HTTP error (non-200 status code) or fails to
// provide a valid JSON response, the `ProcessResponse` method will interpret
// this as `UserStatusCode::SERVER_UNAVAILABLE`. In this scenario, the locally
// cached user status (if any) is *not* overwritten, ensuring that the Glic
// enabling state remains consistent based on the last known good status. The
// callback will still be executed to notify the observer of the fetch attempt
// outcome.
//
// Clock Changes:
// The `GlicUserStatusFetcher` use the `base::WallClockTimer` for
// scheduling the periodic fetching. Unlike, `base::TimeTicks`, `base::Time`
// which `base::WallClockTimer` uses does not freeze during suspend. Significant
// system clock errors could lead to unexpected timing of the status updates and
// potentially affect the QPS too. However, we expect it is only minor system
// clock adjustment if any.
class GlicUserStatusFetcher {
 public:
  explicit GlicUserStatusFetcher(Profile* profile,
                                 base::RepeatingClosure callback);
  ~GlicUserStatusFetcher();

  static std::optional<CachedUserStatus> GetCachedUserStatus(Profile* profile);
  static bool IsDisabled(Profile* profile);

  bool IsEnterpriseAccount();
  void InvalidateCachedStatus();
  void UpdateUserStatus();
  void UpdateUserStatusIfNeeded();
  void ScheduleUserStatusUpdate(base::TimeDelta time_to_next_update);
  void CancelUserStatusUpdateIfNeeded();

  void SetGlicUserStatusUrlForTest(GURL test_url) { endpoint_ = test_url; }

 private:
  static std::optional<signin::GaiaIdHash> GetGaiaIdHashForPrimaryAccount(
      Profile* profile);

  void CreateRequestSender();

  void FetchNow();

  void ProcessResponse(const std::string& account_id_hash,
                       UserStatusCode result_code);

  bool is_user_status_waiting_for_refresh_token_ = false;
  raw_ptr<Profile> profile_;
  const base::RepeatingClosure callback_;
  GURL endpoint_;
  std::string oauth2_scope_;
  base::WallClockTimer refresh_status_timer_;
  std::unique_ptr<google_apis::RequestSender> request_sender_;
  base::OnceClosure cancel_closure_;

  base::WeakPtrFactory<GlicUserStatusFetcher> weak_ptr_factory_{this};
};

}  // namespace glic
#endif  // CHROME_BROWSER_GLIC_GLIC_USER_STATUS_FETCHER_H_