Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / webui / print_preview / print_preview_handler.h
blob7e927282cc4f97884955bb3619459cd64a796b0d
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 "chrome/browser/ui/webui/print_preview/print_preview_distiller.h"
16 #include "components/signin/core/browser/gaia_cookie_manager_service.h"
17 #include "content/public/browser/web_ui_message_handler.h"
18 #include "ui/shell_dialogs/select_file_dialog.h"
20 #if defined(ENABLE_SERVICE_DISCOVERY)
21 #include "chrome/browser/local_discovery/privet_local_printer_lister.h"
22 #include "chrome/browser/local_discovery/service_discovery_shared_client.h"
23 #endif // ENABLE_SERVICE_DISCOVERY
25 class PrinterHandler;
26 class PrintPreviewUI;
27 class PrintSystemTaskProxy;
29 namespace base {
30 class DictionaryValue;
31 class RefCountedBytes;
34 namespace content {
35 class WebContents;
38 namespace gfx {
39 class Size;
42 // The handler for Javascript messages related to the print preview dialog.
43 class PrintPreviewHandler
44 : public content::WebUIMessageHandler,
45 #if defined(ENABLE_SERVICE_DISCOVERY)
46 public local_discovery::PrivetLocalPrinterLister::Delegate,
47 public local_discovery::PrivetLocalPrintOperation::Delegate,
48 #endif
49 public ui::SelectFileDialog::Listener,
50 public printing::PrintViewManagerObserver,
51 public GaiaCookieManagerService::Observer {
52 public:
53 PrintPreviewHandler();
54 ~PrintPreviewHandler() override;
56 // WebUIMessageHandler implementation.
57 void RegisterMessages() override;
59 // SelectFileDialog::Listener implementation.
60 void FileSelected(const base::FilePath& path,
61 int index,
62 void* params) override;
63 void FileSelectionCanceled(void* params) override;
65 // PrintViewManagerObserver implementation.
66 void OnPrintDialogShown() override;
68 // GaiaCookieManagerService::Observer implementation.
69 void OnAddAccountToCookieCompleted(
70 const std::string& account_id,
71 const GoogleServiceAuthError& error) override;
73 // Called when the print preview dialog is destroyed. This is the last time
74 // this object has access to the PrintViewManager in order to disconnect the
75 // observer.
76 void OnPrintPreviewDialogDestroyed();
78 // Called when print preview failed.
79 void OnPrintPreviewFailed();
81 #if defined(ENABLE_BASIC_PRINTING)
82 // Called when the user press ctrl+shift+p to display the native system
83 // dialog.
84 void ShowSystemDialog();
85 #endif // ENABLE_BASIC_PRINTING
87 #if defined(ENABLE_SERVICE_DISCOVERY)
88 // PrivetLocalPrinterLister::Delegate implementation.
89 void LocalPrinterChanged(
90 bool added,
91 const std::string& name,
92 bool has_local_printing,
93 const local_discovery::DeviceDescription& description) override;
94 void LocalPrinterRemoved(const std::string& name) override;
95 void LocalPrinterCacheFlushed() override;
97 // PrivetLocalPrintOperation::Delegate implementation.
98 void OnPrivetPrintingDone(const local_discovery::PrivetLocalPrintOperation*
99 print_operation) override;
100 void OnPrivetPrintingError(
101 const local_discovery::PrivetLocalPrintOperation* print_operation,
102 int http_code) override;
103 #endif // ENABLE_SERVICE_DISCOVERY
104 int regenerate_preview_request_count() const {
105 return regenerate_preview_request_count_;
108 // Sets |pdf_file_saved_closure_| to |closure|.
109 void SetPdfSavedClosureForTesting(const base::Closure& closure);
111 private:
112 friend class PrintPreviewPdfGeneratedBrowserTest;
113 FRIEND_TEST_ALL_PREFIXES(PrintPreviewPdfGeneratedBrowserTest,
114 MANUAL_DummyTest);
115 class AccessTokenService;
117 static bool PrivetPrintingEnabled();
119 content::WebContents* preview_web_contents() const;
121 PrintPreviewUI* print_preview_ui() const;
123 // Gets the list of printers. |args| is unused.
124 void HandleGetPrinters(const base::ListValue* args);
126 // Starts getting all local privet printers. |args| is unused.
127 void HandleGetPrivetPrinters(const base::ListValue* args);
129 // Starts getting all local extension managed printers. |args| is unused.
130 void HandleGetExtensionPrinters(const base::ListValue* args);
132 // Grants an extension access to a provisional printer. First element of
133 // |args| is the provisional printer ID.
134 void HandleGrantExtensionPrinterAccess(const base::ListValue* args);
136 // Stops getting all local privet printers. |arg| is unused.
137 void HandleStopGetPrivetPrinters(const base::ListValue* args);
139 // Asks the initiator renderer to generate a preview. First element of |args|
140 // is a job settings JSON string.
141 void HandleGetPreview(const base::ListValue* args);
143 // Gets the job settings from Web UI and initiate printing. First element of
144 // |args| is a job settings JSON string.
145 void HandlePrint(const base::ListValue* args);
147 // Handles the request to hide the preview dialog for printing.
148 // |args| is unused.
149 void HandleHidePreview(const base::ListValue* args);
151 // Handles the request to cancel the pending print request. |args| is unused.
152 void HandleCancelPendingPrintRequest(const base::ListValue* args);
154 // Handles a request to store data that the web ui wishes to persist.
155 // First element of |args| is the data to persist.
156 void HandleSaveAppState(const base::ListValue* args);
158 // Gets the printer capabilities. First element of |args| is the printer name.
159 void HandleGetPrinterCapabilities(const base::ListValue* args);
161 #if defined(ENABLE_BASIC_PRINTING)
162 // Asks the initiator renderer to show the native print system dialog. |args|
163 // is unused.
164 void HandleShowSystemDialog(const base::ListValue* args);
165 #endif // ENABLE_BASIC_PRINTING
167 // Callback for the signin dialog to call once signin is complete.
168 void OnSigninComplete();
170 // Brings up a dialog to allow the user to sign into cloud print.
171 // |args| is unused.
172 void HandleSignin(const base::ListValue* args);
174 // Generates new token and sends back to UI.
175 void HandleGetAccessToken(const base::ListValue* args);
177 // Brings up a web page to allow the user to configure cloud print.
178 // |args| is unused.
179 void HandleManageCloudPrint(const base::ListValue* args);
181 // Gathers UMA stats when the print preview dialog is about to close.
182 // |args| is unused.
183 void HandleClosePreviewDialog(const base::ListValue* args);
185 // Asks the browser to show the native printer management dialog.
186 // |args| is unused.
187 void HandleManagePrinters(const base::ListValue* args);
189 // Asks the browser for several settings that are needed before the first
190 // preview is displayed.
191 void HandleGetInitialSettings(const base::ListValue* args);
193 // Reports histogram data for a print preview UI action. |args| should consist
194 // of two elements: the bucket name, and the bucket event.
195 void HandleReportUiEvent(const base::ListValue* args);
197 // Forces the opening of a new tab. |args| should consist of one element: the
198 // URL to set the new tab to.
200 // NOTE: This is needed to open FedEx confirmation window as a new tab.
201 // Javascript's "window.open" opens a new window popup (since initiated from
202 // async HTTP request) and worse yet, on Windows and Chrome OS, the opened
203 // window opens behind the initiator window.
204 void HandleForceOpenNewTab(const base::ListValue* args);
206 void HandleGetPrivetPrinterCapabilities(const base::ListValue* arg);
208 // Requests an extension managed printer's capabilities.
209 // |arg| contains the ID of the printer whose capabilities are requested.
210 void HandleGetExtensionPrinterCapabilities(const base::ListValue* args);
212 void SendInitialSettings(const std::string& default_printer);
214 // Send OAuth2 access token.
215 void SendAccessToken(const std::string& type,
216 const std::string& access_token);
218 // Sends the printer capabilities to the Web UI. |settings_info| contains
219 // printer capabilities information.
220 void SendPrinterCapabilities(const base::DictionaryValue* settings_info);
222 // Sends error notification to the Web UI when unable to return the printer
223 // capabilities.
224 void SendFailedToGetPrinterCapabilities(const std::string& printer_name);
226 // Send the list of printers to the Web UI.
227 void SetupPrinterList(const base::ListValue* printers);
229 // Send whether cloud print integration should be enabled.
230 void SendCloudPrintEnabled();
232 // Send the PDF data to the cloud to print.
233 void SendCloudPrintJob(const base::RefCountedBytes* data);
235 // Handles printing to PDF.
236 void PrintToPdf();
238 // Gets the initiator for the print preview dialog.
239 content::WebContents* GetInitiator() const;
241 // Closes the preview dialog.
242 void ClosePreviewDialog();
244 // Adds all the recorded stats taken so far to histogram counts.
245 void ReportStats();
247 // Clears initiator details for the print preview dialog.
248 void ClearInitiatorDetails();
250 // Posts a task to save |data| to pdf at |print_to_pdf_path_|.
251 void PostPrintToPdfTask();
253 // Populates |settings| according to the current locale.
254 void GetNumberFormatAndMeasurementSystem(base::DictionaryValue* settings);
256 bool GetPreviewDataAndTitle(scoped_refptr<base::RefCountedBytes>* data,
257 base::string16* title) const;
259 // If |prompt_user| is true, displays a modal dialog, prompting the user to
260 // select a file. Otherwise, just accept |default_path| and uniquify it.
261 void SelectFile(const base::FilePath& default_path, bool prompt_user);
263 // Helper for getting a unique file name for SelectFile() without prompting
264 // the user. Just an adaptor for FileSelected().
265 void OnGotUniqueFileName(const base::FilePath& path);
267 #if defined(USE_CUPS)
268 void SaveCUPSColorSetting(const base::DictionaryValue* settings);
270 void ConvertColorSettingToCUPSColorModel(
271 base::DictionaryValue* settings) const;
272 #endif
274 #if defined(ENABLE_SERVICE_DISCOVERY)
275 void StartPrivetLister(const scoped_refptr<
276 local_discovery::ServiceDiscoverySharedClient>& client);
277 void OnPrivetCapabilities(const base::DictionaryValue* capabilities);
278 void PrivetCapabilitiesUpdateClient(
279 scoped_ptr<local_discovery::PrivetHTTPClient> http_client);
280 void PrivetLocalPrintUpdateClient(
281 std::string print_ticket,
282 std::string capabilities,
283 gfx::Size page_size,
284 scoped_ptr<local_discovery::PrivetHTTPClient> http_client);
285 bool PrivetUpdateClient(
286 scoped_ptr<local_discovery::PrivetHTTPClient> http_client);
287 void StartPrivetLocalPrint(const std::string& print_ticket,
288 const std::string& capabilities,
289 const gfx::Size& page_size);
290 void SendPrivetCapabilitiesError(const std::string& id);
291 void PrintToPrivetPrinter(const std::string& printer_name,
292 const std::string& print_ticket,
293 const std::string& capabilities,
294 const gfx::Size& page_size);
295 bool CreatePrivetHTTP(
296 const std::string& name,
297 const local_discovery::PrivetHTTPAsynchronousFactory::ResultCallback&
298 callback);
299 void FillPrinterDescription(
300 const std::string& name,
301 const local_discovery::DeviceDescription& description,
302 bool has_local_printing,
303 base::DictionaryValue* printer_value);
304 #endif
306 // Lazily creates |extension_printer_handler_| that can be used to handle
307 // extension printers requests.
308 void EnsureExtensionPrinterHandlerSet();
310 // Called when a list of printers is reported by an extension.
311 // |printers|: The list of printers managed by the extension.
312 // |done|: Whether all the extensions have reported the list of printers
313 // they manage.
314 void OnGotPrintersForExtension(const base::ListValue& printers, bool done);
316 // Called when an extension reports information requested for a provisional
317 // printer.
318 // |printer_id|: The provisional printer id.
319 // |printer_info|: The data reported by the extension.
320 void OnGotExtensionPrinterInfo(const std::string& printer_id,
321 const base::DictionaryValue& printer_info);
323 // Called when an extension reports the set of print capabilites for a
324 // printer.
325 // |printer_id|: The id of the printer whose capabilities are reported.
326 // |capabilities|: The printer capabilities.
327 void OnGotExtensionPrinterCapabilities(
328 const std::string& printer_id,
329 const base::DictionaryValue& capabilities);
331 // Called when an extension print job is completed.
332 // |success|: Whether the job succeeded.
333 // |status|: The returned print job status. Useful for reporting a specific
334 // error.
335 void OnExtensionPrintResult(bool success, const std::string& status);
337 // Called when the DOM Distiller determines whether or not this page can
338 // be distilled.
339 // |distillable|: Whether or not this page can be distilled.
340 void HandleIsPageDistillableResult(bool distillable);
342 // Register/unregister from notifications of changes done to the GAIA
343 // cookie.
344 void RegisterForGaiaCookieChanges();
345 void UnregisterForGaiaCookieChanges();
347 // The underlying dialog object.
348 scoped_refptr<ui::SelectFileDialog> select_file_dialog_;
350 // A count of how many requests received to regenerate preview data.
351 // Initialized to 0 then incremented and emitted to a histogram.
352 int regenerate_preview_request_count_;
354 // A count of how many requests received to show manage printers dialog.
355 int manage_printers_dialog_request_count_;
356 int manage_cloud_printers_dialog_request_count_;
358 // Whether we have already logged a failed print preview.
359 bool reported_failed_preview_;
361 // Whether we have already logged the number of printers this session.
362 bool has_logged_printers_count_;
364 // Holds the path to the print to pdf request. It is empty if no such request
365 // exists.
366 base::FilePath print_to_pdf_path_;
368 // Holds token service to get OAuth2 access tokens.
369 scoped_ptr<AccessTokenService> token_service_;
371 // Pointer to cookie manager service so that print preview can listen for GAIA
372 // cookie changes.
373 GaiaCookieManagerService* gaia_cookie_manager_service_;
375 #if defined(ENABLE_SERVICE_DISCOVERY)
376 scoped_refptr<local_discovery::ServiceDiscoverySharedClient>
377 service_discovery_client_;
378 scoped_ptr<local_discovery::PrivetLocalPrinterLister> printer_lister_;
380 scoped_ptr<local_discovery::PrivetHTTPAsynchronousFactory>
381 privet_http_factory_;
382 scoped_ptr<local_discovery::PrivetHTTPResolution> privet_http_resolution_;
383 scoped_ptr<local_discovery::PrivetV1HTTPClient> privet_http_client_;
384 scoped_ptr<local_discovery::PrivetJSONOperation>
385 privet_capabilities_operation_;
386 scoped_ptr<local_discovery::PrivetLocalPrintOperation>
387 privet_local_print_operation_;
388 #endif
390 // Handles requests for extension printers. Created lazily by calling
391 // |EnsureExtensionPrinterHandlerSet|.
392 scoped_ptr<PrinterHandler> extension_printer_handler_;
394 // Notifies tests that want to know if the PDF has been saved. This doesn't
395 // notify the test if it was a successful save, only that it was attempted.
396 base::Closure pdf_file_saved_closure_;
398 // A print preview that is responsible for rendering the page after
399 // being processed by the DOM Distiller.
400 scoped_ptr<PrintPreviewDistiller> print_preview_distiller_;
402 base::WeakPtrFactory<PrintPreviewHandler> weak_factory_;
404 DISALLOW_COPY_AND_ASSIGN(PrintPreviewHandler);
407 #endif // CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_PRINT_PREVIEW_HANDLER_H_