blob: 73bd63acf390d84bed655192a3f355e3949edb8e [file] [log] [blame]
[email protected]979574d2013-10-30 04:45:561// Copyright 2013 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 "apps/app_keep_alive_service.h"
6
7#include "apps/app_lifetime_monitor.h"
8#include "apps/app_lifetime_monitor_factory.h"
9#include "base/message_loop/message_loop.h"
10#include "chrome/browser/lifetime/application_lifetime.h"
11#include "chrome/browser/profiles/profile.h"
12
13namespace apps {
14
15AppKeepAliveService::AppKeepAliveService(content::BrowserContext* context)
[email protected]b7ebe4de2013-11-07 01:28:4516 : context_(context), shut_down_(false) {
[email protected]979574d2013-10-30 04:45:5617 AppLifetimeMonitor* app_lifetime_monitor =
18 AppLifetimeMonitorFactory::GetForProfile(static_cast<Profile*>(context));
19 app_lifetime_monitor->AddObserver(this);
20}
21
22AppKeepAliveService::~AppKeepAliveService() {}
23
24void AppKeepAliveService::Shutdown() {
25 AppLifetimeMonitor* app_lifetime_monitor =
26 AppLifetimeMonitorFactory::GetForProfile(static_cast<Profile*>(context_));
27 app_lifetime_monitor->RemoveObserver(this);
28 OnChromeTerminating();
29}
30
31void AppKeepAliveService::OnAppStart(Profile* profile,
32 const std::string& app_id) {
[email protected]b7ebe4de2013-11-07 01:28:4533 if (profile != context_ || shut_down_)
[email protected]979574d2013-10-30 04:45:5634 return;
35
36 if (running_apps_.insert(app_id).second)
37 chrome::StartKeepAlive();
38}
39
40void AppKeepAliveService::OnAppStop(Profile* profile,
41 const std::string& app_id) {
42 if (profile != context_)
43 return;
44
45 if (running_apps_.erase(app_id))
46 chrome::EndKeepAlive();
47}
48
49void AppKeepAliveService::OnAppActivated(Profile* profile,
50 const std::string& app_id) {}
51
52void AppKeepAliveService::OnAppDeactivated(Profile* profile,
53 const std::string& app_id) {}
54
55void AppKeepAliveService::OnChromeTerminating() {
[email protected]b7ebe4de2013-11-07 01:28:4556 shut_down_ = true;
[email protected]979574d2013-10-30 04:45:5657 size_t keep_alives = running_apps_.size();
58 running_apps_.clear();
59
60 // In some tests, the message loop isn't running during shutdown and ending
61 // the last keep alive in that case CHECKs.
62 if (!base::MessageLoop::current() ||
63 base::MessageLoop::current()->is_running()) {
64 while (keep_alives--)
65 chrome::EndKeepAlive();
66 }
67}
68
69} // namespace apps