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/time/time.h"
14 #include "chrome/browser/ui/webui/constrained_web_dialog_ui.h"
16 class PrintPreviewDataService
;
17 class PrintPreviewHandler
;
18 struct PrintHostMsg_DidGetPreviewPageCount_Params
;
19 struct PrintHostMsg_RequestPrintPreview_Params
;
20 struct PrintHostMsg_SetOptionsFromDocument_Params
;
24 class RefCountedBytes
;
32 struct PageSizeMargins
;
35 class PrintPreviewUI
: public ConstrainedWebDialogUI
{
37 explicit PrintPreviewUI(content::WebUI
* web_ui
);
38 ~PrintPreviewUI() override
;
40 // Gets the print preview |data|. |index| is zero-based, and can be
41 // |printing::COMPLETE_PREVIEW_DOCUMENT_INDEX| to get the entire preview
43 void GetPrintPreviewDataForIndex(int index
,
44 scoped_refptr
<base::RefCountedBytes
>* data
);
46 // Sets the print preview |data|. |index| is zero-based, and can be
47 // |printing::COMPLETE_PREVIEW_DOCUMENT_INDEX| to set the entire preview
49 void SetPrintPreviewDataForIndex(int index
,
50 const base::RefCountedBytes
* data
);
52 // Clear the existing print preview data.
53 void ClearAllPreviewData();
55 // Returns the available draft page count.
56 int GetAvailableDraftPageCount();
59 void SetInitiatorTitle(const base::string16
& initiator_title
);
61 base::string16
initiator_title() { return initiator_title_
; }
63 bool source_is_modifiable() { return source_is_modifiable_
; }
65 bool source_has_selection() { return source_has_selection_
; }
67 bool print_selection_only() { return print_selection_only_
; }
69 // Set initial settings for PrintPreviewUI.
70 static void SetInitialParams(
71 content::WebContents
* print_preview_dialog
,
72 const PrintHostMsg_RequestPrintPreview_Params
& params
);
74 // Determines whether to cancel a print preview request based on
75 // |preview_ui_id| and |request_id|.
76 // Can be called from any thread.
77 static void GetCurrentPrintPreviewStatus(int32 preview_ui_id
,
81 // Returns an id to uniquely identify this PrintPreviewUI.
82 int32
GetIDForPrintPreviewUI() const;
84 // Notifies the Web UI of a print preview request with |request_id|.
85 void OnPrintPreviewRequest(int request_id
);
87 #if defined(ENABLE_BASIC_PRINTING)
88 // Notifies the Web UI to show the system dialog.
89 void OnShowSystemDialog();
90 #endif // ENABLE_BASIC_PRINTING
92 // Notifies the Web UI about the page count of the request preview.
93 void OnDidGetPreviewPageCount(
94 const PrintHostMsg_DidGetPreviewPageCount_Params
& params
);
96 // Notifies the Web UI of the default page layout according to the currently
97 // selected printer and page size.
98 void OnDidGetDefaultPageLayout(const printing::PageSizeMargins
& page_layout
,
99 const gfx::Rect
& printable_area
,
100 bool has_custom_page_size_style
);
102 // Notifies the Web UI that the 0-based page |page_number| has been rendered.
103 // |preview_request_id| indicates wich request resulted in this response.
104 void OnDidPreviewPage(int page_number
, int preview_request_id
);
106 // Notifies the Web UI renderer that preview data is available.
107 // |expected_pages_count| specifies the total number of pages.
108 // |preview_request_id| indicates which request resulted in this response.
109 void OnPreviewDataIsAvailable(int expected_pages_count
,
110 int preview_request_id
);
112 // Notifies the Web UI that preview dialog has been destroyed. This is the
113 // last chance to communicate with the initiator before the association is
115 void OnPrintPreviewDialogDestroyed();
117 // Notifies the Web UI that the print preview failed to render.
118 void OnPrintPreviewFailed();
120 // Notified the Web UI that this print preview dialog's RenderProcess has been
121 // closed, which may occur for several reasons, e.g. tab closure or crash.
122 void OnPrintPreviewDialogClosed();
124 // Notifies the Web UI that initiator is closed, so we can disable all the
125 // controls that need the initiator for generating the preview data.
126 void OnInitiatorClosed();
128 // Notifies the Web UI renderer that file selection has been cancelled.
129 void OnFileSelectionCancelled();
131 // Notifies the Web UI that the printer is unavailable or its settings are
133 void OnInvalidPrinterSettings();
135 // Notifies the Web UI to cancel the pending preview request.
136 void OnCancelPendingPreviewRequest();
138 // Hides the print preview dialog.
139 void OnHidePreviewDialog();
141 // Closes the print preview dialog.
142 void OnClosePrintPreviewDialog();
144 // Reload the printers list.
145 void OnReloadPrintersList();
147 // Notifies the WebUI to set print preset options from source PDF.
148 void OnSetOptionsFromDocument(
149 const PrintHostMsg_SetOptionsFromDocument_Params
& params
);
151 // Allows tests to wait until the print preview dialog is loaded. Optionally
152 // also instructs the dialog to auto-cancel, which is used for testing only.
153 class TestingDelegate
{
155 virtual bool IsAutoCancelEnabled() = 0;
156 virtual void DidGetPreviewPageCount(int page_count
) = 0;
157 virtual void DidRenderPreviewPage(content::WebContents
* preview_dialog
) = 0;
160 static void SetDelegateForTesting(TestingDelegate
* delegate
);
162 // Allows for tests to set a file path to print a PDF to. This also initiates
163 // the printing without having to click a button on the print preview dialog.
164 void SetSelectedFileForTesting(const base::FilePath
& path
);
166 // Passes |closure| to PrintPreviewHandler::SetPdfSavedClosureForTesting().
167 void SetPdfSavedClosureForTesting(const base::Closure
& closure
);
170 friend class PrintPreviewHandlerTest
;
171 FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest
, StickyMarginsCustom
);
172 FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest
, StickyMarginsDefault
);
173 FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest
,
174 StickyMarginsCustomThenDefault
);
175 FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest
,
176 GetLastUsedMarginSettingsCustom
);
177 FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest
,
178 GetLastUsedMarginSettingsDefault
);
179 FRIEND_TEST_ALL_PREFIXES(PrintPreviewDialogControllerUnitTest
,
182 // Returns the Singleton instance of the PrintPreviewDataService.
183 PrintPreviewDataService
* print_preview_data_service();
185 base::TimeTicks initial_preview_start_time_
;
187 // The unique ID for this class instance. Stored here to avoid calling
188 // GetIDForPrintPreviewUI() everywhere.
191 // Weak pointer to the WebUI handler.
192 PrintPreviewHandler
* handler_
;
194 // Indicates whether the source document can be modified.
195 bool source_is_modifiable_
;
197 // Indicates whether the source document has selection.
198 bool source_has_selection_
;
200 // Indicates whether only the selection should be printed.
201 bool print_selection_only_
;
203 // Store the initiator title, used for populating the print preview dialog
205 base::string16 initiator_title_
;
207 // Keeps track of whether OnClosePrintPreviewDialog() has been called or not.
210 DISALLOW_COPY_AND_ASSIGN(PrintPreviewUI
);
213 #endif // CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_PRINT_PREVIEW_UI_H_