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

Side by Side Diff: headless/lib/browser/headless_print_manager.h

Issue 2829973002: add customized printing setting for headless (Closed)
Patch Set: adjust command interface and add unit tests Created 3 years, 7 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
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
19 class HeadlessPrintManager 24 // Exported for tests.
25 class PRINTING_EXPORT HeadlessPrintSettings {
26 public:
27 enum PaperType {
28 NA_LETTER,
29 NA_LEGAL,
30 ISO_A4,
31 ISO_A3,
32 CUSTOM,
33 };
34
35 HeadlessPrintSettings();
36 HeadlessPrintSettings(const HeadlessPrintSettings& obj);
37 ~HeadlessPrintSettings();
38
39 gfx::Size PaperSizeInPoints();
40 PageMargins MarginInPoints();
41
42 PaperType paper_type;
43 double paper_width_in_inch;
44 double paper_height_in_inch;
45
46 MarginType margin_type;
47 double margin_top_in_inch;
48 double margin_bottom_in_inch;
49 double margin_left_in_inch;
50 double margin_right_in_inch;
51
52 bool landscape;
53 bool display_header_footer;
54 bool should_print_backgrounds;
55 // scale = 1 means 100%.
56 double scale;
57
58 std::string page_ranges;
59 };
60
61 // Exported for tests.
62 class PRINTING_EXPORT HeadlessPrintManager
20 : public PrintManager, 63 : public PrintManager,
21 public content::WebContentsUserData<HeadlessPrintManager> { 64 public content::WebContentsUserData<HeadlessPrintManager> {
22 public: 65 public:
23 enum PrintResult { 66 enum PrintResult {
24 PRINT_SUCCESS, 67 PRINT_SUCCESS,
25 PRINTING_FAILED, 68 PRINTING_FAILED,
26 INVALID_PRINTER_SETTINGS, 69 INVALID_PRINTER_SETTINGS,
27 INVALID_MEMORY_HANDLE, 70 INVALID_MEMORY_HANDLE,
28 METAFILE_MAP_ERROR, 71 METAFILE_MAP_ERROR,
29 UNEXPECTED_VALID_MEMORY_HANDLE, 72 UNEXPECTED_VALID_MEMORY_HANDLE,
30 METAFILE_INVALID_HEADER, 73 METAFILE_INVALID_HEADER,
31 METAFILE_GET_DATA_ERROR, 74 METAFILE_GET_DATA_ERROR,
32 SIMULTANEOUS_PRINT_ACTIVE, 75 SIMULTANEOUS_PRINT_ACTIVE,
76 PAGE_RANGE_SYNTAX_ERROR,
77 PAGE_COUNT_EXCEEDED,
78 };
79
80 enum PageRangeStatus {
81 NO_ERROR,
82 SYNTAX_ERROR,
83 LIMIT_ERROR,
33 }; 84 };
34 85
35 using GetPDFCallback = base::Callback<void(PrintResult, const std::string&)>; 86 using GetPDFCallback = base::Callback<void(PrintResult, const std::string&)>;
36 87
37 ~HeadlessPrintManager() override; 88 ~HeadlessPrintManager() override;
38 89
39 static std::string PrintResultToString(PrintResult result); 90 static std::string PrintResultToString(PrintResult result);
40 static std::unique_ptr<base::DictionaryValue> PDFContentsToDictionaryValue( 91 static std::unique_ptr<base::DictionaryValue> PDFContentsToDictionaryValue(
41 const std::string& data); 92 const std::string& data);
93 static PageRangeStatus PageRangeTextToPages(std::string page_range_text,
94 int pages_count,
95 std::vector<int>* pages);
42 96
43 // Prints the current document immediately. Since the rendering is 97 // Prints the current document immediately. Since the rendering is
44 // asynchronous, the actual printing will not be completed on the return of 98 // asynchronous, the actual printing will not be completed on the return of
45 // this function, and |callback| will always get called when printing 99 // this function, and |callback| will always get called when printing
46 // finishes. 100 // finishes.
47 void GetPDFContents(content::RenderFrameHost* rfh, 101 void GetPDFContents(content::RenderFrameHost* rfh,
102 const HeadlessPrintSettings& settings,
48 const GetPDFCallback& callback); 103 const GetPDFCallback& callback);
49 104
50 private: 105 private:
51 explicit HeadlessPrintManager(content::WebContents* web_contents); 106 explicit HeadlessPrintManager(content::WebContents* web_contents);
52 friend class content::WebContentsUserData<HeadlessPrintManager>; 107 friend class content::WebContentsUserData<HeadlessPrintManager>;
53 108
109 std::unique_ptr<PrintMsg_PrintPages_Params> PrintParams(
110 HeadlessPrintSettings settings);
54 // content::WebContentsObserver implementation. 111 // content::WebContentsObserver implementation.
55 bool OnMessageReceived(const IPC::Message& message, 112 bool OnMessageReceived(const IPC::Message& message,
56 content::RenderFrameHost* render_frame_host) override; 113 content::RenderFrameHost* render_frame_host) override;
57 114
58 // IPC Message handlers. 115 // IPC Message handlers.
59 void OnGetDefaultPrintSettings(IPC::Message* reply_msg); 116 void OnGetDefaultPrintSettings(IPC::Message* reply_msg);
117 void OnScriptedPrint(const PrintHostMsg_ScriptedPrint_Params& params,
118 IPC::Message* reply_msg);
60 void OnShowInvalidPrinterSettingsError(); 119 void OnShowInvalidPrinterSettingsError();
61 void OnPrintingFailed(int cookie) override; 120 void OnPrintingFailed(int cookie) override;
121 void OnDidGetPrintedPagesCount(int cookie, int number_pages) override;
62 void OnDidPrintPage(const PrintHostMsg_DidPrintPage_Params& params); 122 void OnDidPrintPage(const PrintHostMsg_DidPrintPage_Params& params);
63 123
64 void Reset(); 124 void Reset();
65 void ReleaseJob(PrintResult result); 125 void ReleaseJob(PrintResult result);
66 126
67 content::RenderFrameHost* printing_rfh_; 127 content::RenderFrameHost* printing_rfh_;
68 GetPDFCallback callback_; 128 GetPDFCallback callback_;
129 std::unique_ptr<PrintMsg_PrintPages_Params> print_params_;
130 std::string page_ranges_text_;
69 std::string data_; 131 std::string data_;
70 132
71 // Set to true when OnDidPrintPage() should be expecting the first page. 133 // Set to true when OnDidPrintPage() should be expecting the first page.
72 bool expecting_first_page_; 134 bool expecting_first_page_;
73 135
74 DISALLOW_COPY_AND_ASSIGN(HeadlessPrintManager); 136 DISALLOW_COPY_AND_ASSIGN(HeadlessPrintManager);
75 }; 137 };
76 138
77 } // namespace printing 139 } // namespace printing
78 140
79 #endif // HEADLESS_LIB_BROWSER_HEADLESS_PRINT_MANAGER_H_ 141 #endif // HEADLESS_LIB_BROWSER_HEADLESS_PRINT_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698