Add more checks to investigate SupervisedUserPrefStore crash at startup.
[chromium-blink-merge.git] / chrome / browser / background / background_contents.cc
blobbf94280656bfba469b1214b986e149f9f62a0682
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/extension_host_delegate.h"
20 #include "extensions/browser/extension_host_queue.h"
21 #include "extensions/browser/extensions_browser_client.h"
22 #include "extensions/browser/view_type_utils.h"
23 #include "ui/gfx/geometry/rect.h"
25 using content::SiteInstance;
26 using content::WebContents;
28 BackgroundContents::BackgroundContents(
29 SiteInstance* site_instance,
30 int routing_id,
31 int main_frame_routing_id,
32 Delegate* delegate,
33 const std::string& partition_id,
34 content::SessionStorageNamespace* session_storage_namespace)
35 : delegate_(delegate),
36 extension_host_delegate_(extensions::ExtensionsBrowserClient::Get()
37 ->CreateExtensionHostDelegate()) {
38 profile_ = Profile::FromBrowserContext(
39 site_instance->GetBrowserContext());
41 WebContents::CreateParams create_params(profile_, site_instance);
42 create_params.routing_id = routing_id;
43 create_params.main_frame_routing_id = main_frame_routing_id;
44 if (session_storage_namespace) {
45 content::SessionStorageNamespaceMap session_storage_namespace_map;
46 session_storage_namespace_map.insert(
47 std::make_pair(partition_id, session_storage_namespace));
48 web_contents_.reset(WebContents::CreateWithSessionStorage(
49 create_params, session_storage_namespace_map));
50 } else {
51 web_contents_.reset(WebContents::Create(create_params));
53 extensions::SetViewType(
54 web_contents_.get(), extensions::VIEW_TYPE_BACKGROUND_CONTENTS);
55 web_contents_->SetDelegate(this);
56 content::WebContentsObserver::Observe(web_contents_.get());
57 extensions::ChromeExtensionWebContentsObserver::CreateForWebContents(
58 web_contents_.get());
60 // Close ourselves when the application is shutting down.
61 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
62 content::NotificationService::AllSources());
64 // Register for our parent profile to shutdown, so we can shut ourselves down
65 // as well (should only be called for OTR profiles, as we should receive
66 // APP_TERMINATING before non-OTR profiles are destroyed).
67 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED,
68 content::Source<Profile>(profile_));
71 // Exposed to allow creating mocks.
72 BackgroundContents::BackgroundContents()
73 : delegate_(NULL),
74 profile_(NULL) {
77 BackgroundContents::~BackgroundContents() {
78 if (!web_contents_.get()) // Will be null for unit tests.
79 return;
81 // Unregister for any notifications before notifying observers that we are
82 // going away - this prevents any re-entrancy due to chained notifications
83 // (http://crbug.com/237781).
84 registrar_.RemoveAll();
86 content::NotificationService::current()->Notify(
87 chrome::NOTIFICATION_BACKGROUND_CONTENTS_DELETED,
88 content::Source<Profile>(profile_),
89 content::Details<BackgroundContents>(this));
91 extension_host_delegate_->GetExtensionHostQueue()->Remove(this);
94 const GURL& BackgroundContents::GetURL() const {
95 return web_contents_.get() ? web_contents_->GetURL() : GURL::EmptyGURL();
98 void BackgroundContents::CreateRenderViewSoon(const GURL& url) {
99 initial_url_ = url;
100 extension_host_delegate_->GetExtensionHostQueue()->Add(this);
103 void BackgroundContents::CloseContents(WebContents* source) {
104 content::NotificationService::current()->Notify(
105 chrome::NOTIFICATION_BACKGROUND_CONTENTS_CLOSED,
106 content::Source<Profile>(profile_),
107 content::Details<BackgroundContents>(this));
108 delete this;
111 bool BackgroundContents::ShouldSuppressDialogs(WebContents* source) {
112 return true;
115 void BackgroundContents::DidNavigateMainFramePostCommit(WebContents* tab) {
116 // Note: because BackgroundContents are only available to extension apps,
117 // navigation is limited to urls within the app's extent. This is enforced in
118 // RenderView::decidePolicyForNavigation. If BackgroundContents become
119 // available as a part of the web platform, it probably makes sense to have
120 // some way to scope navigation of a background page to its opener's security
121 // origin. Note: if the first navigation is to a URL outside the app's
122 // extent a background page will be opened but will remain at about:blank.
123 content::NotificationService::current()->Notify(
124 chrome::NOTIFICATION_BACKGROUND_CONTENTS_NAVIGATED,
125 content::Source<Profile>(profile_),
126 content::Details<BackgroundContents>(this));
129 // Forward requests to add a new WebContents to our delegate.
130 void BackgroundContents::AddNewContents(WebContents* source,
131 WebContents* new_contents,
132 WindowOpenDisposition disposition,
133 const gfx::Rect& initial_rect,
134 bool user_gesture,
135 bool* was_blocked) {
136 delegate_->AddWebContents(
137 new_contents, disposition, initial_rect, user_gesture, was_blocked);
140 bool BackgroundContents::IsNeverVisible(content::WebContents* web_contents) {
141 DCHECK_EQ(extensions::VIEW_TYPE_BACKGROUND_CONTENTS,
142 extensions::GetViewType(web_contents));
143 return true;
146 void BackgroundContents::RenderProcessGone(base::TerminationStatus status) {
147 content::NotificationService::current()->Notify(
148 chrome::NOTIFICATION_BACKGROUND_CONTENTS_TERMINATED,
149 content::Source<Profile>(profile_),
150 content::Details<BackgroundContents>(this));
152 // Our RenderView went away, so we should go away also, so killing the process
153 // via the TaskManager doesn't permanently leave a BackgroundContents hanging
154 // around the system, blocking future instances from being created
155 // <http://crbug.com/65189>.
156 delete this;
159 void BackgroundContents::Observe(int type,
160 const content::NotificationSource& source,
161 const content::NotificationDetails& details) {
162 // TODO(rafaelw): Implement pagegroup ref-counting so that non-persistent
163 // background pages are closed when the last referencing frame is closed.
164 switch (type) {
165 case chrome::NOTIFICATION_PROFILE_DESTROYED:
166 case chrome::NOTIFICATION_APP_TERMINATING: {
167 delete this;
168 break;
170 default:
171 NOTREACHED() << "Unexpected notification sent.";
172 break;
176 void BackgroundContents::CreateRenderViewNow() {
177 web_contents()->GetController().LoadURL(initial_url_, content::Referrer(),
178 ui::PAGE_TRANSITION_LINK,
179 std::string());