Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(516)

Side by Side Diff: chrome/browser/ui/webui/print_preview/print_preview_handler.cc

Issue 2463473002: Present the printer list from preferences for Chrome OS. (Closed)
Patch Set: present display name as mac does Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
377 media.AddDefaultOption(media_option, 367 media.AddDefaultOption(media_option,
378 default_media.type == media_option.type); 368 default_media.type == media_option.type);
379 } 369 }
380 media.SaveTo(&description); 370 media.SaveTo(&description);
381 371
382 return std::unique_ptr<base::DictionaryValue>(description.root().DeepCopy()); 372 return std::unique_ptr<base::DictionaryValue>(description.root().DeepCopy());
383 } 373 }
384 374
385 std::pair<std::string, std::string> GetPrinterNameAndDescription( 375 std::pair<std::string, std::string> GetPrinterNameAndDescription(
386 const printing::PrinterBasicInfo& printer) { 376 const printing::PrinterBasicInfo& printer) {
387 #if defined(OS_MACOSX) 377 #if defined(OS_MACOSX) || defined(OS_CHROMEOS)
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.
Lei Zhang 2016/11/03 22:12:29 Add a comment to say Chrome OS emulates the Mac be
skau 2016/11/03 22:30:08 Done.
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);
396 #else 386 #else
397 return std::make_pair(printer.printer_name, printer.printer_description); 387 return std::make_pair(printer.printer_name, printer.printer_description);
398 #endif 388 #endif
399 } 389 }
400 390
401 void EnumeratePrintersOnBlockingPoolThread(base::ListValue* printers) { 391 void PrintersToValues(const printing::PrinterList& printer_list,
402 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); 392 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) { 393 for (const printing::PrinterBasicInfo& printer : printer_list) {
412 std::unique_ptr<base::DictionaryValue> printer_info( 394 std::unique_ptr<base::DictionaryValue> printer_info(
413 new base::DictionaryValue); 395 new base::DictionaryValue);
414 const auto printer_name_description = GetPrinterNameAndDescription(printer); 396 const auto printer_name_description = GetPrinterNameAndDescription(printer);
415 const std::string& printer_name = printer_name_description.first; 397 const std::string& printer_name = printer_name_description.first;
416 const std::string& printer_description = printer_name_description.second; 398 const std::string& printer_description = printer_name_description.second;
417 printer_info->SetString(printing::kSettingDeviceName, printer.printer_name); 399 printer_info->SetString(printing::kSettingDeviceName, printer.printer_name);
418 printer_info->SetString(printing::kSettingPrinterName, printer_name); 400 printer_info->SetString(printing::kSettingPrinterName, printer_name);
419 printer_info->SetString(printing::kSettingPrinterDescription, 401 printer_info->SetString(printing::kSettingPrinterDescription,
420 printer_description); 402 printer_description);
421 403
422 base::DictionaryValue* options = new base::DictionaryValue; 404 base::DictionaryValue* options = new base::DictionaryValue;
423 printer_info->Set(printing::kSettingPrinterOptions, options); 405 printer_info->Set(printing::kSettingPrinterOptions, options);
424 for (const auto opt_it : printer.options) 406 for (const auto opt_it : printer.options)
425 options->SetString(opt_it.first, opt_it.second); 407 options->SetString(opt_it.first, opt_it.second);
426 408
427 printers->Append(std::move(printer_info)); 409 printers->Append(std::move(printer_info));
428 410
429 VLOG(1) << "Found printer " << printer_name << " with device name " 411 VLOG(1) << "Found printer " << printer_name << " with device name "
430 << printer.printer_name; 412 << printer.printer_name;
431 } 413 }
414 }
415
416 void EnumeratePrinters(Profile* profile, base::ListValue* printers) {
417 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
418
419 VLOG(1) << "Enumerate printers start";
420 printing::PrinterList printer_list =
421 printing::EnumeratePrintersOnBlockingPoolThread(profile);
422 PrintersToValues(printer_list, printers);
432 VLOG(1) << "Enumerate printers finished, found " << printers->GetSize() 423 VLOG(1) << "Enumerate printers finished, found " << printers->GetSize()
433 << " printers"; 424 << " printers";
434 } 425 }
435 426
436 std::unique_ptr<base::DictionaryValue> 427 std::unique_ptr<base::DictionaryValue>
437 GetPrinterCapabilitiesOnBlockingPoolThread(const std::string& device_name) { 428 GetPrinterCapabilitiesOnBlockingPoolThread(const std::string& device_name) {
438 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); 429 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
439 DCHECK(!device_name.empty()); 430 DCHECK(!device_name.empty());
440 431
441 scoped_refptr<printing::PrintBackend> print_backend( 432 scoped_refptr<printing::PrintBackend> print_backend(
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after
681 return web_ui()->GetWebContents(); 672 return web_ui()->GetWebContents();
682 } 673 }
683 674
684 PrintPreviewUI* PrintPreviewHandler::print_preview_ui() const { 675 PrintPreviewUI* PrintPreviewHandler::print_preview_ui() const {
685 return static_cast<PrintPreviewUI*>(web_ui()->GetController()); 676 return static_cast<PrintPreviewUI*>(web_ui()->GetController());
686 } 677 }
687 678
688 void PrintPreviewHandler::HandleGetPrinters(const base::ListValue* /*args*/) { 679 void PrintPreviewHandler::HandleGetPrinters(const base::ListValue* /*args*/) {
689 base::ListValue* results = new base::ListValue; 680 base::ListValue* results = new base::ListValue;
690 BrowserThread::PostBlockingPoolTaskAndReply( 681 BrowserThread::PostBlockingPoolTaskAndReply(
691 FROM_HERE, base::Bind(&EnumeratePrintersOnBlockingPoolThread, 682 FROM_HERE, base::Bind(&EnumeratePrinters, Profile::FromWebUI(web_ui()),
692 base::Unretained(results)), 683 base::Unretained(results)),
693 base::Bind(&PrintPreviewHandler::SetupPrinterList, 684 base::Bind(&PrintPreviewHandler::SetupPrinterList,
694 weak_factory_.GetWeakPtr(), base::Owned(results))); 685 weak_factory_.GetWeakPtr(), base::Owned(results)));
695 } 686 }
696 687
697 void PrintPreviewHandler::HandleGetPrivetPrinters(const base::ListValue* args) { 688 void PrintPreviewHandler::HandleGetPrivetPrinters(const base::ListValue* args) {
698 if (!PrivetPrintingEnabled()) 689 if (!PrivetPrintingEnabled())
699 return web_ui()->CallJavascriptFunctionUnsafe("onPrivetPrinterSearchDone"); 690 return web_ui()->CallJavascriptFunctionUnsafe("onPrivetPrinterSearchDone");
700 #if BUILDFLAG(ENABLE_SERVICE_DISCOVERY) 691 #if BUILDFLAG(ENABLE_SERVICE_DISCOVERY)
701 using local_discovery::ServiceDiscoverySharedClient; 692 using local_discovery::ServiceDiscoverySharedClient;
(...skipping 515 matching lines...) Expand 10 before | Expand all | Expand 10 after
1217 settings->SetString(kNumberFormat, base::FormatDouble(123456.78, 2)); 1208 settings->SetString(kNumberFormat, base::FormatDouble(123456.78, 2));
1218 settings->SetInteger(kMeasurementSystem, system); 1209 settings->SetInteger(kMeasurementSystem, system);
1219 } 1210 }
1220 1211
1221 void PrintPreviewHandler::HandleGetInitialSettings( 1212 void PrintPreviewHandler::HandleGetInitialSettings(
1222 const base::ListValue* /*args*/) { 1213 const base::ListValue* /*args*/) {
1223 // Send before SendInitialSettings() to allow cloud printer auto select. 1214 // Send before SendInitialSettings() to allow cloud printer auto select.
1224 SendCloudPrintEnabled(); 1215 SendCloudPrintEnabled();
1225 base::PostTaskAndReplyWithResult( 1216 base::PostTaskAndReplyWithResult(
1226 BrowserThread::GetBlockingPool(), FROM_HERE, 1217 BrowserThread::GetBlockingPool(), FROM_HERE,
1227 base::Bind(&GetDefaultPrinterOnBlockingPoolThread), 1218 base::Bind(&printing::GetDefaultPrinterOnBlockingPoolThread),
1228 base::Bind(&PrintPreviewHandler::SendInitialSettings, 1219 base::Bind(&PrintPreviewHandler::SendInitialSettings,
1229 weak_factory_.GetWeakPtr())); 1220 weak_factory_.GetWeakPtr()));
1230 } 1221 }
1231 1222
1232 void PrintPreviewHandler::HandleForceOpenNewTab(const base::ListValue* args) { 1223 void PrintPreviewHandler::HandleForceOpenNewTab(const base::ListValue* args) {
1233 std::string url; 1224 std::string url;
1234 if (!args->GetString(0, &url)) 1225 if (!args->GetString(0, &url))
1235 return; 1226 return;
1236 Browser* browser = chrome::FindBrowserWithWebContents(GetInitiator()); 1227 Browser* browser = chrome::FindBrowserWithWebContents(GetInitiator());
1237 if (!browser) 1228 if (!browser)
(...skipping 527 matching lines...) Expand 10 before | Expand all | Expand 10 after
1765 1756
1766 void PrintPreviewHandler::UnregisterForGaiaCookieChanges() { 1757 void PrintPreviewHandler::UnregisterForGaiaCookieChanges() {
1767 if (gaia_cookie_manager_service_) 1758 if (gaia_cookie_manager_service_)
1768 gaia_cookie_manager_service_->RemoveObserver(this); 1759 gaia_cookie_manager_service_->RemoveObserver(this);
1769 } 1760 }
1770 1761
1771 void PrintPreviewHandler::SetPdfSavedClosureForTesting( 1762 void PrintPreviewHandler::SetPdfSavedClosureForTesting(
1772 const base::Closure& closure) { 1763 const base::Closure& closure) {
1773 pdf_file_saved_closure_ = closure; 1764 pdf_file_saved_closure_ = closure;
1774 } 1765 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698