- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / print_preview / print_preview_handler.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_HANDLER_H_
6 #define CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_PRINT_PREVIEW_HANDLER_H_
7
8 #include <string>
9
10 #include "base/files/file_path.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "chrome/browser/printing/print_view_manager_observer.h"
15 #include "content/public/browser/web_ui_message_handler.h"
16 #include "ui/shell_dialogs/select_file_dialog.h"
17
18 class PrintSystemTaskProxy;
19
20 namespace base {
21 class DictionaryValue;
22 class RefCountedBytes;
23 }
24
25 namespace content {
26 class WebContents;
27 }
28
29 namespace printing {
30 struct PageSizeMargins;
31 class PrintBackend;
32 }
33
34 // The handler for Javascript messages related to the print preview dialog.
35 class PrintPreviewHandler : public content::WebUIMessageHandler,
36                             public ui::SelectFileDialog::Listener,
37                             public printing::PrintViewManagerObserver {
38  public:
39   PrintPreviewHandler();
40   virtual ~PrintPreviewHandler();
41
42   // WebUIMessageHandler implementation.
43   virtual void RegisterMessages() OVERRIDE;
44
45   // SelectFileDialog::Listener implementation.
46   virtual void FileSelected(const base::FilePath& path,
47                             int index,
48                             void* params) OVERRIDE;
49   virtual void FileSelectionCanceled(void* params) OVERRIDE;
50
51   // PrintViewManagerObserver implementation.
52   virtual void OnPrintDialogShown() OVERRIDE;
53
54   // Displays a modal dialog, prompting the user to select a file.
55   void SelectFile(const base::FilePath& default_path);
56
57   // Called when the print preview dialog is destroyed. This is the last time
58   // this object has access to the PrintViewManager in order to disconnect the
59   // observer.
60   void OnPrintPreviewDialogDestroyed();
61
62   // Called when print preview failed.
63   void OnPrintPreviewFailed();
64
65   // Called when the user press ctrl+shift+p to display the native system
66   // dialog.
67   void ShowSystemDialog();
68
69  private:
70   class AccessTokenService;
71   struct CUPSPrinterColorModels;
72
73   content::WebContents* preview_web_contents() const;
74
75   // Gets the list of printers. |args| is unused.
76   void HandleGetPrinters(const base::ListValue* args);
77
78   // Asks the initiator renderer to generate a preview.  First element of |args|
79   // is a job settings JSON string.
80   void HandleGetPreview(const base::ListValue* args);
81
82   // Gets the job settings from Web UI and initiate printing. First element of
83   // |args| is a job settings JSON string.
84   void HandlePrint(const base::ListValue* args);
85
86   // Handles the request to hide the preview dialog for printing.
87   // |args| is unused.
88   void HandleHidePreview(const base::ListValue* args);
89
90   // Handles the request to cancel the pending print request. |args| is unused.
91   void HandleCancelPendingPrintRequest(const base::ListValue* args);
92
93   // Handles a request to store data that the web ui wishes to persist.
94   // First element of |args| is the data to persist.
95   void HandleSaveAppState(const base::ListValue* args);
96
97   // Gets the printer capabilities. First element of |args| is the printer name.
98   void HandleGetPrinterCapabilities(const base::ListValue* args);
99
100   // Asks the initiator renderer to show the native print system dialog. |args|
101   // is unused.
102   void HandleShowSystemDialog(const base::ListValue* args);
103
104   // Callback for the signin dialog to call once signin is complete.
105   void OnSigninComplete();
106
107   // Brings up a dialog to allow the user to sign into cloud print.
108   // |args| is unused.
109   void HandleSignin(const base::ListValue* args);
110
111   // Generates new token and sends back to UI.
112   void HandleGetAccessToken(const base::ListValue* args);
113
114   // Brings up a web page to allow the user to configure cloud print.
115   // |args| is unused.
116   void HandleManageCloudPrint(const base::ListValue* args);
117
118   // Gathers UMA stats when the print preview dialog is about to close.
119   // |args| is unused.
120   void HandleClosePreviewDialog(const base::ListValue* args);
121
122   // Asks the browser to show the native printer management dialog.
123   // |args| is unused.
124   void HandleManagePrinters(const base::ListValue* args);
125
126   // Asks the browser to show the cloud print dialog. |args| is signle int with
127   // page count.
128   void HandlePrintWithCloudPrintDialog(const base::ListValue* args);
129
130   // Asks the browser for several settings that are needed before the first
131   // preview is displayed.
132   void HandleGetInitialSettings(const base::ListValue* args);
133
134   // Reports histogram data for a print preview UI action. |args| should consist
135   // of two elements: the bucket name, and the bucket event.
136   void HandleReportUiEvent(const base::ListValue* args);
137
138   // Forces the opening of a new tab. |args| should consist of one element: the
139   // URL to set the new tab to.
140   //
141   // NOTE: This is needed to open FedEx confirmation window as a new tab.
142   // Javascript's "window.open" opens a new window popup (since initiated from
143   // async HTTP request) and worse yet, on Windows and Chrome OS, the opened
144   // window opens behind the initiator window.
145   void HandleForceOpenNewTab(const base::ListValue* args);
146
147   void SendInitialSettings(const std::string& default_printer);
148
149   // Send OAuth2 access token.
150   void SendAccessToken(const std::string& type,
151                        const std::string& access_token);
152
153   // Sends the printer capabilities to the Web UI. |settings_info| contains
154   // printer capabilities information.
155   void SendPrinterCapabilities(const base::DictionaryValue* settings_info);
156
157   // Sends error notification to the Web UI when unable to return the printer
158   // capabilities.
159   void SendFailedToGetPrinterCapabilities(const std::string& printer_name);
160
161   // Send the list of printers to the Web UI.
162   void SetupPrinterList(const base::ListValue* printers);
163
164   // Send whether cloud print integration should be enabled.
165   void SendCloudPrintEnabled();
166
167   // Send the PDF data to the cloud to print.
168   void SendCloudPrintJob(const base::RefCountedBytes* data);
169
170   // Handles printing to PDF.
171   void PrintToPdf();
172
173   // Asks the browser to show the cloud print dialog.
174   void PrintWithCloudPrintDialog();
175
176   // Gets the initiator for the print preview dialog.
177   content::WebContents* GetInitiator() const;
178
179   // Closes the preview dialog.
180   void ClosePreviewDialog();
181
182   // Adds all the recorded stats taken so far to histogram counts.
183   void ReportStats();
184
185   // Clears initiator details for the print preview dialog.
186   void ClearInitiatorDetails();
187
188   // Posts a task to save |data| to pdf at |print_to_pdf_path_|.
189   void PostPrintToPdfTask();
190
191   // Populates |settings| according to the current locale.
192   void GetNumberFormatAndMeasurementSystem(base::DictionaryValue* settings);
193
194   bool GetPreviewDataAndTitle(scoped_refptr<base::RefCountedBytes>* data,
195                               string16* title) const;
196
197 #if defined(USE_CUPS)
198   void SaveCUPSColorSetting(const base::DictionaryValue* settings);
199
200   void ConvertColorSettingToCUPSColorModel(
201       base::DictionaryValue* settings) const;
202 #endif
203
204   // Pointer to current print system.
205   scoped_refptr<printing::PrintBackend> print_backend_;
206
207   // The underlying dialog object.
208   scoped_refptr<ui::SelectFileDialog> select_file_dialog_;
209
210   // A count of how many requests received to regenerate preview data.
211   // Initialized to 0 then incremented and emitted to a histogram.
212   int regenerate_preview_request_count_;
213
214   // A count of how many requests received to show manage printers dialog.
215   int manage_printers_dialog_request_count_;
216   int manage_cloud_printers_dialog_request_count_;
217
218   // Whether we have already logged a failed print preview.
219   bool reported_failed_preview_;
220
221   // Whether we have already logged the number of printers this session.
222   bool has_logged_printers_count_;
223
224   // Holds the path to the print to pdf request. It is empty if no such request
225   // exists.
226   base::FilePath print_to_pdf_path_;
227
228   // Holds token service to get OAuth2 access tokens.
229   scoped_ptr<AccessTokenService> token_service_;
230
231 #if defined(USE_CUPS)
232   // The color capabilities from the last printer queried.
233   scoped_ptr<CUPSPrinterColorModels> cups_printer_color_models_;
234 #endif
235
236   base::WeakPtrFactory<PrintPreviewHandler> weak_factory_;
237
238   DISALLOW_COPY_AND_ASSIGN(PrintPreviewHandler);
239 };
240
241 #endif  // CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_PRINT_PREVIEW_HANDLER_H_