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