Home | History | Annotate | Download | only in browser
      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_CONTENTS_SERVICE_H_
      6 #define CHROME_BROWSER_BACKGROUND_CONTENTS_SERVICE_H_
      7 #pragma once
      8 
      9 #include <map>
     10 #include <vector>
     11 
     12 #include "base/gtest_prod_util.h"
     13 #include "base/memory/ref_counted.h"
     14 #include "base/task.h"
     15 #include "chrome/browser/profiles/profile_keyed_service.h"
     16 #include "chrome/browser/tab_contents/background_contents.h"
     17 #include "content/common/notification_observer.h"
     18 #include "content/common/notification_registrar.h"
     19 #include "content/common/window_container_type.h"
     20 #include "googleurl/src/gurl.h"
     21 #include "webkit/glue/window_open_disposition.h"
     22 
     23 class CommandLine;
     24 class DictionaryValue;
     25 class NotificationDelegate;
     26 class PrefService;
     27 class Profile;
     28 class TabContents;
     29 
     30 namespace gfx {
     31 class Rect;
     32 }
     33 
     34 struct BackgroundContentsOpenedDetails;
     35 
     36 // BackgroundContentsService is owned by the profile, and is responsible for
     37 // managing the lifetime of BackgroundContents (tracking the set of background
     38 // urls, loading them at startup, and keeping the browser process alive as long
     39 // as there are BackgroundContents loaded).
     40 //
     41 // It is also responsible for tracking the association between
     42 // BackgroundContents and their parent app, and shutting them down when the
     43 // parent app is unloaded.
     44 class BackgroundContentsService : private NotificationObserver,
     45                                   public BackgroundContents::Delegate,
     46                                   public ProfileKeyedService {
     47  public:
     48   BackgroundContentsService(Profile* profile, const CommandLine* command_line);
     49   virtual ~BackgroundContentsService();
     50 
     51   // Returns the BackgroundContents associated with the passed application id,
     52   // or NULL if none.
     53   BackgroundContents* GetAppBackgroundContents(const string16& appid);
     54 
     55   // Returns all currently opened BackgroundContents (used by the task manager).
     56   std::vector<BackgroundContents*> GetBackgroundContents() const;
     57 
     58   static void RegisterUserPrefs(PrefService* prefs);
     59 
     60   // BackgroundContents::Delegate implementation.
     61   virtual void AddTabContents(TabContents* new_contents,
     62                               WindowOpenDisposition disposition,
     63                               const gfx::Rect& initial_pos,
     64                               bool user_gesture);
     65 
     66   // Gets the parent application id for the passed BackgroundContents. Returns
     67   // an empty string if no parent application found (e.g. passed
     68   // BackgroundContents has already shut down).
     69   const string16& GetParentApplicationId(BackgroundContents* contents) const;
     70 
     71   // Creates a new BackgroundContents using the passed |site| and
     72   // the |route_id| and begins tracking the object internally so it can be
     73   // shutdown if the parent application is uninstalled.
     74   // A BACKGROUND_CONTENTS_OPENED notification will be generated with the passed
     75   // |frame_name| and |application_id| values, using the passed |profile| as the
     76   // Source..
     77   BackgroundContents* CreateBackgroundContents(SiteInstance* site,
     78                                                int route_id,
     79                                                Profile* profile,
     80                                                const string16& frame_name,
     81                                                const string16& application_id);
     82 
     83   // Load the manifest-specified background page for the specified hosted app.
     84   // If the manifest doesn't specify one, then load the BackgroundContents
     85   // registered in the pref. This is typically used to reload a crashed
     86   // background page.
     87   void LoadBackgroundContentsForExtension(Profile* profile,
     88                                           const std::string& extension_id);
     89 
     90  private:
     91   friend class BackgroundContentsServiceTest;
     92   friend class MockBackgroundContents;
     93   friend class TaskManagerBrowserTest;
     94 
     95   FRIEND_TEST_ALL_PREFIXES(BackgroundContentsServiceTest,
     96                            BackgroundContentsCreateDestroy);
     97   FRIEND_TEST_ALL_PREFIXES(BackgroundContentsServiceTest,
     98                            TestApplicationIDLinkage);
     99   FRIEND_TEST_ALL_PREFIXES(TaskManagerBrowserTest,
    100                            NoticeBGContentsChanges);
    101   FRIEND_TEST_ALL_PREFIXES(TaskManagerBrowserTest,
    102                            KillBGContents);
    103 
    104   // Registers for various notifications.
    105   void StartObserving(Profile* profile);
    106 
    107   // NotificationObserver implementation.
    108   virtual void Observe(NotificationType type,
    109                        const NotificationSource& source,
    110                        const NotificationDetails& details);
    111 
    112   // Loads all registered BackgroundContents at startup.
    113   void LoadBackgroundContentsFromPrefs(Profile* profile);
    114 
    115   // Load a BackgroundContent; the settings are read from the provided
    116   // dictionary.
    117   void LoadBackgroundContentsFromDictionary(Profile* profile,
    118                                             const std::string& extension_id,
    119                                             const DictionaryValue* contents);
    120 
    121   // Load the manifest-specified BackgroundContents for all apps for the
    122   // profile.
    123   void LoadBackgroundContentsFromManifests(Profile* profile);
    124 
    125   // Creates a single BackgroundContents associated with the specified |appid|,
    126   // creates an associated RenderView with the name specified by |frame_name|,
    127   // and navigates to the passed |url|.
    128   void LoadBackgroundContents(Profile* profile,
    129                               const GURL& url,
    130                               const string16& frame_name,
    131                               const string16& appid);
    132 
    133   // Invoked when a new BackgroundContents is opened.
    134   void BackgroundContentsOpened(BackgroundContentsOpenedDetails* details);
    135 
    136   // Invoked when a BackgroundContents object is destroyed.
    137   void BackgroundContentsShutdown(BackgroundContents* contents);
    138 
    139   // Registers the |contents->GetURL()| to be run at startup. Only happens for
    140   // the first navigation after window.open() (future calls to
    141   // RegisterBackgroundContents() for the same BackgroundContents will do
    142   // nothing).
    143   void RegisterBackgroundContents(BackgroundContents* contents);
    144 
    145   // Stops loading the passed BackgroundContents on startup.
    146   void UnregisterBackgroundContents(BackgroundContents* contents);
    147 
    148   // Unregisters and deletes the BackgroundContents associated with the
    149   // passed extension.
    150   void ShutdownAssociatedBackgroundContents(const string16& appid);
    151 
    152   // Returns true if this BackgroundContents is in the contents_list_.
    153   bool IsTracked(BackgroundContents* contents) const;
    154 
    155   // PrefService used to store list of background pages (or NULL if this is
    156   // running under an incognito profile).
    157   PrefService* prefs_;
    158   NotificationRegistrar registrar_;
    159 
    160   // Information we track about each BackgroundContents.
    161   struct BackgroundContentsInfo {
    162     // The BackgroundContents whose information we are tracking.
    163     BackgroundContents* contents;
    164     // The name of the top level frame for this BackgroundContents.
    165     string16 frame_name;
    166   };
    167 
    168   // Map associating currently loaded BackgroundContents with their parent
    169   // applications.
    170   // Key: application id
    171   // Value: BackgroundContentsInfo for the BC associated with that application
    172   typedef std::map<string16, BackgroundContentsInfo> BackgroundContentsMap;
    173   BackgroundContentsMap contents_map_;
    174 
    175   DISALLOW_COPY_AND_ASSIGN(BackgroundContentsService);
    176 };
    177 
    178 #endif  // CHROME_BROWSER_BACKGROUND_CONTENTS_SERVICE_H_
    179