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 1077 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1088 | 1088 |
1089 // |args| is expected to contain a string with representing the callback id | 1089 // |args| is expected to contain a string with representing the callback id |
1090 // followed by a list of arguments the first of which should be the printer id. | 1090 // followed by a list of arguments the first of which should be the printer id. |
1091 void PrintPreviewHandler::HandlePrinterSetup(const base::ListValue* args) { | 1091 void PrintPreviewHandler::HandlePrinterSetup(const base::ListValue* args) { |
1092 AllowJavascript(); | 1092 AllowJavascript(); |
1093 | 1093 |
1094 std::string callback_id; | 1094 std::string callback_id; |
1095 std::string printer_name; | 1095 std::string printer_name; |
1096 if (!args->GetString(0, &callback_id) || !args->GetString(1, &printer_name) || | 1096 if (!args->GetString(0, &callback_id) || !args->GetString(1, &printer_name) || |
1097 callback_id.empty() || printer_name.empty()) { | 1097 callback_id.empty() || printer_name.empty()) { |
1098 RejectJavascriptCallback(base::StringValue(callback_id), | 1098 RejectJavascriptCallback(base::Value(callback_id), |
1099 base::StringValue(printer_name)); | 1099 base::Value(printer_name)); |
1100 return; | 1100 return; |
1101 } | 1101 } |
1102 | 1102 |
1103 printer_backend_proxy()->ConfigurePrinterAndFetchCapabilities( | 1103 printer_backend_proxy()->ConfigurePrinterAndFetchCapabilities( |
1104 printer_name, | 1104 printer_name, |
1105 base::Bind(&PrintPreviewHandler::SendPrinterSetup, | 1105 base::Bind(&PrintPreviewHandler::SendPrinterSetup, |
1106 weak_factory_.GetWeakPtr(), callback_id, printer_name)); | 1106 weak_factory_.GetWeakPtr(), callback_id, printer_name)); |
1107 } | 1107 } |
1108 | 1108 |
1109 void PrintPreviewHandler::OnSigninComplete() { | 1109 void PrintPreviewHandler::OnSigninComplete() { |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1280 initial_settings); | 1280 initial_settings); |
1281 } | 1281 } |
1282 | 1282 |
1283 void PrintPreviewHandler::ClosePreviewDialog() { | 1283 void PrintPreviewHandler::ClosePreviewDialog() { |
1284 print_preview_ui()->OnClosePrintPreviewDialog(); | 1284 print_preview_ui()->OnClosePrintPreviewDialog(); |
1285 } | 1285 } |
1286 | 1286 |
1287 void PrintPreviewHandler::SendAccessToken(const std::string& type, | 1287 void PrintPreviewHandler::SendAccessToken(const std::string& type, |
1288 const std::string& access_token) { | 1288 const std::string& access_token) { |
1289 VLOG(1) << "Get getAccessToken finished"; | 1289 VLOG(1) << "Get getAccessToken finished"; |
1290 web_ui()->CallJavascriptFunctionUnsafe("onDidGetAccessToken", | 1290 web_ui()->CallJavascriptFunctionUnsafe( |
1291 base::StringValue(type), | 1291 "onDidGetAccessToken", base::Value(type), base::Value(access_token)); |
1292 base::StringValue(access_token)); | |
1293 } | 1292 } |
1294 | 1293 |
1295 void PrintPreviewHandler::SendPrinterCapabilities( | 1294 void PrintPreviewHandler::SendPrinterCapabilities( |
1296 const std::string& printer_name, | 1295 const std::string& printer_name, |
1297 std::unique_ptr<base::DictionaryValue> settings_info) { | 1296 std::unique_ptr<base::DictionaryValue> settings_info) { |
1298 if (!settings_info) { | 1297 if (!settings_info) { |
1299 VLOG(1) << "Get printer capabilities failed"; | 1298 VLOG(1) << "Get printer capabilities failed"; |
1300 web_ui()->CallJavascriptFunctionUnsafe("failedToGetPrinterCapabilities", | 1299 web_ui()->CallJavascriptFunctionUnsafe("failedToGetPrinterCapabilities", |
1301 base::StringValue(printer_name)); | 1300 base::Value(printer_name)); |
1302 return; | 1301 return; |
1303 } | 1302 } |
1304 VLOG(1) << "Get printer capabilities finished"; | 1303 VLOG(1) << "Get printer capabilities finished"; |
1305 web_ui()->CallJavascriptFunctionUnsafe("updateWithPrinterCapabilities", | 1304 web_ui()->CallJavascriptFunctionUnsafe("updateWithPrinterCapabilities", |
1306 *settings_info); | 1305 *settings_info); |
1307 } | 1306 } |
1308 | 1307 |
1309 void PrintPreviewHandler::SendPrinterSetup( | 1308 void PrintPreviewHandler::SendPrinterSetup( |
1310 const std::string& callback_id, | 1309 const std::string& callback_id, |
1311 const std::string& printer_name, | 1310 const std::string& printer_name, |
1312 std::unique_ptr<base::DictionaryValue> destination_info) { | 1311 std::unique_ptr<base::DictionaryValue> destination_info) { |
1313 auto response = base::MakeUnique<base::DictionaryValue>(); | 1312 auto response = base::MakeUnique<base::DictionaryValue>(); |
1314 bool success = true; | 1313 bool success = true; |
1315 auto caps_value = base::Value::CreateNullValue(); | 1314 auto caps_value = base::Value::CreateNullValue(); |
1316 auto caps = base::MakeUnique<base::DictionaryValue>(); | 1315 auto caps = base::MakeUnique<base::DictionaryValue>(); |
1317 if (destination_info && | 1316 if (destination_info && |
1318 destination_info->Remove(printing::kPrinterCapabilities, &caps_value) && | 1317 destination_info->Remove(printing::kPrinterCapabilities, &caps_value) && |
1319 caps_value->IsType(base::Value::Type::DICTIONARY)) { | 1318 caps_value->IsType(base::Value::Type::DICTIONARY)) { |
1320 caps = base::DictionaryValue::From(std::move(caps_value)); | 1319 caps = base::DictionaryValue::From(std::move(caps_value)); |
1321 } else { | 1320 } else { |
1322 LOG(WARNING) << "Printer setup failed"; | 1321 LOG(WARNING) << "Printer setup failed"; |
1323 success = false; | 1322 success = false; |
1324 } | 1323 } |
1325 | 1324 |
1326 response->SetString("printerId", printer_name); | 1325 response->SetString("printerId", printer_name); |
1327 response->SetBoolean("success", success); | 1326 response->SetBoolean("success", success); |
1328 response->Set("capabilities", std::move(caps)); | 1327 response->Set("capabilities", std::move(caps)); |
1329 | 1328 |
1330 ResolveJavascriptCallback(base::StringValue(callback_id), *response); | 1329 ResolveJavascriptCallback(base::Value(callback_id), *response); |
1331 } | 1330 } |
1332 | 1331 |
1333 void PrintPreviewHandler::SetupPrinterList( | 1332 void PrintPreviewHandler::SetupPrinterList( |
1334 const printing::PrinterList& printer_list) { | 1333 const printing::PrinterList& printer_list) { |
1335 base::ListValue printers; | 1334 base::ListValue printers; |
1336 PrintersToValues(printer_list, &printers); | 1335 PrintersToValues(printer_list, &printers); |
1337 | 1336 |
1338 VLOG(1) << "Enumerate printers finished, found " << printers.GetSize() | 1337 VLOG(1) << "Enumerate printers finished, found " << printers.GetSize() |
1339 << " printers"; | 1338 << " printers"; |
1340 | 1339 |
(...skipping 17 matching lines...) Expand all Loading... |
1358 web_ui()->CallJavascriptFunctionUnsafe("setUseCloudPrint", settings); | 1357 web_ui()->CallJavascriptFunctionUnsafe("setUseCloudPrint", settings); |
1359 } | 1358 } |
1360 } | 1359 } |
1361 | 1360 |
1362 void PrintPreviewHandler::SendCloudPrintJob(const base::RefCountedBytes* data) { | 1361 void PrintPreviewHandler::SendCloudPrintJob(const base::RefCountedBytes* data) { |
1363 // BASE64 encode the job data. | 1362 // BASE64 encode the job data. |
1364 const base::StringPiece raw_data(reinterpret_cast<const char*>(data->front()), | 1363 const base::StringPiece raw_data(reinterpret_cast<const char*>(data->front()), |
1365 data->size()); | 1364 data->size()); |
1366 std::string base64_data; | 1365 std::string base64_data; |
1367 base::Base64Encode(raw_data, &base64_data); | 1366 base::Base64Encode(raw_data, &base64_data); |
1368 base::StringValue data_value(base64_data); | 1367 base::Value data_value(base64_data); |
1369 | 1368 |
1370 web_ui()->CallJavascriptFunctionUnsafe("printToCloud", data_value); | 1369 web_ui()->CallJavascriptFunctionUnsafe("printToCloud", data_value); |
1371 } | 1370 } |
1372 | 1371 |
1373 WebContents* PrintPreviewHandler::GetInitiator() const { | 1372 WebContents* PrintPreviewHandler::GetInitiator() const { |
1374 printing::PrintPreviewDialogController* dialog_controller = | 1373 printing::PrintPreviewDialogController* dialog_controller = |
1375 printing::PrintPreviewDialogController::GetInstance(); | 1374 printing::PrintPreviewDialogController::GetInstance(); |
1376 if (!dialog_controller) | 1375 if (!dialog_controller) |
1377 return NULL; | 1376 return NULL; |
1378 return dialog_controller->GetInitiator(preview_web_contents()); | 1377 return dialog_controller->GetInitiator(preview_web_contents()); |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1651 FillPrinterDescription(name, *description, true, &printer_info); | 1650 FillPrinterDescription(name, *description, true, &printer_info); |
1652 | 1651 |
1653 web_ui()->CallJavascriptFunctionUnsafe("onPrivetCapabilitiesSet", | 1652 web_ui()->CallJavascriptFunctionUnsafe("onPrivetCapabilitiesSet", |
1654 printer_info, *capabilities); | 1653 printer_info, *capabilities); |
1655 | 1654 |
1656 privet_capabilities_operation_.reset(); | 1655 privet_capabilities_operation_.reset(); |
1657 } | 1656 } |
1658 | 1657 |
1659 void PrintPreviewHandler::SendPrivetCapabilitiesError( | 1658 void PrintPreviewHandler::SendPrivetCapabilitiesError( |
1660 const std::string& device_name) { | 1659 const std::string& device_name) { |
1661 base::StringValue name_value(device_name); | 1660 base::Value name_value(device_name); |
1662 web_ui()->CallJavascriptFunctionUnsafe("failedToGetPrivetPrinterCapabilities", | 1661 web_ui()->CallJavascriptFunctionUnsafe("failedToGetPrivetPrinterCapabilities", |
1663 name_value); | 1662 name_value); |
1664 } | 1663 } |
1665 | 1664 |
1666 void PrintPreviewHandler::PrintToPrivetPrinter(const std::string& device_name, | 1665 void PrintPreviewHandler::PrintToPrivetPrinter(const std::string& device_name, |
1667 const std::string& ticket, | 1666 const std::string& ticket, |
1668 const std::string& capabilities, | 1667 const std::string& capabilities, |
1669 const gfx::Size& page_size) { | 1668 const gfx::Size& page_size) { |
1670 CreatePrivetHTTP( | 1669 CreatePrivetHTTP( |
1671 device_name, | 1670 device_name, |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1741 bool done) { | 1740 bool done) { |
1742 web_ui()->CallJavascriptFunctionUnsafe("onExtensionPrintersAdded", printers, | 1741 web_ui()->CallJavascriptFunctionUnsafe("onExtensionPrintersAdded", printers, |
1743 base::Value(done)); | 1742 base::Value(done)); |
1744 } | 1743 } |
1745 | 1744 |
1746 void PrintPreviewHandler::OnGotExtensionPrinterInfo( | 1745 void PrintPreviewHandler::OnGotExtensionPrinterInfo( |
1747 const std::string& printer_id, | 1746 const std::string& printer_id, |
1748 const base::DictionaryValue& printer_info) { | 1747 const base::DictionaryValue& printer_info) { |
1749 if (printer_info.empty()) { | 1748 if (printer_info.empty()) { |
1750 web_ui()->CallJavascriptFunctionUnsafe("failedToResolveProvisionalPrinter", | 1749 web_ui()->CallJavascriptFunctionUnsafe("failedToResolveProvisionalPrinter", |
1751 base::StringValue(printer_id)); | 1750 base::Value(printer_id)); |
1752 return; | 1751 return; |
1753 } | 1752 } |
1754 | 1753 |
1755 web_ui()->CallJavascriptFunctionUnsafe("onProvisionalPrinterResolved", | 1754 web_ui()->CallJavascriptFunctionUnsafe("onProvisionalPrinterResolved", |
1756 base::StringValue(printer_id), | 1755 base::Value(printer_id), printer_info); |
1757 printer_info); | |
1758 } | 1756 } |
1759 | 1757 |
1760 void PrintPreviewHandler::OnGotExtensionPrinterCapabilities( | 1758 void PrintPreviewHandler::OnGotExtensionPrinterCapabilities( |
1761 const std::string& printer_id, | 1759 const std::string& printer_id, |
1762 const base::DictionaryValue& capabilities) { | 1760 const base::DictionaryValue& capabilities) { |
1763 if (capabilities.empty()) { | 1761 if (capabilities.empty()) { |
1764 web_ui()->CallJavascriptFunctionUnsafe( | 1762 web_ui()->CallJavascriptFunctionUnsafe( |
1765 "failedToGetExtensionPrinterCapabilities", | 1763 "failedToGetExtensionPrinterCapabilities", base::Value(printer_id)); |
1766 base::StringValue(printer_id)); | |
1767 return; | 1764 return; |
1768 } | 1765 } |
1769 | 1766 |
1770 web_ui()->CallJavascriptFunctionUnsafe("onExtensionCapabilitiesSet", | 1767 web_ui()->CallJavascriptFunctionUnsafe("onExtensionCapabilitiesSet", |
1771 base::StringValue(printer_id), | 1768 base::Value(printer_id), capabilities); |
1772 capabilities); | |
1773 } | 1769 } |
1774 | 1770 |
1775 void PrintPreviewHandler::OnExtensionPrintResult(bool success, | 1771 void PrintPreviewHandler::OnExtensionPrintResult(bool success, |
1776 const std::string& status) { | 1772 const std::string& status) { |
1777 if (success) { | 1773 if (success) { |
1778 ClosePreviewDialog(); | 1774 ClosePreviewDialog(); |
1779 return; | 1775 return; |
1780 } | 1776 } |
1781 | 1777 |
1782 // TODO(tbarzic): This function works for extension printers case too, but it | 1778 // TODO(tbarzic): This function works for extension printers case too, but it |
1783 // should be renamed to something more generic. | 1779 // should be renamed to something more generic. |
1784 web_ui()->CallJavascriptFunctionUnsafe("onPrivetPrintFailed", | 1780 web_ui()->CallJavascriptFunctionUnsafe("onPrivetPrintFailed", |
1785 base::StringValue(status)); | 1781 base::Value(status)); |
1786 } | 1782 } |
1787 | 1783 |
1788 void PrintPreviewHandler::RegisterForGaiaCookieChanges() { | 1784 void PrintPreviewHandler::RegisterForGaiaCookieChanges() { |
1789 DCHECK(!gaia_cookie_manager_service_); | 1785 DCHECK(!gaia_cookie_manager_service_); |
1790 Profile* profile = Profile::FromWebUI(web_ui()); | 1786 Profile* profile = Profile::FromWebUI(web_ui()); |
1791 if (switches::IsEnableAccountConsistency() && !profile->IsOffTheRecord()) { | 1787 if (switches::IsEnableAccountConsistency() && !profile->IsOffTheRecord()) { |
1792 gaia_cookie_manager_service_ = | 1788 gaia_cookie_manager_service_ = |
1793 GaiaCookieManagerServiceFactory::GetForProfile(profile); | 1789 GaiaCookieManagerServiceFactory::GetForProfile(profile); |
1794 if (gaia_cookie_manager_service_) | 1790 if (gaia_cookie_manager_service_) |
1795 gaia_cookie_manager_service_->AddObserver(this); | 1791 gaia_cookie_manager_service_->AddObserver(this); |
1796 } | 1792 } |
1797 } | 1793 } |
1798 | 1794 |
1799 void PrintPreviewHandler::UnregisterForGaiaCookieChanges() { | 1795 void PrintPreviewHandler::UnregisterForGaiaCookieChanges() { |
1800 if (gaia_cookie_manager_service_) | 1796 if (gaia_cookie_manager_service_) |
1801 gaia_cookie_manager_service_->RemoveObserver(this); | 1797 gaia_cookie_manager_service_->RemoveObserver(this); |
1802 } | 1798 } |
1803 | 1799 |
1804 void PrintPreviewHandler::SetPdfSavedClosureForTesting( | 1800 void PrintPreviewHandler::SetPdfSavedClosureForTesting( |
1805 const base::Closure& closure) { | 1801 const base::Closure& closure) { |
1806 pdf_file_saved_closure_ = closure; | 1802 pdf_file_saved_closure_ = closure; |
1807 } | 1803 } |
OLD | NEW |