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

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: done 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 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 const base::Closure& pdf_file_saved_closure) { 289 const base::Closure& pdf_file_saved_closure) {
289 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); 290 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
290 base::File file(path, 291 base::File file(path,
291 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); 292 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
292 file.WriteAtCurrentPos(reinterpret_cast<const char*>(data->front()), 293 file.WriteAtCurrentPos(reinterpret_cast<const char*>(data->front()),
293 base::checked_cast<int>(data->size())); 294 base::checked_cast<int>(data->size()));
294 if (!pdf_file_saved_closure.is_null()) 295 if (!pdf_file_saved_closure.is_null())
295 pdf_file_saved_closure.Run(); 296 pdf_file_saved_closure.Run();
296 } 297 }
297 298
298 std::string GetDefaultPrinterOnBlockingPoolThread() {
299 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
300
301 scoped_refptr<printing::PrintBackend> print_backend(
302 printing::PrintBackend::CreateInstance(nullptr));
303
304 std::string default_printer = print_backend->GetDefaultPrinterName();
305 VLOG(1) << "Default Printer: " << default_printer;
306 return default_printer;
307 }
308
309 class PrintingContextDelegate : public printing::PrintingContext::Delegate { 299 class PrintingContextDelegate : public printing::PrintingContext::Delegate {
310 public: 300 public:
311 // PrintingContext::Delegate methods. 301 // PrintingContext::Delegate methods.
312 gfx::NativeView GetParentView() override { return NULL; } 302 gfx::NativeView GetParentView() override { return NULL; }
313 std::string GetAppLocale() override { 303 std::string GetAppLocale() override {
314 return g_browser_process->GetApplicationLocale(); 304 return g_browser_process->GetApplicationLocale();
315 } 305 }
316 }; 306 };
317 307
318 gfx::Size GetDefaultPdfMediaSizeMicrons() { 308 gfx::Size GetDefaultPdfMediaSizeMicrons() {
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
378 media.AddDefaultOption(media_option, 368 media.AddDefaultOption(media_option,
379 default_media.type == media_option.type); 369 default_media.type == media_option.type);
380 } 370 }
381 media.SaveTo(&description); 371 media.SaveTo(&description);
382 372
383 return std::unique_ptr<base::DictionaryValue>(description.root().DeepCopy()); 373 return std::unique_ptr<base::DictionaryValue>(description.root().DeepCopy());
384 } 374 }
385 375
386 std::pair<std::string, std::string> GetPrinterNameAndDescription( 376 std::pair<std::string, std::string> GetPrinterNameAndDescription(
387 const printing::PrinterBasicInfo& printer) { 377 const printing::PrinterBasicInfo& printer) {
388 #if defined(OS_MACOSX) 378 #if defined(OS_MACOSX) || defined(OS_CHROMEOS)
389 // On Mac, |printer.printer_description| specifies the printer name and 379 // On Mac, |printer.printer_description| specifies the printer name and
390 // |printer.printer_name| specifies the device name / printer queue name. 380 // |printer.printer_name| specifies the device name / printer queue name.
381 // Chrome OS emulates the Mac behavior.
391 const std::string& real_name = printer.printer_description; 382 const std::string& real_name = printer.printer_description;
392 std::string real_description; 383 std::string real_description;
393 const auto it = printer.options.find(kDriverNameTagName); 384 const auto it = printer.options.find(kDriverNameTagName);
394 if (it != printer.options.end()) 385 if (it != printer.options.end())
395 real_description = it->second; 386 real_description = it->second;
396 return std::make_pair(real_name, real_description); 387 return std::make_pair(real_name, real_description);
397 #else 388 #else
398 return std::make_pair(printer.printer_name, printer.printer_description); 389 return std::make_pair(printer.printer_name, printer.printer_description);
399 #endif 390 #endif
400 } 391 }
401 392
402 void EnumeratePrintersOnBlockingPoolThread(base::ListValue* printers) { 393 void PrintersToValues(const printing::PrinterList& printer_list,
403 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); 394 base::ListValue* printers) {
404
405 scoped_refptr<printing::PrintBackend> print_backend(
406 printing::PrintBackend::CreateInstance(nullptr));
407
408 VLOG(1) << "Enumerate printers start";
409 printing::PrinterList printer_list;
410 print_backend->EnumeratePrinters(&printer_list);
411
412 for (const printing::PrinterBasicInfo& printer : printer_list) { 395 for (const printing::PrinterBasicInfo& printer : printer_list) {
413 std::unique_ptr<base::DictionaryValue> printer_info( 396 std::unique_ptr<base::DictionaryValue> printer_info(
414 new base::DictionaryValue); 397 new base::DictionaryValue);
415 const auto printer_name_description = GetPrinterNameAndDescription(printer); 398 const auto printer_name_description = GetPrinterNameAndDescription(printer);
416 const std::string& printer_name = printer_name_description.first; 399 const std::string& printer_name = printer_name_description.first;
417 const std::string& printer_description = printer_name_description.second; 400 const std::string& printer_description = printer_name_description.second;
418 printer_info->SetString(printing::kSettingDeviceName, printer.printer_name); 401 printer_info->SetString(printing::kSettingDeviceName, printer.printer_name);
419 printer_info->SetString(printing::kSettingPrinterName, printer_name); 402 printer_info->SetString(printing::kSettingPrinterName, printer_name);
420 printer_info->SetString(printing::kSettingPrinterDescription, 403 printer_info->SetString(printing::kSettingPrinterDescription,
421 printer_description); 404 printer_description);
422 405
423 base::DictionaryValue* options = new base::DictionaryValue; 406 base::DictionaryValue* options = new base::DictionaryValue;
424 printer_info->Set(printing::kSettingPrinterOptions, options); 407 printer_info->Set(printing::kSettingPrinterOptions, options);
425 for (const auto opt_it : printer.options) 408 for (const auto opt_it : printer.options)
426 options->SetString(opt_it.first, opt_it.second); 409 options->SetString(opt_it.first, opt_it.second);
427 410
428 printers->Append(std::move(printer_info)); 411 printers->Append(std::move(printer_info));
429 412
430 VLOG(1) << "Found printer " << printer_name << " with device name " 413 VLOG(1) << "Found printer " << printer_name << " with device name "
431 << printer.printer_name; 414 << printer.printer_name;
432 } 415 }
433 VLOG(1) << "Enumerate printers finished, found " << printers->GetSize()
434 << " printers";
435 } 416 }
436 417
437 std::unique_ptr<base::DictionaryValue> 418 std::unique_ptr<base::DictionaryValue>
438 GetPrinterCapabilitiesOnBlockingPoolThread(const std::string& device_name) { 419 GetPrinterCapabilitiesOnBlockingPoolThread(const std::string& device_name) {
439 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); 420 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
440 DCHECK(!device_name.empty()); 421 DCHECK(!device_name.empty());
441 422
442 scoped_refptr<printing::PrintBackend> print_backend( 423 scoped_refptr<printing::PrintBackend> print_backend(
443 printing::PrintBackend::CreateInstance(nullptr)); 424 printing::PrintBackend::CreateInstance(nullptr));
444 425
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
680 661
681 WebContents* PrintPreviewHandler::preview_web_contents() const { 662 WebContents* PrintPreviewHandler::preview_web_contents() const {
682 return web_ui()->GetWebContents(); 663 return web_ui()->GetWebContents();
683 } 664 }
684 665
685 PrintPreviewUI* PrintPreviewHandler::print_preview_ui() const { 666 PrintPreviewUI* PrintPreviewHandler::print_preview_ui() const {
686 return static_cast<PrintPreviewUI*>(web_ui()->GetController()); 667 return static_cast<PrintPreviewUI*>(web_ui()->GetController());
687 } 668 }
688 669
689 void PrintPreviewHandler::HandleGetPrinters(const base::ListValue* /*args*/) { 670 void PrintPreviewHandler::HandleGetPrinters(const base::ListValue* /*args*/) {
690 base::ListValue* results = new base::ListValue; 671 VLOG(1) << "Enumerate printers start";
691 BrowserThread::PostBlockingPoolTaskAndReply( 672 printing::EnumeratePrinters(Profile::FromWebUI(web_ui()),
692 FROM_HERE, base::Bind(&EnumeratePrintersOnBlockingPoolThread, 673 base::Bind(&PrintPreviewHandler::SetupPrinterList,
693 base::Unretained(results)), 674 weak_factory_.GetWeakPtr()));
694 base::Bind(&PrintPreviewHandler::SetupPrinterList,
695 weak_factory_.GetWeakPtr(), base::Owned(results)));
696 } 675 }
697 676
698 void PrintPreviewHandler::HandleGetPrivetPrinters(const base::ListValue* args) { 677 void PrintPreviewHandler::HandleGetPrivetPrinters(const base::ListValue* args) {
699 if (!PrivetPrintingEnabled()) 678 if (!PrivetPrintingEnabled())
700 return web_ui()->CallJavascriptFunctionUnsafe("onPrivetPrinterSearchDone"); 679 return web_ui()->CallJavascriptFunctionUnsafe("onPrivetPrinterSearchDone");
701 #if BUILDFLAG(ENABLE_SERVICE_DISCOVERY) 680 #if BUILDFLAG(ENABLE_SERVICE_DISCOVERY)
702 using local_discovery::ServiceDiscoverySharedClient; 681 using local_discovery::ServiceDiscoverySharedClient;
703 scoped_refptr<ServiceDiscoverySharedClient> service_discovery = 682 scoped_refptr<ServiceDiscoverySharedClient> service_discovery =
704 ServiceDiscoverySharedClient::GetInstance(); 683 ServiceDiscoverySharedClient::GetInstance();
705 StartPrivetLister(service_discovery); 684 StartPrivetLister(service_discovery);
(...skipping 512 matching lines...) Expand 10 before | Expand all | Expand 10 after
1218 settings->SetString(kNumberFormat, base::FormatDouble(123456.78, 2)); 1197 settings->SetString(kNumberFormat, base::FormatDouble(123456.78, 2));
1219 settings->SetInteger(kMeasurementSystem, system); 1198 settings->SetInteger(kMeasurementSystem, system);
1220 } 1199 }
1221 1200
1222 void PrintPreviewHandler::HandleGetInitialSettings( 1201 void PrintPreviewHandler::HandleGetInitialSettings(
1223 const base::ListValue* /*args*/) { 1202 const base::ListValue* /*args*/) {
1224 // Send before SendInitialSettings() to allow cloud printer auto select. 1203 // Send before SendInitialSettings() to allow cloud printer auto select.
1225 SendCloudPrintEnabled(); 1204 SendCloudPrintEnabled();
1226 base::PostTaskAndReplyWithResult( 1205 base::PostTaskAndReplyWithResult(
1227 BrowserThread::GetBlockingPool(), FROM_HERE, 1206 BrowserThread::GetBlockingPool(), FROM_HERE,
1228 base::Bind(&GetDefaultPrinterOnBlockingPoolThread), 1207 base::Bind(&printing::GetDefaultPrinterOnBlockingPoolThread),
1229 base::Bind(&PrintPreviewHandler::SendInitialSettings, 1208 base::Bind(&PrintPreviewHandler::SendInitialSettings,
1230 weak_factory_.GetWeakPtr())); 1209 weak_factory_.GetWeakPtr()));
1231 } 1210 }
1232 1211
1233 void PrintPreviewHandler::HandleForceOpenNewTab(const base::ListValue* args) { 1212 void PrintPreviewHandler::HandleForceOpenNewTab(const base::ListValue* args) {
1234 std::string url; 1213 std::string url;
1235 if (!args->GetString(0, &url)) 1214 if (!args->GetString(0, &url))
1236 return; 1215 return;
1237 Browser* browser = chrome::FindBrowserWithWebContents(GetInitiator()); 1216 Browser* browser = chrome::FindBrowserWithWebContents(GetInitiator());
1238 if (!browser) 1217 if (!browser)
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
1300 VLOG(1) << "Get printer capabilities failed"; 1279 VLOG(1) << "Get printer capabilities failed";
1301 web_ui()->CallJavascriptFunctionUnsafe("failedToGetPrinterCapabilities", 1280 web_ui()->CallJavascriptFunctionUnsafe("failedToGetPrinterCapabilities",
1302 base::StringValue(printer_name)); 1281 base::StringValue(printer_name));
1303 return; 1282 return;
1304 } 1283 }
1305 VLOG(1) << "Get printer capabilities finished"; 1284 VLOG(1) << "Get printer capabilities finished";
1306 web_ui()->CallJavascriptFunctionUnsafe("updateWithPrinterCapabilities", 1285 web_ui()->CallJavascriptFunctionUnsafe("updateWithPrinterCapabilities",
1307 *settings_info); 1286 *settings_info);
1308 } 1287 }
1309 1288
1310 void PrintPreviewHandler::SetupPrinterList(const base::ListValue* printers) { 1289 void PrintPreviewHandler::SetupPrinterList(
1290 const printing::PrinterList& printer_list) {
1291 base::ListValue printers;
1292 PrintersToValues(printer_list, &printers);
1293
1294 VLOG(1) << "Enumerate printers finished, found " << printers.GetSize()
1295 << " printers";
1296
1311 if (!has_logged_printers_count_) { 1297 if (!has_logged_printers_count_) {
1312 UMA_HISTOGRAM_COUNTS("PrintPreview.NumberOfPrinters", printers->GetSize()); 1298 UMA_HISTOGRAM_COUNTS("PrintPreview.NumberOfPrinters", printers.GetSize());
1313 has_logged_printers_count_ = true; 1299 has_logged_printers_count_ = true;
1314 } 1300 }
1315 1301
1316 web_ui()->CallJavascriptFunctionUnsafe("setPrinters", *printers); 1302 web_ui()->CallJavascriptFunctionUnsafe("setPrinters", printers);
1317 } 1303 }
1318 1304
1319 void PrintPreviewHandler::SendCloudPrintEnabled() { 1305 void PrintPreviewHandler::SendCloudPrintEnabled() {
1320 Profile* profile = Profile::FromBrowserContext( 1306 Profile* profile = Profile::FromBrowserContext(
1321 preview_web_contents()->GetBrowserContext()); 1307 preview_web_contents()->GetBrowserContext());
1322 PrefService* prefs = profile->GetPrefs(); 1308 PrefService* prefs = profile->GetPrefs();
1323 if (prefs->GetBoolean(prefs::kCloudPrintSubmitEnabled)) { 1309 if (prefs->GetBoolean(prefs::kCloudPrintSubmitEnabled)) {
1324 base::DictionaryValue settings; 1310 base::DictionaryValue settings;
1325 settings.SetString(kCloudPrintUrl, 1311 settings.SetString(kCloudPrintUrl,
1326 GURL(cloud_devices::GetCloudPrintURL()).spec()); 1312 GURL(cloud_devices::GetCloudPrintURL()).spec());
(...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after
1766 1752
1767 void PrintPreviewHandler::UnregisterForGaiaCookieChanges() { 1753 void PrintPreviewHandler::UnregisterForGaiaCookieChanges() {
1768 if (gaia_cookie_manager_service_) 1754 if (gaia_cookie_manager_service_)
1769 gaia_cookie_manager_service_->RemoveObserver(this); 1755 gaia_cookie_manager_service_->RemoveObserver(this);
1770 } 1756 }
1771 1757
1772 void PrintPreviewHandler::SetPdfSavedClosureForTesting( 1758 void PrintPreviewHandler::SetPdfSavedClosureForTesting(
1773 const base::Closure& closure) { 1759 const base::Closure& closure) {
1774 pdf_file_saved_closure_ = closure; 1760 pdf_file_saved_closure_ = closure;
1775 } 1761 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698