OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/webui/print_preview/print_preview_distiller.h" | 5 #include "chrome/browser/ui/webui/print_preview/print_preview_distiller.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/strings/utf_string_conversions.h" | |
10 #include "chrome/browser/chrome_notification_types.h" | 9 #include "chrome/browser/chrome_notification_types.h" |
11 #include "chrome/browser/dom_distiller/tab_utils.h" | 10 #include "chrome/browser/dom_distiller/tab_utils.h" |
12 #include "chrome/browser/printing/print_preview_dialog_controller.h" | 11 #include "chrome/browser/printing/print_preview_dialog_controller.h" |
13 #include "chrome/browser/printing/print_preview_message_handler.h" | 12 #include "chrome/browser/printing/print_preview_message_handler.h" |
14 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/browser/ui/web_contents_sizer.h" | 14 #include "chrome/browser/ui/web_contents_sizer.h" |
16 #include "chrome/common/prerender_messages.h" | 15 #include "chrome/common/prerender_messages.h" |
17 #include "components/dom_distiller/content/browser/distiller_javascript_utils.h" | 16 #include "components/dom_distiller/content/browser/distiller_javascript_utils.h" |
18 #include "components/printing/common/print_messages.h" | 17 #include "components/printing/common/print_messages.h" |
19 #include "content/public/browser/notification_service.h" | 18 #include "content/public/browser/notification_service.h" |
20 #include "content/public/browser/render_frame_host.h" | 19 #include "content/public/browser/render_frame_host.h" |
21 #include "content/public/browser/render_process_host.h" | 20 #include "content/public/browser/render_process_host.h" |
22 #include "content/public/browser/render_view_host.h" | 21 #include "content/public/browser/render_view_host.h" |
23 #include "content/public/browser/session_storage_namespace.h" | 22 #include "content/public/browser/session_storage_namespace.h" |
24 #include "content/public/browser/web_contents.h" | 23 #include "content/public/browser/web_contents.h" |
25 #include "content/public/browser/web_contents_delegate.h" | 24 #include "content/public/browser/web_contents_delegate.h" |
26 #include "content/public/browser/web_contents_observer.h" | 25 #include "content/public/browser/web_contents_observer.h" |
27 | 26 |
28 using content::OpenURLParams; | 27 using content::OpenURLParams; |
29 using content::RenderViewHost; | 28 using content::RenderViewHost; |
30 using content::SessionStorageNamespace; | 29 using content::SessionStorageNamespace; |
31 using content::WebContents; | 30 using content::WebContents; |
32 | 31 |
32 namespace { | |
33 | |
34 WebContents* CreateWebContents( | |
35 WebContents* source_web_contents) { | |
36 // TODO(ajwong): Remove the temporary map once prerendering is aware of | |
37 // multiple session storage namespaces per tab. | |
38 content::SessionStorageNamespaceMap session_storage_namespace_map; | |
39 session_storage_namespace_map[std::string()] = | |
40 source_web_contents->GetController().GetDefaultSessionStorageNamespace(); | |
41 return WebContents::CreateWithSessionStorage( | |
42 WebContents::CreateParams(source_web_contents->GetBrowserContext()), | |
43 session_storage_namespace_map); | |
44 } | |
45 | |
46 } // namespace | |
47 | |
33 class PrintPreviewDistiller::WebContentsDelegateImpl | 48 class PrintPreviewDistiller::WebContentsDelegateImpl |
34 : public content::WebContentsDelegate, | 49 : public content::WebContentsDelegate, |
35 public content::NotificationObserver, | 50 public content::NotificationObserver, |
36 public content::WebContentsObserver { | 51 public content::WebContentsObserver { |
37 public: | 52 public: |
38 explicit WebContentsDelegateImpl(WebContents* web_contents, | 53 WebContentsDelegateImpl(WebContents* web_contents, |
Lei Zhang
2015/08/14 05:44:35
nit: not explicit
| |
39 scoped_ptr<base::DictionaryValue> settings, | 54 scoped_ptr<base::DictionaryValue> settings, |
40 const base::Closure on_failed_callback) | 55 const base::Closure& on_failed_callback) |
41 : content::WebContentsObserver(web_contents), | 56 : content::WebContentsObserver(web_contents), |
42 settings_(settings.Pass()), | 57 settings_(settings.Pass()), |
43 on_failed_callback_(on_failed_callback) { | 58 on_failed_callback_(on_failed_callback) { |
44 web_contents->SetDelegate(this); | 59 web_contents->SetDelegate(this); |
45 | 60 |
46 // Close ourselves when the application is shutting down. | 61 // Close ourselves when the application is shutting down. |
47 notification_registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING, | 62 notification_registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING, |
48 content::NotificationService::AllSources()); | 63 content::NotificationService::AllSources()); |
49 | 64 |
50 // Register to inform new RenderViews that we're rendering. | 65 // Register to inform new RenderViews that we're rendering. |
51 notification_registrar_.Add( | 66 notification_registrar_.Add( |
52 this, content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED, | 67 this, content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED, |
53 content::Source<WebContents>(web_contents)); | 68 content::Source<WebContents>(web_contents)); |
54 } | 69 } |
55 | 70 |
56 ~WebContentsDelegateImpl() override { web_contents()->SetDelegate(nullptr); } | 71 ~WebContentsDelegateImpl() override { web_contents()->SetDelegate(nullptr); } |
57 | 72 |
58 // content::WebContentsDelegate implementation. | 73 // content::WebContentsDelegate implementation. |
59 WebContents* OpenURLFromTab(WebContents* source, | 74 WebContents* OpenURLFromTab(WebContents* source, |
60 const OpenURLParams& params) override { | 75 const OpenURLParams& params) override { |
61 on_failed_callback_.Run(); | 76 on_failed_callback_.Run(); |
62 return nullptr; | 77 return nullptr; |
63 } | 78 } |
64 | 79 |
65 void CloseContents(content::WebContents* contents) override { | 80 void CloseContents(WebContents* contents) override { |
66 on_failed_callback_.Run(); | 81 on_failed_callback_.Run(); |
67 } | 82 } |
68 | 83 |
69 void CanDownload(const GURL& url, | 84 void CanDownload(const GURL& url, |
70 const std::string& request_method, | 85 const std::string& request_method, |
71 const base::Callback<void(bool)>& callback) override { | 86 const base::Callback<void(bool)>& callback) override { |
72 on_failed_callback_.Run(); | 87 on_failed_callback_.Run(); |
73 // Cancel the download. | 88 // Cancel the download. |
74 callback.Run(false); | 89 callback.Run(false); |
75 } | 90 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
112 const std::string& protocol, | 127 const std::string& protocol, |
113 const GURL& url, | 128 const GURL& url, |
114 bool user_gesture) override { | 129 bool user_gesture) override { |
115 on_failed_callback_.Run(); | 130 on_failed_callback_.Run(); |
116 } | 131 } |
117 | 132 |
118 void RenderFrameCreated( | 133 void RenderFrameCreated( |
119 content::RenderFrameHost* render_frame_host) override { | 134 content::RenderFrameHost* render_frame_host) override { |
120 // When a new RenderFrame is created for a distilled rendering | 135 // When a new RenderFrame is created for a distilled rendering |
121 // WebContents, tell the new RenderFrame it's being used for | 136 // WebContents, tell the new RenderFrame it's being used for |
122 // prerendering before any navigations occur. Note that this is | 137 // prerendering before any navigations occurs. Note that this is |
PhistucK
2015/08/14 06:34:03
I think you introduced a grammar error here - "nav
| |
123 // always triggered before the first navigation, so there's no | 138 // always triggered before the first navigation, so there's no |
124 // need to send the message just after the WebContents is created. | 139 // need to send the message just after the WebContents is created. |
125 render_frame_host->Send(new PrerenderMsg_SetIsPrerendering( | 140 render_frame_host->Send(new PrerenderMsg_SetIsPrerendering( |
126 render_frame_host->GetRoutingID(), true)); | 141 render_frame_host->GetRoutingID(), true)); |
127 } | 142 } |
128 | 143 |
129 void DidFinishLoad(content::RenderFrameHost* render_frame_host, | 144 void DidFinishLoad(content::RenderFrameHost* render_frame_host, |
130 const GURL& validated_url) override { | 145 const GURL& validated_url) override { |
131 // Ask the page to trigger an anchor navigation once the distilled | 146 // Ask the page to trigger an anchor navigation once the distilled |
132 // contents are added to the page. | 147 // contents are added to the page. |
133 dom_distiller::RunIsolatedJavaScript( | 148 dom_distiller::RunIsolatedJavaScript( |
134 web_contents()->GetMainFrame(), | 149 web_contents()->GetMainFrame(), |
135 "navigate_on_initial_content_load = true;"); | 150 "didNavigateOnInitialContentLoad()"); |
Lei Zhang
2015/08/14 05:44:35
I haven't tested this to make sure it works.
csaavedra
2015/08/14 08:21:37
This won't work, as this JS might run before the d
| |
136 } | 151 } |
137 | 152 |
138 void DidNavigateMainFrame( | 153 void DidNavigateMainFrame( |
139 const content::LoadCommittedDetails& details, | 154 const content::LoadCommittedDetails& details, |
140 const content::FrameNavigateParams& params) override { | 155 const content::FrameNavigateParams& params) override { |
141 // The second content loads signals that the distilled contents have | 156 // The second content loads signals that the distilled contents have |
142 // been delivered to the page via inline JavaScript execution. | 157 // been delivered to the page via inline JavaScript execution. |
143 if (web_contents()->GetController().GetEntryCount() > 1) { | 158 if (web_contents()->GetController().GetEntryCount() > 1) { |
144 RenderViewHost* rvh = web_contents()->GetRenderViewHost(); | 159 RenderViewHost* rvh = web_contents()->GetRenderViewHost(); |
145 rvh->Send(new PrintMsg_InitiatePrintPreview(rvh->GetRoutingID(), false)); | 160 rvh->Send(new PrintMsg_InitiatePrintPreview(rvh->GetRoutingID(), false)); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
188 default: | 203 default: |
189 NOTREACHED() << "Unexpected notification sent."; | 204 NOTREACHED() << "Unexpected notification sent."; |
190 break; | 205 break; |
191 } | 206 } |
192 } | 207 } |
193 | 208 |
194 private: | 209 private: |
195 scoped_ptr<base::DictionaryValue> settings_; | 210 scoped_ptr<base::DictionaryValue> settings_; |
196 content::NotificationRegistrar notification_registrar_; | 211 content::NotificationRegistrar notification_registrar_; |
197 | 212 |
198 // The callback called when the preview failed. | 213 // The callback called when the distilling failed. |
199 base::Closure on_failed_callback_; | 214 const base::Closure on_failed_callback_; |
Lei Zhang
2015/08/14 05:44:35
const since it never changes
| |
200 }; | 215 }; |
201 | 216 |
202 PrintPreviewDistiller::PrintPreviewDistiller( | 217 PrintPreviewDistiller::PrintPreviewDistiller( |
203 WebContents* source_web_contents, | 218 WebContents* source_web_contents, |
204 const base::Closure on_failed_callback, | 219 const base::Closure& on_failed_callback, |
205 scoped_ptr<base::DictionaryValue> settings) { | 220 scoped_ptr<base::DictionaryValue> settings) { |
206 content::SessionStorageNamespace* session_storage_namespace = | 221 CreateDestinationWebContents(source_web_contents, |
Lei Zhang
2015/08/14 05:44:35
Since this is derived from |source_web_content|, y
| |
207 source_web_contents->GetController().GetDefaultSessionStorageNamespace(); | 222 settings.Pass(), |
208 CreateDestinationWebContents(session_storage_namespace, source_web_contents, | 223 on_failed_callback); |
209 settings.Pass(), on_failed_callback); | 224 DistillAndView(source_web_contents, web_contents_.get()); |
225 } | |
210 | 226 |
211 DCHECK(web_contents_); | 227 PrintPreviewDistiller::~PrintPreviewDistiller() { |
Lei Zhang
2015/08/14 05:44:35
DistillAndView() will DCHECK its arguments, so thi
| |
212 ::DistillAndView(source_web_contents, web_contents_.get()); | 228 printing::PrintPreviewDialogController* dialog_controller = |
229 printing::PrintPreviewDialogController::GetInstance(); | |
230 if (!dialog_controller) | |
231 return; | |
232 | |
233 dialog_controller->RemoveProxyDialogForWebContents(web_contents_.get()); | |
213 } | 234 } |
214 | 235 |
215 void PrintPreviewDistiller::CreateDestinationWebContents( | 236 void PrintPreviewDistiller::CreateDestinationWebContents( |
216 SessionStorageNamespace* session_storage_namespace, | |
217 WebContents* source_web_contents, | 237 WebContents* source_web_contents, |
218 scoped_ptr<base::DictionaryValue> settings, | 238 scoped_ptr<base::DictionaryValue> settings, |
219 const base::Closure on_failed_callback) { | 239 const base::Closure& on_failed_callback) { |
220 DCHECK(!web_contents_); | 240 DCHECK(!web_contents_); |
221 | 241 |
222 web_contents_.reset( | 242 web_contents_.reset(CreateWebContents(source_web_contents)); |
223 CreateWebContents(session_storage_namespace, source_web_contents)); | |
224 | 243 |
225 printing::PrintPreviewMessageHandler::CreateForWebContents( | 244 printing::PrintPreviewMessageHandler::CreateForWebContents( |
226 web_contents_.get()); | 245 web_contents_.get()); |
227 | 246 |
228 web_contents_delegate_.reset(new WebContentsDelegateImpl( | 247 web_contents_delegate_.reset(new WebContentsDelegateImpl( |
229 web_contents_.get(), settings.Pass(), on_failed_callback)); | 248 web_contents_.get(), settings.Pass(), on_failed_callback)); |
230 | 249 |
231 // Set the size of the distilled WebContents. | 250 // Set the size of the distilled WebContents. |
232 ResizeWebContents(web_contents_.get(), gfx::Size(1, 1)); | 251 ResizeWebContents(web_contents_.get(), gfx::Size(1, 1)); |
233 | 252 |
234 printing::PrintPreviewDialogController* dialog_controller = | 253 printing::PrintPreviewDialogController* dialog_controller = |
235 printing::PrintPreviewDialogController::GetInstance(); | 254 printing::PrintPreviewDialogController::GetInstance(); |
236 if (!dialog_controller) | 255 if (!dialog_controller) |
237 return; | 256 return; |
238 | 257 |
239 dialog_controller->AddProxyDialogForWebContents(web_contents_.get(), | 258 dialog_controller->AddProxyDialogForWebContents(web_contents_.get(), |
240 source_web_contents); | 259 source_web_contents); |
241 } | 260 } |
242 | |
243 PrintPreviewDistiller::~PrintPreviewDistiller() { | |
Lei Zhang
2015/08/14 05:44:35
Please put the dtor right below the ctor, in the s
| |
244 if (web_contents_) { | |
245 printing::PrintPreviewDialogController* dialog_controller = | |
246 printing::PrintPreviewDialogController::GetInstance(); | |
247 if (!dialog_controller) | |
248 return; | |
249 | |
250 dialog_controller->RemoveProxyDialogForWebContents(web_contents_.get()); | |
251 } | |
252 } | |
253 | |
254 WebContents* PrintPreviewDistiller::CreateWebContents( | |
Lei Zhang
2015/08/14 05:44:35
This is a standalone function, so it might as well
| |
255 SessionStorageNamespace* session_storage_namespace, | |
256 WebContents* source_web_contents) { | |
257 // TODO(ajwong): Remove the temporary map once prerendering is aware of | |
258 // multiple session storage namespaces per tab. | |
259 content::SessionStorageNamespaceMap session_storage_namespace_map; | |
260 Profile* profile = | |
261 Profile::FromBrowserContext(source_web_contents->GetBrowserContext()); | |
262 session_storage_namespace_map[std::string()] = session_storage_namespace; | |
263 return WebContents::CreateWithSessionStorage( | |
264 WebContents::CreateParams(profile), session_storage_namespace_map); | |
265 } | |
OLD | NEW |