OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_LIFETIME_KEEP_ALIVE_REGISTRY_H_ | 5 #ifndef CHROME_BROWSER_LIFETIME_KEEP_ALIVE_REGISTRY_H_ |
6 #define CHROME_BROWSER_LIFETIME_KEEP_ALIVE_REGISTRY_H_ | 6 #define CHROME_BROWSER_LIFETIME_KEEP_ALIVE_REGISTRY_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/singleton.h" | 11 #include "base/memory/singleton.h" |
| 12 #include "base/observer_list.h" |
12 | 13 |
13 enum class KeepAliveOrigin; | 14 enum class KeepAliveOrigin; |
| 15 enum class KeepAliveRestartOption; |
| 16 class KeepAliveStateObserver; |
14 | 17 |
15 class KeepAliveRegistry { | 18 class KeepAliveRegistry { |
16 public: | 19 public: |
17 static KeepAliveRegistry* GetInstance(); | 20 static KeepAliveRegistry* GetInstance(); |
18 | 21 |
19 bool WillKeepAlive() const; | 22 // Methods to query the state of the registry. |
| 23 // TODO(dgn): This currently does not give a complete picture. It has no |
| 24 // information about the many places that rely on IncrementKeepAliveCount and |
| 25 // AddRefModule to keep the browser alive. Tracked by https://crbug.com/587926 |
| 26 bool IsKeepingAlive() const; |
| 27 bool IsRestartAllowed() const; |
| 28 |
| 29 void AddObserver(KeepAliveStateObserver* observer); |
| 30 void RemoveObserver(KeepAliveStateObserver* observer); |
20 | 31 |
21 private: | 32 private: |
22 friend struct base::DefaultSingletonTraits<KeepAliveRegistry>; | 33 friend struct base::DefaultSingletonTraits<KeepAliveRegistry>; |
23 // Friend to be able to use Register/Unregister | 34 // Friend to be able to use Register/Unregister |
24 friend class ScopedKeepAlive; | 35 friend class ScopedKeepAlive; |
| 36 friend std::ostream& operator<<(std::ostream& out, |
| 37 const KeepAliveRegistry& registry); |
25 | 38 |
26 KeepAliveRegistry(); | 39 KeepAliveRegistry(); |
27 ~KeepAliveRegistry(); | 40 ~KeepAliveRegistry(); |
28 | 41 |
29 // Add/Remove entries. Do not use directly, use ScopedKeepAlive instead. | 42 // Add/Remove entries. Do not use directly, use ScopedKeepAlive instead. |
30 void Register(KeepAliveOrigin origin); | 43 void Register(KeepAliveOrigin origin, KeepAliveRestartOption restart); |
31 void Unregister(KeepAliveOrigin origin); | 44 void Unregister(KeepAliveOrigin origin, KeepAliveRestartOption restart); |
| 45 |
| 46 // Methods called when a specific aspect of the state of the registry changes. |
| 47 void OnKeepingAliveChanged(bool new_keeping_alive); |
| 48 void OnRestartAllowedChanged(bool new_restart_allowed); |
32 | 49 |
33 // Tracks the registered KeepAlives, storing the origin and the number of | 50 // Tracks the registered KeepAlives, storing the origin and the number of |
34 // registered KeepAlives for each. | 51 // registered KeepAlives for each. |
35 std::map<KeepAliveOrigin, int> registered_keep_alives_; | 52 std::map<KeepAliveOrigin, int> registered_keep_alives_; |
36 | 53 |
37 // Total number of registered KeepAlives | 54 // Total number of registered KeepAlives |
38 int registered_count_; | 55 int registered_count_; |
39 | 56 |
| 57 // Number of registered keep alives that have KeepAliveRestartOption::ENABLED. |
| 58 int restart_allowed_count_; |
| 59 |
| 60 base::ObserverList<KeepAliveStateObserver> observers_; |
| 61 |
40 DISALLOW_COPY_AND_ASSIGN(KeepAliveRegistry); | 62 DISALLOW_COPY_AND_ASSIGN(KeepAliveRegistry); |
41 }; | 63 }; |
42 | 64 |
| 65 #ifndef NDEBUG |
| 66 std::ostream& operator<<(std::ostream& out, const KeepAliveRegistry& registry); |
| 67 #endif // ndef NDEBUG |
| 68 |
43 #endif // CHROME_BROWSER_LIFETIME_KEEP_ALIVE_REGISTRY_H_ | 69 #endif // CHROME_BROWSER_LIFETIME_KEEP_ALIVE_REGISTRY_H_ |
OLD | NEW |