OLD | NEW |
---|---|
(Empty) | |
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 | |
5 #include "chrome/browser/lifetime/keep_alive_registry.h" | |
6 | |
7 #include "chrome/browser/lifetime/application_lifetime.h" | |
8 #include "chrome/browser/lifetime/keep_alive_types.h" | |
9 | |
10 namespace { | |
11 | |
12 KeepAliveRegistry* g_test_instance = nullptr; | |
13 | |
14 } // namespace | |
15 | |
16 //////////////////////////////////////////////////////////////////////////////// | |
17 // Public methods | |
18 | |
19 // static | |
20 KeepAliveRegistry* KeepAliveRegistry::GetInstance() { | |
21 if (g_test_instance) | |
22 return g_test_instance; | |
23 | |
24 return base::Singleton<KeepAliveRegistry>::get(); | |
25 } | |
26 | |
27 bool KeepAliveRegistry::WillKeepAlive() const { | |
28 return registered_count_ > 0; | |
29 } | |
30 | |
31 //////////////////////////////////////////////////////////////////////////////// | |
32 // Private methods | |
33 | |
34 KeepAliveRegistry::KeepAliveRegistry() {} | |
35 | |
36 KeepAliveRegistry::~KeepAliveRegistry() { | |
37 DCHECK_EQ(0, registered_count_); | |
38 } | |
39 | |
40 // static | |
41 void KeepAliveRegistry::SetInstanceForTesting(KeepAliveRegistry* new_instance) { | |
42 g_test_instance = new_instance; | |
43 } | |
44 | |
45 void KeepAliveRegistry::Register(KeepAliveOrigin origin) { | |
46 if (!WillKeepAlive()) | |
47 HugBrowser(); | |
48 | |
49 ++registered_keep_alives_[origin]; | |
50 ++registered_count_; | |
51 } | |
52 | |
53 void KeepAliveRegistry::Unregister(KeepAliveOrigin origin) { | |
54 --registered_count_; | |
55 --registered_keep_alives_[origin]; | |
sky
2016/02/25 00:25:18
remove from rendered_keep_alives if count == 0
dgn
2016/02/25 17:19:46
Done.
| |
56 DCHECK_GE(registered_count_, 0); | |
57 DCHECK_GE(registered_keep_alives_[origin], 0); | |
58 | |
59 if (!WillKeepAlive()) | |
60 ReleaseBrowser(); | |
61 } | |
62 | |
63 void KeepAliveRegistry::HugBrowser() { | |
64 // TODO(dgn): We currently use the plain KeepAliveCount. We will integrate | |
65 // that in this class progressively as mechanisms are merged. | |
66 chrome::IncrementKeepAliveCount(); | |
67 } | |
68 | |
69 void KeepAliveRegistry::ReleaseBrowser() { | |
70 // TODO(dgn): We currently use the plain KeepAliveCount. We will integrate | |
71 // that in this class progressively as mechanisms are merged. | |
72 chrome::DecrementKeepAliveCount(); | |
73 } | |
OLD | NEW |