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.
4 
5 #include "chrome/browser/background/background_contents.h"
6 
7 #include <utility>
8 
9 #include "chrome/browser/background/background_contents_service.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_manager/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/render_frame_host.h"
17 #include "content/public/browser/render_process_host.h"
18 #include "content/public/browser/render_view_host.h"
19 #include "content/public/browser/session_storage_namespace.h"
20 #include "content/public/browser/site_instance.h"
21 #include "content/public/browser/web_contents.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"
27 
28 using content::SiteInstance;
29 using content::WebContents;
30 
BackgroundContents(scoped_refptr<SiteInstance> site_instance,content::RenderFrameHost * opener,bool is_new_browsing_instance,Delegate * delegate,const std::string & partition_id,content::SessionStorageNamespace * session_storage_namespace)31 BackgroundContents::BackgroundContents(
32     scoped_refptr<SiteInstance> site_instance,
33     content::RenderFrameHost* opener,
34     bool is_new_browsing_instance,
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());
43 
44   WebContents::CreateParams create_params(profile_, std::move(site_instance));
45   create_params.opener_render_process_id =
46       opener ? opener->GetProcess()->GetID() : MSG_ROUTING_NONE;
47   create_params.opener_render_frame_id =
48       opener ? opener->GetRoutingID() : MSG_ROUTING_NONE;
49   create_params.is_never_visible = true;
50 
51   // This isn't semantically sensible, but it is what the old code implicitly
52   // did.
53   create_params.renderer_initiated_creation = !is_new_browsing_instance;
54 
55   if (session_storage_namespace) {
56     content::SessionStorageNamespaceMap session_storage_namespace_map;
57     session_storage_namespace_map.insert(
58         std::make_pair(partition_id, session_storage_namespace));
59     web_contents_ = WebContents::CreateWithSessionStorage(
60         create_params, session_storage_namespace_map);
61   } else {
62     web_contents_ = WebContents::Create(create_params);
63   }
64   extensions::SetViewType(
65       web_contents_.get(), extensions::VIEW_TYPE_BACKGROUND_CONTENTS);
66   web_contents_->SetDelegate(this);
67   content::WebContentsObserver::Observe(web_contents_.get());
68   extensions::ChromeExtensionWebContentsObserver::CreateForWebContents(
69       web_contents_.get());
70 
71   // Add the TaskManager-specific tag for the BackgroundContents.
72   task_manager::WebContentsTags::CreateForBackgroundContents(
73       web_contents_.get(), this);
74 }
75 
76 // Exposed to allow creating mocks.
77 BackgroundContents::BackgroundContents() = default;
78 
~BackgroundContents()79 BackgroundContents::~BackgroundContents() {
80   if (!web_contents_.get())   // Will be null for unit tests.
81     return;
82 
83   extensions::ExtensionHostQueue::GetInstance().Remove(this);
84 }
85 
GetURL() const86 const GURL& BackgroundContents::GetURL() const {
87   return web_contents_.get() ? web_contents_->GetURL() : GURL::EmptyGURL();
88 }
89 
CreateRenderViewSoon(const GURL & url)90 void BackgroundContents::CreateRenderViewSoon(const GURL& url) {
91   initial_url_ = url;
92   extensions::ExtensionHostQueue::GetInstance().Add(this);
93 }
94 
CloseContents(WebContents * source)95 void BackgroundContents::CloseContents(WebContents* source) {
96   delegate_->OnBackgroundContentsClosed(this);
97   // |this| is deleted.
98 }
99 
ShouldSuppressDialogs(WebContents * source)100 bool BackgroundContents::ShouldSuppressDialogs(WebContents* source) {
101   return true;
102 }
103 
DidNavigateMainFramePostCommit(WebContents * tab)104 void BackgroundContents::DidNavigateMainFramePostCommit(WebContents* tab) {
105   // Note: because BackgroundContents are only available to extension apps,
106   // navigation is limited to urls within the app's extent. This is enforced in
107   // RenderView::decidePolicyForNavigation. If BackgroundContents become
108   // available as a part of the web platform, it probably makes sense to have
109   // some way to scope navigation of a background page to its opener's security
110   // origin. Note: if the first navigation is to a URL outside the app's
111   // extent a background page will be opened but will remain at about:blank.
112   delegate_->OnBackgroundContentsNavigated(this);
113 }
114 
115 // Forward requests to add a new WebContents to our delegate.
AddNewContents(WebContents * source,std::unique_ptr<WebContents> new_contents,const GURL & target_url,WindowOpenDisposition disposition,const gfx::Rect & initial_rect,bool user_gesture,bool * was_blocked)116 void BackgroundContents::AddNewContents(
117     WebContents* source,
118     std::unique_ptr<WebContents> new_contents,
119     const GURL& target_url,
120     WindowOpenDisposition disposition,
121     const gfx::Rect& initial_rect,
122     bool user_gesture,
123     bool* was_blocked) {
124   delegate_->AddWebContents(std::move(new_contents), target_url, disposition,
125                             initial_rect, was_blocked);
126 }
127 
IsNeverComposited(content::WebContents * web_contents)128 bool BackgroundContents::IsNeverComposited(content::WebContents* web_contents) {
129   DCHECK_EQ(extensions::VIEW_TYPE_BACKGROUND_CONTENTS,
130             extensions::GetViewType(web_contents));
131   return true;
132 }
133 
RenderProcessGone(base::TerminationStatus status)134 void BackgroundContents::RenderProcessGone(base::TerminationStatus status) {
135   delegate_->OnBackgroundContentsTerminated(this);
136   // |this| is deleted.
137 }
138 
CreateRenderViewNow()139 void BackgroundContents::CreateRenderViewNow() {
140   web_contents()->GetController().LoadURL(initial_url_, content::Referrer(),
141                                           ui::PAGE_TRANSITION_LINK,
142                                           std::string());
143 }
144