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 #include "chrome/browser/tab_contents/background_contents.h"
7 #include "chrome/browser/background/background_contents_service.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/extensions/extension_web_contents_observer.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/renderer_preferences_util.h"
12 #include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h"
13 #include "chrome/common/extensions/extension_constants.h"
14 #include "chrome/common/url_constants.h"
15 #include "content/public/browser/notification_service.h"
16 #include "content/public/browser/render_view_host.h"
17 #include "content/public/browser/session_storage_namespace.h"
18 #include "content/public/browser/site_instance.h"
19 #include "content/public/browser/web_contents.h"
20 #include "extensions/browser/view_type_utils.h"
21 #include "ui/gfx/rect.h"
23 using content::SiteInstance
;
24 using content::WebContents
;
26 BackgroundContents::BackgroundContents(
27 SiteInstance
* site_instance
,
30 const std::string
& partition_id
,
31 content::SessionStorageNamespace
* session_storage_namespace
)
32 : delegate_(delegate
) {
33 profile_
= Profile::FromBrowserContext(
34 site_instance
->GetBrowserContext());
36 WebContents::CreateParams
create_params(profile_
, site_instance
);
37 create_params
.routing_id
= routing_id
;
38 if (session_storage_namespace
) {
39 content::SessionStorageNamespaceMap session_storage_namespace_map
;
40 session_storage_namespace_map
.insert(
41 std::make_pair(partition_id
, session_storage_namespace
));
42 web_contents_
.reset(WebContents::CreateWithSessionStorage(
43 create_params
, session_storage_namespace_map
));
45 web_contents_
.reset(WebContents::Create(create_params
));
47 extensions::SetViewType(
48 web_contents_
.get(), extensions::VIEW_TYPE_BACKGROUND_CONTENTS
);
49 web_contents_
->SetDelegate(this);
50 content::WebContentsObserver::Observe(web_contents_
.get());
51 extensions::ExtensionWebContentsObserver::CreateForWebContents(
54 // Close ourselves when the application is shutting down.
55 registrar_
.Add(this, chrome::NOTIFICATION_APP_TERMINATING
,
56 content::NotificationService::AllSources());
58 // Register for our parent profile to shutdown, so we can shut ourselves down
59 // as well (should only be called for OTR profiles, as we should receive
60 // APP_TERMINATING before non-OTR profiles are destroyed).
61 registrar_
.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED
,
62 content::Source
<Profile
>(profile_
));
65 // Exposed to allow creating mocks.
66 BackgroundContents::BackgroundContents()
71 BackgroundContents::~BackgroundContents() {
72 if (!web_contents_
.get()) // Will be null for unit tests.
75 // Unregister for any notifications before notifying observers that we are
76 // going away - this prevents any re-entrancy due to chained notifications
77 // (http://crbug.com/237781).
78 registrar_
.RemoveAll();
80 content::NotificationService::current()->Notify(
81 chrome::NOTIFICATION_BACKGROUND_CONTENTS_DELETED
,
82 content::Source
<Profile
>(profile_
),
83 content::Details
<BackgroundContents
>(this));
86 const GURL
& BackgroundContents::GetURL() const {
87 return web_contents_
.get() ? web_contents_
->GetURL() : GURL::EmptyGURL();
90 void BackgroundContents::CloseContents(WebContents
* source
) {
91 content::NotificationService::current()->Notify(
92 chrome::NOTIFICATION_BACKGROUND_CONTENTS_CLOSED
,
93 content::Source
<Profile
>(profile_
),
94 content::Details
<BackgroundContents
>(this));
98 bool BackgroundContents::ShouldSuppressDialogs() {
102 void BackgroundContents::DidNavigateMainFramePostCommit(WebContents
* tab
) {
103 // Note: because BackgroundContents are only available to extension apps,
104 // navigation is limited to urls within the app's extent. This is enforced in
105 // RenderView::decidePolicyForNavigation. If BackgroundContents become
106 // available as a part of the web platform, it probably makes sense to have
107 // some way to scope navigation of a background page to its opener's security
108 // origin. Note: if the first navigation is to a URL outside the app's
109 // extent a background page will be opened but will remain at about:blank.
110 content::NotificationService::current()->Notify(
111 chrome::NOTIFICATION_BACKGROUND_CONTENTS_NAVIGATED
,
112 content::Source
<Profile
>(profile_
),
113 content::Details
<BackgroundContents
>(this));
116 // Forward requests to add a new WebContents to our delegate.
117 void BackgroundContents::AddNewContents(WebContents
* source
,
118 WebContents
* new_contents
,
119 WindowOpenDisposition disposition
,
120 const gfx::Rect
& initial_pos
,
123 delegate_
->AddWebContents(
124 new_contents
, disposition
, initial_pos
, user_gesture
, was_blocked
);
127 void BackgroundContents::RenderProcessGone(base::TerminationStatus status
) {
128 content::NotificationService::current()->Notify(
129 chrome::NOTIFICATION_BACKGROUND_CONTENTS_TERMINATED
,
130 content::Source
<Profile
>(profile_
),
131 content::Details
<BackgroundContents
>(this));
133 // Our RenderView went away, so we should go away also, so killing the process
134 // via the TaskManager doesn't permanently leave a BackgroundContents hanging
135 // around the system, blocking future instances from being created
136 // <http://crbug.com/65189>.
140 void BackgroundContents::Observe(int type
,
141 const content::NotificationSource
& source
,
142 const content::NotificationDetails
& details
) {
143 // TODO(rafaelw): Implement pagegroup ref-counting so that non-persistent
144 // background pages are closed when the last referencing frame is closed.
146 case chrome::NOTIFICATION_PROFILE_DESTROYED
:
147 case chrome::NOTIFICATION_APP_TERMINATING
: {
152 NOTREACHED() << "Unexpected notification sent.";