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