Remove aura enum from DesktopMediaID to fix desktop mirroring audio (CrOS).
[chromium-blink-merge.git] / chrome / browser / background / background_contents_service.h
blob725f964c1f06c375fa86900c71f99372f5e1da5e
1 // Copyright (c) 2012 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.
5 #ifndef CHROME_BROWSER_BACKGROUND_BACKGROUND_CONTENTS_SERVICE_H_
6 #define CHROME_BROWSER_BACKGROUND_BACKGROUND_CONTENTS_SERVICE_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/scoped_observer.h"
15 #include "chrome/browser/background/background_contents.h"
16 #include "components/keyed_service/core/keyed_service.h"
17 #include "content/public/browser/notification_observer.h"
18 #include "content/public/browser/notification_registrar.h"
19 #include "content/public/common/window_container_type.h"
20 #include "extensions/browser/extension_registry_observer.h"
21 #include "ui/base/window_open_disposition.h"
22 #include "url/gurl.h"
24 class PrefService;
25 class Profile;
27 namespace base {
28 class CommandLine;
29 class DictionaryValue;
32 namespace content {
33 class SessionStorageNamespace;
36 namespace extensions {
37 class Extension;
38 class ExtensionRegistry;
41 namespace gfx {
42 class Rect;
45 struct BackgroundContentsOpenedDetails;
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).
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.
55 class BackgroundContentsService : private content::NotificationObserver,
56 public extensions::ExtensionRegistryObserver,
57 public BackgroundContents::Delegate,
58 public KeyedService {
59 public:
60 BackgroundContentsService(Profile* profile,
61 const base::CommandLine* command_line);
62 ~BackgroundContentsService() override;
64 // Allows tests to reduce the time between a force-installed app/extension
65 // crashing and when we reload it.
66 static void SetRestartDelayForForceInstalledAppsAndExtensionsForTesting(
67 int restart_delay_in_ms);
69 // Get the crash notification's delegate id for the extension.
70 static std::string GetNotificationDelegateIdForExtensionForTesting(
71 const std::string& extension_id);
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);
78 // Disable closing the crash notification balloon for tests.
79 static void DisableCloseBalloonForTesting(
80 bool disable_close_balloon_for_testing);
82 // Returns the BackgroundContents associated with the passed application id,
83 // or NULL if none.
84 BackgroundContents* GetAppBackgroundContents(const base::string16& appid);
86 // Returns true if there's a registered BackgroundContents for this app. It
87 // is possible for this routine to return true when GetAppBackgroundContents()
88 // returns false, if the BackgroundContents closed due to the render process
89 // crashing.
90 bool HasRegisteredBackgroundContents(const base::string16& appid);
92 // Returns all currently opened BackgroundContents (used by the task manager).
93 std::vector<BackgroundContents*> GetBackgroundContents() const;
95 // BackgroundContents::Delegate implementation.
96 void AddWebContents(content::WebContents* new_contents,
97 WindowOpenDisposition disposition,
98 const gfx::Rect& initial_rect,
99 bool user_gesture,
100 bool* was_blocked) override;
102 // Gets the parent application id for the passed BackgroundContents. Returns
103 // an empty string if no parent application found (e.g. passed
104 // BackgroundContents has already shut down).
105 const base::string16& GetParentApplicationId(BackgroundContents* contents) const;
107 // Creates a new BackgroundContents using the passed |site| and
108 // the |route_id| and begins tracking the object internally so it can be
109 // shutdown if the parent application is uninstalled.
110 // A BACKGROUND_CONTENTS_OPENED notification will be generated with the passed
111 // |frame_name| and |application_id| values, using the passed |profile| as the
112 // Source..
113 BackgroundContents* CreateBackgroundContents(
114 content::SiteInstance* site,
115 int route_id,
116 int main_frame_route_id,
117 Profile* profile,
118 const std::string& frame_name,
119 const base::string16& application_id,
120 const std::string& partition_id,
121 content::SessionStorageNamespace* session_storage_namespace);
123 // Load the manifest-specified background page for the specified hosted app.
124 // If the manifest doesn't specify one, then load the BackgroundContents
125 // registered in the pref. This is typically used to reload a crashed
126 // background page.
127 void LoadBackgroundContentsForExtension(Profile* profile,
128 const std::string& extension_id);
130 private:
131 friend class BackgroundContentsServiceTest;
132 friend class MockBackgroundContents;
133 friend class TaskManagerNoShowBrowserTest;
135 FRIEND_TEST_ALL_PREFIXES(BackgroundContentsServiceTest,
136 BackgroundContentsCreateDestroy);
137 FRIEND_TEST_ALL_PREFIXES(BackgroundContentsServiceTest,
138 TestApplicationIDLinkage);
139 FRIEND_TEST_ALL_PREFIXES(TaskManagerNoShowBrowserTest,
140 NoticeBGContentsChanges);
141 FRIEND_TEST_ALL_PREFIXES(TaskManagerNoShowBrowserTest,
142 KillBGContents);
144 // Registers for various notifications.
145 void StartObserving(Profile* profile);
147 // content::NotificationObserver implementation.
148 void Observe(int type,
149 const content::NotificationSource& source,
150 const content::NotificationDetails& details) override;
152 // extensions::ExtensionRegistryObserver implementation.
153 void OnExtensionLoaded(content::BrowserContext* browser_context,
154 const extensions::Extension* extension) override;
155 void OnExtensionUnloaded(
156 content::BrowserContext* browser_context,
157 const extensions::Extension* extension,
158 extensions::UnloadedExtensionInfo::Reason reason) override;
159 void OnExtensionUninstalled(content::BrowserContext* browser_context,
160 const extensions::Extension* extension,
161 extensions::UninstallReason reason) override;
163 // Restarts a force-installed app/extension after a crash.
164 void RestartForceInstalledExtensionOnCrash(
165 const extensions::Extension* extension,
166 Profile* profile);
168 // Loads all registered BackgroundContents at startup.
169 void LoadBackgroundContentsFromPrefs(Profile* profile);
171 // Load a BackgroundContent; the settings are read from the provided
172 // dictionary.
173 void LoadBackgroundContentsFromDictionary(
174 Profile* profile,
175 const std::string& extension_id,
176 const base::DictionaryValue* contents);
178 // Load the manifest-specified BackgroundContents for all apps for the
179 // profile.
180 void LoadBackgroundContentsFromManifests(Profile* profile);
182 // Creates a single BackgroundContents associated with the specified |appid|,
183 // creates an associated RenderView with the name specified by |frame_name|,
184 // and navigates to the passed |url|.
185 void LoadBackgroundContents(Profile* profile,
186 const GURL& url,
187 const std::string& frame_name,
188 const base::string16& appid);
190 // Invoked when a new BackgroundContents is opened.
191 void BackgroundContentsOpened(BackgroundContentsOpenedDetails* details,
192 Profile* profile);
194 // Invoked when a BackgroundContents object is destroyed.
195 void BackgroundContentsShutdown(BackgroundContents* contents);
197 // Registers the |contents->GetURL()| to be run at startup. Only happens for
198 // the first navigation after window.open() (future calls to
199 // RegisterBackgroundContents() for the same BackgroundContents will do
200 // nothing).
201 void RegisterBackgroundContents(BackgroundContents* contents);
203 // Stops loading the passed BackgroundContents on startup.
204 void UnregisterBackgroundContents(BackgroundContents* contents);
206 // Unregisters and deletes the BackgroundContents associated with the
207 // passed extension.
208 void ShutdownAssociatedBackgroundContents(const base::string16& appid);
210 // Returns true if this BackgroundContents is in the contents_list_.
211 bool IsTracked(BackgroundContents* contents) const;
213 // Sends out a notification when our association of background contents with
214 // apps may have changed (used by BackgroundApplicationListModel to update the
215 // set of background apps as new background contents are opened/closed).
216 void SendChangeNotification(Profile* profile);
218 // Delay (in ms) before restarting a force-installed extension that crashed.
219 static int restart_delay_in_ms_;
221 // PrefService used to store list of background pages (or NULL if this is
222 // running under an incognito profile).
223 PrefService* prefs_;
224 content::NotificationRegistrar registrar_;
226 // Information we track about each BackgroundContents.
227 struct BackgroundContentsInfo {
228 // The BackgroundContents whose information we are tracking.
229 BackgroundContents* contents;
230 // The name of the top level frame for this BackgroundContents.
231 std::string frame_name;
234 // Map associating currently loaded BackgroundContents with their parent
235 // applications.
236 // Key: application id
237 // Value: BackgroundContentsInfo for the BC associated with that application
238 typedef std::map<base::string16, BackgroundContentsInfo>
239 BackgroundContentsMap;
240 BackgroundContentsMap contents_map_;
242 ScopedObserver<extensions::ExtensionRegistry,
243 extensions::ExtensionRegistryObserver>
244 extension_registry_observer_;
246 DISALLOW_COPY_AND_ASSIGN(BackgroundContentsService);
249 #endif // CHROME_BROWSER_BACKGROUND_BACKGROUND_CONTENTS_SERVICE_H_