Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/print_preview_handler.h" | 5 #include "chrome/browser/ui/webui/print_preview_handler.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/i18n/file_util_icu.h" | 9 #include "base/i18n/file_util_icu.h" |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| 11 #include "base/metrics/histogram.h" | |
| 11 #include "base/path_service.h" | 12 #include "base/path_service.h" |
| 12 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
| 13 #include "base/threading/thread_restrictions.h" | 14 #include "base/threading/thread_restrictions.h" |
| 14 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
| 15 #include "base/values.h" | 16 #include "base/values.h" |
| 16 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
| 17 #include "chrome/browser/platform_util.h" | 18 #include "chrome/browser/platform_util.h" |
| 18 #include "chrome/browser/printing/background_printing_manager.h" | 19 #include "chrome/browser/printing/background_printing_manager.h" |
| 19 #include "chrome/browser/printing/printer_manager_dialog.h" | 20 #include "chrome/browser/printing/printer_manager_dialog.h" |
| 20 #include "chrome/browser/printing/print_preview_tab_controller.h" | 21 #include "chrome/browser/printing/print_preview_tab_controller.h" |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 80 return settings.release(); | 81 return settings.release(); |
| 81 } | 82 } |
| 82 | 83 |
| 83 } // namespace | 84 } // namespace |
| 84 | 85 |
| 85 class PrintSystemTaskProxy | 86 class PrintSystemTaskProxy |
| 86 : public base::RefCountedThreadSafe<PrintSystemTaskProxy, | 87 : public base::RefCountedThreadSafe<PrintSystemTaskProxy, |
| 87 BrowserThread::DeleteOnUIThread> { | 88 BrowserThread::DeleteOnUIThread> { |
| 88 public: | 89 public: |
| 89 PrintSystemTaskProxy(const base::WeakPtr<PrintPreviewHandler>& handler, | 90 PrintSystemTaskProxy(const base::WeakPtr<PrintPreviewHandler>& handler, |
| 90 printing::PrintBackend* print_backend) | 91 printing::PrintBackend* print_backend, |
| 92 bool has_logged_printers_count) | |
| 91 : handler_(handler), | 93 : handler_(handler), |
| 92 print_backend_(print_backend) { | 94 print_backend_(print_backend), |
| 95 has_logged_printers_count_(has_logged_printers_count) { | |
| 93 } | 96 } |
| 94 | 97 |
| 95 void EnumeratePrinters() { | 98 void EnumeratePrinters() { |
| 96 ListValue* printers = new ListValue; | 99 ListValue* printers = new ListValue; |
| 97 int default_printer_index = -1; | 100 int default_printer_index = -1; |
| 98 | 101 |
| 99 printing::PrinterList printer_list; | 102 printing::PrinterList printer_list; |
| 100 print_backend_->EnumeratePrinters(&printer_list); | 103 print_backend_->EnumeratePrinters(&printer_list); |
| 104 | |
| 105 if (!has_logged_printers_count_) { | |
| 106 // Record the total number of printers. | |
| 107 UMA_HISTOGRAM_COUNTS("PrintPreview.NumberOfPrinters", | |
| 108 printer_list.size()); | |
| 109 } | |
| 110 | |
| 101 int i = 0; | 111 int i = 0; |
| 102 for (printing::PrinterList::iterator index = printer_list.begin(); | 112 for (printing::PrinterList::iterator index = printer_list.begin(); |
| 103 index != printer_list.end(); ++index, ++i) { | 113 index != printer_list.end(); ++index, ++i) { |
| 104 DictionaryValue* printer_info = new DictionaryValue; | 114 DictionaryValue* printer_info = new DictionaryValue; |
| 105 std::string printerName; | 115 std::string printerName; |
| 106 #if defined(OS_MACOSX) | 116 #if defined(OS_MACOSX) |
| 107 // On Mac, |index->printer_description| specifies the printer name and | 117 // On Mac, |index->printer_description| specifies the printer name and |
| 108 // |index->printer_name| specifies the device name / printer queue name. | 118 // |index->printer_name| specifies the device name / printer queue name. |
| 109 printerName = index->printer_description; | 119 printerName = index->printer_description; |
| 110 #else | 120 #else |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 194 private: | 204 private: |
| 195 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; | 205 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; |
| 196 friend class DeleteTask<PrintSystemTaskProxy>; | 206 friend class DeleteTask<PrintSystemTaskProxy>; |
| 197 | 207 |
| 198 ~PrintSystemTaskProxy() {} | 208 ~PrintSystemTaskProxy() {} |
| 199 | 209 |
| 200 base::WeakPtr<PrintPreviewHandler> handler_; | 210 base::WeakPtr<PrintPreviewHandler> handler_; |
| 201 | 211 |
| 202 scoped_refptr<printing::PrintBackend> print_backend_; | 212 scoped_refptr<printing::PrintBackend> print_backend_; |
| 203 | 213 |
| 214 bool has_logged_printers_count_; | |
| 215 | |
| 204 DISALLOW_COPY_AND_ASSIGN(PrintSystemTaskProxy); | 216 DISALLOW_COPY_AND_ASSIGN(PrintSystemTaskProxy); |
| 205 }; | 217 }; |
| 206 | 218 |
| 207 // A Task implementation that stores a PDF file on disk. | 219 // A Task implementation that stores a PDF file on disk. |
| 208 class PrintToPdfTask : public Task { | 220 class PrintToPdfTask : public Task { |
| 209 public: | 221 public: |
| 210 // Takes ownership of |metafile|. | 222 // Takes ownership of |metafile|. |
| 211 PrintToPdfTask(printing::Metafile* metafile, const FilePath& path) | 223 PrintToPdfTask(printing::Metafile* metafile, const FilePath& path) |
| 212 : metafile_(metafile), path_(path) { | 224 : metafile_(metafile), path_(path) { |
| 213 DCHECK(metafile); | 225 DCHECK(metafile); |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 229 scoped_ptr<printing::Metafile> metafile_; | 241 scoped_ptr<printing::Metafile> metafile_; |
| 230 | 242 |
| 231 // The absolute path where the file will be saved. | 243 // The absolute path where the file will be saved. |
| 232 FilePath path_; | 244 FilePath path_; |
| 233 }; | 245 }; |
| 234 | 246 |
| 235 // static | 247 // static |
| 236 FilePath* PrintPreviewHandler::last_saved_path_ = NULL; | 248 FilePath* PrintPreviewHandler::last_saved_path_ = NULL; |
| 237 | 249 |
| 238 PrintPreviewHandler::PrintPreviewHandler() | 250 PrintPreviewHandler::PrintPreviewHandler() |
| 239 : print_backend_(printing::PrintBackend::CreateInstance(NULL)) { | 251 : print_backend_(printing::PrintBackend::CreateInstance(NULL)), |
| 252 regenerate_preview_request_count_(0), | |
| 253 manage_printers_dialog_request_count_(0), | |
| 254 print_preview_failed_count_(0), | |
| 255 has_logged_printers_count_(false) { | |
| 240 } | 256 } |
| 241 | 257 |
| 242 PrintPreviewHandler::~PrintPreviewHandler() { | 258 PrintPreviewHandler::~PrintPreviewHandler() { |
| 243 if (select_file_dialog_.get()) | 259 if (select_file_dialog_.get()) |
| 244 select_file_dialog_->ListenerDestroyed(); | 260 select_file_dialog_->ListenerDestroyed(); |
| 245 } | 261 } |
| 246 | 262 |
| 247 void PrintPreviewHandler::RegisterMessages() { | 263 void PrintPreviewHandler::RegisterMessages() { |
| 248 web_ui_->RegisterMessageCallback("getPrinters", | 264 web_ui_->RegisterMessageCallback("getPrinters", |
| 249 NewCallback(this, &PrintPreviewHandler::HandleGetPrinters)); | 265 NewCallback(this, &PrintPreviewHandler::HandleGetPrinters)); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 260 web_ui_->RegisterMessageCallback("closePrintPreviewTab", | 276 web_ui_->RegisterMessageCallback("closePrintPreviewTab", |
| 261 NewCallback(this, &PrintPreviewHandler::HandleClosePreviewTab)); | 277 NewCallback(this, &PrintPreviewHandler::HandleClosePreviewTab)); |
| 262 } | 278 } |
| 263 | 279 |
| 264 TabContents* PrintPreviewHandler::preview_tab() { | 280 TabContents* PrintPreviewHandler::preview_tab() { |
| 265 return web_ui_->tab_contents(); | 281 return web_ui_->tab_contents(); |
| 266 } | 282 } |
| 267 | 283 |
| 268 void PrintPreviewHandler::HandleGetPrinters(const ListValue*) { | 284 void PrintPreviewHandler::HandleGetPrinters(const ListValue*) { |
| 269 scoped_refptr<PrintSystemTaskProxy> task = | 285 scoped_refptr<PrintSystemTaskProxy> task = |
| 270 new PrintSystemTaskProxy(AsWeakPtr(), print_backend_.get()); | 286 new PrintSystemTaskProxy(AsWeakPtr(), |
| 287 print_backend_.get(), | |
| 288 has_logged_printers_count_); | |
| 289 if (!has_logged_printers_count_) | |
|
Lei Zhang
2011/05/17 00:09:38
nit: you don't need the if statement. The branchin
kmadhusu
2011/05/17 00:22:09
Done.
| |
| 290 has_logged_printers_count_ = true; | |
| 291 | |
| 271 BrowserThread::PostTask( | 292 BrowserThread::PostTask( |
| 272 BrowserThread::FILE, FROM_HERE, | 293 BrowserThread::FILE, FROM_HERE, |
| 273 NewRunnableMethod(task.get(), | 294 NewRunnableMethod(task.get(), |
| 274 &PrintSystemTaskProxy::EnumeratePrinters)); | 295 &PrintSystemTaskProxy::EnumeratePrinters)); |
| 275 } | 296 } |
| 276 | 297 |
| 277 void PrintPreviewHandler::HandleGetPreview(const ListValue* args) { | 298 void PrintPreviewHandler::HandleGetPreview(const ListValue* args) { |
| 299 // Increment request count. | |
| 300 ++regenerate_preview_request_count_; | |
| 301 | |
| 278 TabContents* initiator_tab = GetInitiatorTab(); | 302 TabContents* initiator_tab = GetInitiatorTab(); |
| 279 if (!initiator_tab) { | 303 if (!initiator_tab) { |
| 304 ++print_preview_failed_count_; | |
| 280 web_ui_->CallJavascriptFunction("printPreviewFailed"); | 305 web_ui_->CallJavascriptFunction("printPreviewFailed"); |
| 281 return; | 306 return; |
| 282 } | 307 } |
| 283 scoped_ptr<DictionaryValue> settings(GetSettingsDictionary(args)); | 308 scoped_ptr<DictionaryValue> settings(GetSettingsDictionary(args)); |
| 284 if (!settings.get()) | 309 if (!settings.get()) |
| 285 return; | 310 return; |
| 286 | 311 |
| 287 RenderViewHost* rvh = initiator_tab->render_view_host(); | 312 RenderViewHost* rvh = initiator_tab->render_view_host(); |
| 288 rvh->Send(new PrintMsg_PrintPreview(rvh->routing_id(), *settings)); | 313 rvh->Send(new PrintMsg_PrintPreview(rvh->routing_id(), *settings)); |
| 289 } | 314 } |
| 290 | 315 |
| 291 void PrintPreviewHandler::HandlePrint(const ListValue* args) { | 316 void PrintPreviewHandler::HandlePrint(const ListValue* args) { |
| 317 ReportStats(); | |
| 318 UMA_HISTOGRAM_COUNTS("PrintPreview.Print", 1); | |
| 319 | |
| 320 // Record the number of times the user requests to regenerate preview data | |
| 321 // before printing. | |
| 322 UMA_HISTOGRAM_COUNTS( | |
| 323 "PrintPreview.RegeneratePreviewRequest.BeforePrint", | |
| 324 regenerate_preview_request_count_); | |
| 325 | |
| 292 TabContents* initiator_tab = GetInitiatorTab(); | 326 TabContents* initiator_tab = GetInitiatorTab(); |
| 293 if (initiator_tab) { | 327 if (initiator_tab) { |
| 294 RenderViewHost* rvh = initiator_tab->render_view_host(); | 328 RenderViewHost* rvh = initiator_tab->render_view_host(); |
| 295 rvh->Send(new PrintMsg_ResetScriptedPrintCount(rvh->routing_id())); | 329 rvh->Send(new PrintMsg_ResetScriptedPrintCount(rvh->routing_id())); |
| 296 } | 330 } |
| 297 | 331 |
| 298 scoped_ptr<DictionaryValue> settings(GetSettingsDictionary(args)); | 332 scoped_ptr<DictionaryValue> settings(GetSettingsDictionary(args)); |
| 299 if (!settings.get()) | 333 if (!settings.get()) |
| 300 return; | 334 return; |
| 301 | 335 |
| 302 bool print_to_pdf = false; | 336 bool print_to_pdf = false; |
| 303 settings->GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf); | 337 settings->GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf); |
| 304 | 338 |
| 305 TabContentsWrapper* preview_tab_wrapper = | 339 TabContentsWrapper* preview_tab_wrapper = |
| 306 TabContentsWrapper::GetCurrentWrapperForContents(preview_tab()); | 340 TabContentsWrapper::GetCurrentWrapperForContents(preview_tab()); |
| 307 | 341 |
| 308 if (print_to_pdf) { | 342 if (print_to_pdf) { |
| 343 UMA_HISTOGRAM_COUNTS("PrintPreview.PrintToPDF", 1); | |
| 344 | |
| 309 // Pre-populating select file dialog with print job title. | 345 // Pre-populating select file dialog with print job title. |
| 310 string16 print_job_title_utf16 = | 346 string16 print_job_title_utf16 = |
| 311 preview_tab_wrapper->print_view_manager()->RenderSourceName(); | 347 preview_tab_wrapper->print_view_manager()->RenderSourceName(); |
| 312 | 348 |
| 313 #if defined(OS_WIN) | 349 #if defined(OS_WIN) |
| 314 FilePath::StringType print_job_title(print_job_title_utf16); | 350 FilePath::StringType print_job_title(print_job_title_utf16); |
| 315 #elif defined(OS_POSIX) | 351 #elif defined(OS_POSIX) |
| 316 FilePath::StringType print_job_title = UTF16ToUTF8(print_job_title_utf16); | 352 FilePath::StringType print_job_title = UTF16ToUTF8(print_job_title_utf16); |
| 317 #endif | 353 #endif |
| 318 | 354 |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 335 } | 371 } |
| 336 | 372 |
| 337 void PrintPreviewHandler::HandleGetPrinterCapabilities( | 373 void PrintPreviewHandler::HandleGetPrinterCapabilities( |
| 338 const ListValue* args) { | 374 const ListValue* args) { |
| 339 std::string printer_name; | 375 std::string printer_name; |
| 340 bool ret = args->GetString(0, &printer_name); | 376 bool ret = args->GetString(0, &printer_name); |
| 341 if (!ret || printer_name.empty()) | 377 if (!ret || printer_name.empty()) |
| 342 return; | 378 return; |
| 343 | 379 |
| 344 scoped_refptr<PrintSystemTaskProxy> task = | 380 scoped_refptr<PrintSystemTaskProxy> task = |
| 345 new PrintSystemTaskProxy(AsWeakPtr(), print_backend_.get()); | 381 new PrintSystemTaskProxy(AsWeakPtr(), |
| 382 print_backend_.get(), | |
| 383 has_logged_printers_count_); | |
| 346 | 384 |
| 347 BrowserThread::PostTask( | 385 BrowserThread::PostTask( |
| 348 BrowserThread::FILE, FROM_HERE, | 386 BrowserThread::FILE, FROM_HERE, |
| 349 NewRunnableMethod(task.get(), | 387 NewRunnableMethod(task.get(), |
| 350 &PrintSystemTaskProxy::GetPrinterCapabilities, | 388 &PrintSystemTaskProxy::GetPrinterCapabilities, |
| 351 printer_name)); | 389 printer_name)); |
| 352 } | 390 } |
| 353 | 391 |
| 354 void PrintPreviewHandler::HandleShowSystemDialog(const ListValue* args) { | 392 void PrintPreviewHandler::HandleShowSystemDialog(const ListValue* args) { |
| 393 ReportStats(); | |
| 394 UMA_HISTOGRAM_COUNTS("PrintPreview.ShowAdvanceSettings", 1); | |
| 355 TabContents* initiator_tab = GetInitiatorTab(); | 395 TabContents* initiator_tab = GetInitiatorTab(); |
| 356 if (!initiator_tab) | 396 if (!initiator_tab) |
| 357 return; | 397 return; |
| 358 initiator_tab->Activate(); | 398 initiator_tab->Activate(); |
| 359 | 399 |
| 360 TabContentsWrapper* wrapper = | 400 TabContentsWrapper* wrapper = |
| 361 TabContentsWrapper::GetCurrentWrapperForContents(initiator_tab); | 401 TabContentsWrapper::GetCurrentWrapperForContents(initiator_tab); |
| 362 wrapper->print_view_manager()->PrintNow(); | 402 wrapper->print_view_manager()->PrintNow(); |
| 363 | 403 |
| 364 ClosePrintPreviewTab(); | 404 ClosePrintPreviewTab(); |
| 365 } | 405 } |
| 366 | 406 |
| 367 void PrintPreviewHandler::HandleManagePrinters(const ListValue* args) { | 407 void PrintPreviewHandler::HandleManagePrinters(const ListValue* args) { |
| 408 ++manage_printers_dialog_request_count_; | |
| 368 printing::PrinterManagerDialog::ShowPrinterManagerDialog(); | 409 printing::PrinterManagerDialog::ShowPrinterManagerDialog(); |
| 369 } | 410 } |
| 370 | 411 |
| 371 void PrintPreviewHandler::HandleClosePreviewTab(const ListValue* args) { | 412 void PrintPreviewHandler::HandleClosePreviewTab(const ListValue* args) { |
| 413 ReportStats(); | |
| 414 UMA_HISTOGRAM_COUNTS("PrintPreview.Cancel", 1); | |
| 415 | |
| 416 // Record the number of times the user requests to regenerate preview data | |
| 417 // before cancelling. | |
| 418 UMA_HISTOGRAM_COUNTS( | |
| 419 "PrintPreview.RegeneratePreviewRequest.BeforeCancel", | |
| 420 regenerate_preview_request_count_); | |
| 421 | |
| 372 ActivateInitiatorTabAndClosePreviewTab(); | 422 ActivateInitiatorTabAndClosePreviewTab(); |
| 373 } | 423 } |
| 374 | 424 |
| 425 void PrintPreviewHandler::ReportStats() { | |
| 426 if (print_preview_failed_count_ > 0) { | |
| 427 UMA_HISTOGRAM_COUNTS("PrintPreview.Failed.InitiatorTabDoesNotExist", | |
| 428 print_preview_failed_count_); | |
| 429 } | |
| 430 | |
| 431 UMA_HISTOGRAM_COUNTS("PrintPreview.ManagePrinters", | |
| 432 manage_printers_dialog_request_count_); | |
| 433 } | |
| 434 | |
| 375 void PrintPreviewHandler::ActivateInitiatorTabAndClosePreviewTab() { | 435 void PrintPreviewHandler::ActivateInitiatorTabAndClosePreviewTab() { |
| 376 TabContents* initiator_tab = GetInitiatorTab(); | 436 TabContents* initiator_tab = GetInitiatorTab(); |
| 377 if (initiator_tab) | 437 if (initiator_tab) |
| 378 initiator_tab->Activate(); | 438 initiator_tab->Activate(); |
| 379 ClosePrintPreviewTab(); | 439 ClosePrintPreviewTab(); |
| 380 } | 440 } |
| 381 | 441 |
| 382 void PrintPreviewHandler::SendPrinterCapabilities( | 442 void PrintPreviewHandler::SendPrinterCapabilities( |
| 383 const DictionaryValue& settings_info) { | 443 const DictionaryValue& settings_info) { |
| 384 web_ui_->CallJavascriptFunction("updateWithPrinterCapabilities", | 444 web_ui_->CallJavascriptFunction("updateWithPrinterCapabilities", |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 453 metafile->InitFromData(data.first->memory(), data.second); | 513 metafile->InitFromData(data.first->memory(), data.second); |
| 454 | 514 |
| 455 // Updating last_saved_path_ to the newly selected folder. | 515 // Updating last_saved_path_ to the newly selected folder. |
| 456 *last_saved_path_ = path.DirName(); | 516 *last_saved_path_ = path.DirName(); |
| 457 | 517 |
| 458 PrintToPdfTask* task = new PrintToPdfTask(metafile, path); | 518 PrintToPdfTask* task = new PrintToPdfTask(metafile, path); |
| 459 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, task); | 519 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, task); |
| 460 | 520 |
| 461 ActivateInitiatorTabAndClosePreviewTab(); | 521 ActivateInitiatorTabAndClosePreviewTab(); |
| 462 } | 522 } |
| OLD | NEW |