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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_PRINT_PREVIEW_UI_H_
6 #define CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_PRINT_PREVIEW_UI_H_
10 #include "base/callback_forward.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/time/time.h"
15 #include "chrome/browser/ui/webui/constrained_web_dialog_ui.h"
17 class PrintPreviewDataService
;
18 class PrintPreviewHandler
;
19 struct PrintHostMsg_DidGetPreviewPageCount_Params
;
20 struct PrintHostMsg_RequestPrintPreview_Params
;
21 struct PrintHostMsg_SetOptionsFromDocument_Params
;
25 class RefCountedBytes
;
33 struct PageSizeMargins
;
36 class PrintPreviewUI
: public ConstrainedWebDialogUI
{
38 explicit PrintPreviewUI(content::WebUI
* web_ui
);
39 ~PrintPreviewUI() override
;
41 // Gets the print preview |data|. |index| is zero-based, and can be
42 // |printing::COMPLETE_PREVIEW_DOCUMENT_INDEX| to get the entire preview
44 void GetPrintPreviewDataForIndex(int index
,
45 scoped_refptr
<base::RefCountedBytes
>* data
);
47 // Sets the print preview |data|. |index| is zero-based, and can be
48 // |printing::COMPLETE_PREVIEW_DOCUMENT_INDEX| to set the entire preview
50 void SetPrintPreviewDataForIndex(int index
,
51 const base::RefCountedBytes
* data
);
53 // Clear the existing print preview data.
54 void ClearAllPreviewData();
56 // Returns the available draft page count.
57 int GetAvailableDraftPageCount();
60 void SetInitiatorTitle(const base::string16
& initiator_title
);
62 base::string16
initiator_title() { return initiator_title_
; }
64 bool source_is_modifiable() { return source_is_modifiable_
; }
66 bool source_has_selection() { return source_has_selection_
; }
68 bool print_selection_only() { return print_selection_only_
; }
70 // Set initial settings for PrintPreviewUI.
71 static void SetInitialParams(
72 content::WebContents
* print_preview_dialog
,
73 const PrintHostMsg_RequestPrintPreview_Params
& params
);
75 // Determines whether to cancel a print preview request based on
76 // |preview_ui_id| and |request_id|.
77 // Can be called from any thread.
78 static void GetCurrentPrintPreviewStatus(int32 preview_ui_id
,
82 // Returns an id to uniquely identify this PrintPreviewUI.
83 int32
GetIDForPrintPreviewUI() const;
85 // Notifies the Web UI of a print preview request with |request_id|.
86 void OnPrintPreviewRequest(int request_id
);
88 #if defined(ENABLE_BASIC_PRINTING)
89 // Notifies the Web UI to show the system dialog.
90 void OnShowSystemDialog();
91 #endif // ENABLE_BASIC_PRINTING
93 // Notifies the Web UI about the page count of the request preview.
94 void OnDidGetPreviewPageCount(
95 const PrintHostMsg_DidGetPreviewPageCount_Params
& params
);
97 // Notifies the Web UI of the default page layout according to the currently
98 // selected printer and page size.
99 void OnDidGetDefaultPageLayout(const printing::PageSizeMargins
& page_layout
,
100 const gfx::Rect
& printable_area
,
101 bool has_custom_page_size_style
);
103 // Notifies the Web UI that the 0-based page |page_number| has been rendered.
104 // |preview_request_id| indicates wich request resulted in this response.
105 void OnDidPreviewPage(int page_number
, int preview_request_id
);
107 // Notifies the Web UI renderer that preview data is available.
108 // |expected_pages_count| specifies the total number of pages.
109 // |preview_request_id| indicates which request resulted in this response.
110 void OnPreviewDataIsAvailable(int expected_pages_count
,
111 int preview_request_id
);
113 // Notifies the Web UI that preview dialog has been destroyed. This is the
114 // last chance to communicate with the initiator before the association is
116 void OnPrintPreviewDialogDestroyed();
118 // Notifies the Web UI that the print preview failed to render.
119 void OnPrintPreviewFailed();
121 // Notified the Web UI that this print preview dialog's RenderProcess has been
122 // closed, which may occur for several reasons, e.g. tab closure or crash.
123 void OnPrintPreviewDialogClosed();
125 // Notifies the Web UI that initiator is closed, so we can disable all the
126 // controls that need the initiator for generating the preview data.
127 void OnInitiatorClosed();
129 // Notifies the Web UI renderer that file selection has been cancelled.
130 void OnFileSelectionCancelled();
132 // Notifies the Web UI that the printer is unavailable or its settings are
134 void OnInvalidPrinterSettings();
136 // Notifies the Web UI to cancel the pending preview request.
137 void OnCancelPendingPreviewRequest();
139 // Hides the print preview dialog.
140 void OnHidePreviewDialog();
142 // Closes the print preview dialog.
143 void OnClosePrintPreviewDialog();
145 // Reload the printers list.
146 void OnReloadPrintersList();
148 // Notifies the WebUI to set print preset options from source PDF.
149 void OnSetOptionsFromDocument(
150 const PrintHostMsg_SetOptionsFromDocument_Params
& params
);
152 // Allows tests to wait until the print preview dialog is loaded. Optionally
153 // also instructs the dialog to auto-cancel, which is used for testing only.
154 class TestingDelegate
{
156 virtual bool IsAutoCancelEnabled() = 0;
157 virtual void DidGetPreviewPageCount(int page_count
) = 0;
158 virtual void DidRenderPreviewPage(content::WebContents
* preview_dialog
) = 0;
161 static void SetDelegateForTesting(TestingDelegate
* delegate
);
163 // Allows for tests to set a file path to print a PDF to. This also initiates
164 // the printing without having to click a button on the print preview dialog.
165 void SetSelectedFileForTesting(const base::FilePath
& path
);
167 // Passes |closure| to PrintPreviewHandler::SetPdfSavedClosureForTesting().
168 void SetPdfSavedClosureForTesting(const base::Closure
& closure
);
170 base::WeakPtr
<PrintPreviewUI
> GetWeakPtr();
173 friend class PrintPreviewHandlerTest
;
174 FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest
, StickyMarginsCustom
);
175 FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest
, StickyMarginsDefault
);
176 FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest
,
177 StickyMarginsCustomThenDefault
);
178 FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest
,
179 GetLastUsedMarginSettingsCustom
);
180 FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest
,
181 GetLastUsedMarginSettingsDefault
);
182 FRIEND_TEST_ALL_PREFIXES(PrintPreviewDialogControllerUnitTest
,
185 // Returns the Singleton instance of the PrintPreviewDataService.
186 PrintPreviewDataService
* print_preview_data_service();
188 base::TimeTicks initial_preview_start_time_
;
190 // The unique ID for this class instance. Stored here to avoid calling
191 // GetIDForPrintPreviewUI() everywhere.
194 // Weak pointer to the WebUI handler.
195 PrintPreviewHandler
* handler_
;
197 // Indicates whether the source document can be modified.
198 bool source_is_modifiable_
;
200 // Indicates whether the source document has selection.
201 bool source_has_selection_
;
203 // Indicates whether only the selection should be printed.
204 bool print_selection_only_
;
206 // Store the initiator title, used for populating the print preview dialog
208 base::string16 initiator_title_
;
210 // Keeps track of whether OnClosePrintPreviewDialog() has been called or not.
213 base::WeakPtrFactory
<PrintPreviewUI
> weak_ptr_factory_
;
215 DISALLOW_COPY_AND_ASSIGN(PrintPreviewUI
);
218 #endif // CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_PRINT_PREVIEW_UI_H_