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 "google_apis/gaia/merge_session_helper.h"
17 #include "ui/shell_dialogs/select_file_dialog.h"
19 #if defined(ENABLE_SERVICE_DISCOVERY)
20 #include "chrome/browser/local_discovery/privet_local_printer_lister.h"
21 #include "chrome/browser/local_discovery/service_discovery_shared_client.h"
22 #endif // ENABLE_SERVICE_DISCOVERY
24 class AccountReconcilor;
25 class PrintSystemTaskProxy;
28 class DictionaryValue;
29 class RefCountedBytes;
40 // The handler for Javascript messages related to the print preview dialog.
41 class PrintPreviewHandler
42 : public content::WebUIMessageHandler,
43 #if defined(ENABLE_SERVICE_DISCOVERY)
44 public local_discovery::PrivetLocalPrinterLister::Delegate,
45 public local_discovery::PrivetLocalPrintOperation::Delegate,
47 public ui::SelectFileDialog::Listener,
48 public printing::PrintViewManagerObserver,
49 public MergeSessionHelper::Observer {
51 PrintPreviewHandler();
52 virtual ~PrintPreviewHandler();
54 // WebUIMessageHandler implementation.
55 virtual void RegisterMessages() OVERRIDE;
57 // SelectFileDialog::Listener implementation.
58 virtual void FileSelected(const base::FilePath& path,
60 void* params) OVERRIDE;
61 virtual void FileSelectionCanceled(void* params) OVERRIDE;
63 // PrintViewManagerObserver implementation.
64 virtual void OnPrintDialogShown() OVERRIDE;
66 // MergeSessionHelper::Observer implementation.
67 virtual void MergeSessionCompleted(
68 const std::string& account_id,
69 const GoogleServiceAuthError& error) OVERRIDE;
71 // Displays a modal dialog, prompting the user to select a file.
72 void SelectFile(const base::FilePath& default_path);
74 // Called when the print preview dialog is destroyed. This is the last time
75 // this object has access to the PrintViewManager in order to disconnect the
77 void OnPrintPreviewDialogDestroyed();
79 // Called when print preview failed.
80 void OnPrintPreviewFailed();
82 #if !defined(DISABLE_BASIC_PRINTING)
83 // Called when the user press ctrl+shift+p to display the native system
85 void ShowSystemDialog();
86 #endif // !DISABLE_BASIC_PRINTING
88 #if defined(ENABLE_SERVICE_DISCOVERY)
89 // PrivetLocalPrinterLister::Delegate implementation.
90 virtual void LocalPrinterChanged(
92 const std::string& name,
93 bool has_local_printing,
94 const local_discovery::DeviceDescription& description) OVERRIDE;
95 virtual void LocalPrinterRemoved(const std::string& name) OVERRIDE;
96 virtual void LocalPrinterCacheFlushed() OVERRIDE;
98 // PrivetLocalPrintOperation::Delegate implementation.
99 virtual void OnPrivetPrintingDone(
100 const local_discovery::PrivetLocalPrintOperation*
101 print_operation) OVERRIDE;
102 virtual void OnPrivetPrintingError(
103 const local_discovery::PrivetLocalPrintOperation* print_operation,
104 int http_code) OVERRIDE;
105 #endif // ENABLE_SERVICE_DISCOVERY
106 int regenerate_preview_request_count() const {
107 return regenerate_preview_request_count_;
110 // Sets |pdf_file_saved_closure_| to |closure|.
111 void SetPdfSavedClosureForTesting(const base::Closure& closure);
114 friend class PrintPreviewPdfGeneratedBrowserTest;
115 FRIEND_TEST_ALL_PREFIXES(PrintPreviewPdfGeneratedBrowserTest,
117 class AccessTokenService;
119 static bool PrivetPrintingEnabled();
121 content::WebContents* preview_web_contents() const;
123 // Gets the list of printers. |args| is unused.
124 void HandleGetPrinters(const base::ListValue* args);
126 // Starts getting all local privet printers. |arg| is unused.
127 void HandleGetPrivetPrinters(const base::ListValue* args);
129 // Stops getting all local privet printers. |arg| is unused.
130 void HandleStopGetPrivetPrinters(const base::ListValue* args);
132 // Asks the initiator renderer to generate a preview. First element of |args|
133 // is a job settings JSON string.
134 void HandleGetPreview(const base::ListValue* args);
136 // Gets the job settings from Web UI and initiate printing. First element of
137 // |args| is a job settings JSON string.
138 void HandlePrint(const base::ListValue* args);
140 // Handles the request to hide the preview dialog for printing.
142 void HandleHidePreview(const base::ListValue* args);
144 // Handles the request to cancel the pending print request. |args| is unused.
145 void HandleCancelPendingPrintRequest(const base::ListValue* args);
147 // Handles a request to store data that the web ui wishes to persist.
148 // First element of |args| is the data to persist.
149 void HandleSaveAppState(const base::ListValue* args);
151 // Gets the printer capabilities. First element of |args| is the printer name.
152 void HandleGetPrinterCapabilities(const base::ListValue* args);
154 #if !defined(DISABLE_BASIC_PRINTING)
155 // Asks the initiator renderer to show the native print system dialog. |args|
157 void HandleShowSystemDialog(const base::ListValue* args);
158 #endif // !DISABLE_BASIC_PRINTING
160 // Callback for the signin dialog to call once signin is complete.
161 void OnSigninComplete();
163 // Brings up a dialog to allow the user to sign into cloud print.
165 void HandleSignin(const base::ListValue* args);
167 // Generates new token and sends back to UI.
168 void HandleGetAccessToken(const base::ListValue* args);
170 // Brings up a web page to allow the user to configure cloud print.
172 void HandleManageCloudPrint(const base::ListValue* args);
174 // Gathers UMA stats when the print preview dialog is about to close.
176 void HandleClosePreviewDialog(const base::ListValue* args);
178 // Asks the browser to show the native printer management dialog.
180 void HandleManagePrinters(const base::ListValue* args);
182 // Asks the browser to show the cloud print dialog. |args| is signle int with
184 void HandlePrintWithCloudPrintDialog(const base::ListValue* args);
186 // Asks the browser for several settings that are needed before the first
187 // preview is displayed.
188 void HandleGetInitialSettings(const base::ListValue* args);
190 // Reports histogram data for a print preview UI action. |args| should consist
191 // of two elements: the bucket name, and the bucket event.
192 void HandleReportUiEvent(const base::ListValue* args);
194 // Forces the opening of a new tab. |args| should consist of one element: the
195 // URL to set the new tab to.
197 // NOTE: This is needed to open FedEx confirmation window as a new tab.
198 // Javascript's "window.open" opens a new window popup (since initiated from
199 // async HTTP request) and worse yet, on Windows and Chrome OS, the opened
200 // window opens behind the initiator window.
201 void HandleForceOpenNewTab(const base::ListValue* args);
203 void HandleGetPrivetPrinterCapabilities(const base::ListValue* arg);
205 void SendInitialSettings(const std::string& default_printer);
207 // Send OAuth2 access token.
208 void SendAccessToken(const std::string& type,
209 const std::string& access_token);
211 // Sends the printer capabilities to the Web UI. |settings_info| contains
212 // printer capabilities information.
213 void SendPrinterCapabilities(const base::DictionaryValue* settings_info);
215 // Sends error notification to the Web UI when unable to return the printer
217 void SendFailedToGetPrinterCapabilities(const std::string& printer_name);
219 // Send the list of printers to the Web UI.
220 void SetupPrinterList(const base::ListValue* printers);
222 // Send whether cloud print integration should be enabled.
223 void SendCloudPrintEnabled();
225 // Send the PDF data to the cloud to print.
226 void SendCloudPrintJob(const base::RefCountedBytes* data);
228 // Handles printing to PDF.
231 // Asks the browser to show the cloud print dialog.
232 void PrintWithCloudPrintDialog();
234 // Gets the initiator for the print preview dialog.
235 content::WebContents* GetInitiator() const;
237 // Closes the preview dialog.
238 void ClosePreviewDialog();
240 // Adds all the recorded stats taken so far to histogram counts.
243 // Clears initiator details for the print preview dialog.
244 void ClearInitiatorDetails();
246 // Posts a task to save |data| to pdf at |print_to_pdf_path_|.
247 void PostPrintToPdfTask();
249 // Populates |settings| according to the current locale.
250 void GetNumberFormatAndMeasurementSystem(base::DictionaryValue* settings);
252 bool GetPreviewDataAndTitle(scoped_refptr<base::RefCountedBytes>* data,
253 base::string16* title) const;
255 #if defined(USE_CUPS)
256 void SaveCUPSColorSetting(const base::DictionaryValue* settings);
258 void ConvertColorSettingToCUPSColorModel(
259 base::DictionaryValue* settings) const;
262 #if defined(ENABLE_SERVICE_DISCOVERY)
263 void StartPrivetLister(const scoped_refptr<
264 local_discovery::ServiceDiscoverySharedClient>& client);
265 void OnPrivetCapabilities(const base::DictionaryValue* capabilities);
266 void PrivetCapabilitiesUpdateClient(
267 scoped_ptr<local_discovery::PrivetHTTPClient> http_client);
268 void PrivetLocalPrintUpdateClient(
269 std::string print_ticket,
270 std::string capabilities,
272 scoped_ptr<local_discovery::PrivetHTTPClient> http_client);
273 bool PrivetUpdateClient(
274 scoped_ptr<local_discovery::PrivetHTTPClient> http_client);
275 void StartPrivetLocalPrint(const std::string& print_ticket,
276 const std::string& capabilities,
277 const gfx::Size& page_size);
278 void SendPrivetCapabilitiesError(const std::string& id);
279 void PrintToPrivetPrinter(const std::string& printer_name,
280 const std::string& print_ticket,
281 const std::string& capabilities,
282 const gfx::Size& page_size);
283 bool CreatePrivetHTTP(
284 const std::string& name,
285 const local_discovery::PrivetHTTPAsynchronousFactory::ResultCallback&
287 void FillPrinterDescription(
288 const std::string& name,
289 const local_discovery::DeviceDescription& description,
290 bool has_local_printing,
291 base::DictionaryValue* printer_value);
294 // Register/unregister from notifications of changes done to the GAIA
296 void RegisterForMergeSession();
297 void UnregisterForMergeSession();
299 // The underlying dialog object.
300 scoped_refptr<ui::SelectFileDialog> select_file_dialog_;
302 // A count of how many requests received to regenerate preview data.
303 // Initialized to 0 then incremented and emitted to a histogram.
304 int regenerate_preview_request_count_;
306 // A count of how many requests received to show manage printers dialog.
307 int manage_printers_dialog_request_count_;
308 int manage_cloud_printers_dialog_request_count_;
310 // Whether we have already logged a failed print preview.
311 bool reported_failed_preview_;
313 // Whether we have already logged the number of printers this session.
314 bool has_logged_printers_count_;
316 // Holds the path to the print to pdf request. It is empty if no such request
318 base::FilePath print_to_pdf_path_;
320 // Holds token service to get OAuth2 access tokens.
321 scoped_ptr<AccessTokenService> token_service_;
323 // Pointer to reconcilor so that print preview can listen for GAIA cookie
325 AccountReconcilor* reconcilor_;
327 #if defined(ENABLE_SERVICE_DISCOVERY)
328 scoped_refptr<local_discovery::ServiceDiscoverySharedClient>
329 service_discovery_client_;
330 scoped_ptr<local_discovery::PrivetLocalPrinterLister> printer_lister_;
332 scoped_ptr<local_discovery::PrivetHTTPAsynchronousFactory>
333 privet_http_factory_;
334 scoped_ptr<local_discovery::PrivetHTTPResolution> privet_http_resolution_;
335 scoped_ptr<local_discovery::PrivetV1HTTPClient> privet_http_client_;
336 scoped_ptr<local_discovery::PrivetJSONOperation>
337 privet_capabilities_operation_;
338 scoped_ptr<local_discovery::PrivetLocalPrintOperation>
339 privet_local_print_operation_;
342 // Notifies tests that want to know if the PDF has been saved. This doesn't
343 // notify the test if it was a successful save, only that it was attempted.
344 base::Closure pdf_file_saved_closure_;
346 base::WeakPtrFactory<PrintPreviewHandler> weak_factory_;
348 DISALLOW_COPY_AND_ASSIGN(PrintPreviewHandler);
351 #endif // CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_PRINT_PREVIEW_HANDLER_H_