File: google_groups_manager.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (104 lines) | stat: -rw-r--r-- 4,272 bytes parent folder | download | duplicates (6)
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
102
103
104
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_VARIATIONS_SERVICE_GOOGLE_GROUPS_MANAGER_H_
#define COMPONENTS_VARIATIONS_SERVICE_GOOGLE_GROUPS_MANAGER_H_

#include "base/containers/flat_set.h"
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/scoped_observation.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_service.h"
#include "components/sync/service/sync_service_observer.h"
#include "components/variations/service/google_groups_manager_prefs.h"

namespace syncer {
class SyncService;
}

namespace user_prefs {
class PrefRegistrySyncable;
}

// Service responsible for one-way synchronization of Google group information
// from per-profile sync data to local-state.
class GoogleGroupsManager : public KeyedService,
                                   public syncer::SyncServiceObserver {
 public:
  explicit GoogleGroupsManager(PrefService& target_prefs,
                                      const std::string& key,
                                      PrefService& source_prefs);

  GoogleGroupsManager(const GoogleGroupsManager&) = delete;
  GoogleGroupsManager& operator=(const GoogleGroupsManager&) =
      delete;

  ~GoogleGroupsManager() override;

  static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);

  // Returns whether `feature` is group controlled, i.e. whether the study that
  // is connected to this feature has a non-empty `google_groups` filter. This
  // will always return `false` if called before experiment parameters are
  // registered.
  static bool IsFeatureGroupControlled(const base::Feature& feature);

  // Returns whether `feature` is enabled and, if the study that is connected to
  // this feature has a non-empty `google_groups` filter, whether the
  // BrowserContext associated with `this` KeyedService is in at least one of
  // those Google Groups. Note that the Google Groups for the BrowserContext are
  // only populated if the user is syncing their preferences. If the user is not
  // syncing their preferences, this function therefore falls back to
  // `base::FeatureList::IsEnabled`.
  bool IsFeatureEnabledForProfile(const base::Feature& feature) const;

  // KeyedService overrides.
  void Shutdown() override;

  void OnSyncServiceInitialized(syncer::SyncService* sync_service);

  // syncer::SyncServiceObserver overrides.
  void OnStateChanged(syncer::SyncService* sync) override;
  void OnSyncShutdown(syncer::SyncService* sync) override;

 private:
  // Clears state that should only exist for a signed in user.
  // This should be called when the user signs out or disables sync, as the
  // server is the source-of-truth for this state, not the client.
  void ClearSigninScopedState();

  // Update the group memberships in `target_prefs_`.
  // Called when `source_prefs_` have been initialized or modified.
  void UpdateGoogleGroups();

  // The preferences to write to. These are the local-state prefs.
  // Preferences are guaranteed to outlive keyed services, so this reference
  // will stay valid for the lifetime of this service.
  const raw_ref<PrefService> target_prefs_;

  // The key to use in the `target_prefs_` dictionary.
  // This key is immutable (it will not change for eg. a given profile, even
  // across Chrome restarts).
  const std::string key_;

  // The preferences to read from. These are the profile prefs.
  // Preferences are guaranteed to outlive keyed services, so this reference
  // will stay valid for the lifetime of this service.
  const raw_ref<PrefService> source_prefs_;

  PrefChangeRegistrar pref_change_registrar_;

  // A cached version of the (variations-related) Google Groups that the profile
  // is a part of.
  base::flat_set<std::string> google_group_ids_;

  // The SyncService observation. When Sync gets disabled (most likely due to
  // user signout), the groups-related preferences must be cleared.
  base::ScopedObservation<syncer::SyncService, syncer::SyncServiceObserver>
      sync_service_observation_{this};
};

#endif  // COMPONENTS_VARIATIONS_SERVICE_GOOGLE_GROUPS_MANAGER_H_