Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / background / background_contents.cc
blob6b02c78a44190ef227f0d969f3f2cacd2514b52f
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 "base/profiler/scoped_tracker.h"
8 #include "chrome/browser/background/background_contents_service.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/renderer_preferences_util.h"
13 #include "chrome/browser/task_management/web_contents_tags.h"
14 #include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h"
15 #include "chrome/common/url_constants.h"
16 #include "content/public/browser/notification_service.h"
17 #include "content/public/browser/render_view_host.h"
18 #include "content/public/browser/session_storage_namespace.h"
19 #include "content/public/browser/site_instance.h"
20 #include "content/public/browser/web_contents.h"
21 #include "extensions/browser/deferred_start_render_host_observer.h"
22 #include "extensions/browser/extension_host_delegate.h"
23 #include "extensions/browser/extension_host_queue.h"
24 #include "extensions/browser/extensions_browser_client.h"
25 #include "extensions/browser/view_type_utils.h"
26 #include "ui/gfx/geometry/rect.h"
28 using content::SiteInstance;
29 using content::WebContents;
31 BackgroundContents::BackgroundContents(
32 SiteInstance* site_instance,
33 int routing_id,
34 int main_frame_routing_id,
35 Delegate* delegate,
36 const std::string& partition_id,
37 content::SessionStorageNamespace* session_storage_namespace)
38 : delegate_(delegate),
39 extension_host_delegate_(extensions::ExtensionsBrowserClient::Get()
40 ->CreateExtensionHostDelegate()) {
41 profile_ = Profile::FromBrowserContext(
42 site_instance->GetBrowserContext());
44 WebContents::CreateParams create_params(profile_, site_instance);
45 create_params.routing_id = routing_id;
46 create_params.main_frame_routing_id = main_frame_routing_id;
47 create_params.renderer_initiated_creation = routing_id != MSG_ROUTING_NONE;
48 if (session_storage_namespace) {
49 content::SessionStorageNamespaceMap session_storage_namespace_map;
50 session_storage_namespace_map.insert(
51 std::make_pair(partition_id, session_storage_namespace));
52 web_contents_.reset(WebContents::CreateWithSessionStorage(
53 create_params, session_storage_namespace_map));
54 } else {
55 web_contents_.reset(WebContents::Create(create_params));
57 extensions::SetViewType(
58 web_contents_.get(), extensions::VIEW_TYPE_BACKGROUND_CONTENTS);
59 web_contents_->SetDelegate(this);
60 content::WebContentsObserver::Observe(web_contents_.get());
61 extensions::ChromeExtensionWebContentsObserver::CreateForWebContents(
62 web_contents_.get());
64 // Add the TaskManager-specific tag for the BackgroundContents.
65 task_management::WebContentsTags::CreateForBackgroundContents(
66 web_contents_.get(), this);
68 // Close ourselves when the application is shutting down.
69 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
70 content::NotificationService::AllSources());
72 // Register for our parent profile to shutdown, so we can shut ourselves down
73 // as well (should only be called for OTR profiles, as we should receive
74 // APP_TERMINATING before non-OTR profiles are destroyed).
75 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED,
76 content::Source<Profile>(profile_));
79 // Exposed to allow creating mocks.
80 BackgroundContents::BackgroundContents()
81 : delegate_(NULL),
82 profile_(NULL) {
85 BackgroundContents::~BackgroundContents() {
86 if (!web_contents_.get()) // Will be null for unit tests.
87 return;
89 // Unregister for any notifications before notifying observers that we are
90 // going away - this prevents any re-entrancy due to chained notifications
91 // (http://crbug.com/237781).
92 registrar_.RemoveAll();
94 content::NotificationService::current()->Notify(
95 chrome::NOTIFICATION_BACKGROUND_CONTENTS_DELETED,
96 content::Source<Profile>(profile_),
97 content::Details<BackgroundContents>(this));
98 FOR_EACH_OBSERVER(extensions::DeferredStartRenderHostObserver,
99 deferred_start_render_host_observer_list_,
100 OnDeferredStartRenderHostDestroyed(this));
102 extension_host_delegate_->GetExtensionHostQueue()->Remove(this);
105 const GURL& BackgroundContents::GetURL() const {
106 return web_contents_.get() ? web_contents_->GetURL() : GURL::EmptyGURL();
109 void BackgroundContents::CreateRenderViewSoon(const GURL& url) {
110 initial_url_ = url;
111 extension_host_delegate_->GetExtensionHostQueue()->Add(this);
114 void BackgroundContents::CloseContents(WebContents* source) {
115 content::NotificationService::current()->Notify(
116 chrome::NOTIFICATION_BACKGROUND_CONTENTS_CLOSED,
117 content::Source<Profile>(profile_),
118 content::Details<BackgroundContents>(this));
119 delete this;
122 bool BackgroundContents::ShouldSuppressDialogs(WebContents* source) {
123 return true;
126 void BackgroundContents::DidNavigateMainFramePostCommit(WebContents* tab) {
127 // Note: because BackgroundContents are only available to extension apps,
128 // navigation is limited to urls within the app's extent. This is enforced in
129 // RenderView::decidePolicyForNavigation. If BackgroundContents become
130 // available as a part of the web platform, it probably makes sense to have
131 // some way to scope navigation of a background page to its opener's security
132 // origin. Note: if the first navigation is to a URL outside the app's
133 // extent a background page will be opened but will remain at about:blank.
134 content::NotificationService::current()->Notify(
135 chrome::NOTIFICATION_BACKGROUND_CONTENTS_NAVIGATED,
136 content::Source<Profile>(profile_),
137 content::Details<BackgroundContents>(this));
140 // Forward requests to add a new WebContents to our delegate.
141 void BackgroundContents::AddNewContents(WebContents* source,
142 WebContents* new_contents,
143 WindowOpenDisposition disposition,
144 const gfx::Rect& initial_rect,
145 bool user_gesture,
146 bool* was_blocked) {
147 delegate_->AddWebContents(
148 new_contents, disposition, initial_rect, user_gesture, was_blocked);
151 bool BackgroundContents::IsNeverVisible(content::WebContents* web_contents) {
152 DCHECK_EQ(extensions::VIEW_TYPE_BACKGROUND_CONTENTS,
153 extensions::GetViewType(web_contents));
154 return true;
157 void BackgroundContents::RenderProcessGone(base::TerminationStatus status) {
158 content::NotificationService::current()->Notify(
159 chrome::NOTIFICATION_BACKGROUND_CONTENTS_TERMINATED,
160 content::Source<Profile>(profile_),
161 content::Details<BackgroundContents>(this));
163 // Our RenderView went away, so we should go away also, so killing the process
164 // via the TaskManager doesn't permanently leave a BackgroundContents hanging
165 // around the system, blocking future instances from being created
166 // <http://crbug.com/65189>.
167 delete this;
170 void BackgroundContents::DidStartLoading() {
171 // BackgroundContents only loads once, so this can only be the first time it
172 // has started loading.
173 FOR_EACH_OBSERVER(extensions::DeferredStartRenderHostObserver,
174 deferred_start_render_host_observer_list_,
175 OnDeferredStartRenderHostDidStartFirstLoad(this));
178 void BackgroundContents::DidStopLoading() {
179 // BackgroundContents only loads once, so this can only be the first time
180 // it has stopped loading.
181 FOR_EACH_OBSERVER(extensions::DeferredStartRenderHostObserver,
182 deferred_start_render_host_observer_list_,
183 OnDeferredStartRenderHostDidStopFirstLoad(this));
186 void BackgroundContents::Observe(int type,
187 const content::NotificationSource& source,
188 const content::NotificationDetails& details) {
189 // TODO(rafaelw): Implement pagegroup ref-counting so that non-persistent
190 // background pages are closed when the last referencing frame is closed.
191 switch (type) {
192 case chrome::NOTIFICATION_PROFILE_DESTROYED:
193 case chrome::NOTIFICATION_APP_TERMINATING: {
194 delete this;
195 break;
197 default:
198 NOTREACHED() << "Unexpected notification sent.";
199 break;
203 void BackgroundContents::CreateRenderViewNow() {
204 // TODO(robliao): Remove ScopedTracker below once crbug.com/464206 is fixed.
205 tracked_objects::ScopedTracker tracking_profile(
206 FROM_HERE_WITH_EXPLICIT_FUNCTION(
207 "464206 BackgroundContents::CreateRenderViewNow"));
208 web_contents()->GetController().LoadURL(initial_url_, content::Referrer(),
209 ui::PAGE_TRANSITION_LINK,
210 std::string());
213 void BackgroundContents::AddDeferredStartRenderHostObserver(
214 extensions::DeferredStartRenderHostObserver* observer) {
215 deferred_start_render_host_observer_list_.AddObserver(observer);
218 void BackgroundContents::RemoveDeferredStartRenderHostObserver(
219 extensions::DeferredStartRenderHostObserver* observer) {
220 deferred_start_render_host_observer_list_.RemoveObserver(observer);