OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 HEADLESS_LIB_BROWSER_HEADLESS_PRINT_MANAGER_H_ | 5 #ifndef HEADLESS_LIB_BROWSER_HEADLESS_PRINT_MANAGER_H_ |
6 #define HEADLESS_LIB_BROWSER_HEADLESS_PRINT_MANAGER_H_ | 6 #define HEADLESS_LIB_BROWSER_HEADLESS_PRINT_MANAGER_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
| 10 #include <vector> |
10 | 11 |
11 #include "components/printing/browser/print_manager.h" | 12 #include "components/printing/browser/print_manager.h" |
12 #include "content/public/browser/render_frame_host.h" | 13 #include "content/public/browser/render_frame_host.h" |
13 #include "content/public/browser/web_contents_user_data.h" | 14 #include "content/public/browser/web_contents_user_data.h" |
| 15 #include "printing/print_settings.h" |
| 16 #include "printing/printing_export.h" |
14 | 17 |
15 struct PrintHostMsg_DidPrintPage_Params; | 18 struct PrintHostMsg_DidPrintPage_Params; |
| 19 struct PrintHostMsg_ScriptedPrint_Params; |
| 20 struct PrintMsg_PrintPages_Params; |
16 | 21 |
17 namespace printing { | 22 namespace printing { |
18 | 23 |
| 24 struct HeadlessPrintSettings { |
| 25 HeadlessPrintSettings() |
| 26 : landscape(false), |
| 27 display_header_footer(false), |
| 28 should_print_backgrounds(false), |
| 29 scale(1) {} |
| 30 |
| 31 gfx::Size paper_size_in_points; |
| 32 PageMargins margins_in_points; |
| 33 |
| 34 bool landscape; |
| 35 bool display_header_footer; |
| 36 bool should_print_backgrounds; |
| 37 // scale = 1 means 100%. |
| 38 double scale; |
| 39 |
| 40 std::string page_ranges; |
| 41 }; |
| 42 |
19 class HeadlessPrintManager | 43 class HeadlessPrintManager |
20 : public PrintManager, | 44 : public PrintManager, |
21 public content::WebContentsUserData<HeadlessPrintManager> { | 45 public content::WebContentsUserData<HeadlessPrintManager> { |
22 public: | 46 public: |
23 enum PrintResult { | 47 enum PrintResult { |
24 PRINT_SUCCESS, | 48 PRINT_SUCCESS, |
25 PRINTING_FAILED, | 49 PRINTING_FAILED, |
26 INVALID_PRINTER_SETTINGS, | 50 INVALID_PRINTER_SETTINGS, |
27 INVALID_MEMORY_HANDLE, | 51 INVALID_MEMORY_HANDLE, |
28 METAFILE_MAP_ERROR, | 52 METAFILE_MAP_ERROR, |
29 UNEXPECTED_VALID_MEMORY_HANDLE, | 53 UNEXPECTED_VALID_MEMORY_HANDLE, |
30 METAFILE_INVALID_HEADER, | 54 METAFILE_INVALID_HEADER, |
31 METAFILE_GET_DATA_ERROR, | 55 METAFILE_GET_DATA_ERROR, |
32 SIMULTANEOUS_PRINT_ACTIVE, | 56 SIMULTANEOUS_PRINT_ACTIVE, |
| 57 PAGE_RANGE_SYNTAX_ERROR, |
| 58 PAGE_COUNT_EXCEEDED, |
| 59 }; |
| 60 |
| 61 enum PageRangeStatus { |
| 62 NO_ERROR, |
| 63 SYNTAX_ERROR, |
| 64 LIMIT_ERROR, |
33 }; | 65 }; |
34 | 66 |
35 using GetPDFCallback = base::Callback<void(PrintResult, const std::string&)>; | 67 using GetPDFCallback = base::Callback<void(PrintResult, const std::string&)>; |
36 | 68 |
37 ~HeadlessPrintManager() override; | 69 ~HeadlessPrintManager() override; |
38 | 70 |
39 static std::string PrintResultToString(PrintResult result); | 71 static std::string PrintResultToString(PrintResult result); |
40 static std::unique_ptr<base::DictionaryValue> PDFContentsToDictionaryValue( | 72 static std::unique_ptr<base::DictionaryValue> PDFContentsToDictionaryValue( |
41 const std::string& data); | 73 const std::string& data); |
| 74 static PageRangeStatus PageRangeTextToPages(base::StringPiece page_range_text, |
| 75 int pages_count, |
| 76 std::vector<int>* pages); |
42 | 77 |
43 // Prints the current document immediately. Since the rendering is | 78 // Prints the current document immediately. Since the rendering is |
44 // asynchronous, the actual printing will not be completed on the return of | 79 // asynchronous, the actual printing will not be completed on the return of |
45 // this function, and |callback| will always get called when printing | 80 // this function, and |callback| will always get called when printing |
46 // finishes. | 81 // finishes. |
47 void GetPDFContents(content::RenderFrameHost* rfh, | 82 void GetPDFContents(content::RenderFrameHost* rfh, |
| 83 const HeadlessPrintSettings& settings, |
48 const GetPDFCallback& callback); | 84 const GetPDFCallback& callback); |
49 | 85 |
50 private: | 86 private: |
51 explicit HeadlessPrintManager(content::WebContents* web_contents); | 87 explicit HeadlessPrintManager(content::WebContents* web_contents); |
52 friend class content::WebContentsUserData<HeadlessPrintManager>; | 88 friend class content::WebContentsUserData<HeadlessPrintManager>; |
53 | 89 |
| 90 std::unique_ptr<PrintMsg_PrintPages_Params> GetPrintParamsFromSettings( |
| 91 const HeadlessPrintSettings& settings); |
54 // content::WebContentsObserver implementation. | 92 // content::WebContentsObserver implementation. |
55 bool OnMessageReceived(const IPC::Message& message, | 93 bool OnMessageReceived(const IPC::Message& message, |
56 content::RenderFrameHost* render_frame_host) override; | 94 content::RenderFrameHost* render_frame_host) override; |
57 | 95 |
58 // IPC Message handlers. | 96 // IPC Message handlers. |
59 void OnGetDefaultPrintSettings(IPC::Message* reply_msg); | 97 void OnGetDefaultPrintSettings(IPC::Message* reply_msg); |
| 98 void OnScriptedPrint(const PrintHostMsg_ScriptedPrint_Params& params, |
| 99 IPC::Message* reply_msg); |
60 void OnShowInvalidPrinterSettingsError(); | 100 void OnShowInvalidPrinterSettingsError(); |
61 void OnPrintingFailed(int cookie) override; | 101 void OnPrintingFailed(int cookie) override; |
| 102 void OnDidGetPrintedPagesCount(int cookie, int number_pages) override; |
62 void OnDidPrintPage(const PrintHostMsg_DidPrintPage_Params& params); | 103 void OnDidPrintPage(const PrintHostMsg_DidPrintPage_Params& params); |
63 | 104 |
64 void Reset(); | 105 void Reset(); |
65 void ReleaseJob(PrintResult result); | 106 void ReleaseJob(PrintResult result); |
66 | 107 |
67 content::RenderFrameHost* printing_rfh_; | 108 content::RenderFrameHost* printing_rfh_; |
68 GetPDFCallback callback_; | 109 GetPDFCallback callback_; |
| 110 std::unique_ptr<PrintMsg_PrintPages_Params> print_params_; |
| 111 std::string page_ranges_text_; |
69 std::string data_; | 112 std::string data_; |
70 | 113 |
71 // Set to true when OnDidPrintPage() should be expecting the first page. | 114 // Set to true when OnDidPrintPage() should be expecting the first page. |
72 bool expecting_first_page_; | 115 bool expecting_first_page_; |
73 | 116 |
74 DISALLOW_COPY_AND_ASSIGN(HeadlessPrintManager); | 117 DISALLOW_COPY_AND_ASSIGN(HeadlessPrintManager); |
75 }; | 118 }; |
76 | 119 |
77 } // namespace printing | 120 } // namespace printing |
78 | 121 |
79 #endif // HEADLESS_LIB_BROWSER_HEADLESS_PRINT_MANAGER_H_ | 122 #endif // HEADLESS_LIB_BROWSER_HEADLESS_PRINT_MANAGER_H_ |
OLD | NEW |