File: browser_user_data.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 (122 lines) | stat: -rw-r--r-- 4,356 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// 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_UI_BROWSER_USER_DATA_H_
#define CHROME_BROWSER_UI_BROWSER_USER_DATA_H_

#include "base/check.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/supports_user_data.h"
#include "chrome/browser/ui/browser.h"

// Do not introduce new uses of this class. Instead use BrowserWindowFeatures.
// BrowserWindowFeatures is functionally identical but has two benefits: it does
// not force a dependency onto class Browser, and the lifetime semantics are
// explicit rather than implicit.
//
// For example, the following two getters are equivalent:
//   (1) FooFeature::GetOrCreateForBrowser(browser)
//   (2) browser->browser_window_features()->get_foo_feature().
// In (1), FooFeature depends on Browser. As Browser depends on everything, this
// is a circular dependency. In (2), FooFeature does not have to depend on
// Browser.
//
// A base class for classes attached to, and scoped to, the lifetime of a
// Browser. For example:
//
// --- in foo_helper.h ---
// class FooHelper : public BrowserUserData<FooHelper> {
//  public:
//   ~FooHelper() override;
//
//   // ... more public stuff here ...
//
//  private:
//   explicit FooHelper(Browser* browser);
//
//   friend BrowserUserData;
//   BROWSER_USER_DATA_KEY_DECL();
//
//   // ... more private stuff here ...
// };
//
// --- in foo_helper.cc ---
// BROWSER_USER_DATA_KEY_IMPL(FooHelper)

template <typename T>
class BrowserUserData : public base::SupportsUserData::Data {
 public:
  explicit BrowserUserData(Browser& browser) : browser_(&browser) {}
  BrowserUserData(const BrowserUserData&) = delete;
  BrowserUserData& operator=(const BrowserUserData&) = delete;

  // Creates an object of type T, and attaches it to the specified Browser.
  // If an instance is already attached, does nothing.
  template <typename... Args>
  static void CreateForBrowser(Browser* browser, Args&&... args) {
    DCHECK(browser);
    if (!FromBrowser(browser)) {
      browser->SetUserData(
          UserDataKey(),
          base::WrapUnique(new T(browser, std::forward<Args>(args)...)));
    }
  }

  // Retrieves the instance of type T that was attached to the specified
  // Browser (via CreateForBrowser above) and returns it. If no instance
  // of the type was attached, returns nullptr.
  static T* FromBrowser(Browser* browser) {
    DCHECK(browser);
    return static_cast<T*>(browser->GetUserData(UserDataKey()));
  }
  static const T* FromBrowser(const Browser* browser) {
    DCHECK(browser);
    return static_cast<const T*>(browser->GetUserData(UserDataKey()));
  }

  static T* GetOrCreateForBrowser(Browser* browser) {
    if (auto* data = FromBrowser(browser)) {
      return data;
    }

    CreateForBrowser(browser);
    return FromBrowser(browser);
  }

  // Removes the instance attached to the specified Browser.
  static void RemoveFromBrowser(Browser* browser) {
    DCHECK(FromBrowser(browser));
    browser->RemoveUserData(UserDataKey());
  }

  static const void* UserDataKey() { return &T::kUserDataKey; }

  // Returns the Browser associated with `this` object of a subclass
  // which inherits from BrowserUserData.
  //
  // The returned `Browser` is guaranteed to live as long as `this`
  // BrowserUserData (due to how UserData works - Browser
  // owns `this` UserData).
  Browser& GetBrowser() { return *browser_; }
  const Browser& GetBrowser() const { return *browser_; }

 private:
  // Browser associated with subclass which inherits this BrowserUserData.
  const raw_ptr<Browser> browser_ = nullptr;
};

// Users won't be able to instantiate the template if they miss declaring the
// user data key.
// This macro declares a static variable inside the class that inherits from
// BrowserUserData. The address of this static variable is used as
// the key to store/retrieve an instance of the class.
#define BROWSER_USER_DATA_KEY_DECL() static const int kUserDataKey = 0

// This macro instantiates the static variable declared by the previous macro.
// It must live in a .cc file to ensure that there is only one instantiation
// of the static variable.
#define BROWSER_USER_DATA_KEY_IMPL(Type) const int Type::kUserDataKey

#endif  // CHROME_BROWSER_UI_BROWSER_USER_DATA_H_