dgn | 3d351ad1 | 2016-02-26 17:36:45 | [diff] [blame] | 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 | |
dgn | 0237778 | 2016-03-12 00:58:38 | [diff] [blame^] | 7 | #include "chrome/browser/browser_process.h" |
dgn | 3d351ad1 | 2016-02-26 17:36:45 | [diff] [blame] | 8 | #include "chrome/browser/lifetime/application_lifetime.h" |
dgn | 3563ecaf | 2016-03-09 19:28:26 | [diff] [blame] | 9 | #include "chrome/browser/lifetime/keep_alive_state_observer.h" |
dgn | 3d351ad1 | 2016-02-26 17:36:45 | [diff] [blame] | 10 | #include "chrome/browser/lifetime/keep_alive_types.h" |
| 11 | |
| 12 | //////////////////////////////////////////////////////////////////////////////// |
| 13 | // Public methods |
| 14 | |
| 15 | // static |
| 16 | KeepAliveRegistry* KeepAliveRegistry::GetInstance() { |
| 17 | return base::Singleton<KeepAliveRegistry>::get(); |
| 18 | } |
| 19 | |
dgn | 3563ecaf | 2016-03-09 19:28:26 | [diff] [blame] | 20 | bool KeepAliveRegistry::IsKeepingAlive() const { |
dgn | 3d351ad1 | 2016-02-26 17:36:45 | [diff] [blame] | 21 | return registered_count_ > 0; |
| 22 | } |
| 23 | |
dgn | 3563ecaf | 2016-03-09 19:28:26 | [diff] [blame] | 24 | bool KeepAliveRegistry::IsRestartAllowed() const { |
| 25 | return registered_count_ == restart_allowed_count_; |
| 26 | } |
| 27 | |
| 28 | void KeepAliveRegistry::AddObserver(KeepAliveStateObserver* observer) { |
| 29 | observers_.AddObserver(observer); |
| 30 | } |
| 31 | |
| 32 | void KeepAliveRegistry::RemoveObserver(KeepAliveStateObserver* observer) { |
| 33 | observers_.RemoveObserver(observer); |
| 34 | } |
| 35 | |
dgn | 3d351ad1 | 2016-02-26 17:36:45 | [diff] [blame] | 36 | //////////////////////////////////////////////////////////////////////////////// |
| 37 | // Private methods |
| 38 | |
dgn | 3563ecaf | 2016-03-09 19:28:26 | [diff] [blame] | 39 | KeepAliveRegistry::KeepAliveRegistry() |
| 40 | : registered_count_(0), restart_allowed_count_(0) {} |
dgn | 3d351ad1 | 2016-02-26 17:36:45 | [diff] [blame] | 41 | |
| 42 | KeepAliveRegistry::~KeepAliveRegistry() { |
dgn | 0237778 | 2016-03-12 00:58:38 | [diff] [blame^] | 43 | DLOG_IF(ERROR, registered_count_ > 0 || registered_keep_alives_.size() > 0) |
| 44 | << "KeepAliveRegistry not empty at destruction time. State: " << *this; |
dgn | 3d351ad1 | 2016-02-26 17:36:45 | [diff] [blame] | 45 | } |
| 46 | |
dgn | 3563ecaf | 2016-03-09 19:28:26 | [diff] [blame] | 47 | void KeepAliveRegistry::Register(KeepAliveOrigin origin, |
| 48 | KeepAliveRestartOption restart) { |
| 49 | bool old_keeping_alive = IsKeepingAlive(); |
| 50 | bool old_restart_allowed = IsRestartAllowed(); |
dgn | 3d351ad1 | 2016-02-26 17:36:45 | [diff] [blame] | 51 | |
| 52 | ++registered_keep_alives_[origin]; |
| 53 | ++registered_count_; |
dgn | 3563ecaf | 2016-03-09 19:28:26 | [diff] [blame] | 54 | |
| 55 | if (restart == KeepAliveRestartOption::ENABLED) |
| 56 | ++restart_allowed_count_; |
| 57 | |
| 58 | bool new_keeping_alive = IsKeepingAlive(); |
| 59 | bool new_restart_allowed = IsRestartAllowed(); |
| 60 | |
| 61 | if (new_keeping_alive != old_keeping_alive) |
| 62 | OnKeepingAliveChanged(new_keeping_alive); |
| 63 | |
| 64 | if (new_restart_allowed != old_restart_allowed) |
| 65 | OnRestartAllowedChanged(new_restart_allowed); |
| 66 | |
| 67 | DVLOG(1) << "New state of the KeepAliveRegistry: " << *this; |
dgn | 3d351ad1 | 2016-02-26 17:36:45 | [diff] [blame] | 68 | } |
| 69 | |
dgn | 3563ecaf | 2016-03-09 19:28:26 | [diff] [blame] | 70 | void KeepAliveRegistry::Unregister(KeepAliveOrigin origin, |
| 71 | KeepAliveRestartOption restart) { |
| 72 | bool old_keeping_alive = IsKeepingAlive(); |
| 73 | bool old_restart_allowed = IsRestartAllowed(); |
| 74 | |
dgn | 3d351ad1 | 2016-02-26 17:36:45 | [diff] [blame] | 75 | --registered_count_; |
| 76 | DCHECK_GE(registered_count_, 0); |
| 77 | |
| 78 | int new_count = --registered_keep_alives_[origin]; |
| 79 | DCHECK_GE(registered_keep_alives_[origin], 0); |
| 80 | if (new_count == 0) |
| 81 | registered_keep_alives_.erase(origin); |
| 82 | |
dgn | 3563ecaf | 2016-03-09 19:28:26 | [diff] [blame] | 83 | if (restart == KeepAliveRestartOption::ENABLED) |
| 84 | --restart_allowed_count_; |
| 85 | |
| 86 | bool new_keeping_alive = IsKeepingAlive(); |
| 87 | bool new_restart_allowed = IsRestartAllowed(); |
| 88 | |
| 89 | if (new_restart_allowed != old_restart_allowed) |
| 90 | OnRestartAllowedChanged(new_restart_allowed); |
| 91 | |
| 92 | if (new_keeping_alive != old_keeping_alive) |
| 93 | OnKeepingAliveChanged(new_keeping_alive); |
| 94 | |
| 95 | DVLOG(1) << "New state of the KeepAliveRegistry: " << *this; |
dgn | 3d351ad1 | 2016-02-26 17:36:45 | [diff] [blame] | 96 | } |
dgn | 3563ecaf | 2016-03-09 19:28:26 | [diff] [blame] | 97 | |
| 98 | void KeepAliveRegistry::OnKeepingAliveChanged(bool new_keeping_alive) { |
dgn | 0237778 | 2016-03-12 00:58:38 | [diff] [blame^] | 99 | // Although we should have a browser process, if there is none, |
| 100 | // there is nothing to do. |
| 101 | if (!g_browser_process) |
| 102 | return; |
| 103 | |
dgn | 3563ecaf | 2016-03-09 19:28:26 | [diff] [blame] | 104 | if (new_keeping_alive) { |
| 105 | DVLOG(1) << "KeepAliveRegistry is now keeping the browser alive."; |
dgn | 0237778 | 2016-03-12 00:58:38 | [diff] [blame^] | 106 | g_browser_process->AddRefModule(); |
dgn | 3563ecaf | 2016-03-09 19:28:26 | [diff] [blame] | 107 | } else { |
| 108 | DVLOG(1) << "KeepAliveRegistry stopped keeping the browser alive."; |
dgn | 0237778 | 2016-03-12 00:58:38 | [diff] [blame^] | 109 | g_browser_process->ReleaseModule(); |
| 110 | chrome::CloseAllBrowsersIfNeeded(); |
dgn | 3563ecaf | 2016-03-09 19:28:26 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
| 114 | void KeepAliveRegistry::OnRestartAllowedChanged(bool new_restart_allowed) { |
| 115 | DVLOG(1) << "Notifying KeepAliveStateObservers: Restart changed to: " |
| 116 | << new_restart_allowed; |
| 117 | FOR_EACH_OBSERVER(KeepAliveStateObserver, observers_, |
| 118 | OnKeepAliveRestartStateChanged(new_restart_allowed)); |
| 119 | } |
| 120 | |
| 121 | #ifndef NDEBUG |
| 122 | std::ostream& operator<<(std::ostream& out, const KeepAliveRegistry& registry) { |
dgn | 0237778 | 2016-03-12 00:58:38 | [diff] [blame^] | 123 | out << "{registered_count_=" << registry.registered_count_ |
| 124 | << ", restart_allowed_count_=" << registry.restart_allowed_count_ |
| 125 | << ", KeepAlives=["; |
dgn | 3563ecaf | 2016-03-09 19:28:26 | [diff] [blame] | 126 | for (auto counts_per_origin_it : registry.registered_keep_alives_) { |
| 127 | if (counts_per_origin_it != *registry.registered_keep_alives_.begin()) |
| 128 | out << ", "; |
| 129 | out << counts_per_origin_it.first << " (" << counts_per_origin_it.second |
| 130 | << ")"; |
| 131 | } |
| 132 | out << "]}"; |
| 133 | return out; |
| 134 | } |
| 135 | #endif // ndef NDEBUG |