| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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_HANDLER_H_ | |
| 6 #define CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_HANDLER_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/gtest_prod_util.h" | |
| 12 #include "base/memory/ref_counted.h" | |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/memory/weak_ptr.h" | |
| 15 #include "build/build_config.h" | |
| 16 #include "chrome/browser/printing/print_view_manager_observer.h" | |
| 17 #include "chrome/browser/ui/select_file_dialog.h" | |
| 18 #include "content/public/browser/web_ui_message_handler.h" | |
| 19 #include "printing/print_job_constants.h" | |
| 20 | |
| 21 class FilePath; | |
| 22 class PrintSystemTaskProxy; | |
| 23 class TabContentsWrapper; | |
| 24 | |
| 25 namespace base { | |
| 26 class DictionaryValue; | |
| 27 class StringValue; | |
| 28 } | |
| 29 | |
| 30 namespace printing { | |
| 31 struct PageSizeMargins; | |
| 32 class PrintBackend; | |
| 33 } | |
| 34 | |
| 35 // The handler for Javascript messages related to the print preview dialog. | |
| 36 class PrintPreviewHandler : public content::WebUIMessageHandler, | |
| 37 public base::SupportsWeakPtr<PrintPreviewHandler>, | |
| 38 public SelectFileDialog::Listener, | |
| 39 public printing::PrintViewManagerObserver { | |
| 40 public: | |
| 41 PrintPreviewHandler(); | |
| 42 virtual ~PrintPreviewHandler(); | |
| 43 | |
| 44 // WebUIMessageHandler implementation. | |
| 45 virtual void RegisterMessages() OVERRIDE; | |
| 46 | |
| 47 // SelectFileDialog::Listener implementation. | |
| 48 virtual void FileSelected(const FilePath& path, | |
| 49 int index, | |
| 50 void* params) OVERRIDE; | |
| 51 virtual void FileSelectionCanceled(void* params) OVERRIDE; | |
| 52 | |
| 53 // PrintViewManagerObserver implementation. | |
| 54 virtual void OnPrintDialogShown() OVERRIDE; | |
| 55 | |
| 56 // Displays a modal dialog, prompting the user to select a file. | |
| 57 void SelectFile(const FilePath& default_path); | |
| 58 | |
| 59 // Called when the print preview tab is destroyed. This is the last time | |
| 60 // this object has access to the PrintViewManager in order to disconnect the | |
| 61 // observer. | |
| 62 void OnTabDestroyed(); | |
| 63 | |
| 64 // Called when print preview failed. | |
| 65 void OnPrintPreviewFailed(); | |
| 66 | |
| 67 // Called when the user press ctrl+shift+p to display the native system | |
| 68 // dialog. | |
| 69 void ShowSystemDialog(); | |
| 70 | |
| 71 private: | |
| 72 friend class PrintPreviewHandlerTest; | |
| 73 friend class PrintSystemTaskProxy; | |
| 74 FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest, StickyMarginsCustom); | |
| 75 FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest, StickyMarginsDefault); | |
| 76 FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest, | |
| 77 StickyMarginsCustomThenDefault); | |
| 78 FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest, | |
| 79 GetLastUsedMarginSettingsCustom); | |
| 80 FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest, | |
| 81 GetLastUsedMarginSettingsDefault); | |
| 82 | |
| 83 TabContentsWrapper* preview_tab_wrapper() const; | |
| 84 content::WebContents* preview_tab() const; | |
| 85 | |
| 86 // Gets the list of printers. |args| is unused. | |
| 87 void HandleGetPrinters(const base::ListValue* args); | |
| 88 | |
| 89 // Asks the initiator renderer to generate a preview. First element of |args| | |
| 90 // is a job settings JSON string. | |
| 91 void HandleGetPreview(const base::ListValue* args); | |
| 92 | |
| 93 // Gets the job settings from Web UI and initiate printing. First element of | |
| 94 // |args| is a job settings JSON string. | |
| 95 void HandlePrint(const base::ListValue* args); | |
| 96 | |
| 97 // Handles printing to PDF. |settings| points to a dictionary containing all | |
| 98 // the print request parameters. | |
| 99 void HandlePrintToPdf(const base::DictionaryValue& settings); | |
| 100 | |
| 101 // Handles the request to hide the preview tab for printing. |args| is unused. | |
| 102 void HandleHidePreview(const base::ListValue* args); | |
| 103 | |
| 104 // Handles the request to cancel the pending print request. |args| is unused. | |
| 105 void HandleCancelPendingPrintRequest(const base::ListValue* args); | |
| 106 | |
| 107 // Handles a request to back up data about the last used cloud print | |
| 108 // printer. | |
| 109 // First element of |args| is the printer name. | |
| 110 // Second element of |args| is the current cloud print data JSON. | |
| 111 void HandleSaveLastPrinter(const base::ListValue* args); | |
| 112 | |
| 113 // Gets the printer capabilities. First element of |args| is the printer name. | |
| 114 void HandleGetPrinterCapabilities(const base::ListValue* args); | |
| 115 | |
| 116 // Asks the initiator renderer to show the native print system dialog. |args| | |
| 117 // is unused. | |
| 118 void HandleShowSystemDialog(const base::ListValue* args); | |
| 119 | |
| 120 // Brings up a dialog to allow the user to sign into cloud print. | |
| 121 // |args| is unused. | |
| 122 void HandleSignin(const base::ListValue* args); | |
| 123 | |
| 124 // Brings up a web page to allow the user to configure cloud print. | |
| 125 // |args| is unused. | |
| 126 void HandleManageCloudPrint(const base::ListValue* args); | |
| 127 | |
| 128 // Gathers UMA stats when the print preview tab is about to close. | |
| 129 // |args| is unused. | |
| 130 void HandleClosePreviewTab(const base::ListValue* args); | |
| 131 | |
| 132 // Asks the browser to show the native printer management dialog. | |
| 133 // |args| is unused. | |
| 134 void HandleManagePrinters(const base::ListValue* args); | |
| 135 | |
| 136 // Asks the browser to show the cloud print dialog. | |
| 137 void HandlePrintWithCloudPrint(); | |
| 138 | |
| 139 // Asks the browser for several settings that are needed before the first | |
| 140 // preview is displayed. | |
| 141 void HandleGetInitialSettings(const base::ListValue* args); | |
| 142 | |
| 143 void SendInitialSettings( | |
| 144 const std::string& default_printer, | |
| 145 const std::string& cloud_print_data); | |
| 146 | |
| 147 // Sends the printer capabilities to the Web UI. |settings_info| contains | |
| 148 // printer capabilities information. | |
| 149 void SendPrinterCapabilities(const base::DictionaryValue& settings_info); | |
| 150 | |
| 151 // Send the list of printers to the Web UI. | |
| 152 void SetupPrinterList(const base::ListValue& printers); | |
| 153 | |
| 154 // Send whether cloud print integration should be enabled. | |
| 155 void SendCloudPrintEnabled(); | |
| 156 | |
| 157 // Send the PDF data to the cloud to print. | |
| 158 void SendCloudPrintJob(const base::DictionaryValue& settings, | |
| 159 std::string print_ticket); | |
| 160 | |
| 161 // Gets the initiator tab for the print preview tab. | |
| 162 TabContentsWrapper* GetInitiatorTab() const; | |
| 163 | |
| 164 // Activates the initiator tab and close the preview tab. | |
| 165 void ActivateInitiatorTabAndClosePreviewTab(); | |
| 166 | |
| 167 // Adds all the recorded stats taken so far to histogram counts. | |
| 168 void ReportStats(); | |
| 169 | |
| 170 // Clears initiator tab details for this preview tab. | |
| 171 void ClearInitiatorTabDetails(); | |
| 172 | |
| 173 // Posts a task to save to pdf at |print_to_pdf_path_|. | |
| 174 void PostPrintToPdfTask(); | |
| 175 | |
| 176 // Populates |settings| according to the current locale. | |
| 177 void GetNumberFormatAndMeasurementSystem(base::DictionaryValue* settings); | |
| 178 | |
| 179 // Populates |last_used_custom_margins| according to the last used margin | |
| 180 // settings. | |
| 181 void GetLastUsedMarginSettings( | |
| 182 base::DictionaryValue* last_used_custom_margins); | |
| 183 | |
| 184 // Pointer to current print system. | |
| 185 scoped_refptr<printing::PrintBackend> print_backend_; | |
| 186 | |
| 187 // The underlying dialog object. | |
| 188 scoped_refptr<SelectFileDialog> select_file_dialog_; | |
| 189 | |
| 190 static FilePath* last_saved_path_; | |
| 191 static std::string* last_used_printer_cloud_print_data_; | |
| 192 static std::string* last_used_printer_name_; | |
| 193 static printing::ColorModels last_used_color_model_; | |
| 194 static printing::MarginType last_used_margins_type_; | |
| 195 static printing::PageSizeMargins* last_used_page_size_margins_; | |
| 196 | |
| 197 // A count of how many requests received to regenerate preview data. | |
| 198 // Initialized to 0 then incremented and emitted to a histogram. | |
| 199 int regenerate_preview_request_count_; | |
| 200 | |
| 201 // A count of how many requests received to show manage printers dialog. | |
| 202 int manage_printers_dialog_request_count_; | |
| 203 | |
| 204 // Whether we have already logged a failed print preview. | |
| 205 bool reported_failed_preview_; | |
| 206 | |
| 207 // Whether we have already logged the number of printers this session. | |
| 208 bool has_logged_printers_count_; | |
| 209 | |
| 210 // Holds the path to the print to pdf request. It is empty if no such request | |
| 211 // exists. | |
| 212 scoped_ptr<FilePath> print_to_pdf_path_; | |
| 213 | |
| 214 DISALLOW_COPY_AND_ASSIGN(PrintPreviewHandler); | |
| 215 }; | |
| 216 | |
| 217 #endif // CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_HANDLER_H_ | |
| OLD | NEW |