1 // Copyright 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.
5 #include "chrome/browser/ui/webui/print_preview/print_preview_distiller.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/dom_distiller/tab_utils.h"
12 #include "chrome/browser/printing/print_preview_dialog_controller.h"
13 #include "chrome/browser/printing/print_preview_message_handler.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/web_contents_sizer.h"
16 #include "chrome/common/prerender_messages.h"
17 #include "components/dom_distiller/content/browser/distiller_javascript_utils.h"
18 #include "components/printing/common/print_messages.h"
19 #include "content/public/browser/notification_service.h"
20 #include "content/public/browser/render_frame_host.h"
21 #include "content/public/browser/render_process_host.h"
22 #include "content/public/browser/render_view_host.h"
23 #include "content/public/browser/resource_request_details.h"
24 #include "content/public/browser/session_storage_namespace.h"
25 #include "content/public/browser/web_contents.h"
26 #include "content/public/browser/web_contents_delegate.h"
27 #include "content/public/browser/web_contents_observer.h"
29 using content::OpenURLParams
;
30 using content::RenderViewHost
;
31 using content::SessionStorageNamespace
;
32 using content::WebContents
;
34 class PrintPreviewDistiller::WebContentsDelegateImpl
35 : public content::WebContentsDelegate
,
36 public content::NotificationObserver
,
37 public content::WebContentsObserver
{
39 explicit WebContentsDelegateImpl(WebContents
* web_contents
,
40 scoped_ptr
<base::DictionaryValue
> settings
,
41 const base::Closure on_failed_callback
)
42 : content::WebContentsObserver(web_contents
),
43 settings_(settings
.Pass()),
44 on_failed_callback_(on_failed_callback
) {
45 web_contents
->SetDelegate(this);
47 // Close ourselves when the application is shutting down.
48 notification_registrar_
.Add(this, chrome::NOTIFICATION_APP_TERMINATING
,
49 content::NotificationService::AllSources());
51 // Register to inform new RenderViews that we're rendering.
52 notification_registrar_
.Add(
53 this, content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED
,
54 content::Source
<WebContents
>(web_contents
));
57 ~WebContentsDelegateImpl() override
{ web_contents()->SetDelegate(nullptr); }
59 // content::WebContentsDelegate implementation.
60 WebContents
* OpenURLFromTab(WebContents
* source
,
61 const OpenURLParams
& params
) override
{
62 on_failed_callback_
.Run();
66 void CloseContents(content::WebContents
* contents
) override
{
67 on_failed_callback_
.Run();
70 void CanDownload(const GURL
& url
,
71 const std::string
& request_method
,
72 const base::Callback
<void(bool)>& callback
) override
{
73 on_failed_callback_
.Run();
74 // Cancel the download.
78 bool ShouldCreateWebContents(
79 WebContents
* web_contents
,
81 int main_frame_route_id
,
82 WindowContainerType window_container_type
,
83 const std::string
& frame_name
,
84 const GURL
& target_url
,
85 const std::string
& partition_id
,
86 SessionStorageNamespace
* session_storage_namespace
) override
{
87 // Since we don't want to permit child windows that would have a
88 // window.opener property, terminate rendering.
89 on_failed_callback_
.Run();
94 bool OnGoToEntryOffset(int offset
) override
{
95 // This isn't allowed because the history merge operation
96 // does not work if there are renderer issued challenges.
97 // TODO(cbentzel): Cancel in this case? May not need to do
98 // since render-issued offset navigations are not guaranteed,
99 // but indicates that the page cares about the history.
103 bool ShouldSuppressDialogs(WebContents
* source
) override
{
104 // We still want to show the user the message when they navigate to this
105 // page, so cancel this render.
106 on_failed_callback_
.Run();
107 // Always suppress JavaScript messages if they're triggered by a page being
112 void RegisterProtocolHandler(WebContents
* web_contents
,
113 const std::string
& protocol
,
115 bool user_gesture
) override
{
116 on_failed_callback_
.Run();
119 void RenderFrameCreated(
120 content::RenderFrameHost
* render_frame_host
) override
{
121 // When a new RenderFrame is created for a distilled rendering
122 // WebContents, tell the new RenderFrame it's being used for
123 // prerendering before any navigations occur. Note that this is
124 // always triggered before the first navigation, so there's no
125 // need to send the message just after the WebContents is created.
126 render_frame_host
->Send(new PrerenderMsg_SetIsPrerendering(
127 render_frame_host
->GetRoutingID(), true));
130 void DidFinishLoad(content::RenderFrameHost
* render_frame_host
,
131 const GURL
& validated_url
) override
{
132 // Ask the page to trigger an anchor navigation once the distilled
133 // contents are added to the page.
134 dom_distiller::RunIsolatedJavaScript(
135 web_contents()->GetMainFrame(),
136 "navigate_on_initial_content_load = true;");
139 void DidNavigateMainFrame(
140 const content::LoadCommittedDetails
& details
,
141 const content::FrameNavigateParams
& params
) override
{
142 // The second content loads signals that the distilled contents have
143 // been delivered to the page via inline JavaScript execution.
144 if (web_contents()->GetController().GetEntryCount() > 1) {
145 RenderViewHost
* rvh
= web_contents()->GetRenderViewHost();
146 rvh
->Send(new PrintMsg_InitiatePrintPreview(rvh
->GetRoutingID(), false));
147 rvh
->Send(new PrintMsg_PrintPreview(rvh
->GetRoutingID(), *settings_
));
151 void DidGetRedirectForResourceRequest(
152 content::RenderFrameHost
* render_frame_host
,
153 const content::ResourceRedirectDetails
& details
) override
{
154 if (details
.resource_type
!= content::RESOURCE_TYPE_MAIN_FRAME
)
156 // Redirects are unsupported for distilled content renderers.
157 on_failed_callback_
.Run();
160 void RenderProcessGone(base::TerminationStatus status
) override
{
161 on_failed_callback_
.Run();
164 void Observe(int type
,
165 const content::NotificationSource
& source
,
166 const content::NotificationDetails
& details
) override
{
168 // TODO(davidben): Try to remove this in favor of relying on
169 // FINAL_STATUS_PROFILE_DESTROYED.
170 case chrome::NOTIFICATION_APP_TERMINATING
:
171 on_failed_callback_
.Run();
174 case content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED
: {
175 if (web_contents()) {
176 DCHECK_EQ(content::Source
<WebContents
>(source
).ptr(), web_contents());
178 // Make sure the size of the RenderViewHost has been passed to the new
179 // RenderView. Otherwise, the size may not be sent until the
180 // RenderViewReady event makes it from the render process to the UI
181 // thread of the browser process. When the RenderView receives its
182 // size, is also sets itself to be visible, which would then break the
184 content::Details
<RenderViewHost
> new_render_view_host(details
);
185 new_render_view_host
->WasResized();
186 web_contents()->WasHidden();
192 NOTREACHED() << "Unexpected notification sent.";
198 scoped_ptr
<base::DictionaryValue
> settings_
;
199 content::NotificationRegistrar notification_registrar_
;
201 // The callback called when the preview failed.
202 base::Closure on_failed_callback_
;
205 PrintPreviewDistiller::PrintPreviewDistiller(
206 WebContents
* source_web_contents
,
207 const base::Closure on_failed_callback
,
208 scoped_ptr
<base::DictionaryValue
> settings
) {
209 content::SessionStorageNamespace
* session_storage_namespace
=
210 source_web_contents
->GetController().GetDefaultSessionStorageNamespace();
211 CreateDestinationWebContents(session_storage_namespace
, source_web_contents
,
212 settings
.Pass(), on_failed_callback
);
214 DCHECK(web_contents_
);
215 ::DistillAndView(source_web_contents
, web_contents_
.get());
218 void PrintPreviewDistiller::CreateDestinationWebContents(
219 SessionStorageNamespace
* session_storage_namespace
,
220 WebContents
* source_web_contents
,
221 scoped_ptr
<base::DictionaryValue
> settings
,
222 const base::Closure on_failed_callback
) {
223 DCHECK(!web_contents_
);
226 CreateWebContents(session_storage_namespace
, source_web_contents
));
228 printing::PrintPreviewMessageHandler::CreateForWebContents(
229 web_contents_
.get());
231 web_contents_delegate_
.reset(new WebContentsDelegateImpl(
232 web_contents_
.get(), settings
.Pass(), on_failed_callback
));
234 // Set the size of the distilled WebContents.
235 ResizeWebContents(web_contents_
.get(), gfx::Size(1, 1));
237 printing::PrintPreviewDialogController
* dialog_controller
=
238 printing::PrintPreviewDialogController::GetInstance();
239 if (!dialog_controller
)
242 dialog_controller
->AddProxyDialogForWebContents(web_contents_
.get(),
243 source_web_contents
);
246 PrintPreviewDistiller::~PrintPreviewDistiller() {
248 printing::PrintPreviewDialogController
* dialog_controller
=
249 printing::PrintPreviewDialogController::GetInstance();
250 if (!dialog_controller
)
253 dialog_controller
->RemoveProxyDialogForWebContents(web_contents_
.get());
257 WebContents
* PrintPreviewDistiller::CreateWebContents(
258 SessionStorageNamespace
* session_storage_namespace
,
259 WebContents
* source_web_contents
) {
260 // TODO(ajwong): Remove the temporary map once prerendering is aware of
261 // multiple session storage namespaces per tab.
262 content::SessionStorageNamespaceMap session_storage_namespace_map
;
264 Profile::FromBrowserContext(source_web_contents
->GetBrowserContext());
265 session_storage_namespace_map
[std::string()] = session_storage_namespace
;
266 return WebContents::CreateWithSessionStorage(
267 WebContents::CreateParams(profile
), session_storage_namespace_map
);