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

Unified Diff: chrome/browser/ui/webui/settings/chromeos/cups_printers_handler.cc

Issue 2250843002: [CUPS] WebUI handler for CUPS printing: Retrieve the printer list from user preference. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 months 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/webui/settings/chromeos/cups_printers_handler.cc
diff --git a/chrome/browser/ui/webui/settings/chromeos/cups_printers_handler.cc b/chrome/browser/ui/webui/settings/chromeos/cups_printers_handler.cc
index c37eec2954558557377bbecf273bbaf83cc9b6e2..0590ff7c49630a6fde74a27e9ef84e5a3bea99a7 100644
--- a/chrome/browser/ui/webui/settings/chromeos/cups_printers_handler.cc
+++ b/chrome/browser/ui/webui/settings/chromeos/cups_printers_handler.cc
@@ -7,34 +7,62 @@
#include <memory>
#include "base/bind.h"
-#include "base/memory/ref_counted.h"
+#include "base/memory/ptr_util.h"
+#include "base/strings/string_util.h"
#include "base/values.h"
+#include "chrome/browser/chromeos/printing/printer_pref_manager_factory.h"
+#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/web_ui.h"
#include "printing/backend/print_backend.h"
namespace {
-// TODO(xdai): Retrieve the CUPS printers list from user preference instead.
-void EnumeratePrintersOnBlockingPoolThread(base::ListValue* printers) {
- scoped_refptr<printing::PrintBackend> print_backend(
- printing::PrintBackend::CreateInstance(nullptr));
+// Extract protocol and IP address from a given URI. See the format of URI:
+// https://www.cups.org/doc/network.html#PROTOCOLS.
+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
+ std::string* protocol,
+ std::string* ip_address) {
+ size_t first_colon = uri.find_first_of(":");
+ if (first_colon == std::string::npos)
+ return;
+ *protocol = uri.substr(0, first_colon);
+
+ // Trim the protocol and the following "://" from the URI.
+ size_t protocol_end_pos = first_colon + 3;
+ if (protocol_end_pos >= uri.length())
+ return;
+
+ std::string trimmed_uri = uri.substr(protocol_end_pos);
+ size_t user_name_end_pos = trimmed_uri.find_first_of("@");
+ if (user_name_end_pos != std::string::npos &&
+ user_name_end_pos < trimmed_uri.length() - 1) {
+ trimmed_uri = trimmed_uri.substr(user_name_end_pos + 1);
+ }
- printing::PrinterList printer_list;
- print_backend->EnumeratePrinters(&printer_list);
+ size_t ip_address_pos_end = trimmed_uri.find_first_of(":/");
+ if (ip_address_pos_end == std::string::npos)
+ ip_address_pos_end = trimmed_uri.length();
+ *ip_address = trimmed_uri.substr(0, ip_address_pos_end);
+}
- for (const printing::PrinterBasicInfo& printer : printer_list) {
- std::unique_ptr<base::DictionaryValue> printer_info(
- new base::DictionaryValue);
- printer_info->SetString("printerName", printer.printer_name);
- printer_info->SetString("printerDescription", printer.printer_description);
- for (const auto opt_it : printer.options) {
- // TODO(xdai): Get "printerAddress" and "printerProtocol" from URI.
- if (opt_it.first == "printer-make-and-model")
- printer_info->SetString("printerModel", opt_it.second);
- }
- printers->Append(std::move(printer_info));
- }
+std::string GetMappedProtocolString(const std::string& protocol) {
+ std::string lower_protocol = base::ToLowerASCII(protocol);
+ if (lower_protocol == "ipp")
+ 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
+ if (lower_protocol == "ipps")
+ return "Internet Printing Protocol (IPPS)";
+ if (lower_protocol == "http")
+ return "Internet Printing Protocol (HTTP)";
+ if (lower_protocol == "https")
+ return "Internet Printing Protocol (HTTPS)";
+ if (lower_protocol == "socket")
+ return "AppSocket (TCP/IP)";
+ if (lower_protocol == "lpd")
+ return "Line Printer Daemon (LPD)";
+ if (lower_protocol == "usb")
+ return "USB";
+ return "Unknown Protocol";
}
} // namespace
@@ -42,7 +70,8 @@ void EnumeratePrintersOnBlockingPoolThread(base::ListValue* printers) {
namespace chromeos {
namespace settings {
-CupsPrintersHandler::CupsPrintersHandler() : weak_factory_(this) {}
+CupsPrintersHandler::CupsPrintersHandler(content::WebUI* webui)
+ : profile_(Profile::FromWebUI(webui)), weak_factory_(this) {}
CupsPrintersHandler::~CupsPrintersHandler() {}
@@ -61,8 +90,6 @@ void CupsPrintersHandler::RegisterMessages() {
base::Unretained(this)));
}
-// TODO(xdai): Retrieve the CUPS printer list from user preference instead after
-// the CL https://codereview.chromium.org/2161933003/ is landed.
void CupsPrintersHandler::HandleGetCupsPrintersList(
const base::ListValue* args) {
AllowJavascript();
@@ -71,31 +98,54 @@ void CupsPrintersHandler::HandleGetCupsPrintersList(
std::string callback_id;
CHECK(args->GetString(0, &callback_id));
+ std::vector<std::unique_ptr<Printer>> printers =
+ PrinterPrefManagerFactory::GetForBrowserContext(profile_)->GetPrinters();
+
base::ListValue* printers_list = new base::ListValue;
- content::BrowserThread::PostBlockingPoolTaskAndReply(
- FROM_HERE, base::Bind(&EnumeratePrintersOnBlockingPoolThread,
- base::Unretained(printers_list)),
- base::Bind(&CupsPrintersHandler::OnGetCupsPrintersList,
- weak_factory_.GetWeakPtr(), callback_id,
- base::Owned(printers_list)));
-}
+ std::vector<std::unique_ptr<Printer>>::const_iterator iter = printers.begin();
+ 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.
+ std::unique_ptr<base::DictionaryValue> printer_info(
+ new base::DictionaryValue);
skau 2016/08/18 05:27:39 use base::MakeUnique<base::DictionaryValue>()
xdai1 2016/08/18 21:12:18 Done.
+ printer_info->SetString("printerId", (*iter)->id());
+ printer_info->SetString("printerName", (*iter)->display_name());
+ printer_info->SetString("printerDescription", (*iter)->description());
+ printer_info->SetString("printerManufacturer", (*iter)->manufacturer());
+ printer_info->SetString("printerModel", (*iter)->model());
+
+ // Get protocol and ip address from the printer's URI.
+ 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.
+ std::string protocol;
+ std::string ip_address;
+ ExtractProtocolAndIpAddress(uri, &protocol, &ip_address);
+ 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
+ printer_info->SetString("printerProtocol",
+ GetMappedProtocolString(protocol));
+
+ printers_list->Append(std::move(printer_info));
+ }
-void CupsPrintersHandler::OnGetCupsPrintersList(
- const std::string callback_id,
- base::ListValue* printers_list) {
std::unique_ptr<base::DictionaryValue> response(new base::DictionaryValue());
- response->Set("printerList", printers_list->DeepCopy());
+ response->Set("printerList", printers_list);
ResolveJavascriptCallback(base::StringValue(callback_id), *response);
}
void CupsPrintersHandler::HandleUpdateCupsPrinter(const base::ListValue* args) {
- // TODO(xdai): Implement it after https://codereview.chromium.org/2161933003/
- // is landed.
+ std::string printer_id;
+ std::string printer_name;
+ CHECK(args->GetString(0, &printer_id));
+ CHECK(args->GetString(1, &printer_name));
+
+ 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
+ printer->set_display_name(printer_name);
+ PrinterPrefManagerFactory::GetForBrowserContext(profile_)->RegisterPrinter(
+ std::move(printer));
}
void CupsPrintersHandler::HandleRemoveCupsPrinter(const base::ListValue* args) {
- // TODO(xdai): Implement it after https://codereview.chromium.org/2161933003/
- // is landed.
+ std::string printer_id;
+ CHECK(args->GetString(0, &printer_id));
+ PrinterPrefManagerFactory::GetForBrowserContext(profile_)->RemovePrinter(
+ printer_id);
}
} // namespace settings
« no previous file with comments | « chrome/browser/ui/webui/settings/chromeos/cups_printers_handler.h ('k') | chrome/browser/ui/webui/settings/md_settings_ui.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698