1 // Copyright (c) 2016 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 "components/offline_pages/content/background_loader/background_loader_contents.h"
6 
7 #include <utility>
8 
9 #include "content/public/browser/web_contents.h"
10 #include "third_party/blink/public/mojom/mediastream/media_stream.mojom-shared.h"
11 
12 namespace background_loader {
13 
BackgroundLoaderContents(content::BrowserContext * browser_context)14 BackgroundLoaderContents::BackgroundLoaderContents(
15     content::BrowserContext* browser_context)
16     : browser_context_(browser_context) {
17   // It is very important that we create the web contents with
18   // CreateParams::initially_hidden == false, and that we never change the
19   // visibility after that.  If we did change it, then background throttling
20   // could kill the background offliner while it was running.
21   web_contents_ = content::WebContents::Create(
22       content::WebContents::CreateParams(browser_context_));
23   web_contents_->SetAudioMuted(true);
24   web_contents_->SetDelegate(this);
25 }
26 
~BackgroundLoaderContents()27 BackgroundLoaderContents::~BackgroundLoaderContents() {}
28 
LoadPage(const GURL & url)29 void BackgroundLoaderContents::LoadPage(const GURL& url) {
30   web_contents_->GetController().LoadURL(
31       url /* url to be loaded */,
32       content::Referrer() /* Default referrer policy, no referring url */,
33       ui::PAGE_TRANSITION_LINK /* page transition type: clicked on link */,
34       std::string() /* extra headers */);
35 }
36 
Cancel()37 void BackgroundLoaderContents::Cancel() {
38   web_contents_->Close();
39 }
40 
SetDelegate(Delegate * delegate)41 void BackgroundLoaderContents::SetDelegate(Delegate* delegate) {
42   delegate_ = delegate;
43 }
44 
IsNeverComposited(content::WebContents * web_contents)45 bool BackgroundLoaderContents::IsNeverComposited(
46     content::WebContents* web_contents) {
47   // Background, so not user-visible.
48   return true;
49 }
50 
CloseContents(content::WebContents * source)51 void BackgroundLoaderContents::CloseContents(content::WebContents* source) {
52   // Do nothing. Other pages should not be able to close a background page.
53   NOTREACHED();
54 }
55 
ShouldSuppressDialogs(content::WebContents * source)56 bool BackgroundLoaderContents::ShouldSuppressDialogs(
57     content::WebContents* source) {
58   // Dialog prompts are not actionable in the background.
59   return true;
60 }
61 
ShouldFocusPageAfterCrash()62 bool BackgroundLoaderContents::ShouldFocusPageAfterCrash() {
63   // Background page should never be focused.
64   return false;
65 }
66 
CanDownload(const GURL & url,const std::string & request_method,base::OnceCallback<void (bool)> callback)67 void BackgroundLoaderContents::CanDownload(
68     const GURL& url,
69     const std::string& request_method,
70     base::OnceCallback<void(bool)> callback) {
71   if (delegate_) {
72     delegate_->CanDownload(std::move(callback));
73   } else {
74     // Do not download anything if there's no delegate.
75     std::move(callback).Run(false);
76   }
77 }
78 
IsWebContentsCreationOverridden(content::SiteInstance * source_site_instance,content::mojom::WindowContainerType window_container_type,const GURL & opener_url,const std::string & frame_name,const GURL & target_url)79 bool BackgroundLoaderContents::IsWebContentsCreationOverridden(
80     content::SiteInstance* source_site_instance,
81     content::mojom::WindowContainerType window_container_type,
82     const GURL& opener_url,
83     const std::string& frame_name,
84     const GURL& target_url) {
85   // Background pages should not create other webcontents/tabs.
86   return true;
87 }
88 
AddNewContents(content::WebContents * source,std::unique_ptr<content::WebContents> new_contents,const GURL & target_url,WindowOpenDisposition disposition,const gfx::Rect & initial_rect,bool user_gesture,bool * was_blocked)89 void BackgroundLoaderContents::AddNewContents(
90     content::WebContents* source,
91     std::unique_ptr<content::WebContents> new_contents,
92     const GURL& target_url,
93     WindowOpenDisposition disposition,
94     const gfx::Rect& initial_rect,
95     bool user_gesture,
96     bool* was_blocked) {
97   // Pop-ups should be blocked;
98   // background pages should not create other contents
99   if (was_blocked != nullptr)
100     *was_blocked = true;
101 }
102 
103 #if defined(OS_ANDROID)
ShouldBlockMediaRequest(const GURL & url)104 bool BackgroundLoaderContents::ShouldBlockMediaRequest(const GURL& url) {
105   // Background pages should not have access to media.
106   return true;
107 }
108 #endif
109 
RequestMediaAccessPermission(content::WebContents * contents,const content::MediaStreamRequest & request,content::MediaResponseCallback callback)110 void BackgroundLoaderContents::RequestMediaAccessPermission(
111     content::WebContents* contents,
112     const content::MediaStreamRequest& request,
113     content::MediaResponseCallback callback) {
114   // No permissions granted, act as if dismissed.
115   std::move(callback).Run(
116       blink::MediaStreamDevices(),
117       blink::mojom::MediaStreamRequestResult::PERMISSION_DISMISSED,
118       std::unique_ptr<content::MediaStreamUI>());
119 }
120 
CheckMediaAccessPermission(content::RenderFrameHost * render_frame_host,const GURL & security_origin,blink::mojom::MediaStreamType type)121 bool BackgroundLoaderContents::CheckMediaAccessPermission(
122     content::RenderFrameHost* render_frame_host,
123     const GURL& security_origin,
124     blink::mojom::MediaStreamType type) {
125   return false;  // No permissions granted.
126 }
127 
AdjustPreviewsStateForNavigation(content::WebContents * web_contents,blink::PreviewsState * previews_state)128 void BackgroundLoaderContents::AdjustPreviewsStateForNavigation(
129     content::WebContents* web_contents,
130     blink::PreviewsState* previews_state) {
131   if (*previews_state == 0)
132     *previews_state = blink::PreviewsTypes::PREVIEWS_OFF;
133 }
134 
ShouldAllowLazyLoad()135 bool BackgroundLoaderContents::ShouldAllowLazyLoad() {
136   return false;
137 }
138 
BackgroundLoaderContents()139 BackgroundLoaderContents::BackgroundLoaderContents()
140     : browser_context_(nullptr) {
141   web_contents_.reset();
142 }
143 
144 }  // namespace background_loader
145