| 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 #include "base/observer_list.h" |
| 13 | 13 |
| 14 enum class KeepAliveOrigin; | 14 enum class KeepAliveOrigin; |
| 15 enum class KeepAliveRestartOption; | 15 enum class KeepAliveRestartOption; |
| 16 class KeepAliveStateObserver; | 16 class KeepAliveStateObserver; |
| 17 | 17 |
| 18 // Centralized registry that objects in the browser can use to |
| 19 // express their requirements wrt the lifetime of the browser. |
| 20 // Observers registered with it can then react as those requirements |
| 21 // change. |
| 22 // In particular, this is how the browser process knows when to stop |
| 23 // or stay alive. |
| 24 // Note: BrowserProcessImpl registers to react on changes. |
| 25 // TestingBrowserProcess does not do it, meaning that the shutdown |
| 26 // sequence does not happen during unit tests. |
| 18 class KeepAliveRegistry { | 27 class KeepAliveRegistry { |
| 19 public: | 28 public: |
| 20 static KeepAliveRegistry* GetInstance(); | 29 static KeepAliveRegistry* GetInstance(); |
| 21 | 30 |
| 22 // Methods to query the state of the registry. | 31 // 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 AddRefModule to keep the | |
| 25 // browser alive. Tracked by https://crbug.com/587926 | |
| 26 bool IsKeepingAlive() const; | 32 bool IsKeepingAlive() const; |
| 27 bool IsRestartAllowed() const; | 33 bool IsRestartAllowed() const; |
| 34 bool IsOriginRegistered(KeepAliveOrigin origin) const; |
| 28 | 35 |
| 29 void AddObserver(KeepAliveStateObserver* observer); | 36 void AddObserver(KeepAliveStateObserver* observer); |
| 30 void RemoveObserver(KeepAliveStateObserver* observer); | 37 void RemoveObserver(KeepAliveStateObserver* observer); |
| 31 | 38 |
| 32 private: | 39 private: |
| 33 friend struct base::DefaultSingletonTraits<KeepAliveRegistry>; | 40 friend struct base::DefaultSingletonTraits<KeepAliveRegistry>; |
| 34 // Friend to be able to use Register/Unregister | 41 // Friend to be able to use Register/Unregister |
| 35 friend class ScopedKeepAlive; | 42 friend class ScopedKeepAlive; |
| 36 friend std::ostream& operator<<(std::ostream& out, | 43 friend std::ostream& operator<<(std::ostream& out, |
| 37 const KeepAliveRegistry& registry); | 44 const KeepAliveRegistry& registry); |
| 38 | 45 |
| 39 KeepAliveRegistry(); | 46 KeepAliveRegistry(); |
| 40 ~KeepAliveRegistry(); | 47 ~KeepAliveRegistry(); |
| 41 | 48 |
| 42 // Add/Remove entries. Do not use directly, use ScopedKeepAlive instead. | 49 // Add/Remove entries. Do not use directly, use ScopedKeepAlive instead. |
| 43 void Register(KeepAliveOrigin origin, KeepAliveRestartOption restart); | 50 void Register(KeepAliveOrigin origin, KeepAliveRestartOption restart); |
| 44 void Unregister(KeepAliveOrigin origin, KeepAliveRestartOption restart); | 51 void Unregister(KeepAliveOrigin origin, KeepAliveRestartOption restart); |
| 45 | 52 |
| 46 // Methods called when a specific aspect of the state of the registry changes. | 53 // Methods called when a specific aspect of the state of the registry changes. |
| 47 void OnKeepingAliveChanged(bool new_keeping_alive); | 54 void OnKeepAliveStateChanged(bool new_keeping_alive); |
| 48 void OnRestartAllowedChanged(bool new_restart_allowed); | 55 void OnRestartAllowedChanged(bool new_restart_allowed); |
| 49 | 56 |
| 50 // Tracks the registered KeepAlives, storing the origin and the number of | 57 // Tracks the registered KeepAlives, storing the origin and the number of |
| 51 // registered KeepAlives for each. | 58 // registered KeepAlives for each. |
| 52 std::map<KeepAliveOrigin, int> registered_keep_alives_; | 59 std::map<KeepAliveOrigin, int> registered_keep_alives_; |
| 53 | 60 |
| 54 // Total number of registered KeepAlives | 61 // Total number of registered KeepAlives |
| 55 int registered_count_; | 62 int registered_count_; |
| 56 | 63 |
| 57 // Number of registered keep alives that have KeepAliveRestartOption::ENABLED. | 64 // Number of registered keep alives that have KeepAliveRestartOption::ENABLED. |
| 58 int restart_allowed_count_; | 65 int restart_allowed_count_; |
| 59 | 66 |
| 60 base::ObserverList<KeepAliveStateObserver> observers_; | 67 base::ObserverList<KeepAliveStateObserver> observers_; |
| 61 | 68 |
| 62 DISALLOW_COPY_AND_ASSIGN(KeepAliveRegistry); | 69 DISALLOW_COPY_AND_ASSIGN(KeepAliveRegistry); |
| 63 }; | 70 }; |
| 64 | 71 |
| 65 #ifndef NDEBUG | 72 #ifndef NDEBUG |
| 66 std::ostream& operator<<(std::ostream& out, const KeepAliveRegistry& registry); | 73 std::ostream& operator<<(std::ostream& out, const KeepAliveRegistry& registry); |
| 67 #endif // ndef NDEBUG | 74 #endif // ndef NDEBUG |
| 68 | 75 |
| 69 #endif // CHROME_BROWSER_LIFETIME_KEEP_ALIVE_REGISTRY_H_ | 76 #endif // CHROME_BROWSER_LIFETIME_KEEP_ALIVE_REGISTRY_H_ |
| OLD | NEW |