keep_alive_registry.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_KEEP_ALIVE_REGISTRY_KEEP_ALIVE_REGISTRY_H_
  5. #define COMPONENTS_KEEP_ALIVE_REGISTRY_KEEP_ALIVE_REGISTRY_H_
  6. #include <unordered_map>
  7. #include <vector>
  8. #include "base/memory/singleton.h"
  9. #include "base/observer_list.h"
  10. enum class KeepAliveOrigin;
  11. enum class KeepAliveRestartOption;
  12. class KeepAliveStateObserver;
  13. // Centralized registry that objects in the browser can use to
  14. // express their requirements wrt the lifetime of the browser.
  15. // Observers registered with it can then react as those requirements
  16. // change.
  17. // In particular, this is how the browser process knows when to stop
  18. // or stay alive.
  19. // Note: BrowserProcessImpl registers to react on changes.
  20. // TestingBrowserProcess does not do it, meaning that the shutdown
  21. // sequence does not happen during unit tests.
  22. class KeepAliveRegistry {
  23. public:
  24. static KeepAliveRegistry* GetInstance();
  25. KeepAliveRegistry(const KeepAliveRegistry&) = delete;
  26. KeepAliveRegistry& operator=(const KeepAliveRegistry&) = delete;
  27. // Methods to query the state of the registry.
  28. bool IsKeepingAlive() const;
  29. bool IsKeepingAliveOnlyByBrowserOrigin() const;
  30. bool IsRestartAllowed() const;
  31. bool IsOriginRegistered(KeepAliveOrigin origin) const;
  32. void AddObserver(KeepAliveStateObserver* observer);
  33. void RemoveObserver(KeepAliveStateObserver* observer);
  34. // Returns whether restart would be allowed if all the keep alives for the
  35. // provided |origins| were not registered.
  36. bool WouldRestartWithout(const std::vector<KeepAliveOrigin>& origins) const;
  37. // True if shutdown is in progress. No new KeepAlive should be registered
  38. // while shutting down.
  39. bool IsShuttingDown() const;
  40. // Call when shutting down to ensure registering a new KeepAlive CHECKs.
  41. void SetIsShuttingDown(bool value = true);
  42. // True if restarting is in progress.
  43. bool IsRestarting() const;
  44. // Called when restarting is triggered.
  45. void SetRestarting();
  46. private:
  47. friend struct base::DefaultSingletonTraits<KeepAliveRegistry>;
  48. // Friend to be able to use Register/Unregister
  49. friend class ScopedKeepAlive;
  50. friend std::ostream& operator<<(std::ostream& out,
  51. const KeepAliveRegistry& registry);
  52. // TODO(dgn): Remove this when std::hash supports enums directly (c++14)
  53. // http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2148
  54. struct EnumClassHash {
  55. std::size_t operator()(KeepAliveOrigin origin) const {
  56. return static_cast<int>(origin);
  57. }
  58. };
  59. using OriginMap = std::unordered_map<KeepAliveOrigin, int, EnumClassHash>;
  60. KeepAliveRegistry();
  61. ~KeepAliveRegistry();
  62. // Add/Remove entries. Do not use directly, use ScopedKeepAlive instead.
  63. void Register(KeepAliveOrigin origin, KeepAliveRestartOption restart);
  64. void Unregister(KeepAliveOrigin origin, KeepAliveRestartOption restart);
  65. // Methods called when a specific aspect of the state of the registry changes.
  66. void OnKeepAliveStateChanged(bool new_keeping_alive);
  67. void OnRestartAllowedChanged(bool new_restart_allowed);
  68. // Unregisters one occurrence of the provided |origin| from |keep_alive_map|
  69. void DecrementCount(KeepAliveOrigin origin, OriginMap* keep_alive_map);
  70. // Tracks the registered KeepAlives, storing the origin and the number of
  71. // registered KeepAlives for each.
  72. OriginMap registered_keep_alives_;
  73. // Tracks the registered KeepAlives that had KeepAliveRestartOption::ENABLED
  74. // set, storing the origin and the number of restart allowed KeepAlives for
  75. // each origin.
  76. OriginMap restart_allowed_keep_alives_;
  77. // Total number of registered KeepAlives
  78. int registered_count_;
  79. // Number of registered keep alives that have KeepAliveRestartOption::ENABLED.
  80. int restart_allowed_count_;
  81. // Used to guard against registering during shutdown.
  82. bool is_shutting_down_ = false;
  83. // Used to handle KeepAliveRestartOption::ENABLED.
  84. bool is_restarting_ = false;
  85. base::ObserverList<KeepAliveStateObserver>::Unchecked observers_;
  86. };
  87. #endif // COMPONENTS_KEEP_ALIVE_REGISTRY_KEEP_ALIVE_REGISTRY_H_