summaryrefslogtreecommitdiffstats
path: root/apps/app_lifetime_monitor.cc
blob: 458ca76d46b05d310a349db4df9835733cda9b8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2013 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.

#include "apps/app_lifetime_monitor.h"

#include "chrome/browser/extensions/extension_host.h"
#include "chrome/browser/ui/extensions/shell_window.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/extensions/extension.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_service.h"

namespace apps {

using extensions::Extension;
using extensions::ExtensionHost;
using extensions::ShellWindowRegistry;

AppLifetimeMonitor::AppLifetimeMonitor(Profile* profile)
    : profile_(profile) {
  registrar_.Add(
      this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING,
      content::NotificationService::AllSources());
  registrar_.Add(
      this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
      content::NotificationService::AllSources());

  ShellWindowRegistry* shell_window_registry =
      ShellWindowRegistry::Factory::GetForProfile(
          profile_, false /* create */);
  if (shell_window_registry)
    shell_window_registry->AddObserver(this);
}

AppLifetimeMonitor::~AppLifetimeMonitor() {}

void AppLifetimeMonitor::AddObserver(Observer* observer) {
  observers_.AddObserver(observer);
}

void AppLifetimeMonitor::RemoveObserver(Observer* observer) {
  observers_.RemoveObserver(observer);
}

void AppLifetimeMonitor::Observe(int type,
                                const content::NotificationSource& source,
                                const content::NotificationDetails& details) {
  ExtensionHost* host = content::Details<ExtensionHost>(details).ptr();
  const Extension* extension = host->extension();
  if (!extension || !extension->is_platform_app())
    return;

  switch (type) {
    case chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING: {
      NotifyAppStart(extension->id());
      break;
    }

    case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: {
      NotifyAppStop(extension->id());
      break;
    }
  }
}

void AppLifetimeMonitor::OnShellWindowAdded(ShellWindow* shell_window) {
  if (shell_window->window_type() != ShellWindow::WINDOW_TYPE_DEFAULT)
    return;

  ShellWindowRegistry::ShellWindowList windows =
      ShellWindowRegistry::Get(shell_window->profile())->
          GetShellWindowsForApp(shell_window->extension_id());
  if (windows.size() == 1)
    NotifyAppActivated(shell_window->extension_id());
}

void AppLifetimeMonitor::OnShellWindowIconChanged(ShellWindow* shell_window) {}

void AppLifetimeMonitor::OnShellWindowRemoved(ShellWindow* shell_window) {
  ShellWindow* window =
      ShellWindowRegistry::Get(shell_window->profile())->
          GetCurrentShellWindowForApp(shell_window->extension_id());
  if (!window)
    NotifyAppDeactivated(shell_window->extension_id());
}

void AppLifetimeMonitor::Shutdown() {
  ShellWindowRegistry* shell_window_registry =
      ShellWindowRegistry::Factory::GetForProfile(
          profile_, false /* create */);
  if (shell_window_registry)
    shell_window_registry->RemoveObserver(this);
}

void AppLifetimeMonitor::NotifyAppStart(const std::string& app_id) {
  FOR_EACH_OBSERVER(Observer, observers_, OnAppStart(profile_, app_id));
}

void AppLifetimeMonitor::NotifyAppActivated(const std::string& app_id) {
  FOR_EACH_OBSERVER(Observer, observers_, OnAppActivated(profile_, app_id));
}

void AppLifetimeMonitor::NotifyAppDeactivated(const std::string& app_id) {
  FOR_EACH_OBSERVER(Observer, observers_, OnAppDeactivated(profile_, app_id));
}

void AppLifetimeMonitor::NotifyAppStop(const std::string& app_id) {
  FOR_EACH_OBSERVER(Observer, observers_, OnAppStop(profile_, app_id));
}

}  // namespace apps