• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_PRINT_PREVIEW_UI_H_
6 #define CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_PRINT_PREVIEW_UI_H_
7 
8 #include <string>
9 
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"
14 
15 class PrintPreviewDataService;
16 class PrintPreviewHandler;
17 struct PrintHostMsg_DidGetPreviewPageCount_Params;
18 struct PrintHostMsg_RequestPrintPreview_Params;
19 
20 namespace base {
21 class RefCountedBytes;
22 }
23 
24 namespace gfx {
25 class Rect;
26 }
27 
28 namespace printing {
29 struct PageSizeMargins;
30 }
31 
32 class PrintPreviewUI : public ConstrainedWebDialogUI {
33  public:
34   explicit PrintPreviewUI(content::WebUI* web_ui);
35   virtual ~PrintPreviewUI();
36 
37   // Gets the print preview |data|. |index| is zero-based, and can be
38   // |printing::COMPLETE_PREVIEW_DOCUMENT_INDEX| to get the entire preview
39   // document.
40   void GetPrintPreviewDataForIndex(int index,
41                                    scoped_refptr<base::RefCountedBytes>* data);
42 
43   // Sets the print preview |data|. |index| is zero-based, and can be
44   // |printing::COMPLETE_PREVIEW_DOCUMENT_INDEX| to set the entire preview
45   // document.
46   void SetPrintPreviewDataForIndex(int index,
47                                    const base::RefCountedBytes* data);
48 
49   // Clear the existing print preview data.
50   void ClearAllPreviewData();
51 
52   // Returns the available draft page count.
53   int GetAvailableDraftPageCount();
54 
55   // Setters
56   void SetInitiatorTitle(const base::string16& initiator_title);
57 
initiator_title()58   base::string16 initiator_title() { return initiator_title_; }
59 
source_is_modifiable()60   bool source_is_modifiable() { return source_is_modifiable_; }
61 
source_has_selection()62   bool source_has_selection() { return source_has_selection_; }
63 
print_selection_only()64   bool print_selection_only() { return print_selection_only_; }
65 
66   // Set initial settings for PrintPreviewUI.
67   static void SetInitialParams(
68       content::WebContents* print_preview_dialog,
69       const PrintHostMsg_RequestPrintPreview_Params& params);
70 
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,
75                                            int request_id,
76                                            bool* cancel);
77 
78   // Returns an id to uniquely identify this PrintPreviewUI.
79   int32 GetIDForPrintPreviewUI() const;
80 
81   // Notifies the Web UI of a print preview request with |request_id|.
82   void OnPrintPreviewRequest(int request_id);
83 
84   // Notifies the Web UI to show the system dialog.
85   void OnShowSystemDialog();
86 
87   // Notifies the Web UI about the page count of the request preview.
88   void OnDidGetPreviewPageCount(
89       const PrintHostMsg_DidGetPreviewPageCount_Params& params);
90 
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);
96 
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);
100 
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);
106 
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
109   // erased.
110   void OnPrintPreviewDialogDestroyed();
111 
112   // Notifies the Web UI that the print preview failed to render.
113   void OnPrintPreviewFailed();
114 
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();
118 
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();
122 
123   // Notifies the Web UI renderer that file selection has been cancelled.
124   void OnFileSelectionCancelled();
125 
126   // Notifies the Web UI that the printer is unavailable or its settings are
127   // invalid.
128   void OnInvalidPrinterSettings();
129 
130   // Notifies the Web UI to cancel the pending preview request.
131   void OnCancelPendingPreviewRequest();
132 
133   // Hides the print preview dialog.
134   void OnHidePreviewDialog();
135 
136   // Closes the print preview dialog.
137   void OnClosePrintPreviewDialog();
138 
139   // Reload the printers list.
140   void OnReloadPrintersList();
141 
142   // Notifies the WebUI that the pdf print scaling option is disabled by
143   // default.
144   void OnPrintPreviewScalingDisabled();
145 
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 {
149    public:
150     virtual bool IsAutoCancelEnabled() = 0;
151     virtual void DidGetPreviewPageCount(int page_count) = 0;
152     virtual void DidRenderPreviewPage(content::WebContents* preview_dialog) = 0;
153   };
154 
155   static void SetDelegateForTesting(TestingDelegate* delegate);
156 
157  private:
158   friend class PrintPreviewHandlerTest;
159   FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest, StickyMarginsCustom);
160   FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest, StickyMarginsDefault);
161   FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest,
162                            StickyMarginsCustomThenDefault);
163   FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest,
164                            GetLastUsedMarginSettingsCustom);
165   FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest,
166                            GetLastUsedMarginSettingsDefault);
167   FRIEND_TEST_ALL_PREFIXES(PrintPreviewDialogControllerUnitTest,
168                            TitleAfterReload);
169 
170   // Returns the Singleton instance of the PrintPreviewDataService.
171   PrintPreviewDataService* print_preview_data_service();
172 
173   base::TimeTicks initial_preview_start_time_;
174 
175   // The unique ID for this class instance. Stored here to avoid calling
176   // GetIDForPrintPreviewUI() everywhere.
177   const int32 id_;
178 
179   // Weak pointer to the WebUI handler.
180   PrintPreviewHandler* handler_;
181 
182   // Indicates whether the source document can be modified.
183   bool source_is_modifiable_;
184 
185   // Indicates whether the source document has selection.
186   bool source_has_selection_;
187 
188   // Indicates whether only the selection should be printed.
189   bool print_selection_only_;
190 
191   // Store the initiator title, used for populating the print preview dialog
192   // title.
193   base::string16 initiator_title_;
194 
195   // Keeps track of whether OnClosePrintPreviewDialog() has been called or not.
196   bool dialog_closed_;
197 
198   DISALLOW_COPY_AND_ASSIGN(PrintPreviewUI);
199 };
200 
201 #endif  // CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_PRINT_PREVIEW_UI_H_
202