blob: 0a07b76b8ed21aed5d503b494de28637333f3c4c [file] [log] [blame]
[email protected]46a32b92012-03-22 13:04:481// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]398206c2010-06-21 01:46:082// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]a07676b22011-06-17 16:36:535#ifndef CHROME_BROWSER_BACKGROUND_BACKGROUND_CONTENTS_SERVICE_H_
6#define CHROME_BROWSER_BACKGROUND_BACKGROUND_CONTENTS_SERVICE_H_
[email protected]398206c2010-06-21 01:46:087
avie4d7b6f2015-12-26 00:59:188#include <stdint.h>
9
[email protected]398206c2010-06-21 01:46:0810#include <map>
[email protected]bb95acab2011-11-16 22:22:2711#include <string>
[email protected]da58f5b12010-11-10 19:31:1212#include <vector>
[email protected]398206c2010-06-21 01:46:0813
[email protected]19118d52010-07-26 22:13:4214#include "base/gtest_prod_util.h"
avie4d7b6f2015-12-26 00:59:1815#include "base/macros.h"
[email protected]2c4fb7b2011-04-02 06:33:2916#include "base/memory/ref_counted.h"
apacible1c2329112016-09-08 16:49:4417#include "base/memory/weak_ptr.h"
[email protected]7cf82862014-07-25 18:52:5218#include "base/scoped_observer.h"
avi71d0f762014-12-15 16:08:4719#include "chrome/browser/background/background_contents.h"
[email protected]95003d522014-03-13 20:22:3120#include "components/keyed_service/core/keyed_service.h"
[email protected]6c2381d2011-10-19 02:52:5321#include "content/public/browser/notification_observer.h"
22#include "content/public/browser/notification_registrar.h"
[email protected]7cf82862014-07-25 18:52:5223#include "extensions/browser/extension_registry_observer.h"
apaciblefb3b9fa2016-08-31 04:28:1524#include "net/base/backoff_entry.h"
[email protected]f47621b2013-01-22 20:50:3325#include "ui/base/window_open_disposition.h"
[email protected]761fa4702013-07-02 15:25:1526#include "url/gurl.h"
[email protected]398206c2010-06-21 01:46:0827
[email protected]398206c2010-06-21 01:46:0828class PrefService;
29class Profile;
[email protected]bc093872010-08-16 21:49:0830
[email protected]f3a1c642011-07-12 19:15:0331namespace base {
[email protected]2f3b1cc2014-03-17 23:07:1532class CommandLine;
[email protected]f3a1c642011-07-12 19:15:0333class DictionaryValue;
Nigel Tao28338b82018-11-27 21:40:3734} // namespace base
[email protected]f3a1c642011-07-12 19:15:0335
[email protected]dd6730412013-08-14 15:03:3736namespace content {
37class SessionStorageNamespace;
38}
39
[email protected]cca7d542013-09-20 07:16:4140namespace extensions {
41class Extension;
[email protected]7cf82862014-07-25 18:52:5242class ExtensionRegistry;
Nigel Tao28338b82018-11-27 21:40:3743} // namespace extensions
[email protected]cca7d542013-09-20 07:16:4144
[email protected]bc093872010-08-16 21:49:0845namespace gfx {
46class Rect;
47}
48
Evan Stade6670c332019-06-18 20:56:1249class BackgroundContentsServiceObserver;
[email protected]398206c2010-06-21 01:46:0850struct BackgroundContentsOpenedDetails;
51
52// BackgroundContentsService is owned by the profile, and is responsible for
53// managing the lifetime of BackgroundContents (tracking the set of background
54// urls, loading them at startup, and keeping the browser process alive as long
55// as there are BackgroundContents loaded).
56//
57// It is also responsible for tracking the association between
58// BackgroundContents and their parent app, and shutting them down when the
59// parent app is unloaded.
[email protected]6c2381d2011-10-19 02:52:5360class BackgroundContentsService : private content::NotificationObserver,
[email protected]7cf82862014-07-25 18:52:5261 public extensions::ExtensionRegistryObserver,
[email protected]d2fad142011-04-15 10:18:4462 public BackgroundContents::Delegate,
[email protected]95003d522014-03-13 20:22:3163 public KeyedService {
[email protected]398206c2010-06-21 01:46:0864 public:
[email protected]2f3b1cc2014-03-17 23:07:1565 BackgroundContentsService(Profile* profile,
66 const base::CommandLine* command_line);
Daniel Chenga542fca2014-10-21 09:51:2967 ~BackgroundContentsService() override;
[email protected]398206c2010-06-21 01:46:0868
[email protected]6b9dafc2013-08-30 15:41:5969 // Allows tests to reduce the time between a force-installed app/extension
[email protected]cca7d542013-09-20 07:16:4170 // crashing and when we reload it.
71 static void SetRestartDelayForForceInstalledAppsAndExtensionsForTesting(
72 int restart_delay_in_ms);
[email protected]6b9dafc2013-08-30 15:41:5973
juyik048931af2014-10-06 17:56:3074 // Get the crash notification's delegate id for the extension.
75 static std::string GetNotificationDelegateIdForExtensionForTesting(
[email protected]84695f22014-02-18 02:29:3276 const std::string& extension_id);
77
78 // Show a popup notification balloon with a crash message for a given app/
79 // extension.
80 static void ShowBalloonForTesting(const extensions::Extension* extension,
81 Profile* profile);
82
skyostil0becb332015-04-27 17:59:3783 // Disable closing the crash notification balloon for tests.
84 static void DisableCloseBalloonForTesting(
85 bool disable_close_balloon_for_testing);
86
Evan Stade6670c332019-06-18 20:56:1287 void AddObserver(BackgroundContentsServiceObserver* observer);
88 void RemoveObserver(BackgroundContentsServiceObserver* observer);
89
[email protected]398206c2010-06-21 01:46:0890 // Returns the BackgroundContents associated with the passed application id,
91 // or NULL if none.
Evan Stade092530b2017-11-21 17:17:0592 BackgroundContents* GetAppBackgroundContents(const std::string& appid);
[email protected]398206c2010-06-21 01:46:0893
[email protected]88ea00f2012-05-04 01:17:2794 // Returns true if there's a registered BackgroundContents for this app. It
95 // is possible for this routine to return true when GetAppBackgroundContents()
96 // returns false, if the BackgroundContents closed due to the render process
97 // crashing.
Evan Stade092530b2017-11-21 17:17:0598 bool HasRegisteredBackgroundContents(const std::string& appid);
[email protected]88ea00f2012-05-04 01:17:2799
[email protected]da58f5b12010-11-10 19:31:12100 // Returns all currently opened BackgroundContents (used by the task manager).
101 std::vector<BackgroundContents*> GetBackgroundContents() const;
102
[email protected]bc093872010-08-16 21:49:08103 // BackgroundContents::Delegate implementation.
erikchenbee5c9622018-04-27 19:30:25104 void AddWebContents(std::unique_ptr<content::WebContents> new_contents,
Daniel Chenga542fca2014-10-21 09:51:29105 WindowOpenDisposition disposition,
bokan107a47f2015-02-03 23:23:39106 const gfx::Rect& initial_rect,
Daniel Chenga542fca2014-10-21 09:51:29107 bool* was_blocked) override;
Evan Stade6670c332019-06-18 20:56:12108 void OnBackgroundContentsNavigated(BackgroundContents* contents) override;
109 void OnBackgroundContentsTerminated(BackgroundContents* contents) override;
110 void OnBackgroundContentsClosed(BackgroundContents* contents) override;
111 void OnBackgroundContentsDeleted(BackgroundContents* contents) override;
[email protected]bc093872010-08-16 21:49:08112
[email protected]da58f5b12010-11-10 19:31:12113 // Gets the parent application id for the passed BackgroundContents. Returns
114 // an empty string if no parent application found (e.g. passed
115 // BackgroundContents has already shut down).
Evan Stade092530b2017-11-21 17:17:05116 const std::string& GetParentApplicationId(BackgroundContents* contents) const;
[email protected]da58f5b12010-11-10 19:31:12117
118 // Creates a new BackgroundContents using the passed |site| and
119 // the |route_id| and begins tracking the object internally so it can be
120 // shutdown if the parent application is uninstalled.
121 // A BACKGROUND_CONTENTS_OPENED notification will be generated with the passed
122 // |frame_name| and |application_id| values, using the passed |profile| as the
123 // Source..
[email protected]dd6730412013-08-14 15:03:37124 BackgroundContents* CreateBackgroundContents(
dchengbccd6b82016-03-30 16:24:19125 scoped_refptr<content::SiteInstance> site,
lukasza6f8ac622017-06-06 03:10:20126 content::RenderFrameHost* opener,
dcheng3ce04b62015-10-26 23:30:55127 int32_t route_id,
128 int32_t main_frame_route_id,
129 int32_t main_frame_widget_route_id,
[email protected]dd6730412013-08-14 15:03:37130 Profile* profile,
nasko48321ca32015-07-02 20:44:12131 const std::string& frame_name,
Evan Stade092530b2017-11-21 17:17:05132 const std::string& application_id,
[email protected]dd6730412013-08-14 15:03:37133 const std::string& partition_id,
134 content::SessionStorageNamespace* session_storage_namespace);
[email protected]da58f5b12010-11-10 19:31:12135
[email protected]41619a9a2011-04-13 20:07:32136 // Load the manifest-specified background page for the specified hosted app.
137 // If the manifest doesn't specify one, then load the BackgroundContents
138 // registered in the pref. This is typically used to reload a crashed
139 // background page.
[email protected]2c4fb7b2011-04-02 06:33:29140 void LoadBackgroundContentsForExtension(Profile* profile,
141 const std::string& extension_id);
142
[email protected]398206c2010-06-21 01:46:08143 private:
144 friend class BackgroundContentsServiceTest;
[email protected]da58f5b12010-11-10 19:31:12145 friend class MockBackgroundContents;
[email protected]2c4fb7b2011-04-02 06:33:29146
[email protected]19118d52010-07-26 22:13:42147 FRIEND_TEST_ALL_PREFIXES(BackgroundContentsServiceTest,
148 BackgroundContentsCreateDestroy);
149 FRIEND_TEST_ALL_PREFIXES(BackgroundContentsServiceTest,
150 TestApplicationIDLinkage);
[email protected]398206c2010-06-21 01:46:08151
152 // Registers for various notifications.
Evan Stade6670c332019-06-18 20:56:12153 void StartObserving();
[email protected]398206c2010-06-21 01:46:08154
[email protected]6c2381d2011-10-19 02:52:53155 // content::NotificationObserver implementation.
Daniel Chenga542fca2014-10-21 09:51:29156 void Observe(int type,
157 const content::NotificationSource& source,
158 const content::NotificationDetails& details) override;
[email protected]398206c2010-06-21 01:46:08159
limasdfa845ec32016-11-01 03:33:27160 // Called when ExtensionSystem is ready.
Evan Stade6670c332019-06-18 20:56:12161 void OnExtensionSystemReady();
limasdfa845ec32016-11-01 03:33:27162
[email protected]7cf82862014-07-25 18:52:52163 // extensions::ExtensionRegistryObserver implementation.
Daniel Chenga542fca2014-10-21 09:51:29164 void OnExtensionLoaded(content::BrowserContext* browser_context,
165 const extensions::Extension* extension) override;
limasdf0deef2042017-05-03 19:17:17166 void OnExtensionUnloaded(content::BrowserContext* browser_context,
167 const extensions::Extension* extension,
168 extensions::UnloadedExtensionReason reason) override;
Daniel Chenga542fca2014-10-21 09:51:29169 void OnExtensionUninstalled(content::BrowserContext* browser_context,
170 const extensions::Extension* extension,
171 extensions::UninstallReason reason) override;
[email protected]7cf82862014-07-25 18:52:52172
[email protected]cca7d542013-09-20 07:16:41173 // Restarts a force-installed app/extension after a crash.
[email protected]6b9dafc2013-08-30 15:41:59174 void RestartForceInstalledExtensionOnCrash(
[email protected]cca7d542013-09-20 07:16:41175 const extensions::Extension* extension,
176 Profile* profile);
[email protected]6b9dafc2013-08-30 15:41:59177
[email protected]398206c2010-06-21 01:46:08178 // Loads all registered BackgroundContents at startup.
[email protected]1e0fe2822010-06-24 21:04:19179 void LoadBackgroundContentsFromPrefs(Profile* profile);
[email protected]398206c2010-06-21 01:46:08180
[email protected]2c4fb7b2011-04-02 06:33:29181 // Load a BackgroundContent; the settings are read from the provided
182 // dictionary.
[email protected]f3a1c642011-07-12 19:15:03183 void LoadBackgroundContentsFromDictionary(
184 Profile* profile,
185 const std::string& extension_id,
186 const base::DictionaryValue* contents);
[email protected]2c4fb7b2011-04-02 06:33:29187
[email protected]41619a9a2011-04-13 20:07:32188 // Load the manifest-specified BackgroundContents for all apps for the
189 // profile.
190 void LoadBackgroundContentsFromManifests(Profile* profile);
191
[email protected]da58f5b12010-11-10 19:31:12192 // Creates a single BackgroundContents associated with the specified |appid|,
193 // creates an associated RenderView with the name specified by |frame_name|,
194 // and navigates to the passed |url|.
195 void LoadBackgroundContents(Profile* profile,
196 const GURL& url,
nasko48321ca32015-07-02 20:44:12197 const std::string& frame_name,
Evan Stade092530b2017-11-21 17:17:05198 const std::string& appid);
[email protected]398206c2010-06-21 01:46:08199
200 // Invoked when a new BackgroundContents is opened.
juyik048931af2014-10-06 17:56:30201 void BackgroundContentsOpened(BackgroundContentsOpenedDetails* details,
202 Profile* profile);
[email protected]398206c2010-06-21 01:46:08203
[email protected]398206c2010-06-21 01:46:08204 // Registers the |contents->GetURL()| to be run at startup. Only happens for
205 // the first navigation after window.open() (future calls to
206 // RegisterBackgroundContents() for the same BackgroundContents will do
207 // nothing).
208 void RegisterBackgroundContents(BackgroundContents* contents);
209
210 // Stops loading the passed BackgroundContents on startup.
211 void UnregisterBackgroundContents(BackgroundContents* contents);
212
213 // Unregisters and deletes the BackgroundContents associated with the
214 // passed extension.
Evan Stade092530b2017-11-21 17:17:05215 void ShutdownAssociatedBackgroundContents(const std::string& appid);
[email protected]398206c2010-06-21 01:46:08216
217 // Returns true if this BackgroundContents is in the contents_list_.
218 bool IsTracked(BackgroundContents* contents) const;
219
[email protected]88ea00f2012-05-04 01:17:27220 // Sends out a notification when our association of background contents with
221 // apps may have changed (used by BackgroundApplicationListModel to update the
222 // set of background apps as new background contents are opened/closed).
Evan Stade6670c332019-06-18 20:56:12223 void SendChangeNotification();
[email protected]88ea00f2012-05-04 01:17:27224
apacible1c2329112016-09-08 16:49:44225 // Checks whether there has been additional |extension_id| failures. If not,
226 // delete the BackoffEntry corresponding to |extension_id|, if exists.
227 void MaybeClearBackoffEntry(const std::string extension_id,
228 int expected_failure_count);
229
Evan Stade6670c332019-06-18 20:56:12230 void HandleExtensionCrashed(const extensions::Extension* extension);
231
[email protected]6b9dafc2013-08-30 15:41:59232 // Delay (in ms) before restarting a force-installed extension that crashed.
233 static int restart_delay_in_ms_;
234
Evan Stade6670c332019-06-18 20:56:12235 Profile* profile_;
236
237 base::ObserverList<BackgroundContentsServiceObserver> observers_;
238
[email protected]398206c2010-06-21 01:46:08239 // PrefService used to store list of background pages (or NULL if this is
[email protected]2c910b72011-03-08 21:16:32240 // running under an incognito profile).
Evan Stade6670c332019-06-18 20:56:12241 PrefService* prefs_ = nullptr;
[email protected]6c2381d2011-10-19 02:52:53242 content::NotificationRegistrar registrar_;
[email protected]398206c2010-06-21 01:46:08243
244 // Information we track about each BackgroundContents.
245 struct BackgroundContentsInfo {
246 // The BackgroundContents whose information we are tracking.
247 BackgroundContents* contents;
248 // The name of the top level frame for this BackgroundContents.
nasko48321ca32015-07-02 20:44:12249 std::string frame_name;
[email protected]398206c2010-06-21 01:46:08250 };
251
252 // Map associating currently loaded BackgroundContents with their parent
253 // applications.
254 // Key: application id
255 // Value: BackgroundContentsInfo for the BC associated with that application
Evan Stade092530b2017-11-21 17:17:05256 typedef std::map<std::string, BackgroundContentsInfo> BackgroundContentsMap;
[email protected]398206c2010-06-21 01:46:08257 BackgroundContentsMap contents_map_;
258
apaciblefb3b9fa2016-08-31 04:28:15259 // Map associating component extensions that have attempted to reload with a
260 // BackoffEntry keeping track of retry timing.
261 typedef std::map<extensions::ExtensionId, std::unique_ptr<net::BackoffEntry>>
apacible1c2329112016-09-08 16:49:44262 ComponentExtensionBackoffEntryMap;
263 ComponentExtensionBackoffEntryMap component_backoff_map_;
apaciblefb3b9fa2016-08-31 04:28:15264
[email protected]7cf82862014-07-25 18:52:52265 ScopedObserver<extensions::ExtensionRegistry,
266 extensions::ExtensionRegistryObserver>
Evan Stade6670c332019-06-18 20:56:12267 extension_registry_observer_{this};
[email protected]7cf82862014-07-25 18:52:52268
Evan Stade6670c332019-06-18 20:56:12269 base::WeakPtrFactory<BackgroundContentsService> weak_ptr_factory_{this};
apacible1c2329112016-09-08 16:49:44270
[email protected]398206c2010-06-21 01:46:08271 DISALLOW_COPY_AND_ASSIGN(BackgroundContentsService);
272};
273
[email protected]a07676b22011-06-17 16:36:53274#endif // CHROME_BROWSER_BACKGROUND_BACKGROUND_CONTENTS_SERVICE_H_