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. 4 5 #ifndef CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_PRINT_PREVIEW_HANDLER_H_ 6 #define CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_PRINT_PREVIEW_HANDLER_H_ 7 8 #include <string> 9 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" 18 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 23 24 class AccountReconcilor; 25 class PrintSystemTaskProxy; 26 27 namespace base { 28 class DictionaryValue; 29 class RefCountedBytes; 30 } 31 32 namespace content { 33 class WebContents; 34 } 35 36 namespace gfx { 37 class Size; 38 } 39 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, 46 #endif 47 public ui::SelectFileDialog::Listener, 48 public printing::PrintViewManagerObserver, 49 public MergeSessionHelper::Observer { 50 public: 51 PrintPreviewHandler(); 52 virtual ~PrintPreviewHandler(); 53 54 // WebUIMessageHandler implementation. 55 virtual void RegisterMessages() OVERRIDE; 56 57 // SelectFileDialog::Listener implementation. 58 virtual void FileSelected(const base::FilePath& path, 59 int index, 60 void* params) OVERRIDE; 61 virtual void FileSelectionCanceled(void* params) OVERRIDE; 62 63 // PrintViewManagerObserver implementation. 64 virtual void OnPrintDialogShown() OVERRIDE; 65 66 // MergeSessionHelper::Observer implementation. 67 virtual void MergeSessionCompleted( 68 const std::string& account_id, 69 const GoogleServiceAuthError& error) OVERRIDE; 70 71 // Displays a modal dialog, prompting the user to select a file. 72 void SelectFile(const base::FilePath& default_path); 73 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 76 // observer. 77 void OnPrintPreviewDialogDestroyed(); 78 79 // Called when print preview failed. 80 void OnPrintPreviewFailed(); 81 82 #if !defined(DISABLE_BASIC_PRINTING) 83 // Called when the user press ctrl+shift+p to display the native system 84 // dialog. 85 void ShowSystemDialog(); 86 #endif // !DISABLE_BASIC_PRINTING 87 88 #if defined(ENABLE_SERVICE_DISCOVERY) 89 // PrivetLocalPrinterLister::Delegate implementation. 90 virtual void LocalPrinterChanged( 91 bool added, 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; 97 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 regenerate_preview_request_count()106 int regenerate_preview_request_count() const { 107 return regenerate_preview_request_count_; 108 } 109 110 // Sets |pdf_file_saved_closure_| to |closure|. 111 void SetPdfSavedClosureForTesting(const base::Closure& closure); 112 113 private: 114 friend class PrintPreviewPdfGeneratedBrowserTest; 115 FRIEND_TEST_ALL_PREFIXES(PrintPreviewPdfGeneratedBrowserTest, 116 MANUAL_DummyTest); 117 class AccessTokenService; 118 119 static bool PrivetPrintingEnabled(); 120 121 content::WebContents* preview_web_contents() const; 122 123 // Gets the list of printers. |args| is unused. 124 void HandleGetPrinters(const base::ListValue* args); 125 126 // Starts getting all local privet printers. |arg| is unused. 127 void HandleGetPrivetPrinters(const base::ListValue* args); 128 129 // Stops getting all local privet printers. |arg| is unused. 130 void HandleStopGetPrivetPrinters(const base::ListValue* args); 131 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); 135 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); 139 140 // Handles the request to hide the preview dialog for printing. 141 // |args| is unused. 142 void HandleHidePreview(const base::ListValue* args); 143 144 // Handles the request to cancel the pending print request. |args| is unused. 145 void HandleCancelPendingPrintRequest(const base::ListValue* args); 146 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); 150 151 // Gets the printer capabilities. First element of |args| is the printer name. 152 void HandleGetPrinterCapabilities(const base::ListValue* args); 153 154 #if !defined(DISABLE_BASIC_PRINTING) 155 // Asks the initiator renderer to show the native print system dialog. |args| 156 // is unused. 157 void HandleShowSystemDialog(const base::ListValue* args); 158 #endif // !DISABLE_BASIC_PRINTING 159 160 // Callback for the signin dialog to call once signin is complete. 161 void OnSigninComplete(); 162 163 // Brings up a dialog to allow the user to sign into cloud print. 164 // |args| is unused. 165 void HandleSignin(const base::ListValue* args); 166 167 // Generates new token and sends back to UI. 168 void HandleGetAccessToken(const base::ListValue* args); 169 170 // Brings up a web page to allow the user to configure cloud print. 171 // |args| is unused. 172 void HandleManageCloudPrint(const base::ListValue* args); 173 174 // Gathers UMA stats when the print preview dialog is about to close. 175 // |args| is unused. 176 void HandleClosePreviewDialog(const base::ListValue* args); 177 178 // Asks the browser to show the native printer management dialog. 179 // |args| is unused. 180 void HandleManagePrinters(const base::ListValue* args); 181 182 // Asks the browser to show the cloud print dialog. |args| is signle int with 183 // page count. 184 void HandlePrintWithCloudPrintDialog(const base::ListValue* args); 185 186 // Asks the browser for several settings that are needed before the first 187 // preview is displayed. 188 void HandleGetInitialSettings(const base::ListValue* args); 189 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); 193 194 // Forces the opening of a new tab. |args| should consist of one element: the 195 // URL to set the new tab to. 196 // 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); 202 203 void HandleGetPrivetPrinterCapabilities(const base::ListValue* arg); 204 205 void SendInitialSettings(const std::string& default_printer); 206 207 // Send OAuth2 access token. 208 void SendAccessToken(const std::string& type, 209 const std::string& access_token); 210 211 // Sends the printer capabilities to the Web UI. |settings_info| contains 212 // printer capabilities information. 213 void SendPrinterCapabilities(const base::DictionaryValue* settings_info); 214 215 // Sends error notification to the Web UI when unable to return the printer 216 // capabilities. 217 void SendFailedToGetPrinterCapabilities(const std::string& printer_name); 218 219 // Send the list of printers to the Web UI. 220 void SetupPrinterList(const base::ListValue* printers); 221 222 // Send whether cloud print integration should be enabled. 223 void SendCloudPrintEnabled(); 224 225 // Send the PDF data to the cloud to print. 226 void SendCloudPrintJob(const base::RefCountedBytes* data); 227 228 // Handles printing to PDF. 229 void PrintToPdf(); 230 231 // Asks the browser to show the cloud print dialog. 232 void PrintWithCloudPrintDialog(); 233 234 // Gets the initiator for the print preview dialog. 235 content::WebContents* GetInitiator() const; 236 237 // Closes the preview dialog. 238 void ClosePreviewDialog(); 239 240 // Adds all the recorded stats taken so far to histogram counts. 241 void ReportStats(); 242 243 // Clears initiator details for the print preview dialog. 244 void ClearInitiatorDetails(); 245 246 // Posts a task to save |data| to pdf at |print_to_pdf_path_|. 247 void PostPrintToPdfTask(); 248 249 // Populates |settings| according to the current locale. 250 void GetNumberFormatAndMeasurementSystem(base::DictionaryValue* settings); 251 252 bool GetPreviewDataAndTitle(scoped_refptr<base::RefCountedBytes>* data, 253 base::string16* title) const; 254 255 #if defined(USE_CUPS) 256 void SaveCUPSColorSetting(const base::DictionaryValue* settings); 257 258 void ConvertColorSettingToCUPSColorModel( 259 base::DictionaryValue* settings) const; 260 #endif 261 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, 271 gfx::Size page_size, 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& 286 callback); 287 void FillPrinterDescription( 288 const std::string& name, 289 const local_discovery::DeviceDescription& description, 290 bool has_local_printing, 291 base::DictionaryValue* printer_value); 292 #endif 293 294 // Register/unregister from notifications of changes done to the GAIA 295 // cookie. 296 void RegisterForMergeSession(); 297 void UnregisterForMergeSession(); 298 299 // The underlying dialog object. 300 scoped_refptr<ui::SelectFileDialog> select_file_dialog_; 301 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_; 305 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_; 309 310 // Whether we have already logged a failed print preview. 311 bool reported_failed_preview_; 312 313 // Whether we have already logged the number of printers this session. 314 bool has_logged_printers_count_; 315 316 // Holds the path to the print to pdf request. It is empty if no such request 317 // exists. 318 base::FilePath print_to_pdf_path_; 319 320 // Holds token service to get OAuth2 access tokens. 321 scoped_ptr<AccessTokenService> token_service_; 322 323 // Pointer to reconcilor so that print preview can listen for GAIA cookie 324 // changes. 325 AccountReconcilor* reconcilor_; 326 327 #if defined(ENABLE_SERVICE_DISCOVERY) 328 scoped_refptr<local_discovery::ServiceDiscoverySharedClient> 329 service_discovery_client_; 330 scoped_ptr<local_discovery::PrivetLocalPrinterLister> printer_lister_; 331 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_; 340 #endif 341 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_; 345 346 base::WeakPtrFactory<PrintPreviewHandler> weak_factory_; 347 348 DISALLOW_COPY_AND_ASSIGN(PrintPreviewHandler); 349 }; 350 351 #endif // CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_PRINT_PREVIEW_HANDLER_H_ 352