Index: chrome/browser/lifetime/keep_alive_registry.h |
diff --git a/chrome/browser/lifetime/keep_alive_registry.h b/chrome/browser/lifetime/keep_alive_registry.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6fc13f65e3b793cd3a065e5ebd2e51e70712826e |
--- /dev/null |
+++ b/chrome/browser/lifetime/keep_alive_registry.h |
@@ -0,0 +1,56 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef CHROME_BROWSER_LIFETIME_KEEP_ALIVE_REGISTRY_H_ |
+#define CHROME_BROWSER_LIFETIME_KEEP_ALIVE_REGISTRY_H_ |
+ |
+#include <map> |
+ |
+#include "base/macros.h" |
+#include "base/memory/singleton.h" |
+ |
+enum class KeepAliveOrigin; |
+ |
+class KeepAliveRegistry { |
+ public: |
+ static KeepAliveRegistry* GetInstance(); |
+ |
+ bool WillKeepAlive() const; |
+ |
+ protected: |
+ KeepAliveRegistry(); |
+ virtual ~KeepAliveRegistry(); |
+ |
+ // Prevent the browser from closing. Called when a KeepAlive is first |
+ // registered. |
+ virtual void HugBrowser(); |
sky
2016/02/25 00:25:18
'hug', eh? How about PinBrowser(), PreventBrowserF
dgn
2016/02/25 17:19:46
Removed, since I'm not mocking the method anymore.
|
+ |
+ // Let the browser close. Called when the last KeepAlive is unregistered. |
+ virtual void ReleaseBrowser(); |
sky
2016/02/25 00:25:18
I'm not a fan of virtual functions like this just
dgn
2016/02/25 17:19:46
I wanted to check the call that has external effec
|
+ |
+ private: |
+ friend struct base::DefaultSingletonTraits<KeepAliveRegistry>; |
+ friend class KeepAliveRegistryTest; |
+ // Friend to be able to use Register/Unregister |
+ friend class ScopedKeepAlive; |
+ |
+ // Allows to provide a specific instance for testing purpose. At the end |
+ // of the test, reset it by setting it to |nullptr|. |
+ static void SetInstanceForTesting(KeepAliveRegistry* new_instance); |
+ |
+ // Add/Remove entries. Do not use directly, use ScopedKeepAlive instead. |
+ void Register(KeepAliveOrigin origin); |
+ void Unregister(KeepAliveOrigin origin); |
+ |
+ // Tracks the registered KeepAlives, storing the origin and the number of |
+ // registered KeepAlives for each. |
+ std::map<KeepAliveOrigin, int> registered_keep_alives_; |
+ |
+ // Total number of registered KeepAlives |
+ int registered_count_; |
sky
2016/02/25 00:25:18
You need to initialize this to 0.
dgn
2016/02/25 17:19:46
Thanks. Done.
|
+ |
+ DISALLOW_COPY_AND_ASSIGN(KeepAliveRegistry); |
+}; |
+ |
+#endif // CHROME_BROWSER_LIFETIME_KEEP_ALIVE_REGISTRY_H_ |