Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / ui / webui / print_preview / print_preview_handler.h
blob5240f9e88927ce09c67131438692af8e32ecf551
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 "components/signin/core/browser/gaia_cookie_manager_service.h"
16 #include "content/public/browser/web_ui_message_handler.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 PrinterHandler;
25 class PrintPreviewUI;
26 class PrintSystemTaskProxy;
28 namespace base {
29 class DictionaryValue;
30 class RefCountedBytes;
33 namespace content {
34 class WebContents;
37 namespace gfx {
38 class Size;
41 // The handler for Javascript messages related to the print preview dialog.
42 class PrintPreviewHandler
43 : public content::WebUIMessageHandler,
44 #if defined(ENABLE_SERVICE_DISCOVERY)
45 public local_discovery::PrivetLocalPrinterLister::Delegate,
46 public local_discovery::PrivetLocalPrintOperation::Delegate,
47 #endif
48 public ui::SelectFileDialog::Listener,
49 public printing::PrintViewManagerObserver,
50 public GaiaCookieManagerService::Observer {
51 public:
52 PrintPreviewHandler();
53 ~PrintPreviewHandler() override;
55 // WebUIMessageHandler implementation.
56 void RegisterMessages() override;
58 // SelectFileDialog::Listener implementation.
59 void FileSelected(const base::FilePath& path,
60 int index,
61 void* params) override;
62 void FileSelectionCanceled(void* params) override;
64 // PrintViewManagerObserver implementation.
65 void OnPrintDialogShown() override;
67 // GaiaCookieManagerService::Observer implementation.
68 void OnAddAccountToCookieCompleted(
69 const std::string& account_id,
70 const GoogleServiceAuthError& error) override;
72 // Called when the print preview dialog is destroyed. This is the last time
73 // this object has access to the PrintViewManager in order to disconnect the
74 // observer.
75 void OnPrintPreviewDialogDestroyed();
77 // Called when print preview failed.
78 void OnPrintPreviewFailed();
80 #if defined(ENABLE_BASIC_PRINTING)
81 // Called when the user press ctrl+shift+p to display the native system
82 // dialog.
83 void ShowSystemDialog();
84 #endif // ENABLE_BASIC_PRINTING
86 #if defined(ENABLE_SERVICE_DISCOVERY)
87 // PrivetLocalPrinterLister::Delegate implementation.
88 void LocalPrinterChanged(
89 bool added,
90 const std::string& name,
91 bool has_local_printing,
92 const local_discovery::DeviceDescription& description) override;
93 void LocalPrinterRemoved(const std::string& name) override;
94 void LocalPrinterCacheFlushed() override;
96 // PrivetLocalPrintOperation::Delegate implementation.
97 void OnPrivetPrintingDone(const local_discovery::PrivetLocalPrintOperation*
98 print_operation) override;
99 void OnPrivetPrintingError(
100 const local_discovery::PrivetLocalPrintOperation* print_operation,
101 int http_code) override;
102 #endif // ENABLE_SERVICE_DISCOVERY
103 int regenerate_preview_request_count() const {
104 return regenerate_preview_request_count_;
107 // Sets |pdf_file_saved_closure_| to |closure|.
108 void SetPdfSavedClosureForTesting(const base::Closure& closure);
110 private:
111 friend class PrintPreviewPdfGeneratedBrowserTest;
112 FRIEND_TEST_ALL_PREFIXES(PrintPreviewPdfGeneratedBrowserTest,
113 MANUAL_DummyTest);
114 class AccessTokenService;
116 static bool PrivetPrintingEnabled();
118 content::WebContents* preview_web_contents() const;
120 PrintPreviewUI* print_preview_ui() const;
122 // Gets the list of printers. |args| is unused.
123 void HandleGetPrinters(const base::ListValue* args);
125 // Starts getting all local privet printers. |arg| is unused.
126 void HandleGetPrivetPrinters(const base::ListValue* args);
128 // Starts getting all local extension managed printers. |arg| is unused.
129 void HandleGetExtensionPrinters(const base::ListValue* args);
131 // Stops getting all local privet printers. |arg| is unused.
132 void HandleStopGetPrivetPrinters(const base::ListValue* args);
134 // Asks the initiator renderer to generate a preview. First element of |args|
135 // is a job settings JSON string.
136 void HandleGetPreview(const base::ListValue* args);
138 // Gets the job settings from Web UI and initiate printing. First element of
139 // |args| is a job settings JSON string.
140 void HandlePrint(const base::ListValue* args);
142 // Handles the request to hide the preview dialog for printing.
143 // |args| is unused.
144 void HandleHidePreview(const base::ListValue* args);
146 // Handles the request to cancel the pending print request. |args| is unused.
147 void HandleCancelPendingPrintRequest(const base::ListValue* args);
149 // Handles a request to store data that the web ui wishes to persist.
150 // First element of |args| is the data to persist.
151 void HandleSaveAppState(const base::ListValue* args);
153 // Gets the printer capabilities. First element of |args| is the printer name.
154 void HandleGetPrinterCapabilities(const base::ListValue* args);
156 #if defined(ENABLE_BASIC_PRINTING)
157 // Asks the initiator renderer to show the native print system dialog. |args|
158 // is unused.
159 void HandleShowSystemDialog(const base::ListValue* args);
160 #endif // ENABLE_BASIC_PRINTING
162 // Callback for the signin dialog to call once signin is complete.
163 void OnSigninComplete();
165 // Brings up a dialog to allow the user to sign into cloud print.
166 // |args| is unused.
167 void HandleSignin(const base::ListValue* args);
169 // Generates new token and sends back to UI.
170 void HandleGetAccessToken(const base::ListValue* args);
172 // Brings up a web page to allow the user to configure cloud print.
173 // |args| is unused.
174 void HandleManageCloudPrint(const base::ListValue* args);
176 // Gathers UMA stats when the print preview dialog is about to close.
177 // |args| is unused.
178 void HandleClosePreviewDialog(const base::ListValue* args);
180 // Asks the browser to show the native printer management dialog.
181 // |args| is unused.
182 void HandleManagePrinters(const base::ListValue* args);
184 // Asks the browser for several settings that are needed before the first
185 // preview is displayed.
186 void HandleGetInitialSettings(const base::ListValue* args);
188 // Reports histogram data for a print preview UI action. |args| should consist
189 // of two elements: the bucket name, and the bucket event.
190 void HandleReportUiEvent(const base::ListValue* args);
192 // Forces the opening of a new tab. |args| should consist of one element: the
193 // URL to set the new tab to.
195 // NOTE: This is needed to open FedEx confirmation window as a new tab.
196 // Javascript's "window.open" opens a new window popup (since initiated from
197 // async HTTP request) and worse yet, on Windows and Chrome OS, the opened
198 // window opens behind the initiator window.
199 void HandleForceOpenNewTab(const base::ListValue* args);
201 void HandleGetPrivetPrinterCapabilities(const base::ListValue* arg);
203 // Requests an extension managed printer's capabilities.
204 // |arg| contains the ID of the printer whose capabilities are requested.
205 void HandleGetExtensionPrinterCapabilities(const base::ListValue* args);
207 void SendInitialSettings(const std::string& default_printer);
209 // Send OAuth2 access token.
210 void SendAccessToken(const std::string& type,
211 const std::string& access_token);
213 // Sends the printer capabilities to the Web UI. |settings_info| contains
214 // printer capabilities information.
215 void SendPrinterCapabilities(const base::DictionaryValue* settings_info);
217 // Sends error notification to the Web UI when unable to return the printer
218 // capabilities.
219 void SendFailedToGetPrinterCapabilities(const std::string& printer_name);
221 // Send the list of printers to the Web UI.
222 void SetupPrinterList(const base::ListValue* printers);
224 // Send whether cloud print integration should be enabled.
225 void SendCloudPrintEnabled();
227 // Send the PDF data to the cloud to print.
228 void SendCloudPrintJob(const base::RefCountedBytes* data);
230 // Handles printing to PDF.
231 void PrintToPdf();
233 // Gets the initiator for the print preview dialog.
234 content::WebContents* GetInitiator() const;
236 // Closes the preview dialog.
237 void ClosePreviewDialog();
239 // Adds all the recorded stats taken so far to histogram counts.
240 void ReportStats();
242 // Clears initiator details for the print preview dialog.
243 void ClearInitiatorDetails();
245 // Posts a task to save |data| to pdf at |print_to_pdf_path_|.
246 void PostPrintToPdfTask();
248 // Populates |settings| according to the current locale.
249 void GetNumberFormatAndMeasurementSystem(base::DictionaryValue* settings);
251 bool GetPreviewDataAndTitle(scoped_refptr<base::RefCountedBytes>* data,
252 base::string16* title) const;
254 // If |prompt_user| is true, displays a modal dialog, prompting the user to
255 // select a file. Otherwise, just accept |default_path| and uniquify it.
256 void SelectFile(const base::FilePath& default_path, bool prompt_user);
258 // Helper for getting a unique file name for SelectFile() without prompting
259 // the user. Just an adaptor for FileSelected().
260 void OnGotUniqueFileName(const base::FilePath& path);
262 #if defined(USE_CUPS)
263 void SaveCUPSColorSetting(const base::DictionaryValue* settings);
265 void ConvertColorSettingToCUPSColorModel(
266 base::DictionaryValue* settings) const;
267 #endif
269 #if defined(ENABLE_SERVICE_DISCOVERY)
270 void StartPrivetLister(const scoped_refptr<
271 local_discovery::ServiceDiscoverySharedClient>& client);
272 void OnPrivetCapabilities(const base::DictionaryValue* capabilities);
273 void PrivetCapabilitiesUpdateClient(
274 scoped_ptr<local_discovery::PrivetHTTPClient> http_client);
275 void PrivetLocalPrintUpdateClient(
276 std::string print_ticket,
277 std::string capabilities,
278 gfx::Size page_size,
279 scoped_ptr<local_discovery::PrivetHTTPClient> http_client);
280 bool PrivetUpdateClient(
281 scoped_ptr<local_discovery::PrivetHTTPClient> http_client);
282 void StartPrivetLocalPrint(const std::string& print_ticket,
283 const std::string& capabilities,
284 const gfx::Size& page_size);
285 void SendPrivetCapabilitiesError(const std::string& id);
286 void PrintToPrivetPrinter(const std::string& printer_name,
287 const std::string& print_ticket,
288 const std::string& capabilities,
289 const gfx::Size& page_size);
290 bool CreatePrivetHTTP(
291 const std::string& name,
292 const local_discovery::PrivetHTTPAsynchronousFactory::ResultCallback&
293 callback);
294 void FillPrinterDescription(
295 const std::string& name,
296 const local_discovery::DeviceDescription& description,
297 bool has_local_printing,
298 base::DictionaryValue* printer_value);
299 #endif
301 // Lazily creates |extension_printer_handler_| that can be used to handle
302 // extension printers requests.
303 void EnsureExtensionPrinterHandlerSet();
305 // Called when a list of printers is reported by an extension.
306 // |printers|: The list of printers managed by the extension.
307 // |done|: Whether all the extensions have reported the list of printers
308 // they manage.
309 void OnGotPrintersForExtension(const base::ListValue& printers, bool done);
311 // Called when an extension reports the set of print capabilites for a
312 // printer.
313 // |printer_id|: The id of the printer whose capabilities are reported.
314 // |capabilities|: The printer capabilities.
315 void OnGotExtensionPrinterCapabilities(
316 const std::string& printer_id,
317 const base::DictionaryValue& capabilities);
319 // Called when an extension print job is completed.
320 // |success|: Whether the job succeeded.
321 // |status|: The returned print job status. Useful for reporting a specific
322 // error.
323 void OnExtensionPrintResult(bool success, const std::string& status);
325 // Register/unregister from notifications of changes done to the GAIA
326 // cookie.
327 void RegisterForGaiaCookieChanges();
328 void UnregisterForGaiaCookieChanges();
330 // The underlying dialog object.
331 scoped_refptr<ui::SelectFileDialog> select_file_dialog_;
333 // A count of how many requests received to regenerate preview data.
334 // Initialized to 0 then incremented and emitted to a histogram.
335 int regenerate_preview_request_count_;
337 // A count of how many requests received to show manage printers dialog.
338 int manage_printers_dialog_request_count_;
339 int manage_cloud_printers_dialog_request_count_;
341 // Whether we have already logged a failed print preview.
342 bool reported_failed_preview_;
344 // Whether we have already logged the number of printers this session.
345 bool has_logged_printers_count_;
347 // Holds the path to the print to pdf request. It is empty if no such request
348 // exists.
349 base::FilePath print_to_pdf_path_;
351 // Holds token service to get OAuth2 access tokens.
352 scoped_ptr<AccessTokenService> token_service_;
354 // Pointer to cookie manager service so that print preview can listen for GAIA
355 // cookie changes.
356 GaiaCookieManagerService* gaia_cookie_manager_service_;
358 #if defined(ENABLE_SERVICE_DISCOVERY)
359 scoped_refptr<local_discovery::ServiceDiscoverySharedClient>
360 service_discovery_client_;
361 scoped_ptr<local_discovery::PrivetLocalPrinterLister> printer_lister_;
363 scoped_ptr<local_discovery::PrivetHTTPAsynchronousFactory>
364 privet_http_factory_;
365 scoped_ptr<local_discovery::PrivetHTTPResolution> privet_http_resolution_;
366 scoped_ptr<local_discovery::PrivetV1HTTPClient> privet_http_client_;
367 scoped_ptr<local_discovery::PrivetJSONOperation>
368 privet_capabilities_operation_;
369 scoped_ptr<local_discovery::PrivetLocalPrintOperation>
370 privet_local_print_operation_;
371 #endif
373 // Handles requests for extension printers. Created lazily by calling
374 // |EnsureExtensionPrinterHandlerSet|.
375 scoped_ptr<PrinterHandler> extension_printer_handler_;
377 // Notifies tests that want to know if the PDF has been saved. This doesn't
378 // notify the test if it was a successful save, only that it was attempted.
379 base::Closure pdf_file_saved_closure_;
381 base::WeakPtrFactory<PrintPreviewHandler> weak_factory_;
383 DISALLOW_COPY_AND_ASSIGN(PrintPreviewHandler);
386 #endif // CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_PRINT_PREVIEW_HANDLER_H_