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/background/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/chrome_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/url_constants.h"
14 #include "content/public/browser/notification_service.h"
15 #include "content/public/browser/render_view_host.h"
16 #include "content/public/browser/session_storage_namespace.h"
17 #include "content/public/browser/site_instance.h"
18 #include "content/public/browser/web_contents.h"
19 #include "extensions/browser/deferred_start_render_host_observer.h"
20 #include "extensions/browser/extension_host_delegate.h"
21 #include "extensions/browser/extension_host_queue.h"
22 #include "extensions/browser/extensions_browser_client.h"
23 #include "extensions/browser/view_type_utils.h"
24 #include "ui/gfx/geometry/rect.h"
26 using content::SiteInstance
;
27 using content::WebContents
;
29 BackgroundContents::BackgroundContents(
30 SiteInstance
* site_instance
,
32 int main_frame_routing_id
,
34 const std::string
& partition_id
,
35 content::SessionStorageNamespace
* session_storage_namespace
)
36 : delegate_(delegate
),
37 extension_host_delegate_(extensions::ExtensionsBrowserClient::Get()
38 ->CreateExtensionHostDelegate()) {
39 profile_
= Profile::FromBrowserContext(
40 site_instance
->GetBrowserContext());
42 WebContents::CreateParams
create_params(profile_
, site_instance
);
43 create_params
.routing_id
= routing_id
;
44 create_params
.main_frame_routing_id
= main_frame_routing_id
;
45 create_params
.renderer_initiated_creation
= true;
46 if (session_storage_namespace
) {
47 content::SessionStorageNamespaceMap session_storage_namespace_map
;
48 session_storage_namespace_map
.insert(
49 std::make_pair(partition_id
, session_storage_namespace
));
50 web_contents_
.reset(WebContents::CreateWithSessionStorage(
51 create_params
, session_storage_namespace_map
));
53 web_contents_
.reset(WebContents::Create(create_params
));
55 extensions::SetViewType(
56 web_contents_
.get(), extensions::VIEW_TYPE_BACKGROUND_CONTENTS
);
57 web_contents_
->SetDelegate(this);
58 content::WebContentsObserver::Observe(web_contents_
.get());
59 extensions::ChromeExtensionWebContentsObserver::CreateForWebContents(
62 // Close ourselves when the application is shutting down.
63 registrar_
.Add(this, chrome::NOTIFICATION_APP_TERMINATING
,
64 content::NotificationService::AllSources());
66 // Register for our parent profile to shutdown, so we can shut ourselves down
67 // as well (should only be called for OTR profiles, as we should receive
68 // APP_TERMINATING before non-OTR profiles are destroyed).
69 registrar_
.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED
,
70 content::Source
<Profile
>(profile_
));
73 // Exposed to allow creating mocks.
74 BackgroundContents::BackgroundContents()
79 BackgroundContents::~BackgroundContents() {
80 if (!web_contents_
.get()) // Will be null for unit tests.
83 // Unregister for any notifications before notifying observers that we are
84 // going away - this prevents any re-entrancy due to chained notifications
85 // (http://crbug.com/237781).
86 registrar_
.RemoveAll();
88 content::NotificationService::current()->Notify(
89 chrome::NOTIFICATION_BACKGROUND_CONTENTS_DELETED
,
90 content::Source
<Profile
>(profile_
),
91 content::Details
<BackgroundContents
>(this));
92 FOR_EACH_OBSERVER(extensions::DeferredStartRenderHostObserver
,
93 deferred_start_render_host_observer_list_
,
94 OnDeferredStartRenderHostDestroyed(this));
96 extension_host_delegate_
->GetExtensionHostQueue()->Remove(this);
99 const GURL
& BackgroundContents::GetURL() const {
100 return web_contents_
.get() ? web_contents_
->GetURL() : GURL::EmptyGURL();
103 void BackgroundContents::CreateRenderViewSoon(const GURL
& url
) {
105 extension_host_delegate_
->GetExtensionHostQueue()->Add(this);
108 void BackgroundContents::CloseContents(WebContents
* source
) {
109 content::NotificationService::current()->Notify(
110 chrome::NOTIFICATION_BACKGROUND_CONTENTS_CLOSED
,
111 content::Source
<Profile
>(profile_
),
112 content::Details
<BackgroundContents
>(this));
116 bool BackgroundContents::ShouldSuppressDialogs(WebContents
* source
) {
120 void BackgroundContents::DidNavigateMainFramePostCommit(WebContents
* tab
) {
121 // Note: because BackgroundContents are only available to extension apps,
122 // navigation is limited to urls within the app's extent. This is enforced in
123 // RenderView::decidePolicyForNavigation. If BackgroundContents become
124 // available as a part of the web platform, it probably makes sense to have
125 // some way to scope navigation of a background page to its opener's security
126 // origin. Note: if the first navigation is to a URL outside the app's
127 // extent a background page will be opened but will remain at about:blank.
128 content::NotificationService::current()->Notify(
129 chrome::NOTIFICATION_BACKGROUND_CONTENTS_NAVIGATED
,
130 content::Source
<Profile
>(profile_
),
131 content::Details
<BackgroundContents
>(this));
134 // Forward requests to add a new WebContents to our delegate.
135 void BackgroundContents::AddNewContents(WebContents
* source
,
136 WebContents
* new_contents
,
137 WindowOpenDisposition disposition
,
138 const gfx::Rect
& initial_rect
,
141 delegate_
->AddWebContents(
142 new_contents
, disposition
, initial_rect
, user_gesture
, was_blocked
);
145 bool BackgroundContents::IsNeverVisible(content::WebContents
* web_contents
) {
146 DCHECK_EQ(extensions::VIEW_TYPE_BACKGROUND_CONTENTS
,
147 extensions::GetViewType(web_contents
));
151 void BackgroundContents::RenderProcessGone(base::TerminationStatus status
) {
152 content::NotificationService::current()->Notify(
153 chrome::NOTIFICATION_BACKGROUND_CONTENTS_TERMINATED
,
154 content::Source
<Profile
>(profile_
),
155 content::Details
<BackgroundContents
>(this));
157 // Our RenderView went away, so we should go away also, so killing the process
158 // via the TaskManager doesn't permanently leave a BackgroundContents hanging
159 // around the system, blocking future instances from being created
160 // <http://crbug.com/65189>.
164 void BackgroundContents::DidStartLoading(
165 content::RenderViewHost
* render_view_host
) {
166 FOR_EACH_OBSERVER(extensions::DeferredStartRenderHostObserver
,
167 deferred_start_render_host_observer_list_
,
168 OnDeferredStartRenderHostDidStartLoading(this));
171 void BackgroundContents::DidStopLoading(
172 content::RenderViewHost
* render_view_host
) {
173 FOR_EACH_OBSERVER(extensions::DeferredStartRenderHostObserver
,
174 deferred_start_render_host_observer_list_
,
175 OnDeferredStartRenderHostDidStopLoading(this));
178 void BackgroundContents::Observe(int type
,
179 const content::NotificationSource
& source
,
180 const content::NotificationDetails
& details
) {
181 // TODO(rafaelw): Implement pagegroup ref-counting so that non-persistent
182 // background pages are closed when the last referencing frame is closed.
184 case chrome::NOTIFICATION_PROFILE_DESTROYED
:
185 case chrome::NOTIFICATION_APP_TERMINATING
: {
190 NOTREACHED() << "Unexpected notification sent.";
195 void BackgroundContents::CreateRenderViewNow() {
196 web_contents()->GetController().LoadURL(initial_url_
, content::Referrer(),
197 ui::PAGE_TRANSITION_LINK
,
201 void BackgroundContents::AddDeferredStartRenderHostObserver(
202 extensions::DeferredStartRenderHostObserver
* observer
) {
203 deferred_start_render_host_observer_list_
.AddObserver(observer
);
206 void BackgroundContents::RemoveDeferredStartRenderHostObserver(
207 extensions::DeferredStartRenderHostObserver
* observer
) {
208 deferred_start_render_host_observer_list_
.RemoveObserver(observer
);