File: ukm_database_client.h

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; 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 (104 lines) | stat: -rw-r--r-- 3,690 bytes parent folder | download | duplicates (4)
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 2022 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_SEGMENTATION_PLATFORM_UKM_DATABASE_CLIENT_H_
#define CHROME_BROWSER_SEGMENTATION_PLATFORM_UKM_DATABASE_CLIENT_H_

#include <memory>

#include "base/check.h"
#include "base/memory/raw_ptr.h"
#include "base/no_destructor.h"
#include "base/synchronization/lock.h"
#include "base/thread_annotations.h"
#include "chrome/browser/profiles/profile.h"
#include "components/segmentation_platform/internal/signals/ukm_observer.h"
#include "components/segmentation_platform/internal/ukm_data_manager.h"

namespace ukm {
class UkmRecorderImpl;
}

namespace segmentation_platform {
class UkmDataManager;
class UkmObserver;

// Provides UKM functionality to the segmentation platform service(s).
class UkmDatabaseClient {
 public:
  UkmDatabaseClient();
  ~UkmDatabaseClient();

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

  // Must be called before any profiles (segmentation services) are created.
  // `in_memory_database` is used in tests only to create in memory databases to
  // make tests faster.
  void PreProfileInit(bool in_memory_database);

  // Must be called after profiles are destroyed, but before metrics service is
  // destroyed.
  void PostMessageLoopRun();

  // UkmDataManager will be valid for the lifetime of all the profiles. It is
  // created before profiles are created at startup. It is safe to use this
  // pointer till ProfileManagerDestroying() is called.
  segmentation_platform::UkmDataManager* GetUkmDataManager();

  // UKM observer will use the test recorder to observe metrics.
  void set_ukm_recorder_for_testing(ukm::UkmRecorderImpl* ukm_recorder) {
    DCHECK(!ukm_observer_);
    ukm_recorder_for_testing_ = ukm_recorder;
  }

  UkmObserver* ukm_observer_for_testing() { return ukm_observer_.get(); }

  void TearDownForTesting();

 private:
  bool initialized_{false};
  raw_ptr<ukm::UkmRecorderImpl> ukm_recorder_for_testing_;
  std::unique_ptr<UkmObserver> ukm_observer_;
  std::unique_ptr<UkmDataManager> ukm_data_manager_;
};

// Class to own the UkmDatabaseClient. Supports overriding instances in tests.
class UkmDatabaseClientHolder {
 public:
  // Always returns the main client instance, unless a client was set for
  // testing. Can be called with nullptr to get the main instance when `profile`
  // is not created.
  static UkmDatabaseClient& GetClientInstance(Profile* profile);

  // Sets or removes the instance used by `profile` for testing. Thread safe,
  // and GetClientInstance() will return the new client based on the profile.
  // Note that if GetClientInstance() is called with nullptr, the main instance
  // will still be returned.
  static void SetUkmClientForTesting(Profile* profile,
                                     UkmDatabaseClient* client);

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

 private:
  friend base::NoDestructor<UkmDatabaseClientHolder>;

  static UkmDatabaseClientHolder& GetInstance();
  UkmDatabaseClientHolder();
  ~UkmDatabaseClientHolder();

  void SetUkmClientForTestingInternal(Profile* profile,
                                      UkmDatabaseClient* client);

  base::Lock lock_;
  std::map<raw_ptr<Profile>, raw_ptr<UkmDatabaseClient>> clients_for_testing_
      GUARDED_BY(lock_);

  std::unique_ptr<UkmDatabaseClient> main_client_;
};

}  // namespace segmentation_platform

#endif  // CHROME_BROWSER_SEGMENTATION_PLATFORM_UKM_DATABASE_CLIENT_H_