Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/settings/chromeos/cups_printers_handler.h" | 5 #include "chrome/browser/ui/webui/settings/chromeos/cups_printers_handler.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/strings/string_util.h" | |
| 11 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "chrome/browser/chromeos/printing/printer_pref_manager_factory.h" | |
| 14 #include "chrome/browser/profiles/profile.h" | |
| 12 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 13 #include "content/public/browser/web_ui.h" | 16 #include "content/public/browser/web_ui.h" |
| 14 #include "printing/backend/print_backend.h" | 17 #include "printing/backend/print_backend.h" |
| 15 | 18 |
| 16 namespace { | 19 namespace { |
| 17 | 20 |
| 18 // TODO(xdai): Retrieve the CUPS printers list from user preference instead. | 21 // Extract protocol and IP address from a given URI. See the format of URI: |
| 19 void EnumeratePrintersOnBlockingPoolThread(base::ListValue* printers) { | 22 // https://www.cups.org/doc/network.html#PROTOCOLS. |
| 20 scoped_refptr<printing::PrintBackend> print_backend( | 23 void ExtractProtocolAndIpAddress(const std::string& uri, |
|
Dan Beam
2016/08/18 02:00:54
why can't you use GURL?
skau
2016/08/18 05:27:39
Yes. You definitely want to use GURL to break up
xdai1
2016/08/18 21:12:18
Since the printer URI's scheme is not standard, I
skau
2016/08/19 00:08:45
Per offline discussion, we should probably use htt
| |
| 21 printing::PrintBackend::CreateInstance(nullptr)); | 24 std::string* protocol, |
| 25 std::string* ip_address) { | |
| 26 size_t first_colon = uri.find_first_of(":"); | |
| 27 if (first_colon == std::string::npos) | |
| 28 return; | |
| 29 *protocol = uri.substr(0, first_colon); | |
| 22 | 30 |
| 23 printing::PrinterList printer_list; | 31 // Trim the protocol and the following "://" from the URI. |
| 24 print_backend->EnumeratePrinters(&printer_list); | 32 size_t protocol_end_pos = first_colon + 3; |
| 33 if (protocol_end_pos >= uri.length()) | |
| 34 return; | |
| 25 | 35 |
| 26 for (const printing::PrinterBasicInfo& printer : printer_list) { | 36 std::string trimmed_uri = uri.substr(protocol_end_pos); |
| 27 std::unique_ptr<base::DictionaryValue> printer_info( | 37 size_t user_name_end_pos = trimmed_uri.find_first_of("@"); |
| 28 new base::DictionaryValue); | 38 if (user_name_end_pos != std::string::npos && |
| 29 printer_info->SetString("printerName", printer.printer_name); | 39 user_name_end_pos < trimmed_uri.length() - 1) { |
| 30 printer_info->SetString("printerDescription", printer.printer_description); | 40 trimmed_uri = trimmed_uri.substr(user_name_end_pos + 1); |
| 31 for (const auto opt_it : printer.options) { | |
| 32 // TODO(xdai): Get "printerAddress" and "printerProtocol" from URI. | |
| 33 if (opt_it.first == "printer-make-and-model") | |
| 34 printer_info->SetString("printerModel", opt_it.second); | |
| 35 } | |
| 36 printers->Append(std::move(printer_info)); | |
| 37 } | 41 } |
| 42 | |
| 43 size_t ip_address_pos_end = trimmed_uri.find_first_of(":/"); | |
| 44 if (ip_address_pos_end == std::string::npos) | |
| 45 ip_address_pos_end = trimmed_uri.length(); | |
| 46 *ip_address = trimmed_uri.substr(0, ip_address_pos_end); | |
| 47 } | |
| 48 | |
| 49 std::string GetMappedProtocolString(const std::string& protocol) { | |
| 50 std::string lower_protocol = base::ToLowerASCII(protocol); | |
| 51 if (lower_protocol == "ipp") | |
| 52 return "Internet Printing Protocol (IPP)"; | |
|
skau
2016/08/18 05:27:39
Don't these need to be translated?
xdai1
2016/08/18 21:12:18
I don't know... But it might need to be translated
| |
| 53 if (lower_protocol == "ipps") | |
| 54 return "Internet Printing Protocol (IPPS)"; | |
| 55 if (lower_protocol == "http") | |
| 56 return "Internet Printing Protocol (HTTP)"; | |
| 57 if (lower_protocol == "https") | |
| 58 return "Internet Printing Protocol (HTTPS)"; | |
| 59 if (lower_protocol == "socket") | |
| 60 return "AppSocket (TCP/IP)"; | |
| 61 if (lower_protocol == "lpd") | |
| 62 return "Line Printer Daemon (LPD)"; | |
| 63 if (lower_protocol == "usb") | |
| 64 return "USB"; | |
| 65 return "Unknown Protocol"; | |
| 38 } | 66 } |
| 39 | 67 |
| 40 } // namespace | 68 } // namespace |
| 41 | 69 |
| 42 namespace chromeos { | 70 namespace chromeos { |
| 43 namespace settings { | 71 namespace settings { |
| 44 | 72 |
| 45 CupsPrintersHandler::CupsPrintersHandler() : weak_factory_(this) {} | 73 CupsPrintersHandler::CupsPrintersHandler(content::WebUI* webui) |
| 74 : profile_(Profile::FromWebUI(webui)), weak_factory_(this) {} | |
| 46 | 75 |
| 47 CupsPrintersHandler::~CupsPrintersHandler() {} | 76 CupsPrintersHandler::~CupsPrintersHandler() {} |
| 48 | 77 |
| 49 void CupsPrintersHandler::RegisterMessages() { | 78 void CupsPrintersHandler::RegisterMessages() { |
| 50 web_ui()->RegisterMessageCallback( | 79 web_ui()->RegisterMessageCallback( |
| 51 "getCupsPrintersList", | 80 "getCupsPrintersList", |
| 52 base::Bind(&CupsPrintersHandler::HandleGetCupsPrintersList, | 81 base::Bind(&CupsPrintersHandler::HandleGetCupsPrintersList, |
| 53 base::Unretained(this))); | 82 base::Unretained(this))); |
| 54 web_ui()->RegisterMessageCallback( | 83 web_ui()->RegisterMessageCallback( |
| 55 "updateCupsPrinter", | 84 "updateCupsPrinter", |
| 56 base::Bind(&CupsPrintersHandler::HandleUpdateCupsPrinter, | 85 base::Bind(&CupsPrintersHandler::HandleUpdateCupsPrinter, |
| 57 base::Unretained(this))); | 86 base::Unretained(this))); |
| 58 web_ui()->RegisterMessageCallback( | 87 web_ui()->RegisterMessageCallback( |
| 59 "removeCupsPrinter", | 88 "removeCupsPrinter", |
| 60 base::Bind(&CupsPrintersHandler::HandleRemoveCupsPrinter, | 89 base::Bind(&CupsPrintersHandler::HandleRemoveCupsPrinter, |
| 61 base::Unretained(this))); | 90 base::Unretained(this))); |
| 62 } | 91 } |
| 63 | 92 |
| 64 // TODO(xdai): Retrieve the CUPS printer list from user preference instead after | |
| 65 // the CL https://codereview.chromium.org/2161933003/ is landed. | |
| 66 void CupsPrintersHandler::HandleGetCupsPrintersList( | 93 void CupsPrintersHandler::HandleGetCupsPrintersList( |
| 67 const base::ListValue* args) { | 94 const base::ListValue* args) { |
| 68 AllowJavascript(); | 95 AllowJavascript(); |
| 69 | 96 |
| 70 CHECK_EQ(1U, args->GetSize()); | 97 CHECK_EQ(1U, args->GetSize()); |
| 71 std::string callback_id; | 98 std::string callback_id; |
| 72 CHECK(args->GetString(0, &callback_id)); | 99 CHECK(args->GetString(0, &callback_id)); |
| 73 | 100 |
| 101 std::vector<std::unique_ptr<Printer>> printers = | |
| 102 PrinterPrefManagerFactory::GetForBrowserContext(profile_)->GetPrinters(); | |
| 103 | |
| 74 base::ListValue* printers_list = new base::ListValue; | 104 base::ListValue* printers_list = new base::ListValue; |
| 75 content::BrowserThread::PostBlockingPoolTaskAndReply( | 105 std::vector<std::unique_ptr<Printer>>::const_iterator iter = printers.begin(); |
| 76 FROM_HERE, base::Bind(&EnumeratePrintersOnBlockingPoolThread, | 106 for (; iter != printers.end(); ++iter) { |
|
skau
2016/08/18 05:27:39
This can be for(const std::unique_ptr<Printer>& pr
xdai1
2016/08/18 21:12:18
Done.
| |
| 77 base::Unretained(printers_list)), | 107 std::unique_ptr<base::DictionaryValue> printer_info( |
| 78 base::Bind(&CupsPrintersHandler::OnGetCupsPrintersList, | 108 new base::DictionaryValue); |
|
skau
2016/08/18 05:27:39
use base::MakeUnique<base::DictionaryValue>()
xdai1
2016/08/18 21:12:18
Done.
| |
| 79 weak_factory_.GetWeakPtr(), callback_id, | 109 printer_info->SetString("printerId", (*iter)->id()); |
| 80 base::Owned(printers_list))); | 110 printer_info->SetString("printerName", (*iter)->display_name()); |
| 81 } | 111 printer_info->SetString("printerDescription", (*iter)->description()); |
| 112 printer_info->SetString("printerManufacturer", (*iter)->manufacturer()); | |
| 113 printer_info->SetString("printerModel", (*iter)->model()); | |
| 82 | 114 |
| 83 void CupsPrintersHandler::OnGetCupsPrintersList( | 115 // Get protocol and ip address from the printer's URI. |
| 84 const std::string callback_id, | 116 std::string uri = (*iter)->uri(); |
|
skau
2016/08/18 05:27:39
Per above, just turn this into a GURL.
xdai1
2016/08/18 21:12:18
Done.
| |
| 85 base::ListValue* printers_list) { | 117 std::string protocol; |
| 118 std::string ip_address; | |
| 119 ExtractProtocolAndIpAddress(uri, &protocol, &ip_address); | |
| 120 printer_info->SetString("printerAddress", ip_address); | |
|
skau
2016/08/18 05:27:39
Do we still have 'queue' in the UI? If so, you'll
xdai1
2016/08/18 21:12:18
Yes you're right. Seems we'll need 'queue' informa
| |
| 121 printer_info->SetString("printerProtocol", | |
| 122 GetMappedProtocolString(protocol)); | |
| 123 | |
| 124 printers_list->Append(std::move(printer_info)); | |
| 125 } | |
| 126 | |
| 86 std::unique_ptr<base::DictionaryValue> response(new base::DictionaryValue()); | 127 std::unique_ptr<base::DictionaryValue> response(new base::DictionaryValue()); |
| 87 response->Set("printerList", printers_list->DeepCopy()); | 128 response->Set("printerList", printers_list); |
| 88 ResolveJavascriptCallback(base::StringValue(callback_id), *response); | 129 ResolveJavascriptCallback(base::StringValue(callback_id), *response); |
| 89 } | 130 } |
| 90 | 131 |
| 91 void CupsPrintersHandler::HandleUpdateCupsPrinter(const base::ListValue* args) { | 132 void CupsPrintersHandler::HandleUpdateCupsPrinter(const base::ListValue* args) { |
| 92 // TODO(xdai): Implement it after https://codereview.chromium.org/2161933003/ | 133 std::string printer_id; |
| 93 // is landed. | 134 std::string printer_name; |
| 135 CHECK(args->GetString(0, &printer_id)); | |
| 136 CHECK(args->GetString(1, &printer_name)); | |
| 137 | |
| 138 std::unique_ptr<Printer> printer = base::MakeUnique<Printer>(printer_id); | |
|
skau
2016/08/18 05:27:39
Are you handling add differently? RegisterPrinter
xdai1
2016/08/18 21:12:18
I think adding printer should also be able to use
| |
| 139 printer->set_display_name(printer_name); | |
| 140 PrinterPrefManagerFactory::GetForBrowserContext(profile_)->RegisterPrinter( | |
| 141 std::move(printer)); | |
| 94 } | 142 } |
| 95 | 143 |
| 96 void CupsPrintersHandler::HandleRemoveCupsPrinter(const base::ListValue* args) { | 144 void CupsPrintersHandler::HandleRemoveCupsPrinter(const base::ListValue* args) { |
| 97 // TODO(xdai): Implement it after https://codereview.chromium.org/2161933003/ | 145 std::string printer_id; |
| 98 // is landed. | 146 CHECK(args->GetString(0, &printer_id)); |
| 147 PrinterPrefManagerFactory::GetForBrowserContext(profile_)->RemovePrinter( | |
| 148 printer_id); | |
| 99 } | 149 } |
| 100 | 150 |
| 101 } // namespace settings | 151 } // namespace settings |
| 102 } // namespace chromeos | 152 } // namespace chromeos |
| OLD | NEW |