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_HANDLER_H_
6 #define CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_PRINT_PREVIEW_HANDLER_H_
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"
18 class PrintSystemTaskProxy;
21 class DictionaryValue;
22 class RefCountedBytes;
30 struct PageSizeMargins;
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 {
39 PrintPreviewHandler();
40 virtual ~PrintPreviewHandler();
42 // WebUIMessageHandler implementation.
43 virtual void RegisterMessages() OVERRIDE;
45 // SelectFileDialog::Listener implementation.
46 virtual void FileSelected(const base::FilePath& path,
48 void* params) OVERRIDE;
49 virtual void FileSelectionCanceled(void* params) OVERRIDE;
51 // PrintViewManagerObserver implementation.
52 virtual void OnPrintDialogShown() OVERRIDE;
54 // Displays a modal dialog, prompting the user to select a file.
55 void SelectFile(const base::FilePath& default_path);
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
60 void OnPrintPreviewDialogDestroyed();
62 // Called when print preview failed.
63 void OnPrintPreviewFailed();
65 // Called when the user press ctrl+shift+p to display the native system
67 void ShowSystemDialog();
70 class AccessTokenService;
71 struct CUPSPrinterColorModels;
73 content::WebContents* preview_web_contents() const;
75 // Gets the list of printers. |args| is unused.
76 void HandleGetPrinters(const base::ListValue* args);
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);
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);
86 // Handles the request to hide the preview dialog for printing.
88 void HandleHidePreview(const base::ListValue* args);
90 // Handles the request to cancel the pending print request. |args| is unused.
91 void HandleCancelPendingPrintRequest(const base::ListValue* args);
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);
97 // Gets the printer capabilities. First element of |args| is the printer name.
98 void HandleGetPrinterCapabilities(const base::ListValue* args);
100 // Asks the initiator renderer to show the native print system dialog. |args|
102 void HandleShowSystemDialog(const base::ListValue* args);
104 // Callback for the signin dialog to call once signin is complete.
105 void OnSigninComplete();
107 // Brings up a dialog to allow the user to sign into cloud print.
109 void HandleSignin(const base::ListValue* args);
111 // Generates new token and sends back to UI.
112 void HandleGetAccessToken(const base::ListValue* args);
114 // Brings up a web page to allow the user to configure cloud print.
116 void HandleManageCloudPrint(const base::ListValue* args);
118 // Gathers UMA stats when the print preview dialog is about to close.
120 void HandleClosePreviewDialog(const base::ListValue* args);
122 // Asks the browser to show the native printer management dialog.
124 void HandleManagePrinters(const base::ListValue* args);
126 // Asks the browser to show the cloud print dialog. |args| is signle int with
128 void HandlePrintWithCloudPrintDialog(const base::ListValue* args);
130 // Asks the browser for several settings that are needed before the first
131 // preview is displayed.
132 void HandleGetInitialSettings(const base::ListValue* args);
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);
138 // Forces the opening of a new tab. |args| should consist of one element: the
139 // URL to set the new tab to.
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);
147 void SendInitialSettings(const std::string& default_printer);
149 // Send OAuth2 access token.
150 void SendAccessToken(const std::string& type,
151 const std::string& access_token);
153 // Sends the printer capabilities to the Web UI. |settings_info| contains
154 // printer capabilities information.
155 void SendPrinterCapabilities(const base::DictionaryValue* settings_info);
157 // Sends error notification to the Web UI when unable to return the printer
159 void SendFailedToGetPrinterCapabilities(const std::string& printer_name);
161 // Send the list of printers to the Web UI.
162 void SetupPrinterList(const base::ListValue* printers);
164 // Send whether cloud print integration should be enabled.
165 void SendCloudPrintEnabled();
167 // Send the PDF data to the cloud to print.
168 void SendCloudPrintJob(const base::RefCountedBytes* data);
170 // Handles printing to PDF.
173 // Asks the browser to show the cloud print dialog.
174 void PrintWithCloudPrintDialog();
176 // Gets the initiator for the print preview dialog.
177 content::WebContents* GetInitiator() const;
179 // Closes the preview dialog.
180 void ClosePreviewDialog();
182 // Adds all the recorded stats taken so far to histogram counts.
185 // Clears initiator details for the print preview dialog.
186 void ClearInitiatorDetails();
188 // Posts a task to save |data| to pdf at |print_to_pdf_path_|.
189 void PostPrintToPdfTask();
191 // Populates |settings| according to the current locale.
192 void GetNumberFormatAndMeasurementSystem(base::DictionaryValue* settings);
194 bool GetPreviewDataAndTitle(scoped_refptr<base::RefCountedBytes>* data,
195 string16* title) const;
197 #if defined(USE_CUPS)
198 void SaveCUPSColorSetting(const base::DictionaryValue* settings);
200 void ConvertColorSettingToCUPSColorModel(
201 base::DictionaryValue* settings) const;
204 // Pointer to current print system.
205 scoped_refptr<printing::PrintBackend> print_backend_;
207 // The underlying dialog object.
208 scoped_refptr<ui::SelectFileDialog> select_file_dialog_;
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_;
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_;
218 // Whether we have already logged a failed print preview.
219 bool reported_failed_preview_;
221 // Whether we have already logged the number of printers this session.
222 bool has_logged_printers_count_;
224 // Holds the path to the print to pdf request. It is empty if no such request
226 base::FilePath print_to_pdf_path_;
228 // Holds token service to get OAuth2 access tokens.
229 scoped_ptr<AccessTokenService> token_service_;
231 #if defined(USE_CUPS)
232 // The color capabilities from the last printer queried.
233 scoped_ptr<CUPSPrinterColorModels> cups_printer_color_models_;
236 base::WeakPtrFactory<PrintPreviewHandler> weak_factory_;
238 DISALLOW_COPY_AND_ASSIGN(PrintPreviewHandler);
241 #endif // CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_PRINT_PREVIEW_HANDLER_H_