• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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 #ifndef CHROME_BROWSER_BACKGROUND_PAGE_TRACKER_H_
6 #define CHROME_BROWSER_BACKGROUND_PAGE_TRACKER_H_
7 #pragma once
8 
9 #include <map>
10 #include <string>
11 
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/singleton.h"
14 #include "content/common/notification_observer.h"
15 #include "content/common/notification_registrar.h"
16 
17 class PrefService;
18 
19 ///////////////////////////////////////////////////////////////////////////////
20 // BackgroundPageTracker
21 //
22 // This class is a singleton class that monitors when new background pages are
23 // loaded, and sends out a notification.
24 //
25 class BackgroundPageTracker : public NotificationObserver {
26  public:
27   static void RegisterPrefs(PrefService* prefs);
28 
29   // Convenience routine which gets the singleton object.
30   static BackgroundPageTracker* GetInstance();
31 
32   // Returns the number of background apps/extensions currently loaded.
33   int GetBackgroundPageCount();
34 
35   // Returns the number of unacknowledged background pages (for use in badges,
36   // etc).
37   int GetUnacknowledgedBackgroundPageCount();
38 
39   // Called when the user has acknowledged that new background apps have been
40   // loaded. Future calls to GetUnacknowledgedBackgroundPageCount() will return
41   // 0 until another background page is installed.
42   void AcknowledgeBackgroundPages();
43 
44  protected:
45   // Constructor marked as protected so we can create mock subclasses.
46   BackgroundPageTracker();
47   ~BackgroundPageTracker();
48   friend struct DefaultSingletonTraits<BackgroundPageTracker>;
49   friend class BackgroundPageTrackerTest;
50   FRIEND_TEST_ALL_PREFIXES(BackgroundPageTrackerTest, OnBackgroundPageLoaded);
51   FRIEND_TEST_ALL_PREFIXES(BackgroundPageTrackerTest,
52                            AcknowledgeBackgroundPages);
53   FRIEND_TEST_ALL_PREFIXES(BackgroundPageTrackerTest,
54                            TestTrackerChangedNotifications);
55 
56   // Mockable method to get the PrefService where we store information about
57   // background pages.
58   virtual PrefService* GetPrefService();
59 
60   // Helper routine which checks to see if this object should be enabled or not.
61   // If false, the BackgroundPageTracker always acts as if there are no
62   // background pages. Virtual to allow enabling in mocks.
63   virtual bool IsEnabled();
64 
65  private:
66   // NotificationObserver implementation.
67   virtual void Observe(NotificationType type,
68                        const NotificationSource& source,
69                        const NotificationDetails& details);
70 
71   // Invoked once all extensions have been loaded, so we can update our list of
72   // tracked pages and send out the appropriate notifications. Returns true if
73   // the extension list was changed.
74   bool UpdateExtensionList();
75 
76   // Called when a background page is loaded (either because a hosted app
77   // opened a BackgroundContents or a packaged extension was loaded with an
78   // extension background page). Updates our internal list of unacknowledged
79   // background pages.
80   void OnBackgroundPageLoaded(const std::string& parent_application_id);
81 
82   // When an extension is unloaded, removes it from the list of background pages
83   // so we no longer nag the user about it (if it is still unacknowledged) and
84   // we inform the user if it is ever re-installed/enabled.
85   void OnExtensionUnloaded(const std::string& parent_application_id);
86 
87   // Sends out a notification that the list of background pages has changed.
88   void SendChangeNotification();
89 
90   NotificationRegistrar registrar_;
91 
92   DISALLOW_COPY_AND_ASSIGN(BackgroundPageTracker);
93 };
94 
95 #endif  // CHROME_BROWSER_BACKGROUND_PAGE_TRACKER_H_
96