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 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <string> | 10 #include <string> |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 | 82 |
83 #if defined(OS_CHROMEOS) | 83 #if defined(OS_CHROMEOS) |
84 #include "chrome/browser/chromeos/settings/device_oauth2_token_service.h" | 84 #include "chrome/browser/chromeos/settings/device_oauth2_token_service.h" |
85 #include "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h
" | 85 #include "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h
" |
86 #endif | 86 #endif |
87 | 87 |
88 #if defined(ENABLE_SERVICE_DISCOVERY) | 88 #if defined(ENABLE_SERVICE_DISCOVERY) |
89 #include "chrome/browser/local_discovery/privet_constants.h" | 89 #include "chrome/browser/local_discovery/privet_constants.h" |
90 #endif | 90 #endif |
91 | 91 |
| 92 #if defined(ENABLE_EXTENSIONS) |
| 93 #include "extensions/browser/api/printer_provider/printer_provider_api.h" |
| 94 #endif |
| 95 |
92 using content::BrowserThread; | 96 using content::BrowserThread; |
93 using content::RenderViewHost; | 97 using content::RenderViewHost; |
94 using content::WebContents; | 98 using content::WebContents; |
95 | 99 |
96 namespace { | 100 namespace { |
97 | 101 |
98 enum UserActionBuckets { | 102 enum UserActionBuckets { |
99 PRINT_TO_PRINTER, | 103 PRINT_TO_PRINTER, |
100 PRINT_TO_PDF, | 104 PRINT_TO_PDF, |
101 CANCEL, | 105 CANCEL, |
(...skipping 538 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
640 base::Unretained(this))); | 644 base::Unretained(this))); |
641 web_ui()->RegisterMessageCallback("getPrivetPrinters", | 645 web_ui()->RegisterMessageCallback("getPrivetPrinters", |
642 base::Bind(&PrintPreviewHandler::HandleGetPrivetPrinters, | 646 base::Bind(&PrintPreviewHandler::HandleGetPrivetPrinters, |
643 base::Unretained(this))); | 647 base::Unretained(this))); |
644 web_ui()->RegisterMessageCallback("stopGetPrivetPrinters", | 648 web_ui()->RegisterMessageCallback("stopGetPrivetPrinters", |
645 base::Bind(&PrintPreviewHandler::HandleStopGetPrivetPrinters, | 649 base::Bind(&PrintPreviewHandler::HandleStopGetPrivetPrinters, |
646 base::Unretained(this))); | 650 base::Unretained(this))); |
647 web_ui()->RegisterMessageCallback("getPrivetPrinterCapabilities", | 651 web_ui()->RegisterMessageCallback("getPrivetPrinterCapabilities", |
648 base::Bind(&PrintPreviewHandler::HandleGetPrivetPrinterCapabilities, | 652 base::Bind(&PrintPreviewHandler::HandleGetPrivetPrinterCapabilities, |
649 base::Unretained(this))); | 653 base::Unretained(this))); |
| 654 web_ui()->RegisterMessageCallback( |
| 655 "getExtensionPrinters", |
| 656 base::Bind(&PrintPreviewHandler::HandleGetExtensionPrinters, |
| 657 base::Unretained(this))); |
| 658 web_ui()->RegisterMessageCallback( |
| 659 "getExtensionPrinterCapabilities", |
| 660 base::Bind(&PrintPreviewHandler::HandleGetExtensionPrinterCapabilities, |
| 661 base::Unretained(this))); |
650 RegisterForMergeSession(); | 662 RegisterForMergeSession(); |
651 } | 663 } |
652 | 664 |
653 bool PrintPreviewHandler::PrivetPrintingEnabled() { | 665 bool PrintPreviewHandler::PrivetPrintingEnabled() { |
654 #if defined(ENABLE_SERVICE_DISCOVERY) | 666 #if defined(ENABLE_SERVICE_DISCOVERY) |
655 return true; | 667 return true; |
656 #else | 668 #else |
657 return false; | 669 return false; |
658 #endif | 670 #endif |
659 } | 671 } |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
699 bool success = args->GetString(0, &name); | 711 bool success = args->GetString(0, &name); |
700 DCHECK(success); | 712 DCHECK(success); |
701 | 713 |
702 CreatePrivetHTTP( | 714 CreatePrivetHTTP( |
703 name, | 715 name, |
704 base::Bind(&PrintPreviewHandler::PrivetCapabilitiesUpdateClient, | 716 base::Bind(&PrintPreviewHandler::PrivetCapabilitiesUpdateClient, |
705 base::Unretained(this))); | 717 base::Unretained(this))); |
706 #endif | 718 #endif |
707 } | 719 } |
708 | 720 |
| 721 void PrintPreviewHandler::HandleGetExtensionPrinters( |
| 722 const base::ListValue* args) { |
| 723 #if defined(ENABLE_EXTENSIONS) |
| 724 // TODO(tbarzic): Handle case where a new search is initiated before the |
| 725 // previous one finishes. Currently, in this case Web UI layer may get |
| 726 // multiple events with printer list from some extensions, and it may get |
| 727 // fooled by |done| flag from the first search into marking the search |
| 728 // complete. |
| 729 extensions::PrinterProviderAPI::GetFactoryInstance() |
| 730 ->Get(preview_web_contents()->GetBrowserContext()) |
| 731 ->DispatchGetPrintersRequested( |
| 732 base::Bind(&PrintPreviewHandler::OnGotPrintersForExtension, |
| 733 weak_factory_.GetWeakPtr())); |
| 734 #else |
| 735 OnGotPrintersForExtension(base::ListValue(), true /* done */); |
| 736 #endif |
| 737 } |
| 738 |
| 739 void PrintPreviewHandler::HandleGetExtensionPrinterCapabilities( |
| 740 const base::ListValue* args) { |
| 741 std::string printer_id; |
| 742 bool ok = args->GetString(0, &printer_id); |
| 743 DCHECK(ok); |
| 744 |
| 745 #if defined(ENABLE_EXTENSIONS) |
| 746 extensions::PrinterProviderAPI::GetFactoryInstance() |
| 747 ->Get(preview_web_contents()->GetBrowserContext()) |
| 748 ->DispatchGetCapabilityRequested( |
| 749 printer_id, |
| 750 base::Bind(&PrintPreviewHandler::OnGotExtensionPrinterCapabilities, |
| 751 weak_factory_.GetWeakPtr(), printer_id)); |
| 752 #else |
| 753 OnGotExtensionPrinterCapabilities(printer_id, base::DictionaryValue()); |
| 754 #endif |
| 755 } |
| 756 |
709 void PrintPreviewHandler::HandleGetPreview(const base::ListValue* args) { | 757 void PrintPreviewHandler::HandleGetPreview(const base::ListValue* args) { |
710 DCHECK_EQ(3U, args->GetSize()); | 758 DCHECK_EQ(3U, args->GetSize()); |
711 scoped_ptr<base::DictionaryValue> settings(GetSettingsDictionary(args)); | 759 scoped_ptr<base::DictionaryValue> settings(GetSettingsDictionary(args)); |
712 if (!settings.get()) | 760 if (!settings.get()) |
713 return; | 761 return; |
714 int request_id = -1; | 762 int request_id = -1; |
715 if (!settings->GetInteger(printing::kPreviewRequestID, &request_id)) | 763 if (!settings->GetInteger(printing::kPreviewRequestID, &request_id)) |
716 return; | 764 return; |
717 | 765 |
718 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( | 766 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( |
(...skipping 862 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1581 printer_value->SetBoolean("hasLocalPrinting", has_local_printing); | 1629 printer_value->SetBoolean("hasLocalPrinting", has_local_printing); |
1582 printer_value->SetBoolean( | 1630 printer_value->SetBoolean( |
1583 "isUnregistered", | 1631 "isUnregistered", |
1584 description.id.empty() && | 1632 description.id.empty() && |
1585 command_line->HasSwitch(switches::kEnablePrintPreviewRegisterPromos)); | 1633 command_line->HasSwitch(switches::kEnablePrintPreviewRegisterPromos)); |
1586 printer_value->SetString("cloudID", description.id); | 1634 printer_value->SetString("cloudID", description.id); |
1587 } | 1635 } |
1588 | 1636 |
1589 #endif // defined(ENABLE_SERVICE_DISCOVERY) | 1637 #endif // defined(ENABLE_SERVICE_DISCOVERY) |
1590 | 1638 |
| 1639 void PrintPreviewHandler::OnGotPrintersForExtension( |
| 1640 const base::ListValue& printers, |
| 1641 bool done) { |
| 1642 web_ui()->CallJavascriptFunction("onExtensionPrintersAdded", printers, |
| 1643 base::FundamentalValue(done)); |
| 1644 } |
| 1645 |
| 1646 void PrintPreviewHandler::OnGotExtensionPrinterCapabilities( |
| 1647 const std::string& printer_id, |
| 1648 const base::DictionaryValue& capabilities) { |
| 1649 if (capabilities.empty()) { |
| 1650 web_ui()->CallJavascriptFunction("failedToGetExtensionPrinterCapabilities", |
| 1651 base::StringValue(printer_id)); |
| 1652 return; |
| 1653 } |
| 1654 |
| 1655 web_ui()->CallJavascriptFunction("onExtensionCapabilitiesSet", |
| 1656 base::StringValue(printer_id), capabilities); |
| 1657 } |
| 1658 |
1591 void PrintPreviewHandler::RegisterForMergeSession() { | 1659 void PrintPreviewHandler::RegisterForMergeSession() { |
1592 DCHECK(!reconcilor_); | 1660 DCHECK(!reconcilor_); |
1593 Profile* profile = Profile::FromWebUI(web_ui()); | 1661 Profile* profile = Profile::FromWebUI(web_ui()); |
1594 if (switches::IsEnableAccountConsistency() && !profile->IsOffTheRecord()) { | 1662 if (switches::IsEnableAccountConsistency() && !profile->IsOffTheRecord()) { |
1595 reconcilor_ = AccountReconcilorFactory::GetForProfile(profile); | 1663 reconcilor_ = AccountReconcilorFactory::GetForProfile(profile); |
1596 if (reconcilor_) | 1664 if (reconcilor_) |
1597 reconcilor_->AddMergeSessionObserver(this); | 1665 reconcilor_->AddMergeSessionObserver(this); |
1598 } | 1666 } |
1599 } | 1667 } |
1600 | 1668 |
1601 void PrintPreviewHandler::UnregisterForMergeSession() { | 1669 void PrintPreviewHandler::UnregisterForMergeSession() { |
1602 if (reconcilor_) | 1670 if (reconcilor_) |
1603 reconcilor_->RemoveMergeSessionObserver(this); | 1671 reconcilor_->RemoveMergeSessionObserver(this); |
1604 } | 1672 } |
1605 | 1673 |
1606 void PrintPreviewHandler::SetPdfSavedClosureForTesting( | 1674 void PrintPreviewHandler::SetPdfSavedClosureForTesting( |
1607 const base::Closure& closure) { | 1675 const base::Closure& closure) { |
1608 pdf_file_saved_closure_ = closure; | 1676 pdf_file_saved_closure_ = closure; |
1609 } | 1677 } |
OLD | NEW |