blob: d9f859de0ea0e5c0a1867f6e92af89f1e3faee3d [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
8#include <map>
[email protected]bb95acab2011-11-16 22:22:279#include <string>
[email protected]da58f5b12010-11-10 19:31:1210#include <vector>
[email protected]398206c2010-06-21 01:46:0811
[email protected]19118d52010-07-26 22:13:4212#include "base/gtest_prod_util.h"
[email protected]2c4fb7b2011-04-02 06:33:2913#include "base/memory/ref_counted.h"
[email protected]7cf82862014-07-25 18:52:5214#include "base/scoped_observer.h"
[email protected]bc093872010-08-16 21:49:0815#include "chrome/browser/tab_contents/background_contents.h"
[email protected]95003d522014-03-13 20:22:3116#include "components/keyed_service/core/keyed_service.h"
[email protected]6c2381d2011-10-19 02:52:5317#include "content/public/browser/notification_observer.h"
18#include "content/public/browser/notification_registrar.h"
[email protected]87f3c082011-10-19 18:07:4419#include "content/public/common/window_container_type.h"
[email protected]7cf82862014-07-25 18:52:5220#include "extensions/browser/extension_registry_observer.h"
[email protected]f47621b2013-01-22 20:50:3321#include "ui/base/window_open_disposition.h"
[email protected]761fa4702013-07-02 15:25:1522#include "url/gurl.h"
[email protected]398206c2010-06-21 01:46:0823
[email protected]398206c2010-06-21 01:46:0824class PrefService;
25class Profile;
[email protected]bc093872010-08-16 21:49:0826
[email protected]f3a1c642011-07-12 19:15:0327namespace base {
[email protected]2f3b1cc2014-03-17 23:07:1528class CommandLine;
[email protected]f3a1c642011-07-12 19:15:0329class DictionaryValue;
30}
31
[email protected]dd6730412013-08-14 15:03:3732namespace content {
33class SessionStorageNamespace;
34}
35
[email protected]cca7d542013-09-20 07:16:4136namespace extensions {
37class Extension;
[email protected]7cf82862014-07-25 18:52:5238class ExtensionRegistry;
[email protected]cca7d542013-09-20 07:16:4139}
40
[email protected]bc093872010-08-16 21:49:0841namespace gfx {
42class Rect;
43}
44
[email protected]398206c2010-06-21 01:46:0845struct 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]6c2381d2011-10-19 02:52:5355class BackgroundContentsService : private content::NotificationObserver,
[email protected]7cf82862014-07-25 18:52:5256 public extensions::ExtensionRegistryObserver,
[email protected]d2fad142011-04-15 10:18:4457 public BackgroundContents::Delegate,
[email protected]95003d522014-03-13 20:22:3158 public KeyedService {
[email protected]398206c2010-06-21 01:46:0859 public:
[email protected]2f3b1cc2014-03-17 23:07:1560 BackgroundContentsService(Profile* profile,
61 const base::CommandLine* command_line);
[email protected]398206c2010-06-21 01:46:0862 virtual ~BackgroundContentsService();
63
[email protected]6b9dafc2013-08-30 15:41:5964 // Allows tests to reduce the time between a force-installed app/extension
[email protected]cca7d542013-09-20 07:16:4165 // crashing and when we reload it.
66 static void SetRestartDelayForForceInstalledAppsAndExtensionsForTesting(
67 int restart_delay_in_ms);
[email protected]6b9dafc2013-08-30 15:41:5968
[email protected]84695f22014-02-18 02:29:3269 // 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]398206c2010-06-21 01:46:0878 // Returns the BackgroundContents associated with the passed application id,
79 // or NULL if none.
[email protected]96920152013-12-04 21:00:1680 BackgroundContents* GetAppBackgroundContents(const base::string16& appid);
[email protected]398206c2010-06-21 01:46:0881
[email protected]88ea00f2012-05-04 01:17:2782 // 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]96920152013-12-04 21:00:1686 bool HasRegisteredBackgroundContents(const base::string16& appid);
[email protected]88ea00f2012-05-04 01:17:2787
[email protected]da58f5b12010-11-10 19:31:1288 // Returns all currently opened BackgroundContents (used by the task manager).
89 std::vector<BackgroundContents*> GetBackgroundContents() const;
90
[email protected]bc093872010-08-16 21:49:0891 // BackgroundContents::Delegate implementation.
[email protected]2a6bc3e2011-12-28 23:51:3392 virtual void AddWebContents(content::WebContents* new_contents,
[email protected]bc093872010-08-16 21:49:0893 WindowOpenDisposition disposition,
94 const gfx::Rect& initial_pos,
[email protected]eda238a12012-09-07 23:44:0095 bool user_gesture,
96 bool* was_blocked) OVERRIDE;
[email protected]bc093872010-08-16 21:49:0897
[email protected]da58f5b12010-11-10 19:31:1298 // 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]96920152013-12-04 21:00:16101 const base::string16& GetParentApplicationId(BackgroundContents* contents) const;
[email protected]da58f5b12010-11-10 19:31:12102
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]dd6730412013-08-14 15:03:37109 BackgroundContents* CreateBackgroundContents(
110 content::SiteInstance* site,
111 int route_id,
112 Profile* profile,
[email protected]96920152013-12-04 21:00:16113 const base::string16& frame_name,
114 const base::string16& application_id,
[email protected]dd6730412013-08-14 15:03:37115 const std::string& partition_id,
116 content::SessionStorageNamespace* session_storage_namespace);
[email protected]da58f5b12010-11-10 19:31:12117
[email protected]41619a9a2011-04-13 20:07:32118 // 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]2c4fb7b2011-04-02 06:33:29122 void LoadBackgroundContentsForExtension(Profile* profile,
123 const std::string& extension_id);
124
[email protected]398206c2010-06-21 01:46:08125 private:
126 friend class BackgroundContentsServiceTest;
[email protected]da58f5b12010-11-10 19:31:12127 friend class MockBackgroundContents;
[email protected]c4a9d852013-09-26 17:10:36128 friend class TaskManagerNoShowBrowserTest;
[email protected]2c4fb7b2011-04-02 06:33:29129
[email protected]19118d52010-07-26 22:13:42130 FRIEND_TEST_ALL_PREFIXES(BackgroundContentsServiceTest,
131 BackgroundContentsCreateDestroy);
132 FRIEND_TEST_ALL_PREFIXES(BackgroundContentsServiceTest,
133 TestApplicationIDLinkage);
[email protected]c4a9d852013-09-26 17:10:36134 FRIEND_TEST_ALL_PREFIXES(TaskManagerNoShowBrowserTest,
[email protected]da58f5b12010-11-10 19:31:12135 NoticeBGContentsChanges);
[email protected]c4a9d852013-09-26 17:10:36136 FRIEND_TEST_ALL_PREFIXES(TaskManagerNoShowBrowserTest,
[email protected]235b69a2011-01-18 19:57:53137 KillBGContents);
[email protected]398206c2010-06-21 01:46:08138
139 // Registers for various notifications.
140 void StartObserving(Profile* profile);
141
[email protected]6c2381d2011-10-19 02:52:53142 // content::NotificationObserver implementation.
[email protected]432115822011-07-10 15:52:27143 virtual void Observe(int type,
[email protected]6c2381d2011-10-19 02:52:53144 const content::NotificationSource& source,
[email protected]0d5c08e2011-11-21 16:51:06145 const content::NotificationDetails& details) OVERRIDE;
[email protected]398206c2010-06-21 01:46:08146
[email protected]7cf82862014-07-25 18:52:52147 // 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]cca7d542013-09-20 07:16:41160 // Restarts a force-installed app/extension after a crash.
[email protected]6b9dafc2013-08-30 15:41:59161 void RestartForceInstalledExtensionOnCrash(
[email protected]cca7d542013-09-20 07:16:41162 const extensions::Extension* extension,
163 Profile* profile);
[email protected]6b9dafc2013-08-30 15:41:59164
[email protected]398206c2010-06-21 01:46:08165 // Loads all registered BackgroundContents at startup.
[email protected]1e0fe2822010-06-24 21:04:19166 void LoadBackgroundContentsFromPrefs(Profile* profile);
[email protected]398206c2010-06-21 01:46:08167
[email protected]2c4fb7b2011-04-02 06:33:29168 // Load a BackgroundContent; the settings are read from the provided
169 // dictionary.
[email protected]f3a1c642011-07-12 19:15:03170 void LoadBackgroundContentsFromDictionary(
171 Profile* profile,
172 const std::string& extension_id,
173 const base::DictionaryValue* contents);
[email protected]2c4fb7b2011-04-02 06:33:29174
[email protected]41619a9a2011-04-13 20:07:32175 // Load the manifest-specified BackgroundContents for all apps for the
176 // profile.
177 void LoadBackgroundContentsFromManifests(Profile* profile);
178
[email protected]da58f5b12010-11-10 19:31:12179 // 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]96920152013-12-04 21:00:16184 const base::string16& frame_name,
185 const base::string16& appid);
[email protected]398206c2010-06-21 01:46:08186
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]96920152013-12-04 21:00:16204 void ShutdownAssociatedBackgroundContents(const base::string16& appid);
[email protected]398206c2010-06-21 01:46:08205
206 // Returns true if this BackgroundContents is in the contents_list_.
207 bool IsTracked(BackgroundContents* contents) const;
208
[email protected]88ea00f2012-05-04 01:17:27209 // 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]6b9dafc2013-08-30 15:41:59214 // Delay (in ms) before restarting a force-installed extension that crashed.
215 static int restart_delay_in_ms_;
216
[email protected]398206c2010-06-21 01:46:08217 // PrefService used to store list of background pages (or NULL if this is
[email protected]2c910b72011-03-08 21:16:32218 // running under an incognito profile).
[email protected]398206c2010-06-21 01:46:08219 PrefService* prefs_;
[email protected]6c2381d2011-10-19 02:52:53220 content::NotificationRegistrar registrar_;
[email protected]398206c2010-06-21 01:46:08221
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]96920152013-12-04 21:00:16227 base::string16 frame_name;
[email protected]398206c2010-06-21 01:46:08228 };
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]d2065e062013-12-12 23:49:52234 typedef std::map<base::string16, BackgroundContentsInfo>
235 BackgroundContentsMap;
[email protected]398206c2010-06-21 01:46:08236 BackgroundContentsMap contents_map_;
237
[email protected]7cf82862014-07-25 18:52:52238 ScopedObserver<extensions::ExtensionRegistry,
239 extensions::ExtensionRegistryObserver>
240 extension_registry_observer_;
241
[email protected]398206c2010-06-21 01:46:08242 DISALLOW_COPY_AND_ASSIGN(BackgroundContentsService);
243};
244
[email protected]a07676b22011-06-17 16:36:53245#endif // CHROME_BROWSER_BACKGROUND_BACKGROUND_CONTENTS_SERVICE_H_