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