[email protected] | 46a32b9 | 2012-03-22 13:04:48 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | a07676b2 | 2011-06-17 16:36:53 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_BACKGROUND_BACKGROUND_CONTENTS_SERVICE_H_ |
| 6 | #define CHROME_BROWSER_BACKGROUND_BACKGROUND_CONTENTS_SERVICE_H_ |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 7 | |
| 8 | #include <map> |
[email protected] | bb95acab | 2011-11-16 22:22:27 | [diff] [blame] | 9 | #include <string> |
[email protected] | da58f5b1 | 2010-11-10 19:31:12 | [diff] [blame] | 10 | #include <vector> |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 11 | |
[email protected] | 19118d5 | 2010-07-26 22:13:42 | [diff] [blame] | 12 | #include "base/gtest_prod_util.h" |
[email protected] | 2c4fb7b | 2011-04-02 06:33:29 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
[email protected] | 7cf8286 | 2014-07-25 18:52:52 | [diff] [blame^] | 14 | #include "base/scoped_observer.h" |
[email protected] | bc09387 | 2010-08-16 21:49:08 | [diff] [blame] | 15 | #include "chrome/browser/tab_contents/background_contents.h" |
[email protected] | 95003d52 | 2014-03-13 20:22:31 | [diff] [blame] | 16 | #include "components/keyed_service/core/keyed_service.h" |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 17 | #include "content/public/browser/notification_observer.h" |
| 18 | #include "content/public/browser/notification_registrar.h" |
[email protected] | 87f3c08 | 2011-10-19 18:07:44 | [diff] [blame] | 19 | #include "content/public/common/window_container_type.h" |
[email protected] | 7cf8286 | 2014-07-25 18:52:52 | [diff] [blame^] | 20 | #include "extensions/browser/extension_registry_observer.h" |
[email protected] | f47621b | 2013-01-22 20:50:33 | [diff] [blame] | 21 | #include "ui/base/window_open_disposition.h" |
[email protected] | 761fa470 | 2013-07-02 15:25:15 | [diff] [blame] | 22 | #include "url/gurl.h" |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 23 | |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 24 | class PrefService; |
| 25 | class Profile; |
[email protected] | bc09387 | 2010-08-16 21:49:08 | [diff] [blame] | 26 | |
[email protected] | f3a1c64 | 2011-07-12 19:15:03 | [diff] [blame] | 27 | namespace base { |
[email protected] | 2f3b1cc | 2014-03-17 23:07:15 | [diff] [blame] | 28 | class CommandLine; |
[email protected] | f3a1c64 | 2011-07-12 19:15:03 | [diff] [blame] | 29 | class DictionaryValue; |
| 30 | } |
| 31 | |
[email protected] | dd673041 | 2013-08-14 15:03:37 | [diff] [blame] | 32 | namespace content { |
| 33 | class SessionStorageNamespace; |
| 34 | } |
| 35 | |
[email protected] | cca7d54 | 2013-09-20 07:16:41 | [diff] [blame] | 36 | namespace extensions { |
| 37 | class Extension; |
[email protected] | 7cf8286 | 2014-07-25 18:52:52 | [diff] [blame^] | 38 | class ExtensionRegistry; |
[email protected] | cca7d54 | 2013-09-20 07:16:41 | [diff] [blame] | 39 | } |
| 40 | |
[email protected] | bc09387 | 2010-08-16 21:49:08 | [diff] [blame] | 41 | namespace gfx { |
| 42 | class Rect; |
| 43 | } |
| 44 | |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 45 | struct BackgroundContentsOpenedDetails; |
| 46 | |
| 47 | // BackgroundContentsService is owned by the profile, and is responsible for |
| 48 | // managing the lifetime of BackgroundContents (tracking the set of background |
| 49 | // urls, loading them at startup, and keeping the browser process alive as long |
| 50 | // as there are BackgroundContents loaded). |
| 51 | // |
| 52 | // It is also responsible for tracking the association between |
| 53 | // BackgroundContents and their parent app, and shutting them down when the |
| 54 | // parent app is unloaded. |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 55 | class BackgroundContentsService : private content::NotificationObserver, |
[email protected] | 7cf8286 | 2014-07-25 18:52:52 | [diff] [blame^] | 56 | public extensions::ExtensionRegistryObserver, |
[email protected] | d2fad14 | 2011-04-15 10:18:44 | [diff] [blame] | 57 | public BackgroundContents::Delegate, |
[email protected] | 95003d52 | 2014-03-13 20:22:31 | [diff] [blame] | 58 | public KeyedService { |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 59 | public: |
[email protected] | 2f3b1cc | 2014-03-17 23:07:15 | [diff] [blame] | 60 | BackgroundContentsService(Profile* profile, |
| 61 | const base::CommandLine* command_line); |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 62 | virtual ~BackgroundContentsService(); |
| 63 | |
[email protected] | 6b9dafc | 2013-08-30 15:41:59 | [diff] [blame] | 64 | // Allows tests to reduce the time between a force-installed app/extension |
[email protected] | cca7d54 | 2013-09-20 07:16:41 | [diff] [blame] | 65 | // crashing and when we reload it. |
| 66 | static void SetRestartDelayForForceInstalledAppsAndExtensionsForTesting( |
| 67 | int restart_delay_in_ms); |
[email protected] | 6b9dafc | 2013-08-30 15:41:59 | [diff] [blame] | 68 | |
[email protected] | 84695f2 | 2014-02-18 02:29:32 | [diff] [blame] | 69 | // Get the crash notification's id for the extension. |
| 70 | static std::string GetNotificationIdForExtensionForTesting( |
| 71 | const std::string& extension_id); |
| 72 | |
| 73 | // Show a popup notification balloon with a crash message for a given app/ |
| 74 | // extension. |
| 75 | static void ShowBalloonForTesting(const extensions::Extension* extension, |
| 76 | Profile* profile); |
| 77 | |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 78 | // Returns the BackgroundContents associated with the passed application id, |
| 79 | // or NULL if none. |
[email protected] | 9692015 | 2013-12-04 21:00:16 | [diff] [blame] | 80 | BackgroundContents* GetAppBackgroundContents(const base::string16& appid); |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 81 | |
[email protected] | 88ea00f | 2012-05-04 01:17:27 | [diff] [blame] | 82 | // Returns true if there's a registered BackgroundContents for this app. It |
| 83 | // is possible for this routine to return true when GetAppBackgroundContents() |
| 84 | // returns false, if the BackgroundContents closed due to the render process |
| 85 | // crashing. |
[email protected] | 9692015 | 2013-12-04 21:00:16 | [diff] [blame] | 86 | bool HasRegisteredBackgroundContents(const base::string16& appid); |
[email protected] | 88ea00f | 2012-05-04 01:17:27 | [diff] [blame] | 87 | |
[email protected] | da58f5b1 | 2010-11-10 19:31:12 | [diff] [blame] | 88 | // Returns all currently opened BackgroundContents (used by the task manager). |
| 89 | std::vector<BackgroundContents*> GetBackgroundContents() const; |
| 90 | |
[email protected] | bc09387 | 2010-08-16 21:49:08 | [diff] [blame] | 91 | // BackgroundContents::Delegate implementation. |
[email protected] | 2a6bc3e | 2011-12-28 23:51:33 | [diff] [blame] | 92 | virtual void AddWebContents(content::WebContents* new_contents, |
[email protected] | bc09387 | 2010-08-16 21:49:08 | [diff] [blame] | 93 | WindowOpenDisposition disposition, |
| 94 | const gfx::Rect& initial_pos, |
[email protected] | eda238a1 | 2012-09-07 23:44:00 | [diff] [blame] | 95 | bool user_gesture, |
| 96 | bool* was_blocked) OVERRIDE; |
[email protected] | bc09387 | 2010-08-16 21:49:08 | [diff] [blame] | 97 | |
[email protected] | da58f5b1 | 2010-11-10 19:31:12 | [diff] [blame] | 98 | // Gets the parent application id for the passed BackgroundContents. Returns |
| 99 | // an empty string if no parent application found (e.g. passed |
| 100 | // BackgroundContents has already shut down). |
[email protected] | 9692015 | 2013-12-04 21:00:16 | [diff] [blame] | 101 | const base::string16& GetParentApplicationId(BackgroundContents* contents) const; |
[email protected] | da58f5b1 | 2010-11-10 19:31:12 | [diff] [blame] | 102 | |
| 103 | // Creates a new BackgroundContents using the passed |site| and |
| 104 | // the |route_id| and begins tracking the object internally so it can be |
| 105 | // shutdown if the parent application is uninstalled. |
| 106 | // A BACKGROUND_CONTENTS_OPENED notification will be generated with the passed |
| 107 | // |frame_name| and |application_id| values, using the passed |profile| as the |
| 108 | // Source.. |
[email protected] | dd673041 | 2013-08-14 15:03:37 | [diff] [blame] | 109 | BackgroundContents* CreateBackgroundContents( |
| 110 | content::SiteInstance* site, |
| 111 | int route_id, |
| 112 | Profile* profile, |
[email protected] | 9692015 | 2013-12-04 21:00:16 | [diff] [blame] | 113 | const base::string16& frame_name, |
| 114 | const base::string16& application_id, |
[email protected] | dd673041 | 2013-08-14 15:03:37 | [diff] [blame] | 115 | const std::string& partition_id, |
| 116 | content::SessionStorageNamespace* session_storage_namespace); |
[email protected] | da58f5b1 | 2010-11-10 19:31:12 | [diff] [blame] | 117 | |
[email protected] | 41619a9a | 2011-04-13 20:07:32 | [diff] [blame] | 118 | // Load the manifest-specified background page for the specified hosted app. |
| 119 | // If the manifest doesn't specify one, then load the BackgroundContents |
| 120 | // registered in the pref. This is typically used to reload a crashed |
| 121 | // background page. |
[email protected] | 2c4fb7b | 2011-04-02 06:33:29 | [diff] [blame] | 122 | void LoadBackgroundContentsForExtension(Profile* profile, |
| 123 | const std::string& extension_id); |
| 124 | |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 125 | private: |
| 126 | friend class BackgroundContentsServiceTest; |
[email protected] | da58f5b1 | 2010-11-10 19:31:12 | [diff] [blame] | 127 | friend class MockBackgroundContents; |
[email protected] | c4a9d85 | 2013-09-26 17:10:36 | [diff] [blame] | 128 | friend class TaskManagerNoShowBrowserTest; |
[email protected] | 2c4fb7b | 2011-04-02 06:33:29 | [diff] [blame] | 129 | |
[email protected] | 19118d5 | 2010-07-26 22:13:42 | [diff] [blame] | 130 | FRIEND_TEST_ALL_PREFIXES(BackgroundContentsServiceTest, |
| 131 | BackgroundContentsCreateDestroy); |
| 132 | FRIEND_TEST_ALL_PREFIXES(BackgroundContentsServiceTest, |
| 133 | TestApplicationIDLinkage); |
[email protected] | c4a9d85 | 2013-09-26 17:10:36 | [diff] [blame] | 134 | FRIEND_TEST_ALL_PREFIXES(TaskManagerNoShowBrowserTest, |
[email protected] | da58f5b1 | 2010-11-10 19:31:12 | [diff] [blame] | 135 | NoticeBGContentsChanges); |
[email protected] | c4a9d85 | 2013-09-26 17:10:36 | [diff] [blame] | 136 | FRIEND_TEST_ALL_PREFIXES(TaskManagerNoShowBrowserTest, |
[email protected] | 235b69a | 2011-01-18 19:57:53 | [diff] [blame] | 137 | KillBGContents); |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 138 | |
| 139 | // Registers for various notifications. |
| 140 | void StartObserving(Profile* profile); |
| 141 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 142 | // content::NotificationObserver implementation. |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 143 | virtual void Observe(int type, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 144 | const content::NotificationSource& source, |
[email protected] | 0d5c08e | 2011-11-21 16:51:06 | [diff] [blame] | 145 | const content::NotificationDetails& details) OVERRIDE; |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 146 | |
[email protected] | 7cf8286 | 2014-07-25 18:52:52 | [diff] [blame^] | 147 | // extensions::ExtensionRegistryObserver implementation. |
| 148 | virtual void OnExtensionLoaded( |
| 149 | content::BrowserContext* browser_context, |
| 150 | const extensions::Extension* extension) OVERRIDE; |
| 151 | virtual void OnExtensionUnloaded( |
| 152 | content::BrowserContext* browser_context, |
| 153 | const extensions::Extension* extension, |
| 154 | extensions::UnloadedExtensionInfo::Reason reason) OVERRIDE; |
| 155 | virtual void OnExtensionUninstalled( |
| 156 | content::BrowserContext* browser_context, |
| 157 | const extensions::Extension* extension, |
| 158 | extensions::UninstallReason reason) OVERRIDE; |
| 159 | |
[email protected] | cca7d54 | 2013-09-20 07:16:41 | [diff] [blame] | 160 | // Restarts a force-installed app/extension after a crash. |
[email protected] | 6b9dafc | 2013-08-30 15:41:59 | [diff] [blame] | 161 | void RestartForceInstalledExtensionOnCrash( |
[email protected] | cca7d54 | 2013-09-20 07:16:41 | [diff] [blame] | 162 | const extensions::Extension* extension, |
| 163 | Profile* profile); |
[email protected] | 6b9dafc | 2013-08-30 15:41:59 | [diff] [blame] | 164 | |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 165 | // Loads all registered BackgroundContents at startup. |
[email protected] | 1e0fe282 | 2010-06-24 21:04:19 | [diff] [blame] | 166 | void LoadBackgroundContentsFromPrefs(Profile* profile); |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 167 | |
[email protected] | 2c4fb7b | 2011-04-02 06:33:29 | [diff] [blame] | 168 | // Load a BackgroundContent; the settings are read from the provided |
| 169 | // dictionary. |
[email protected] | f3a1c64 | 2011-07-12 19:15:03 | [diff] [blame] | 170 | void LoadBackgroundContentsFromDictionary( |
| 171 | Profile* profile, |
| 172 | const std::string& extension_id, |
| 173 | const base::DictionaryValue* contents); |
[email protected] | 2c4fb7b | 2011-04-02 06:33:29 | [diff] [blame] | 174 | |
[email protected] | 41619a9a | 2011-04-13 20:07:32 | [diff] [blame] | 175 | // Load the manifest-specified BackgroundContents for all apps for the |
| 176 | // profile. |
| 177 | void LoadBackgroundContentsFromManifests(Profile* profile); |
| 178 | |
[email protected] | da58f5b1 | 2010-11-10 19:31:12 | [diff] [blame] | 179 | // Creates a single BackgroundContents associated with the specified |appid|, |
| 180 | // creates an associated RenderView with the name specified by |frame_name|, |
| 181 | // and navigates to the passed |url|. |
| 182 | void LoadBackgroundContents(Profile* profile, |
| 183 | const GURL& url, |
[email protected] | 9692015 | 2013-12-04 21:00:16 | [diff] [blame] | 184 | const base::string16& frame_name, |
| 185 | const base::string16& appid); |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 186 | |
| 187 | // Invoked when a new BackgroundContents is opened. |
| 188 | void BackgroundContentsOpened(BackgroundContentsOpenedDetails* details); |
| 189 | |
| 190 | // Invoked when a BackgroundContents object is destroyed. |
| 191 | void BackgroundContentsShutdown(BackgroundContents* contents); |
| 192 | |
| 193 | // Registers the |contents->GetURL()| to be run at startup. Only happens for |
| 194 | // the first navigation after window.open() (future calls to |
| 195 | // RegisterBackgroundContents() for the same BackgroundContents will do |
| 196 | // nothing). |
| 197 | void RegisterBackgroundContents(BackgroundContents* contents); |
| 198 | |
| 199 | // Stops loading the passed BackgroundContents on startup. |
| 200 | void UnregisterBackgroundContents(BackgroundContents* contents); |
| 201 | |
| 202 | // Unregisters and deletes the BackgroundContents associated with the |
| 203 | // passed extension. |
[email protected] | 9692015 | 2013-12-04 21:00:16 | [diff] [blame] | 204 | void ShutdownAssociatedBackgroundContents(const base::string16& appid); |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 205 | |
| 206 | // Returns true if this BackgroundContents is in the contents_list_. |
| 207 | bool IsTracked(BackgroundContents* contents) const; |
| 208 | |
[email protected] | 88ea00f | 2012-05-04 01:17:27 | [diff] [blame] | 209 | // Sends out a notification when our association of background contents with |
| 210 | // apps may have changed (used by BackgroundApplicationListModel to update the |
| 211 | // set of background apps as new background contents are opened/closed). |
| 212 | void SendChangeNotification(Profile* profile); |
| 213 | |
[email protected] | 6b9dafc | 2013-08-30 15:41:59 | [diff] [blame] | 214 | // Delay (in ms) before restarting a force-installed extension that crashed. |
| 215 | static int restart_delay_in_ms_; |
| 216 | |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 217 | // PrefService used to store list of background pages (or NULL if this is |
[email protected] | 2c910b7 | 2011-03-08 21:16:32 | [diff] [blame] | 218 | // running under an incognito profile). |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 219 | PrefService* prefs_; |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 220 | content::NotificationRegistrar registrar_; |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 221 | |
| 222 | // Information we track about each BackgroundContents. |
| 223 | struct BackgroundContentsInfo { |
| 224 | // The BackgroundContents whose information we are tracking. |
| 225 | BackgroundContents* contents; |
| 226 | // The name of the top level frame for this BackgroundContents. |
[email protected] | 9692015 | 2013-12-04 21:00:16 | [diff] [blame] | 227 | base::string16 frame_name; |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 228 | }; |
| 229 | |
| 230 | // Map associating currently loaded BackgroundContents with their parent |
| 231 | // applications. |
| 232 | // Key: application id |
| 233 | // Value: BackgroundContentsInfo for the BC associated with that application |
[email protected] | d2065e06 | 2013-12-12 23:49:52 | [diff] [blame] | 234 | typedef std::map<base::string16, BackgroundContentsInfo> |
| 235 | BackgroundContentsMap; |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 236 | BackgroundContentsMap contents_map_; |
| 237 | |
[email protected] | 7cf8286 | 2014-07-25 18:52:52 | [diff] [blame^] | 238 | ScopedObserver<extensions::ExtensionRegistry, |
| 239 | extensions::ExtensionRegistryObserver> |
| 240 | extension_registry_observer_; |
| 241 | |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 242 | DISALLOW_COPY_AND_ASSIGN(BackgroundContentsService); |
| 243 | }; |
| 244 | |
[email protected] | a07676b2 | 2011-06-17 16:36:53 | [diff] [blame] | 245 | #endif // CHROME_BROWSER_BACKGROUND_BACKGROUND_CONTENTS_SERVICE_H_ |