| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef COMPONENTS_PRINTING_RENDERER_PRINT_WEB_VIEW_HELPER_H_ | 5 #ifndef COMPONENTS_PRINTING_RENDERER_PRINT_WEB_VIEW_HELPER_H_ |
| 6 #define COMPONENTS_PRINTING_RENDERER_PRINT_WEB_VIEW_HELPER_H_ | 6 #define COMPONENTS_PRINTING_RENDERER_PRINT_WEB_VIEW_HELPER_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/shared_memory.h" | 14 #include "base/memory/shared_memory.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 17 #include "build/build_config.h" | 17 #include "build/build_config.h" |
| 18 #include "content/public/renderer/render_view_observer.h" | 18 #include "content/public/renderer/render_view_observer.h" |
| 19 #include "content/public/renderer/render_view_observer_tracker.h" | 19 #include "content/public/renderer/render_view_observer_tracker.h" |
| 20 #include "printing/features/features.h" |
| 20 #include "printing/pdf_metafile_skia.h" | 21 #include "printing/pdf_metafile_skia.h" |
| 21 #include "third_party/WebKit/public/platform/WebCanvas.h" | 22 #include "third_party/WebKit/public/platform/WebCanvas.h" |
| 22 #include "third_party/WebKit/public/web/WebNode.h" | 23 #include "third_party/WebKit/public/web/WebNode.h" |
| 23 #include "third_party/WebKit/public/web/WebPrintParams.h" | 24 #include "third_party/WebKit/public/web/WebPrintParams.h" |
| 24 #include "ui/gfx/geometry/size.h" | 25 #include "ui/gfx/geometry/size.h" |
| 25 | 26 |
| 26 struct PrintMsg_Print_Params; | 27 struct PrintMsg_Print_Params; |
| 27 struct PrintMsg_PrintPage_Params; | 28 struct PrintMsg_PrintPage_Params; |
| 28 struct PrintMsg_PrintPages_Params; | 29 struct PrintMsg_PrintPages_Params; |
| 29 struct PrintHostMsg_SetOptionsFromDocument_Params; | 30 struct PrintHostMsg_SetOptionsFromDocument_Params; |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 BlockScriptInitiatedPrintingFromPopup); | 132 BlockScriptInitiatedPrintingFromPopup); |
| 132 #if defined(OS_WIN) || defined(OS_MACOSX) | 133 #if defined(OS_WIN) || defined(OS_MACOSX) |
| 133 FRIEND_TEST_ALL_PREFIXES(MAYBE_PrintWebViewHelperTest, PrintLayoutTest); | 134 FRIEND_TEST_ALL_PREFIXES(MAYBE_PrintWebViewHelperTest, PrintLayoutTest); |
| 134 FRIEND_TEST_ALL_PREFIXES(MAYBE_PrintWebViewHelperTest, PrintWithIframe); | 135 FRIEND_TEST_ALL_PREFIXES(MAYBE_PrintWebViewHelperTest, PrintWithIframe); |
| 135 #endif // defined(OS_WIN) || defined(OS_MACOSX) | 136 #endif // defined(OS_WIN) || defined(OS_MACOSX) |
| 136 | 137 |
| 137 enum PrintingResult { | 138 enum PrintingResult { |
| 138 OK, | 139 OK, |
| 139 FAIL_PRINT_INIT, | 140 FAIL_PRINT_INIT, |
| 140 FAIL_PRINT, | 141 FAIL_PRINT, |
| 141 #if defined(ENABLE_PRINT_PREVIEW) | 142 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) |
| 142 FAIL_PREVIEW, | 143 FAIL_PREVIEW, |
| 143 #endif | 144 #endif |
| 144 }; | 145 }; |
| 145 | 146 |
| 146 enum PrintPreviewErrorBuckets { | 147 enum PrintPreviewErrorBuckets { |
| 147 PREVIEW_ERROR_NONE, // Always first. | 148 PREVIEW_ERROR_NONE, // Always first. |
| 148 PREVIEW_ERROR_BAD_SETTING, | 149 PREVIEW_ERROR_BAD_SETTING, |
| 149 PREVIEW_ERROR_METAFILE_COPY_FAILED, | 150 PREVIEW_ERROR_METAFILE_COPY_FAILED, |
| 150 PREVIEW_ERROR_METAFILE_INIT_FAILED_DEPRECATED, | 151 PREVIEW_ERROR_METAFILE_INIT_FAILED_DEPRECATED, |
| 151 PREVIEW_ERROR_ZERO_PAGES, | 152 PREVIEW_ERROR_ZERO_PAGES, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 163 }; | 164 }; |
| 164 | 165 |
| 165 // RenderViewObserver implementation. | 166 // RenderViewObserver implementation. |
| 166 bool OnMessageReceived(const IPC::Message& message) override; | 167 bool OnMessageReceived(const IPC::Message& message) override; |
| 167 void PrintPage(blink::WebLocalFrame* frame, bool user_initiated) override; | 168 void PrintPage(blink::WebLocalFrame* frame, bool user_initiated) override; |
| 168 void DidStartLoading() override; | 169 void DidStartLoading() override; |
| 169 void DidStopLoading() override; | 170 void DidStopLoading() override; |
| 170 void OnDestruct() override; | 171 void OnDestruct() override; |
| 171 | 172 |
| 172 // Message handlers --------------------------------------------------------- | 173 // Message handlers --------------------------------------------------------- |
| 173 #if defined(ENABLE_BASIC_PRINTING) | 174 #if BUILDFLAG(ENABLE_BASIC_PRINTING) |
| 174 void OnPrintPages(); | 175 void OnPrintPages(); |
| 175 void OnPrintForSystemDialog(); | 176 void OnPrintForSystemDialog(); |
| 176 void OnPrintForPrintPreview(const base::DictionaryValue& job_settings); | 177 void OnPrintForPrintPreview(const base::DictionaryValue& job_settings); |
| 177 #endif // defined(ENABLE_BASIC_PRINTING) | 178 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING) |
| 178 #if defined(ENABLE_PRINT_PREVIEW) | 179 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) |
| 179 void OnInitiatePrintPreview(bool selection_only); | 180 void OnInitiatePrintPreview(bool selection_only); |
| 180 void OnPrintPreview(const base::DictionaryValue& settings); | 181 void OnPrintPreview(const base::DictionaryValue& settings); |
| 181 #endif // defined(ENABLE_PRINT_PREVIEW) | 182 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) |
| 182 void OnPrintingDone(bool success); | 183 void OnPrintingDone(bool success); |
| 183 | 184 |
| 184 // Get |page_size| and |content_area| information from | 185 // Get |page_size| and |content_area| information from |
| 185 // |page_layout_in_points|. | 186 // |page_layout_in_points|. |
| 186 void GetPageSizeAndContentAreaFromPageLayout( | 187 void GetPageSizeAndContentAreaFromPageLayout( |
| 187 const PageSizeMargins& page_layout_in_points, | 188 const PageSizeMargins& page_layout_in_points, |
| 188 gfx::Size* page_size, | 189 gfx::Size* page_size, |
| 189 gfx::Rect* content_area); | 190 gfx::Rect* content_area); |
| 190 | 191 |
| 191 // Update |ignore_css_margins_| based on settings. | 192 // Update |ignore_css_margins_| based on settings. |
| 192 void UpdateFrameMarginsCssInfo(const base::DictionaryValue& settings); | 193 void UpdateFrameMarginsCssInfo(const base::DictionaryValue& settings); |
| 193 | 194 |
| 194 #if defined(ENABLE_PRINT_PREVIEW) | 195 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) |
| 195 // Prepare frame for creating preview document. | 196 // Prepare frame for creating preview document. |
| 196 void PrepareFrameForPreviewDocument(); | 197 void PrepareFrameForPreviewDocument(); |
| 197 | 198 |
| 198 // Continue creating preview document. | 199 // Continue creating preview document. |
| 199 void OnFramePreparedForPreviewDocument(); | 200 void OnFramePreparedForPreviewDocument(); |
| 200 | 201 |
| 201 // Initialize the print preview document. | 202 // Initialize the print preview document. |
| 202 bool CreatePreviewDocument(); | 203 bool CreatePreviewDocument(); |
| 203 | 204 |
| 204 // Renders a print preview page. |page_number| is 0-based. | 205 // Renders a print preview page. |page_number| is 0-based. |
| 205 // Returns true if print preview should continue, false on failure. | 206 // Returns true if print preview should continue, false on failure. |
| 206 bool RenderPreviewPage(int page_number, | 207 bool RenderPreviewPage(int page_number, |
| 207 const PrintMsg_Print_Params& print_params); | 208 const PrintMsg_Print_Params& print_params); |
| 208 | 209 |
| 209 // Finalize the print ready preview document. | 210 // Finalize the print ready preview document. |
| 210 bool FinalizePrintReadyDocument(); | 211 bool FinalizePrintReadyDocument(); |
| 211 #endif // defined(ENABLE_PRINT_PREVIEW) | 212 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) |
| 212 | 213 |
| 213 // Enable/Disable window.print calls. If |blocked| is true window.print | 214 // Enable/Disable window.print calls. If |blocked| is true window.print |
| 214 // calls will silently fail. Call with |blocked| set to false to reenable. | 215 // calls will silently fail. Call with |blocked| set to false to reenable. |
| 215 void SetScriptedPrintBlocked(bool blocked); | 216 void SetScriptedPrintBlocked(bool blocked); |
| 216 | 217 |
| 217 // Main printing code ------------------------------------------------------- | 218 // Main printing code ------------------------------------------------------- |
| 218 | 219 |
| 219 #if defined(ENABLE_BASIC_PRINTING) | 220 #if BUILDFLAG(ENABLE_BASIC_PRINTING) |
| 220 // |is_scripted| should be true when the call is coming from window.print() | 221 // |is_scripted| should be true when the call is coming from window.print() |
| 221 void Print(blink::WebLocalFrame* frame, | 222 void Print(blink::WebLocalFrame* frame, |
| 222 const blink::WebNode& node, | 223 const blink::WebNode& node, |
| 223 bool is_scripted); | 224 bool is_scripted); |
| 224 #endif // defined(ENABLE_BASIC_PRINTING) | 225 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING) |
| 225 | 226 |
| 226 // Notification when printing is done - signal tear-down/free resources. | 227 // Notification when printing is done - signal tear-down/free resources. |
| 227 void DidFinishPrinting(PrintingResult result); | 228 void DidFinishPrinting(PrintingResult result); |
| 228 | 229 |
| 229 // Print Settings ----------------------------------------------------------- | 230 // Print Settings ----------------------------------------------------------- |
| 230 | 231 |
| 231 // Initialize print page settings with default settings. | 232 // Initialize print page settings with default settings. |
| 232 // Used only for native printing workflow. | 233 // Used only for native printing workflow. |
| 233 bool InitPrintSettings(bool fit_to_paper_size); | 234 bool InitPrintSettings(bool fit_to_paper_size); |
| 234 | 235 |
| 235 // Calculate number of pages in source document. | 236 // Calculate number of pages in source document. |
| 236 bool CalculateNumberOfPages(blink::WebLocalFrame* frame, | 237 bool CalculateNumberOfPages(blink::WebLocalFrame* frame, |
| 237 const blink::WebNode& node, | 238 const blink::WebNode& node, |
| 238 int* number_of_pages); | 239 int* number_of_pages); |
| 239 | 240 |
| 240 #if defined(ENABLE_PRINT_PREVIEW) | 241 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) |
| 241 // Set options for print preset from source PDF document. | 242 // Set options for print preset from source PDF document. |
| 242 bool SetOptionsFromPdfDocument( | 243 bool SetOptionsFromPdfDocument( |
| 243 PrintHostMsg_SetOptionsFromDocument_Params* options); | 244 PrintHostMsg_SetOptionsFromDocument_Params* options); |
| 244 | 245 |
| 245 // Update the current print settings with new |passed_job_settings|. | 246 // Update the current print settings with new |passed_job_settings|. |
| 246 // |passed_job_settings| dictionary contains print job details such as printer | 247 // |passed_job_settings| dictionary contains print job details such as printer |
| 247 // name, number of copies, page range, etc. | 248 // name, number of copies, page range, etc. |
| 248 bool UpdatePrintSettings(blink::WebLocalFrame* frame, | 249 bool UpdatePrintSettings(blink::WebLocalFrame* frame, |
| 249 const blink::WebNode& node, | 250 const blink::WebNode& node, |
| 250 const base::DictionaryValue& passed_job_settings); | 251 const base::DictionaryValue& passed_job_settings); |
| 251 #endif // defined(ENABLE_PRINT_PREVIEW) | 252 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) |
| 252 | 253 |
| 253 // Get final print settings from the user. | 254 // Get final print settings from the user. |
| 254 // Return false if the user cancels or on error. | 255 // Return false if the user cancels or on error. |
| 255 bool GetPrintSettingsFromUser(blink::WebLocalFrame* frame, | 256 bool GetPrintSettingsFromUser(blink::WebLocalFrame* frame, |
| 256 const blink::WebNode& node, | 257 const blink::WebNode& node, |
| 257 int expected_pages_count, | 258 int expected_pages_count, |
| 258 bool is_scripted); | 259 bool is_scripted); |
| 259 | 260 |
| 260 // Page Printing / Rendering ------------------------------------------------ | 261 // Page Printing / Rendering ------------------------------------------------ |
| 261 | 262 |
| 262 #if defined(ENABLE_BASIC_PRINTING) | 263 #if BUILDFLAG(ENABLE_BASIC_PRINTING) |
| 263 void OnFramePreparedForPrintPages(); | 264 void OnFramePreparedForPrintPages(); |
| 264 void PrintPages(); | 265 void PrintPages(); |
| 265 bool PrintPagesNative(blink::WebLocalFrame* frame, int page_count); | 266 bool PrintPagesNative(blink::WebLocalFrame* frame, int page_count); |
| 266 void FinishFramePrinting(); | 267 void FinishFramePrinting(); |
| 267 // Render the frame for printing. | 268 // Render the frame for printing. |
| 268 bool RenderPagesForPrint(blink::WebLocalFrame* frame, | 269 bool RenderPagesForPrint(blink::WebLocalFrame* frame, |
| 269 const blink::WebNode& node); | 270 const blink::WebNode& node); |
| 270 #endif // defined(ENABLE_BASIC_PRINTING) | 271 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING) |
| 271 | 272 |
| 272 // Prints the page listed in |params|. | 273 // Prints the page listed in |params|. |
| 273 #if defined(OS_MACOSX) | 274 #if defined(OS_MACOSX) |
| 274 void PrintPageInternal(const PrintMsg_PrintPage_Params& params, | 275 void PrintPageInternal(const PrintMsg_PrintPage_Params& params, |
| 275 blink::WebLocalFrame* frame); | 276 blink::WebLocalFrame* frame); |
| 276 #else | 277 #else |
| 277 void PrintPageInternal(const PrintMsg_PrintPage_Params& params, | 278 void PrintPageInternal(const PrintMsg_PrintPage_Params& params, |
| 278 blink::WebLocalFrame* frame, | 279 blink::WebLocalFrame* frame, |
| 279 PdfMetafileSkia* metafile, | 280 PdfMetafileSkia* metafile, |
| 280 gfx::Size* page_size_in_dpi, | 281 gfx::Size* page_size_in_dpi, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 bool ignore_css_margins, | 317 bool ignore_css_margins, |
| 317 double* scale_factor, | 318 double* scale_factor, |
| 318 PageSizeMargins* page_layout_in_points); | 319 PageSizeMargins* page_layout_in_points); |
| 319 | 320 |
| 320 // Return an array of pages to print given the print |params| and an expected | 321 // Return an array of pages to print given the print |params| and an expected |
| 321 // |page_count|. Page numbers are zero-based. | 322 // |page_count|. Page numbers are zero-based. |
| 322 static std::vector<int> GetPrintedPages( | 323 static std::vector<int> GetPrintedPages( |
| 323 const PrintMsg_PrintPages_Params& params, | 324 const PrintMsg_PrintPages_Params& params, |
| 324 int page_count); | 325 int page_count); |
| 325 | 326 |
| 326 #if defined(ENABLE_PRINT_PREVIEW) | 327 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) |
| 327 // Given the |device| and |canvas| to draw on, prints the appropriate headers | 328 // Given the |device| and |canvas| to draw on, prints the appropriate headers |
| 328 // and footers using strings from |header_footer_info| on to the canvas. | 329 // and footers using strings from |header_footer_info| on to the canvas. |
| 329 static void PrintHeaderAndFooter(blink::WebCanvas* canvas, | 330 static void PrintHeaderAndFooter(blink::WebCanvas* canvas, |
| 330 int page_number, | 331 int page_number, |
| 331 int total_pages, | 332 int total_pages, |
| 332 const blink::WebFrame& source_frame, | 333 const blink::WebFrame& source_frame, |
| 333 float webkit_scale_factor, | 334 float webkit_scale_factor, |
| 334 const PageSizeMargins& page_layout_in_points, | 335 const PageSizeMargins& page_layout_in_points, |
| 335 const PrintMsg_Print_Params& params); | 336 const PrintMsg_Print_Params& params); |
| 336 #endif // defined(ENABLE_PRINT_PREVIEW) | 337 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) |
| 337 | 338 |
| 338 bool GetPrintFrame(blink::WebLocalFrame** frame); | 339 bool GetPrintFrame(blink::WebLocalFrame** frame); |
| 339 | 340 |
| 340 // Script Initiated Printing ------------------------------------------------ | 341 // Script Initiated Printing ------------------------------------------------ |
| 341 | 342 |
| 342 // Return true if script initiated printing is currently | 343 // Return true if script initiated printing is currently |
| 343 // allowed. |user_initiated| should be true when a user event triggered the | 344 // allowed. |user_initiated| should be true when a user event triggered the |
| 344 // script, most likely by pressing a print button on the page. | 345 // script, most likely by pressing a print button on the page. |
| 345 bool IsScriptInitiatedPrintAllowed(blink::WebFrame* frame, | 346 bool IsScriptInitiatedPrintAllowed(blink::WebFrame* frame, |
| 346 bool user_initiated); | 347 bool user_initiated); |
| 347 | 348 |
| 348 #if defined(ENABLE_PRINT_PREVIEW) | 349 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) |
| 349 // Shows scripted print preview when options from plugin are available. | 350 // Shows scripted print preview when options from plugin are available. |
| 350 void ShowScriptedPrintPreview(); | 351 void ShowScriptedPrintPreview(); |
| 351 | 352 |
| 352 void RequestPrintPreview(PrintPreviewRequestType type); | 353 void RequestPrintPreview(PrintPreviewRequestType type); |
| 353 | 354 |
| 354 // Checks whether print preview should continue or not. | 355 // Checks whether print preview should continue or not. |
| 355 // Returns true if canceling, false if continuing. | 356 // Returns true if canceling, false if continuing. |
| 356 bool CheckForCancel(); | 357 bool CheckForCancel(); |
| 357 | 358 |
| 358 // Notifies the browser a print preview page has been rendered. | 359 // Notifies the browser a print preview page has been rendered. |
| 359 // |page_number| is 0-based. | 360 // |page_number| is 0-based. |
| 360 // For a valid |page_number| with modifiable content, | 361 // For a valid |page_number| with modifiable content, |
| 361 // |metafile| is the rendered page. Otherwise |metafile| is NULL. | 362 // |metafile| is the rendered page. Otherwise |metafile| is NULL. |
| 362 // Returns true if print preview should continue, false on failure. | 363 // Returns true if print preview should continue, false on failure. |
| 363 bool PreviewPageRendered(int page_number, PdfMetafileSkia* metafile); | 364 bool PreviewPageRendered(int page_number, PdfMetafileSkia* metafile); |
| 364 #endif // defined(ENABLE_PRINT_PREVIEW) | 365 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) |
| 365 | 366 |
| 366 void SetPrintPagesParams(const PrintMsg_PrintPages_Params& settings); | 367 void SetPrintPagesParams(const PrintMsg_PrintPages_Params& settings); |
| 367 | 368 |
| 368 // WebView used only to print the selection. | 369 // WebView used only to print the selection. |
| 369 std::unique_ptr<PrepareFrameAndViewForPrint> prep_frame_view_; | 370 std::unique_ptr<PrepareFrameAndViewForPrint> prep_frame_view_; |
| 370 bool reset_prep_frame_view_; | 371 bool reset_prep_frame_view_; |
| 371 | 372 |
| 372 std::unique_ptr<PrintMsg_PrintPages_Params> print_pages_params_; | 373 std::unique_ptr<PrintMsg_PrintPages_Params> print_pages_params_; |
| 373 bool is_print_ready_metafile_sent_; | 374 bool is_print_ready_metafile_sent_; |
| 374 bool ignore_css_margins_; | 375 bool ignore_css_margins_; |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 526 base::Closure on_stop_loading_closure_; | 527 base::Closure on_stop_loading_closure_; |
| 527 | 528 |
| 528 base::WeakPtrFactory<PrintWebViewHelper> weak_ptr_factory_; | 529 base::WeakPtrFactory<PrintWebViewHelper> weak_ptr_factory_; |
| 529 | 530 |
| 530 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelper); | 531 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelper); |
| 531 }; | 532 }; |
| 532 | 533 |
| 533 } // namespace printing | 534 } // namespace printing |
| 534 | 535 |
| 535 #endif // COMPONENTS_PRINTING_RENDERER_PRINT_WEB_VIEW_HELPER_H_ | 536 #endif // COMPONENTS_PRINTING_RENDERER_PRINT_WEB_VIEW_HELPER_H_ |
| OLD | NEW |