Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: chrome/browser/lifetime/keep_alive_registry.cc

Issue 1708343002: Add ScopedKeepAlive to c/b/lifetime (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments on PS15 Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 ////////////////////////////////////////////////////////////////////////////////
11 // Public methods
12
13 // static
14 KeepAliveRegistry* KeepAliveRegistry::GetInstance() {
15 return base::Singleton<KeepAliveRegistry>::get();
16 }
17
18 bool KeepAliveRegistry::WillKeepAlive() const {
19 return registered_count_ > 0;
20 }
21
22 ////////////////////////////////////////////////////////////////////////////////
23 // Private methods
24
25 KeepAliveRegistry::KeepAliveRegistry() : registered_count_(0) {}
26
27 KeepAliveRegistry::~KeepAliveRegistry() {
28 DCHECK_EQ(0, registered_count_);
29 DCHECK_EQ(0u, registered_keep_alives_.size());
30 }
31
32 void KeepAliveRegistry::Register(KeepAliveOrigin origin) {
33 if (!WillKeepAlive())
34 chrome::IncrementKeepAliveCount();
35
36 auto count_per_origin_it = registered_keep_alives_.find(origin);
sky 2016/02/25 18:43:50 Is there a reason you converted away from the simp
dgn 2016/02/25 20:08:49 I wanted to be sure about the initial value in the
dgn 2016/02/26 11:51:59 Done.
37 if (count_per_origin_it == registered_keep_alives_.end())
38 registered_keep_alives_.insert(std::make_pair(origin, 1));
39 else
40 ++count_per_origin_it->second;
41
42 ++registered_count_;
43 }
44
45 void KeepAliveRegistry::Unregister(KeepAliveOrigin origin) {
46 --registered_count_;
47 DCHECK_GE(registered_count_, 0);
48
49 int new_count = --registered_keep_alives_[origin];
50 DCHECK_GE(registered_keep_alives_[origin], 0);
51 if (new_count == 0)
52 registered_keep_alives_.erase(origin);
53
54 if (!WillKeepAlive())
55 chrome::DecrementKeepAliveCount();
56 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698