Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/print_preview_handler.h" | 5 #include "chrome/browser/ui/webui/print_preview/print_preview_handler.h" |
| 6 | 6 |
| 7 #include <ctype.h> | 7 #include <ctype.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 42 #include "chrome/browser/printing/printer_manager_dialog.h" | 42 #include "chrome/browser/printing/printer_manager_dialog.h" |
| 43 #include "chrome/browser/profiles/profile.h" | 43 #include "chrome/browser/profiles/profile.h" |
| 44 #include "chrome/browser/signin/gaia_cookie_manager_service_factory.h" | 44 #include "chrome/browser/signin/gaia_cookie_manager_service_factory.h" |
| 45 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 45 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 46 #include "chrome/browser/signin/signin_manager_factory.h" | 46 #include "chrome/browser/signin/signin_manager_factory.h" |
| 47 #include "chrome/browser/ui/browser_finder.h" | 47 #include "chrome/browser/ui/browser_finder.h" |
| 48 #include "chrome/browser/ui/browser_tabstrip.h" | 48 #include "chrome/browser/ui/browser_tabstrip.h" |
| 49 #include "chrome/browser/ui/chrome_select_file_policy.h" | 49 #include "chrome/browser/ui/chrome_select_file_policy.h" |
| 50 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" | 50 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" |
| 51 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" | 51 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" |
| 52 #include "chrome/browser/ui/webui/print_preview/printer_backend_proxy.h" | |
| 52 #include "chrome/browser/ui/webui/print_preview/printer_handler.h" | 53 #include "chrome/browser/ui/webui/print_preview/printer_handler.h" |
| 53 #include "chrome/browser/ui/webui/print_preview/sticky_settings.h" | 54 #include "chrome/browser/ui/webui/print_preview/sticky_settings.h" |
| 54 #include "chrome/common/chrome_switches.h" | 55 #include "chrome/common/chrome_switches.h" |
| 55 #include "chrome/common/cloud_print/cloud_print_cdd_conversion.h" | 56 #include "chrome/common/cloud_print/cloud_print_cdd_conversion.h" |
| 56 #include "chrome/common/cloud_print/cloud_print_constants.h" | 57 #include "chrome/common/cloud_print/cloud_print_constants.h" |
| 57 #include "chrome/common/crash_keys.h" | 58 #include "chrome/common/crash_keys.h" |
| 58 #include "chrome/common/features.h" | 59 #include "chrome/common/features.h" |
| 59 #include "chrome/common/pref_names.h" | 60 #include "chrome/common/pref_names.h" |
| 60 #include "components/cloud_devices/common/cloud_device_description.h" | 61 #include "components/cloud_devices/common/cloud_device_description.h" |
| 61 #include "components/cloud_devices/common/cloud_devices_urls.h" | 62 #include "components/cloud_devices/common/cloud_devices_urls.h" |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 287 const base::Closure& pdf_file_saved_closure) { | 288 const base::Closure& pdf_file_saved_closure) { |
| 288 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 289 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| 289 base::File file(path, | 290 base::File file(path, |
| 290 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); | 291 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); |
| 291 file.WriteAtCurrentPos(reinterpret_cast<const char*>(data->front()), | 292 file.WriteAtCurrentPos(reinterpret_cast<const char*>(data->front()), |
| 292 base::checked_cast<int>(data->size())); | 293 base::checked_cast<int>(data->size())); |
| 293 if (!pdf_file_saved_closure.is_null()) | 294 if (!pdf_file_saved_closure.is_null()) |
| 294 pdf_file_saved_closure.Run(); | 295 pdf_file_saved_closure.Run(); |
| 295 } | 296 } |
| 296 | 297 |
| 297 std::string GetDefaultPrinterOnBlockingPoolThread() { | |
| 298 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
| 299 | |
| 300 scoped_refptr<printing::PrintBackend> print_backend( | |
| 301 printing::PrintBackend::CreateInstance(nullptr)); | |
| 302 | |
| 303 std::string default_printer = print_backend->GetDefaultPrinterName(); | |
| 304 VLOG(1) << "Default Printer: " << default_printer; | |
| 305 return default_printer; | |
| 306 } | |
| 307 | |
| 308 class PrintingContextDelegate : public printing::PrintingContext::Delegate { | 298 class PrintingContextDelegate : public printing::PrintingContext::Delegate { |
| 309 public: | 299 public: |
| 310 // PrintingContext::Delegate methods. | 300 // PrintingContext::Delegate methods. |
| 311 gfx::NativeView GetParentView() override { return NULL; } | 301 gfx::NativeView GetParentView() override { return NULL; } |
| 312 std::string GetAppLocale() override { | 302 std::string GetAppLocale() override { |
| 313 return g_browser_process->GetApplicationLocale(); | 303 return g_browser_process->GetApplicationLocale(); |
| 314 } | 304 } |
| 315 }; | 305 }; |
| 316 | 306 |
| 317 gfx::Size GetDefaultPdfMediaSizeMicrons() { | 307 gfx::Size GetDefaultPdfMediaSizeMicrons() { |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 386 const printing::PrinterBasicInfo& printer) { | 376 const printing::PrinterBasicInfo& printer) { |
| 387 #if defined(OS_MACOSX) | 377 #if defined(OS_MACOSX) |
| 388 // On Mac, |printer.printer_description| specifies the printer name and | 378 // On Mac, |printer.printer_description| specifies the printer name and |
| 389 // |printer.printer_name| specifies the device name / printer queue name. | 379 // |printer.printer_name| specifies the device name / printer queue name. |
| 390 const std::string& real_name = printer.printer_description; | 380 const std::string& real_name = printer.printer_description; |
| 391 std::string real_description; | 381 std::string real_description; |
| 392 const auto it = printer.options.find(kDriverNameTagName); | 382 const auto it = printer.options.find(kDriverNameTagName); |
| 393 if (it != printer.options.end()) | 383 if (it != printer.options.end()) |
| 394 real_description = it->second; | 384 real_description = it->second; |
| 395 return std::make_pair(real_name, real_description); | 385 return std::make_pair(real_name, real_description); |
| 386 #elif defined(OS_CHROMEOS) | |
| 387 // For Chrome OS |printer.printer_display_name| is the human readable name and | |
|
Lei Zhang
2016/11/03 07:39:50
So is ChromeOS more in line with Mac then? Maybe d
skau
2016/11/03 21:58:27
Per discussion, I'm going to try to align all the
Lei Zhang
2016/11/03 22:06:12
Agreed, I think that's a safer bet.
| |
| 388 // |printer.printer_name| is the name used in CUPS. | |
| 389 return std::make_pair(printer.printer_display_name, | |
| 390 printer.printer_description); | |
| 396 #else | 391 #else |
| 397 return std::make_pair(printer.printer_name, printer.printer_description); | 392 return std::make_pair(printer.printer_name, printer.printer_description); |
| 398 #endif | 393 #endif |
| 399 } | 394 } |
| 400 | 395 |
| 401 void EnumeratePrintersOnBlockingPoolThread(base::ListValue* printers) { | 396 void PrintersToValues(const printing::PrinterList& printer_list, |
| 402 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 397 base::ListValue* printers) { |
| 403 | |
| 404 scoped_refptr<printing::PrintBackend> print_backend( | |
| 405 printing::PrintBackend::CreateInstance(nullptr)); | |
| 406 | |
| 407 VLOG(1) << "Enumerate printers start"; | |
| 408 printing::PrinterList printer_list; | |
| 409 print_backend->EnumeratePrinters(&printer_list); | |
| 410 | |
| 411 for (const printing::PrinterBasicInfo& printer : printer_list) { | 398 for (const printing::PrinterBasicInfo& printer : printer_list) { |
| 412 std::unique_ptr<base::DictionaryValue> printer_info( | 399 std::unique_ptr<base::DictionaryValue> printer_info( |
| 413 new base::DictionaryValue); | 400 new base::DictionaryValue); |
| 414 const auto printer_name_description = GetPrinterNameAndDescription(printer); | 401 const auto printer_name_description = GetPrinterNameAndDescription(printer); |
| 415 const std::string& printer_name = printer_name_description.first; | 402 const std::string& printer_name = printer_name_description.first; |
| 416 const std::string& printer_description = printer_name_description.second; | 403 const std::string& printer_description = printer_name_description.second; |
| 417 printer_info->SetString(printing::kSettingDeviceName, printer.printer_name); | 404 printer_info->SetString(printing::kSettingDeviceName, printer.printer_name); |
| 418 printer_info->SetString(printing::kSettingPrinterName, printer_name); | 405 printer_info->SetString(printing::kSettingPrinterName, printer_name); |
| 419 printer_info->SetString(printing::kSettingPrinterDescription, | 406 printer_info->SetString(printing::kSettingPrinterDescription, |
| 420 printer_description); | 407 printer_description); |
| 421 | 408 |
| 422 base::DictionaryValue* options = new base::DictionaryValue; | 409 base::DictionaryValue* options = new base::DictionaryValue; |
| 423 printer_info->Set(printing::kSettingPrinterOptions, options); | 410 printer_info->Set(printing::kSettingPrinterOptions, options); |
| 424 for (const auto opt_it : printer.options) | 411 for (const auto opt_it : printer.options) |
| 425 options->SetString(opt_it.first, opt_it.second); | 412 options->SetString(opt_it.first, opt_it.second); |
| 426 | 413 |
| 427 printers->Append(std::move(printer_info)); | 414 printers->Append(std::move(printer_info)); |
| 428 | 415 |
| 429 VLOG(1) << "Found printer " << printer_name << " with device name " | 416 VLOG(1) << "Found printer " << printer_name << " with device name " |
| 430 << printer.printer_name; | 417 << printer.printer_name; |
| 431 } | 418 } |
| 419 } | |
| 420 | |
| 421 void EnumeratePrinters(Profile* profile, base::ListValue* printers) { | |
| 422 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
| 423 | |
| 424 VLOG(1) << "Enumerate printers start"; | |
| 425 printing::PrinterList printer_list = | |
| 426 printing::EnumeratePrintersOnBlockingPoolThread(profile); | |
| 427 PrintersToValues(printer_list, printers); | |
| 432 VLOG(1) << "Enumerate printers finished, found " << printers->GetSize() | 428 VLOG(1) << "Enumerate printers finished, found " << printers->GetSize() |
| 433 << " printers"; | 429 << " printers"; |
| 434 } | 430 } |
| 435 | 431 |
| 436 std::unique_ptr<base::DictionaryValue> | 432 std::unique_ptr<base::DictionaryValue> |
| 437 GetPrinterCapabilitiesOnBlockingPoolThread(const std::string& device_name) { | 433 GetPrinterCapabilitiesOnBlockingPoolThread(const std::string& device_name) { |
| 438 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 434 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| 439 DCHECK(!device_name.empty()); | 435 DCHECK(!device_name.empty()); |
| 440 | 436 |
| 441 scoped_refptr<printing::PrintBackend> print_backend( | 437 scoped_refptr<printing::PrintBackend> print_backend( |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 681 return web_ui()->GetWebContents(); | 677 return web_ui()->GetWebContents(); |
| 682 } | 678 } |
| 683 | 679 |
| 684 PrintPreviewUI* PrintPreviewHandler::print_preview_ui() const { | 680 PrintPreviewUI* PrintPreviewHandler::print_preview_ui() const { |
| 685 return static_cast<PrintPreviewUI*>(web_ui()->GetController()); | 681 return static_cast<PrintPreviewUI*>(web_ui()->GetController()); |
| 686 } | 682 } |
| 687 | 683 |
| 688 void PrintPreviewHandler::HandleGetPrinters(const base::ListValue* /*args*/) { | 684 void PrintPreviewHandler::HandleGetPrinters(const base::ListValue* /*args*/) { |
| 689 base::ListValue* results = new base::ListValue; | 685 base::ListValue* results = new base::ListValue; |
| 690 BrowserThread::PostBlockingPoolTaskAndReply( | 686 BrowserThread::PostBlockingPoolTaskAndReply( |
| 691 FROM_HERE, base::Bind(&EnumeratePrintersOnBlockingPoolThread, | 687 FROM_HERE, base::Bind(&EnumeratePrinters, Profile::FromWebUI(web_ui()), |
| 692 base::Unretained(results)), | 688 base::Unretained(results)), |
| 693 base::Bind(&PrintPreviewHandler::SetupPrinterList, | 689 base::Bind(&PrintPreviewHandler::SetupPrinterList, |
| 694 weak_factory_.GetWeakPtr(), base::Owned(results))); | 690 weak_factory_.GetWeakPtr(), base::Owned(results))); |
| 695 } | 691 } |
| 696 | 692 |
| 697 void PrintPreviewHandler::HandleGetPrivetPrinters(const base::ListValue* args) { | 693 void PrintPreviewHandler::HandleGetPrivetPrinters(const base::ListValue* args) { |
| 698 if (!PrivetPrintingEnabled()) | 694 if (!PrivetPrintingEnabled()) |
| 699 return web_ui()->CallJavascriptFunctionUnsafe("onPrivetPrinterSearchDone"); | 695 return web_ui()->CallJavascriptFunctionUnsafe("onPrivetPrinterSearchDone"); |
| 700 #if BUILDFLAG(ENABLE_SERVICE_DISCOVERY) | 696 #if BUILDFLAG(ENABLE_SERVICE_DISCOVERY) |
| 701 using local_discovery::ServiceDiscoverySharedClient; | 697 using local_discovery::ServiceDiscoverySharedClient; |
| (...skipping 515 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1217 settings->SetString(kNumberFormat, base::FormatDouble(123456.78, 2)); | 1213 settings->SetString(kNumberFormat, base::FormatDouble(123456.78, 2)); |
| 1218 settings->SetInteger(kMeasurementSystem, system); | 1214 settings->SetInteger(kMeasurementSystem, system); |
| 1219 } | 1215 } |
| 1220 | 1216 |
| 1221 void PrintPreviewHandler::HandleGetInitialSettings( | 1217 void PrintPreviewHandler::HandleGetInitialSettings( |
| 1222 const base::ListValue* /*args*/) { | 1218 const base::ListValue* /*args*/) { |
| 1223 // Send before SendInitialSettings() to allow cloud printer auto select. | 1219 // Send before SendInitialSettings() to allow cloud printer auto select. |
| 1224 SendCloudPrintEnabled(); | 1220 SendCloudPrintEnabled(); |
| 1225 base::PostTaskAndReplyWithResult( | 1221 base::PostTaskAndReplyWithResult( |
| 1226 BrowserThread::GetBlockingPool(), FROM_HERE, | 1222 BrowserThread::GetBlockingPool(), FROM_HERE, |
| 1227 base::Bind(&GetDefaultPrinterOnBlockingPoolThread), | 1223 base::Bind(&printing::GetDefaultPrinterOnBlockingPoolThread), |
| 1228 base::Bind(&PrintPreviewHandler::SendInitialSettings, | 1224 base::Bind(&PrintPreviewHandler::SendInitialSettings, |
| 1229 weak_factory_.GetWeakPtr())); | 1225 weak_factory_.GetWeakPtr())); |
| 1230 } | 1226 } |
| 1231 | 1227 |
| 1232 void PrintPreviewHandler::HandleForceOpenNewTab(const base::ListValue* args) { | 1228 void PrintPreviewHandler::HandleForceOpenNewTab(const base::ListValue* args) { |
| 1233 std::string url; | 1229 std::string url; |
| 1234 if (!args->GetString(0, &url)) | 1230 if (!args->GetString(0, &url)) |
| 1235 return; | 1231 return; |
| 1236 Browser* browser = chrome::FindBrowserWithWebContents(GetInitiator()); | 1232 Browser* browser = chrome::FindBrowserWithWebContents(GetInitiator()); |
| 1237 if (!browser) | 1233 if (!browser) |
| (...skipping 527 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1765 | 1761 |
| 1766 void PrintPreviewHandler::UnregisterForGaiaCookieChanges() { | 1762 void PrintPreviewHandler::UnregisterForGaiaCookieChanges() { |
| 1767 if (gaia_cookie_manager_service_) | 1763 if (gaia_cookie_manager_service_) |
| 1768 gaia_cookie_manager_service_->RemoveObserver(this); | 1764 gaia_cookie_manager_service_->RemoveObserver(this); |
| 1769 } | 1765 } |
| 1770 | 1766 |
| 1771 void PrintPreviewHandler::SetPdfSavedClosureForTesting( | 1767 void PrintPreviewHandler::SetPdfSavedClosureForTesting( |
| 1772 const base::Closure& closure) { | 1768 const base::Closure& closure) { |
| 1773 pdf_file_saved_closure_ = closure; | 1769 pdf_file_saved_closure_ = closure; |
| 1774 } | 1770 } |
| OLD | NEW |