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/gtest_prod_util.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/time/time.h"
13 #include "chrome/browser/ui/webui/constrained_web_dialog_ui.h"
15 class PrintPreviewDataService
;
16 class PrintPreviewHandler
;
17 struct PrintHostMsg_DidGetPreviewPageCount_Params
;
18 struct PrintHostMsg_RequestPrintPreview_Params
;
21 class RefCountedBytes
;
29 struct PageSizeMargins
;
32 class PrintPreviewUI
: public ConstrainedWebDialogUI
{
34 explicit PrintPreviewUI(content::WebUI
* web_ui
);
35 virtual ~PrintPreviewUI();
37 // Gets the print preview |data|. |index| is zero-based, and can be
38 // |printing::COMPLETE_PREVIEW_DOCUMENT_INDEX| to get the entire preview
40 void GetPrintPreviewDataForIndex(int index
,
41 scoped_refptr
<base::RefCountedBytes
>* data
);
43 // Sets the print preview |data|. |index| is zero-based, and can be
44 // |printing::COMPLETE_PREVIEW_DOCUMENT_INDEX| to set the entire preview
46 void SetPrintPreviewDataForIndex(int index
,
47 const base::RefCountedBytes
* data
);
49 // Clear the existing print preview data.
50 void ClearAllPreviewData();
52 // Returns the available draft page count.
53 int GetAvailableDraftPageCount();
56 void SetInitiatorTitle(const base::string16
& initiator_title
);
58 base::string16
initiator_title() { return initiator_title_
; }
60 bool source_is_modifiable() { return source_is_modifiable_
; }
62 bool source_has_selection() { return source_has_selection_
; }
64 bool print_selection_only() { return print_selection_only_
; }
66 // Set initial settings for PrintPreviewUI.
67 static void SetInitialParams(
68 content::WebContents
* print_preview_dialog
,
69 const PrintHostMsg_RequestPrintPreview_Params
& params
);
71 // Determines whether to cancel a print preview request based on
72 // |preview_ui_id| and |request_id|.
73 // Can be called from any thread.
74 static void GetCurrentPrintPreviewStatus(int32 preview_ui_id
,
78 // Returns an id to uniquely identify this PrintPreviewUI.
79 int32
GetIDForPrintPreviewUI() const;
81 // Notifies the Web UI of a print preview request with |request_id|.
82 void OnPrintPreviewRequest(int request_id
);
84 // Notifies the Web UI to show the system dialog.
85 void OnShowSystemDialog();
87 // Notifies the Web UI about the page count of the request preview.
88 void OnDidGetPreviewPageCount(
89 const PrintHostMsg_DidGetPreviewPageCount_Params
& params
);
91 // Notifies the Web UI of the default page layout according to the currently
92 // selected printer and page size.
93 void OnDidGetDefaultPageLayout(const printing::PageSizeMargins
& page_layout
,
94 const gfx::Rect
& printable_area
,
95 bool has_custom_page_size_style
);
97 // Notifies the Web UI that the 0-based page |page_number| has been rendered.
98 // |preview_request_id| indicates wich request resulted in this response.
99 void OnDidPreviewPage(int page_number
, int preview_request_id
);
101 // Notifies the Web UI renderer that preview data is available.
102 // |expected_pages_count| specifies the total number of pages.
103 // |preview_request_id| indicates which request resulted in this response.
104 void OnPreviewDataIsAvailable(int expected_pages_count
,
105 int preview_request_id
);
107 // Notifies the Web UI that preview dialog has been destroyed. This is the
108 // last chance to communicate with the initiator before the association is
110 void OnPrintPreviewDialogDestroyed();
112 // Notifies the Web UI that the print preview failed to render.
113 void OnPrintPreviewFailed();
115 // Notified the Web UI that this print preview dialog's RenderProcess has been
116 // closed, which may occur for several reasons, e.g. tab closure or crash.
117 void OnPrintPreviewDialogClosed();
119 // Notifies the Web UI that initiator is closed, so we can disable all the
120 // controls that need the initiator for generating the preview data.
121 void OnInitiatorClosed();
123 // Notifies the Web UI renderer that file selection has been cancelled.
124 void OnFileSelectionCancelled();
126 // Notifies the Web UI that the printer is unavailable or its settings are
128 void OnInvalidPrinterSettings();
130 // Notifies the Web UI to cancel the pending preview request.
131 void OnCancelPendingPreviewRequest();
133 // Hides the print preview dialog.
134 void OnHidePreviewDialog();
136 // Closes the print preview dialog.
137 void OnClosePrintPreviewDialog();
139 // Reload the printers list.
140 void OnReloadPrintersList();
142 // Notifies the WebUI that the pdf print scaling option is disabled by
144 void OnPrintPreviewScalingDisabled();
146 // Allows tests to wait until the print preview dialog is loaded. Optionally
147 // also instructs the dialog to auto-cancel, which is used for testing only.
148 class TestingDelegate
{
150 virtual bool IsAutoCancelEnabled() = 0;
151 virtual void DidGetPreviewPageCount(int page_count
) = 0;
152 virtual void DidRenderPreviewPage(
153 const content::WebContents
& preview_dialog
) = 0;
156 static void SetDelegateForTesting(TestingDelegate
* delegate
);
159 friend class PrintPreviewHandlerTest
;
160 FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest
, StickyMarginsCustom
);
161 FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest
, StickyMarginsDefault
);
162 FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest
,
163 StickyMarginsCustomThenDefault
);
164 FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest
,
165 GetLastUsedMarginSettingsCustom
);
166 FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest
,
167 GetLastUsedMarginSettingsDefault
);
168 FRIEND_TEST_ALL_PREFIXES(PrintPreviewDialogControllerUnitTest
,
171 // Returns the Singleton instance of the PrintPreviewDataService.
172 PrintPreviewDataService
* print_preview_data_service();
174 base::TimeTicks initial_preview_start_time_
;
176 // The unique ID for this class instance. Stored here to avoid calling
177 // GetIDForPrintPreviewUI() everywhere.
180 // Weak pointer to the WebUI handler.
181 PrintPreviewHandler
* handler_
;
183 // Indicates whether the source document can be modified.
184 bool source_is_modifiable_
;
186 // Indicates whether the source document has selection.
187 bool source_has_selection_
;
189 // Indicates whether only the selection should be printed.
190 bool print_selection_only_
;
192 // Store the initiator title, used for populating the print preview dialog
194 base::string16 initiator_title_
;
196 // Keeps track of whether OnClosePrintPreviewDialog() has been called or not.
199 DISALLOW_COPY_AND_ASSIGN(PrintPreviewUI
);
202 #endif // CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_PRINT_PREVIEW_UI_H_