- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / print_preview / print_preview_ui.h
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 string16& initiator_title);
57
58   string16 initiator_title() { return initiator_title_; }
59
60   bool source_is_modifiable() { return source_is_modifiable_; }
61
62   bool source_has_selection() { return source_has_selection_; }
63
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 renderer to reuse the preview data.
108   // |preview_request_id| indicates which request resulted in this response.
109   void OnReusePreviewData(int preview_request_id);
110
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
113   // erased.
114   void OnPrintPreviewDialogDestroyed();
115
116   // Notifies the Web UI that the print preview failed to render.
117   void OnPrintPreviewFailed();
118
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();
122
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();
126
127   // Notifies the Web UI renderer that file selection has been cancelled.
128   void OnFileSelectionCancelled();
129
130   // Notifies the Web UI that the printer is unavailable or its settings are
131   // invalid.
132   void OnInvalidPrinterSettings();
133
134   // Notifies the Web UI to cancel the pending preview request.
135   void OnCancelPendingPreviewRequest();
136
137   // Hides the print preview dialog.
138   void OnHidePreviewDialog();
139
140   // Closes the print preview dialog.
141   void OnClosePrintPreviewDialog();
142
143   // Reload the printers list.
144   void OnReloadPrintersList();
145
146   // Notifies the WebUI that the pdf print scaling option is disabled by
147   // default.
148   void OnPrintPreviewScalingDisabled();
149
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 {
153    public:
154     virtual bool IsAutoCancelEnabled() = 0;
155     virtual void DidGetPreviewPageCount(int page_count) = 0;
156     virtual void DidRenderPreviewPage(
157         const content::WebContents& preview_dialog) = 0;
158   };
159
160   static void SetDelegateForTesting(TestingDelegate* delegate);
161
162  private:
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,
173                            TitleAfterReload);
174
175   // Returns the Singleton instance of the PrintPreviewDataService.
176   PrintPreviewDataService* print_preview_data_service();
177
178   base::TimeTicks initial_preview_start_time_;
179
180   // The unique ID for this class instance. Stored here to avoid calling
181   // GetIDForPrintPreviewUI() everywhere.
182   const int32 id_;
183
184   // Weak pointer to the WebUI handler.
185   PrintPreviewHandler* handler_;
186
187   // Indicates whether the source document can be modified.
188   bool source_is_modifiable_;
189
190   // Indicates whether the source document has selection.
191   bool source_has_selection_;
192
193   // Indicates whether only the selection should be printed.
194   bool print_selection_only_;
195
196   // Store the initiator title, used for populating the print preview dialog
197   // title.
198   string16 initiator_title_;
199
200   // Keeps track of whether OnClosePrintPreviewDialog() has been called or not.
201   bool dialog_closed_;
202
203   DISALLOW_COPY_AND_ASSIGN(PrintPreviewUI);
204 };
205
206 #endif  // CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_PRINT_PREVIEW_UI_H_