Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(210)

Side by Side Diff: chrome/browser/ui/webui/print_preview/hidden_web_contents.cc

Issue 1125343004: Add a "Simplify Page" option to the print preview dialog (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase and remove custom URL source Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2015 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/ui/webui/print_preview/hidden_web_contents.h"
6
7 #include <string>
8
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/tab_helpers.h"
13 #include "chrome/browser/ui/web_contents_sizer.h"
14 #include "chrome/common/prerender_messages.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/notification_service.h"
17 #include "content/public/browser/render_frame_host.h"
18 #include "content/public/browser/render_process_host.h"
19 #include "content/public/browser/render_view_host.h"
20 #include "content/public/browser/session_storage_namespace.h"
21 #include "content/public/browser/web_contents.h"
22 #include "content/public/browser/web_contents_delegate.h"
23
24 using content::BrowserThread;
25 using content::OpenURLParams;
26 using content::RenderViewHost;
27 using content::SessionStorageNamespace;
28 using content::WebContents;
29
30 class HiddenWebContents::WebContentsDelegateImpl
31 : public content::WebContentsDelegate {
32 public:
33 explicit WebContentsDelegateImpl(HiddenWebContents* hidden_web_contents)
34 : hidden_web_contents_(hidden_web_contents) {
35 }
36
37 // content::WebContentsDelegate implementation:
38 WebContents* OpenURLFromTab(WebContents* source,
39 const OpenURLParams& params) override {
40 // |OpenURLFromTab| is typically called when a frame performs a navigation
41 // that requires the browser to perform the transition instead of WebKit.
42 // Examples include rendering a site that redirects to an app URL,
43 // or if --enable-strict-site-isolation is specified and the rendered
44 // frame redirects to a different origin.
45 hidden_web_contents_->Destroy();
46 return NULL;
47 }
48
49 void CloseContents(content::WebContents* contents) override {
50 hidden_web_contents_->Destroy();
51 }
52
53 void CanDownload(const GURL& url,
54 const std::string& request_method,
55 const base::Callback<void(bool)>& callback) override {
56 hidden_web_contents_->Destroy();
57 // Cancel the download.
58 callback.Run(false);
59 }
60
61 bool ShouldCreateWebContents(
62 WebContents* web_contents,
63 int route_id,
64 int main_frame_route_id,
65 WindowContainerType window_container_type,
66 const base::string16& frame_name,
67 const GURL& target_url,
68 const std::string& partition_id,
69 SessionStorageNamespace* session_storage_namespace) override {
70 // Since we don't want to permit child windows that would have a
71 // window.opener property, terminate rendering.
72 hidden_web_contents_->Destroy();
73 // Cancel the popup.
74 return false;
75 }
76
77 bool OnGoToEntryOffset(int offset) override {
78 // This isn't allowed because the history merge operation
79 // does not work if there are renderer issued challenges.
80 // TODO(cbentzel): Cancel in this case? May not need to do
81 // since render-issued offset navigations are not guaranteed,
82 // but indicates that the page cares about the history.
83 return false;
84 }
85
86 bool ShouldSuppressDialogs(WebContents* source) override {
87 // We still want to show the user the message when they navigate to this
88 // page, so cancel this render.
89 hidden_web_contents_->Destroy();
90 // Always suppress JavaScript messages if they're triggered by a page being
91 // rendered.
92 return true;
93 }
94
95 void RegisterProtocolHandler(WebContents* web_contents,
96 const std::string& protocol,
97 const GURL& url,
98 bool user_gesture) override {
99 hidden_web_contents_->Destroy();
100 }
101
102 gfx::Size GetSizeForNewRenderView(WebContents* web_contents) const override {
103 // Have to set the size of the RenderView on initialization to be sure it is
104 // set before the RenderView is hidden on all platforms (esp. Android).
105 return hidden_web_contents_->size_;
106 }
107
108 private:
109 HiddenWebContents* hidden_web_contents_;
110 };
111
112 void HiddenWebContents::Observer::OnFinishedLoad(
113 HiddenWebContents* contents) {
114 }
115
116 HiddenWebContents::Observer::Observer() {
117 }
118
119 HiddenWebContents::Observer::~Observer() {
120 }
121
122 HiddenWebContents::HiddenWebContents(Profile* profile)
123 : session_storage_namespace_id_(-1),
124 profile_(profile),
125 rendering_has_been_cancelled_(false) {
126 }
127
128 void HiddenWebContents::StartRendering(
129 const gfx::Size& size,
130 SessionStorageNamespace* session_storage_namespace) {
131 DCHECK(profile_ != NULL);
132 DCHECK(!size.IsEmpty());
133 DCHECK(web_contents_.get() == NULL);
134 DCHECK(size_.IsEmpty());
135
136 session_storage_namespace_id_ = session_storage_namespace->id();
137 size_ = size;
138
139 web_contents_.reset(CreateWebContents(session_storage_namespace));
140 TabHelpers::AttachTabHelpers(web_contents_.get());
141 content::WebContentsObserver::Observe(web_contents_.get());
142
143 web_contents_delegate_.reset(new WebContentsDelegateImpl(this));
144 web_contents_.get()->SetDelegate(web_contents_delegate_.get());
145
146 // Set the size of the hidden WebContents.
147 ResizeWebContents(web_contents_.get(), size_);
148
149 // Close ourselves when the application is shutting down.
150 notification_registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
151 content::NotificationService::AllSources());
152
153 // Register to inform new RenderViews that we're rendering.
154 notification_registrar_.Add(
155 this, content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED,
156 content::Source<WebContents>(web_contents_.get()));
157 }
158
159 HiddenWebContents::~HiddenWebContents() {
160 if (web_contents_.get()) {
161 web_contents_->SetDelegate(NULL);
162 content::WebContentsObserver::Observe(NULL);
163 }
164 }
165
166 void HiddenWebContents::AddObserver(Observer* observer) {
167 observer_list_.AddObserver(observer);
168 }
169
170 void HiddenWebContents::RemoveObserver(Observer* observer) {
171 observer_list_.RemoveObserver(observer);
172 }
173
174 void HiddenWebContents::Observe(int type,
175 const content::NotificationSource& source,
176 const content::NotificationDetails& details) {
177 switch (type) {
178 // TODO(davidben): Try to remove this in favor of relying on
179 // FINAL_STATUS_PROFILE_DESTROYED.
180 case chrome::NOTIFICATION_APP_TERMINATING:
181 Destroy();
182 return;
183
184 case content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED: {
185 if (web_contents_.get()) {
186 DCHECK_EQ(content::Source<WebContents>(source).ptr(),
187 web_contents_.get());
188
189 // Make sure the size of the RenderViewHost has been passed to the new
190 // RenderView. Otherwise, the size may not be sent until the
191 // RenderViewReady event makes it from the render process to the UI
192 // thread of the browser process. When the RenderView receives its
193 // size, is also sets itself to be visible, which would then break the
194 // visibility API.
195 content::Details<RenderViewHost> new_render_view_host(details);
196 new_render_view_host->WasResized();
197 web_contents_->WasHidden();
198 }
199 break;
200 }
201
202 default:
203 NOTREACHED() << "Unexpected notification sent.";
204 break;
205 }
206 }
207
208 WebContents* HiddenWebContents::CreateWebContents(
209 SessionStorageNamespace* session_storage_namespace) {
210 // TODO(ajwong): Remove the temporary map once prerendering is aware of
211 // multiple session storage namespaces per tab.
212 content::SessionStorageNamespaceMap session_storage_namespace_map;
213 session_storage_namespace_map[std::string()] = session_storage_namespace;
214 return WebContents::CreateWithSessionStorage(
215 WebContents::CreateParams(profile_), session_storage_namespace_map);
216 }
217
218 void HiddenWebContents::NotifyFail() {
219 FOR_EACH_OBSERVER(Observer, observer_list_, OnFail(this));
220 observer_list_.Clear();
221 }
222
223 void HiddenWebContents::NotifyFinishedLoad() {
224 FOR_EACH_OBSERVER(Observer, observer_list_, OnFinishedLoad(this));
225 }
226
227 void HiddenWebContents::RenderProcessGone(base::TerminationStatus status) {
228 Destroy();
229 }
230
231 void HiddenWebContents::RenderFrameCreated(
232 content::RenderFrameHost* render_frame_host) {
233 // When a new RenderFrame is created for a hidden rendering WebContents, tell
234 // the new RenderFrame it's being used for prerendering before any
235 // navigations occur. Note that this is always triggered before the first
236 // navigation, so there's no need to send the message just after the
237 // WebContents is created.
238 render_frame_host->Send(new PrerenderMsg_SetIsPrerendering(
239 render_frame_host->GetRoutingID(), true));
240 }
241
242 void HiddenWebContents::DidStopLoading() {
243 }
244
245 void HiddenWebContents::DocumentLoadedInFrame(
246 content::RenderFrameHost* render_frame_host) {
247 }
248
249 void HiddenWebContents::DidStartProvisionalLoadForFrame(
250 content::RenderFrameHost* render_frame_host,
251 const GURL& validated_url,
252 bool is_error_page,
253 bool is_iframe_srcdoc) {
254 }
255
256 void HiddenWebContents::DidFinishLoad(
257 content::RenderFrameHost* render_frame_host,
258 const GURL& validated_url) {
259 // Ask the page to trigger an anchor navigation once the distilled
260 // contents are added to the page.
261 web_contents()->GetMainFrame()->ExecuteJavaScript(
262 base::UTF8ToUTF16("setNavigateOnInitialContentLoad(true);"));
263 }
264
265 void HiddenWebContents::DidNavigateMainFrame(
266 const content::LoadCommittedDetails& details,
267 const content::FrameNavigateParams& params) {
268 // The second content loads signals that the distilled contents have
269 // been delivered to the page via inline JavaScript execution.
270 if (web_contents_->GetController().GetEntryCount() > 1)
271 NotifyFinishedLoad();
272 }
273
274 void HiddenWebContents::DidGetRedirectForResourceRequest(
275 content::RenderFrameHost* render_frame_host,
276 const content::ResourceRedirectDetails& details) {
277 // Redirects are unsupported for hidden renderers.
278 Destroy();
279 }
280
281 void HiddenWebContents::Destroy() {
282 if (rendering_has_been_cancelled_)
283 return;
284
285 rendering_has_been_cancelled_ = true;
286 NotifyFail();
287 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698