File: keep_alive_registry.h

package info (click to toggle)
chromium 138.0.7204.92-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,576 kB
  • sloc: cpp: 34,933,512; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,956; 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 (115 lines) | stat: -rw-r--r-- 4,273 bytes parent folder | download | duplicates (5)
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
// Copyright 2016 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_KEEP_ALIVE_REGISTRY_KEEP_ALIVE_REGISTRY_H_
#define COMPONENTS_KEEP_ALIVE_REGISTRY_KEEP_ALIVE_REGISTRY_H_

#include <unordered_map>
#include <vector>

#include "base/memory/singleton.h"
#include "base/observer_list.h"

enum class KeepAliveOrigin;
enum class KeepAliveRestartOption;
class KeepAliveStateObserver;

// Centralized registry that objects in the browser can use to
// express their requirements wrt the lifetime of the browser.
// Observers registered with it can then react as those requirements
// change.
// In particular, this is how the browser process knows when to stop
// or stay alive.
// Note: BrowserProcessImpl registers to react on changes.
// TestingBrowserProcess does not do it, meaning that the shutdown
// sequence does not happen during unit tests.
// Note: This is not thread-safe, and should only be used on the main thread.
class KeepAliveRegistry {
 public:
  static KeepAliveRegistry* GetInstance();

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

  // Methods to query the state of the registry.
  bool IsKeepingAlive() const;
  bool IsKeepingAliveOnlyByBrowserOrigin() const;
  bool IsRestartAllowed() const;
  bool IsOriginRegistered(KeepAliveOrigin origin) const;

  void AddObserver(KeepAliveStateObserver* observer);
  void RemoveObserver(KeepAliveStateObserver* observer);

  // Returns whether restart would be allowed if all the keep alives for the
  // provided |origins| were not registered.
  bool WouldRestartWithout(const std::vector<KeepAliveOrigin>& origins) const;

  // True if shutdown is in progress. No new KeepAlive should be registered
  // while shutting down.
  bool IsShuttingDown() const;
  // Call when shutting down to ensure registering a new KeepAlive CHECKs.
  void SetIsShuttingDown(bool value = true);

  // True if restarting is in progress.
  bool IsRestarting() const;

  // Called when restarting is triggered.
  void SetRestarting();

 private:
  friend struct base::DefaultSingletonTraits<KeepAliveRegistry>;
  // Friend to be able to use Register/Unregister
  friend class ScopedKeepAlive;
  friend std::ostream& operator<<(std::ostream& out,
                                  const KeepAliveRegistry& registry);

  // TODO(dgn): Remove this when std::hash supports enums directly (c++14)
  // http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2148
  struct EnumClassHash {
    std::size_t operator()(KeepAliveOrigin origin) const {
      return static_cast<int>(origin);
    }
  };

  using OriginMap = std::unordered_map<KeepAliveOrigin, int, EnumClassHash>;

  KeepAliveRegistry();
  ~KeepAliveRegistry();

  // Add/Remove entries. Do not use directly, use ScopedKeepAlive instead.
  void Register(KeepAliveOrigin origin, KeepAliveRestartOption restart);
  void Unregister(KeepAliveOrigin origin, KeepAliveRestartOption restart);

  // Methods called when a specific aspect of the state of the registry changes.
  void OnKeepAliveStateChanged(bool new_keeping_alive);
  void OnRestartAllowedChanged(bool new_restart_allowed);

  // Unregisters one occurrence of the provided |origin| from |keep_alive_map|
  void DecrementCount(KeepAliveOrigin origin, OriginMap* keep_alive_map);

  // Tracks the registered KeepAlives, storing the origin and the number of
  // registered KeepAlives for each.
  OriginMap registered_keep_alives_;

  // Tracks the registered KeepAlives that had KeepAliveRestartOption::ENABLED
  // set, storing the origin and the number of restart allowed KeepAlives for
  // each origin.
  OriginMap restart_allowed_keep_alives_;

  // Total number of registered KeepAlives
  int registered_count_;

  // Number of registered keep alives that have KeepAliveRestartOption::ENABLED.
  int restart_allowed_count_;

  // Used to guard against registering during shutdown.
  bool is_shutting_down_ = false;

  // Used to handle KeepAliveRestartOption::ENABLED.
  bool is_restarting_ = false;

  base::ObserverList<KeepAliveStateObserver>::Unchecked observers_;
};

#endif  // COMPONENTS_KEEP_ALIVE_REGISTRY_KEEP_ALIVE_REGISTRY_H_