Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / webui / print_preview / print_preview_handler.h
blobf4fe70093b91cb992fa9ec61956778c2fca665c7
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_
8 #include <string>
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 #if defined(ENABLE_MDNS)
19 #include "chrome/browser/local_discovery/privet_local_printer_lister.h"
20 #include "chrome/browser/local_discovery/service_discovery_shared_client.h"
21 #endif // ENABLE_MDNS
23 class PrintSystemTaskProxy;
25 namespace base {
26 class DictionaryValue;
27 class RefCountedBytes;
30 namespace content {
31 class WebContents;
34 namespace gfx {
35 class Size;
38 namespace printing {
39 struct PageSizeMargins;
40 class PrintBackend;
43 // The handler for Javascript messages related to the print preview dialog.
44 class PrintPreviewHandler
45 : public content::WebUIMessageHandler,
46 #if defined(ENABLE_MDNS)
47 public local_discovery::PrivetLocalPrinterLister::Delegate,
48 public local_discovery::PrivetLocalPrintOperation::Delegate,
49 #endif
50 public ui::SelectFileDialog::Listener,
51 public printing::PrintViewManagerObserver {
52 public:
53 PrintPreviewHandler();
54 virtual ~PrintPreviewHandler();
56 // WebUIMessageHandler implementation.
57 virtual void RegisterMessages() OVERRIDE;
59 // SelectFileDialog::Listener implementation.
60 virtual void FileSelected(const base::FilePath& path,
61 int index,
62 void* params) OVERRIDE;
63 virtual void FileSelectionCanceled(void* params) OVERRIDE;
65 // PrintViewManagerObserver implementation.
66 virtual void OnPrintDialogShown() OVERRIDE;
68 // Displays a modal dialog, prompting the user to select a file.
69 void SelectFile(const base::FilePath& default_path);
71 // Called when the print preview dialog is destroyed. This is the last time
72 // this object has access to the PrintViewManager in order to disconnect the
73 // observer.
74 void OnPrintPreviewDialogDestroyed();
76 // Called when print preview failed.
77 void OnPrintPreviewFailed();
79 // Called when the user press ctrl+shift+p to display the native system
80 // dialog.
81 void ShowSystemDialog();
83 #if defined(ENABLE_MDNS)
84 // PrivetLocalPrinterLister::Delegate implementation.
85 virtual void LocalPrinterChanged(
86 bool added,
87 const std::string& name,
88 bool has_local_printing,
89 const local_discovery::DeviceDescription& description) OVERRIDE;
90 virtual void LocalPrinterRemoved(const std::string& name) OVERRIDE;
91 virtual void LocalPrinterCacheFlushed() OVERRIDE;
93 // PrivetLocalPrintOperation::Delegate implementation.
94 virtual void OnPrivetPrintingDone(
95 const local_discovery::PrivetLocalPrintOperation*
96 print_operation) OVERRIDE;
97 virtual void OnPrivetPrintingError(
98 const local_discovery::PrivetLocalPrintOperation* print_operation,
99 int http_code) OVERRIDE;
100 #endif // ENABLE_MDNS
101 int regenerate_preview_request_count() const {
102 return regenerate_preview_request_count_;
105 private:
106 class AccessTokenService;
107 struct CUPSPrinterColorModels;
109 static bool PrivetPrintingEnabled();
111 content::WebContents* preview_web_contents() const;
113 // Gets the list of printers. |args| is unused.
114 void HandleGetPrinters(const base::ListValue* args);
116 // Starts getting all local privet printers. |arg| is unused.
117 void HandleGetPrivetPrinters(const base::ListValue* args);
119 // Stops getting all local privet printers. |arg| is unused.
120 void HandleStopGetPrivetPrinters(const base::ListValue* args);
122 // Asks the initiator renderer to generate a preview. First element of |args|
123 // is a job settings JSON string.
124 void HandleGetPreview(const base::ListValue* args);
126 // Gets the job settings from Web UI and initiate printing. First element of
127 // |args| is a job settings JSON string.
128 void HandlePrint(const base::ListValue* args);
130 // Handles the request to hide the preview dialog for printing.
131 // |args| is unused.
132 void HandleHidePreview(const base::ListValue* args);
134 // Handles the request to cancel the pending print request. |args| is unused.
135 void HandleCancelPendingPrintRequest(const base::ListValue* args);
137 // Handles a request to store data that the web ui wishes to persist.
138 // First element of |args| is the data to persist.
139 void HandleSaveAppState(const base::ListValue* args);
141 // Gets the printer capabilities. First element of |args| is the printer name.
142 void HandleGetPrinterCapabilities(const base::ListValue* args);
144 // Asks the initiator renderer to show the native print system dialog. |args|
145 // is unused.
146 void HandleShowSystemDialog(const base::ListValue* args);
148 // Callback for the signin dialog to call once signin is complete.
149 void OnSigninComplete();
151 // Brings up a dialog to allow the user to sign into cloud print.
152 // |args| is unused.
153 void HandleSignin(const base::ListValue* args);
155 // Generates new token and sends back to UI.
156 void HandleGetAccessToken(const base::ListValue* args);
158 // Brings up a web page to allow the user to configure cloud print.
159 // |args| is unused.
160 void HandleManageCloudPrint(const base::ListValue* args);
162 // Gathers UMA stats when the print preview dialog is about to close.
163 // |args| is unused.
164 void HandleClosePreviewDialog(const base::ListValue* args);
166 // Asks the browser to show the native printer management dialog.
167 // |args| is unused.
168 void HandleManagePrinters(const base::ListValue* args);
170 // Asks the browser to show the cloud print dialog. |args| is signle int with
171 // page count.
172 void HandlePrintWithCloudPrintDialog(const base::ListValue* args);
174 // Asks the browser for several settings that are needed before the first
175 // preview is displayed.
176 void HandleGetInitialSettings(const base::ListValue* args);
178 // Reports histogram data for a print preview UI action. |args| should consist
179 // of two elements: the bucket name, and the bucket event.
180 void HandleReportUiEvent(const base::ListValue* args);
182 // Forces the opening of a new tab. |args| should consist of one element: the
183 // URL to set the new tab to.
185 // NOTE: This is needed to open FedEx confirmation window as a new tab.
186 // Javascript's "window.open" opens a new window popup (since initiated from
187 // async HTTP request) and worse yet, on Windows and Chrome OS, the opened
188 // window opens behind the initiator window.
189 void HandleForceOpenNewTab(const base::ListValue* args);
191 void HandleGetPrivetPrinterCapabilities(const base::ListValue* arg);
193 void SendInitialSettings(const std::string& default_printer);
195 // Send OAuth2 access token.
196 void SendAccessToken(const std::string& type,
197 const std::string& access_token);
199 // Sends the printer capabilities to the Web UI. |settings_info| contains
200 // printer capabilities information.
201 void SendPrinterCapabilities(const base::DictionaryValue* settings_info);
203 // Sends error notification to the Web UI when unable to return the printer
204 // capabilities.
205 void SendFailedToGetPrinterCapabilities(const std::string& printer_name);
207 // Send the list of printers to the Web UI.
208 void SetupPrinterList(const base::ListValue* printers);
210 // Send whether cloud print integration should be enabled.
211 void SendCloudPrintEnabled();
213 // Send the PDF data to the cloud to print.
214 void SendCloudPrintJob(const base::RefCountedBytes* data);
216 // Handles printing to PDF.
217 void PrintToPdf();
219 // Asks the browser to show the cloud print dialog.
220 void PrintWithCloudPrintDialog();
222 // Gets the initiator for the print preview dialog.
223 content::WebContents* GetInitiator() const;
225 // Closes the preview dialog.
226 void ClosePreviewDialog();
228 // Adds all the recorded stats taken so far to histogram counts.
229 void ReportStats();
231 // Clears initiator details for the print preview dialog.
232 void ClearInitiatorDetails();
234 // Posts a task to save |data| to pdf at |print_to_pdf_path_|.
235 void PostPrintToPdfTask();
237 // Populates |settings| according to the current locale.
238 void GetNumberFormatAndMeasurementSystem(base::DictionaryValue* settings);
240 bool GetPreviewDataAndTitle(scoped_refptr<base::RefCountedBytes>* data,
241 base::string16* title) const;
243 #if defined(USE_CUPS)
244 void SaveCUPSColorSetting(const base::DictionaryValue* settings);
246 void ConvertColorSettingToCUPSColorModel(
247 base::DictionaryValue* settings) const;
248 #endif
250 #if defined(ENABLE_MDNS)
251 void OnPrivetCapabilities(const base::DictionaryValue* capabilities);
254 void PrivetCapabilitiesUpdateClient(
255 scoped_ptr<local_discovery::PrivetHTTPClient> http_client);
256 void PrivetLocalPrintUpdateClient(
257 std::string print_ticket,
258 gfx::Size page_size,
259 scoped_ptr<local_discovery::PrivetHTTPClient> http_client);
260 bool PrivetUpdateClient(
261 scoped_ptr<local_discovery::PrivetHTTPClient> http_client);
262 void StartPrivetLocalPrint(const std::string& print_ticket,
263 const gfx::Size& page_size);
264 void SendPrivetCapabilitiesError(const std::string& id);
265 void PrintToPrivetPrinter(const std::string& printer_name,
266 const std::string& print_ticket,
267 const gfx::Size& page_size);
268 bool CreatePrivetHTTP(
269 const std::string& name,
270 const local_discovery::PrivetHTTPAsynchronousFactory::ResultCallback&
271 callback);
272 void FillPrinterDescription(
273 const std::string& name,
274 const local_discovery::DeviceDescription& description,
275 bool has_local_printing,
276 base::DictionaryValue* printer_value);
277 #endif
279 // Pointer to current print system.
280 scoped_refptr<printing::PrintBackend> print_backend_;
282 // The underlying dialog object.
283 scoped_refptr<ui::SelectFileDialog> select_file_dialog_;
285 // A count of how many requests received to regenerate preview data.
286 // Initialized to 0 then incremented and emitted to a histogram.
287 int regenerate_preview_request_count_;
289 // A count of how many requests received to show manage printers dialog.
290 int manage_printers_dialog_request_count_;
291 int manage_cloud_printers_dialog_request_count_;
293 // Whether we have already logged a failed print preview.
294 bool reported_failed_preview_;
296 // Whether we have already logged the number of printers this session.
297 bool has_logged_printers_count_;
299 // Holds the path to the print to pdf request. It is empty if no such request
300 // exists.
301 base::FilePath print_to_pdf_path_;
303 // Holds token service to get OAuth2 access tokens.
304 scoped_ptr<AccessTokenService> token_service_;
306 #if defined(USE_CUPS)
307 // The color capabilities from the last printer queried.
308 scoped_ptr<CUPSPrinterColorModels> cups_printer_color_models_;
309 #endif
311 #if defined(ENABLE_MDNS)
312 scoped_refptr<local_discovery::ServiceDiscoverySharedClient>
313 service_discovery_client_;
314 scoped_ptr<local_discovery::PrivetLocalPrinterLister> printer_lister_;
316 scoped_ptr<local_discovery::PrivetHTTPAsynchronousFactory>
317 privet_http_factory_;
318 scoped_ptr<local_discovery::PrivetHTTPResolution> privet_http_resolution_;
319 scoped_ptr<local_discovery::PrivetHTTPClient> privet_http_client_;
320 scoped_ptr<local_discovery::PrivetJSONOperation>
321 privet_capabilities_operation_;
322 scoped_ptr<local_discovery::PrivetLocalPrintOperation>
323 privet_local_print_operation_;
324 #endif
326 base::WeakPtrFactory<PrintPreviewHandler> weak_factory_;
328 DISALLOW_COPY_AND_ASSIGN(PrintPreviewHandler);
331 #endif // CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_PRINT_PREVIEW_HANDLER_H_