summaryrefslogtreecommitdiffstats
path: root/chrome/browser/background_page_tracker.h
blob: e00f5eb17582d147d5dbe301f8b4b924568cdd26 (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
// Copyright (c) 2011 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.

#ifndef CHROME_BROWSER_BACKGROUND_PAGE_TRACKER_H_
#define CHROME_BROWSER_BACKGROUND_PAGE_TRACKER_H_
#pragma once

#include <map>
#include <string>

#include "base/gtest_prod_util.h"
#include "base/memory/singleton.h"
#include "content/common/notification_observer.h"
#include "content/common/notification_registrar.h"

class PrefService;

///////////////////////////////////////////////////////////////////////////////
// BackgroundPageTracker
//
// This class is a singleton class that monitors when new background pages are
// loaded, and sends out a notification.
//
class BackgroundPageTracker : public NotificationObserver {
 public:
  static void RegisterPrefs(PrefService* prefs);

  // Convenience routine which gets the singleton object.
  static BackgroundPageTracker* GetInstance();

  // Returns the number of background apps/extensions currently loaded.
  int GetBackgroundPageCount();

  // Returns the number of unacknowledged background pages (for use in badges,
  // etc).
  int GetUnacknowledgedBackgroundPageCount();

  // Called when the user has acknowledged that new background apps have been
  // loaded. Future calls to GetUnacknowledgedBackgroundPageCount() will return
  // 0 until another background page is installed.
  void AcknowledgeBackgroundPages();

 protected:
  // Constructor marked as protected so we can create mock subclasses.
  BackgroundPageTracker();
  ~BackgroundPageTracker();
  friend struct DefaultSingletonTraits<BackgroundPageTracker>;
  friend class BackgroundPageTrackerTest;
  FRIEND_TEST_ALL_PREFIXES(BackgroundPageTrackerTest, OnBackgroundPageLoaded);
  FRIEND_TEST_ALL_PREFIXES(BackgroundPageTrackerTest,
                           AcknowledgeBackgroundPages);
  FRIEND_TEST_ALL_PREFIXES(BackgroundPageTrackerTest,
                           TestTrackerChangedNotifications);

  // Mockable method to get the PrefService where we store information about
  // background pages.
  virtual PrefService* GetPrefService();

  // Helper routine which checks to see if this object should be enabled or not.
  // If false, the BackgroundPageTracker always acts as if there are no
  // background pages. Virtual to allow enabling in mocks.
  virtual bool IsEnabled();

 private:
  // NotificationObserver implementation.
  virtual void Observe(NotificationType type,
                       const NotificationSource& source,
                       const NotificationDetails& details);

  // Invoked once all extensions have been loaded, so we can update our list of
  // tracked pages and send out the appropriate notifications. Returns true if
  // the extension list was changed.
  bool UpdateExtensionList();

  // Called when a background page is loaded (either because a hosted app
  // opened a BackgroundContents or a packaged extension was loaded with an
  // extension background page). Updates our internal list of unacknowledged
  // background pages.
  void OnBackgroundPageLoaded(const std::string& parent_application_id);

  // When an extension is unloaded, removes it from the list of background pages
  // so we no longer nag the user about it (if it is still unacknowledged) and
  // we inform the user if it is ever re-installed/enabled.
  void OnExtensionUnloaded(const std::string& parent_application_id);

  // Sends out a notification that the list of background pages has changed.
  void SendChangeNotification();

  NotificationRegistrar registrar_;

  DISALLOW_COPY_AND_ASSIGN(BackgroundPageTracker);
};

#endif  // CHROME_BROWSER_BACKGROUND_PAGE_TRACKER_H_