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 string16& initiator_title);
58 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 renderer to reuse the preview data.
108 // |preview_request_id| indicates which request resulted in this response.
109 void OnReusePreviewData(int preview_request_id);
111 // Notifies the Web UI that preview dialog has been destroyed. This is the
112 // last chance to communicate with the initiator before the association is
114 void OnPrintPreviewDialogDestroyed();
116 // Notifies the Web UI that the print preview failed to render.
117 void OnPrintPreviewFailed();
119 // Notified the Web UI that this print preview dialog's RenderProcess has been
120 // closed, which may occur for several reasons, e.g. tab closure or crash.
121 void OnPrintPreviewDialogClosed();
123 // Notifies the Web UI that initiator is closed, so we can disable all the
124 // controls that need the initiator for generating the preview data.
125 void OnInitiatorClosed();
127 // Notifies the Web UI renderer that file selection has been cancelled.
128 void OnFileSelectionCancelled();
130 // Notifies the Web UI that the printer is unavailable or its settings are
132 void OnInvalidPrinterSettings();
134 // Notifies the Web UI to cancel the pending preview request.
135 void OnCancelPendingPreviewRequest();
137 // Hides the print preview dialog.
138 void OnHidePreviewDialog();
140 // Closes the print preview dialog.
141 void OnClosePrintPreviewDialog();
143 // Reload the printers list.
144 void OnReloadPrintersList();
146 // Notifies the WebUI that the pdf print scaling option is disabled by
148 void OnPrintPreviewScalingDisabled();
150 // Allows tests to wait until the print preview dialog is loaded. Optionally
151 // also instructs the dialog to auto-cancel, which is used for testing only.
152 class TestingDelegate {
154 virtual bool IsAutoCancelEnabled() = 0;
155 virtual void DidGetPreviewPageCount(int page_count) = 0;
156 virtual void DidRenderPreviewPage(
157 const content::WebContents& preview_dialog) = 0;
160 static void SetDelegateForTesting(TestingDelegate* delegate);
163 friend class PrintPreviewHandlerTest;
164 FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest, StickyMarginsCustom);
165 FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest, StickyMarginsDefault);
166 FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest,
167 StickyMarginsCustomThenDefault);
168 FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest,
169 GetLastUsedMarginSettingsCustom);
170 FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest,
171 GetLastUsedMarginSettingsDefault);
172 FRIEND_TEST_ALL_PREFIXES(PrintPreviewDialogControllerUnitTest,
175 // Returns the Singleton instance of the PrintPreviewDataService.
176 PrintPreviewDataService* print_preview_data_service();
178 base::TimeTicks initial_preview_start_time_;
180 // The unique ID for this class instance. Stored here to avoid calling
181 // GetIDForPrintPreviewUI() everywhere.
184 // Weak pointer to the WebUI handler.
185 PrintPreviewHandler* handler_;
187 // Indicates whether the source document can be modified.
188 bool source_is_modifiable_;
190 // Indicates whether the source document has selection.
191 bool source_has_selection_;
193 // Indicates whether only the selection should be printed.
194 bool print_selection_only_;
196 // Store the initiator title, used for populating the print preview dialog
198 string16 initiator_title_;
200 // Keeps track of whether OnClosePrintPreviewDialog() has been called or not.
203 DISALLOW_COPY_AND_ASSIGN(PrintPreviewUI);
206 #endif // CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_PRINT_PREVIEW_UI_H_