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 d2965906e7c498d48e13ef9c4424137d87f9d2d0..ee48da67f40b54442c0164d4481b3a3d50278961 100644 |
--- a/chrome/browser/ui/webui/settings/chromeos/cups_printers_handler.cc |
+++ b/chrome/browser/ui/webui/settings/chromeos/cups_printers_handler.cc |
@@ -7,11 +7,14 @@ |
#include <memory> |
#include "base/bind.h" |
+#include "base/bind_helpers.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 "chromeos/dbus/dbus_thread_manager.h" |
+#include "chromeos/dbus/debug_daemon_client.h" |
#include "content/public/browser/browser_thread.h" |
#include "content/public/browser/web_ui.h" |
#include "printing/backend/print_backend.h" |
@@ -20,6 +23,12 @@ |
namespace chromeos { |
namespace settings { |
+namespace { |
+ |
+void onRemovedPrinter(bool success) {} |
+ |
+} // namespace |
+ |
CupsPrintersHandler::CupsPrintersHandler(content::WebUI* webui) |
: profile_(Profile::FromWebUI(webui)), weak_factory_(this) {} |
@@ -38,6 +47,9 @@ void CupsPrintersHandler::RegisterMessages() { |
"removeCupsPrinter", |
base::Bind(&CupsPrintersHandler::HandleRemoveCupsPrinter, |
base::Unretained(this))); |
+ web_ui()->RegisterMessageCallback( |
+ "addCupsPrinter", base::Bind(&CupsPrintersHandler::HandleAddCupsPrinter, |
+ base::Unretained(this))); |
} |
void CupsPrintersHandler::HandleGetCupsPrintersList( |
@@ -104,9 +116,79 @@ void CupsPrintersHandler::HandleUpdateCupsPrinter(const base::ListValue* args) { |
void CupsPrintersHandler::HandleRemoveCupsPrinter(const base::ListValue* args) { |
std::string printer_id; |
+ std::string printer_name; |
CHECK(args->GetString(0, &printer_id)); |
+ CHECK(args->GetString(1, &printer_name)); |
PrinterPrefManagerFactory::GetForBrowserContext(profile_)->RemovePrinter( |
printer_id); |
+ |
+ chromeos::DebugDaemonClient* client = |
+ chromeos::DBusThreadManager::Get()->GetDebugDaemonClient(); |
+ client->CupsRemovePrinter(printer_name, base::Bind(&onRemovedPrinter), |
+ base::Bind(&base::DoNothing)); |
+} |
+ |
+void CupsPrintersHandler::HandleAddCupsPrinter(const base::ListValue* args) { |
+ AllowJavascript(); |
+ |
+ const base::DictionaryValue* printer_dict = nullptr; |
+ CHECK(args->GetDictionary(0, &printer_dict)); |
+ |
+ std::string printer_id; |
+ std::string printer_name; |
+ std::string printer_description; |
+ std::string printer_manufacturer; |
+ std::string printer_model; |
+ std::string printer_address; |
+ std::string printer_protocol; |
+ std::string printer_queue; |
+ CHECK(printer_dict->GetString("printerId", &printer_id)); |
+ CHECK(printer_dict->GetString("printerName", &printer_name)); |
+ CHECK(printer_dict->GetString("printerDescription", &printer_description)); |
+ CHECK(printer_dict->GetString("printerManufacturer", &printer_manufacturer)); |
+ CHECK(printer_dict->GetString("printerModel", &printer_model)); |
+ CHECK(printer_dict->GetString("printerAddress", &printer_address)); |
+ CHECK(printer_dict->GetString("printerProtocol", &printer_protocol)); |
+ CHECK(printer_dict->GetString("printerQueue", &printer_queue)); |
+ std::string printer_uri = |
+ printer_protocol + "://" + printer_address + "/" + printer_queue; |
+ |
+ std::unique_ptr<Printer> printer = base::MakeUnique<Printer>(); |
+ printer->set_id(printer_id); |
+ printer->set_display_name(printer_name); |
+ printer->set_description(printer_description); |
+ printer->set_manufacturer(printer_manufacturer); |
+ printer->set_model(printer_model); |
+ printer->set_uri(printer_uri); |
+ |
+ chromeos::DebugDaemonClient* client = |
+ chromeos::DBusThreadManager::Get()->GetDebugDaemonClient(); |
+ client->CupsAddPrinter( |
+ printer_name, printer_uri, |
+ "", // ppd path, will update it later |
+ true, // ipp everywhere |
+ base::Bind(&CupsPrintersHandler::OnAddedPrinter, |
+ weak_factory_.GetWeakPtr(), base::Passed(std::move(printer))), |
+ base::Bind(&CupsPrintersHandler::OnAddPrinterError, |
+ weak_factory_.GetWeakPtr())); |
+} |
+ |
+void CupsPrintersHandler::OnAddedPrinter(std::unique_ptr<Printer> printer, |
+ bool success) { |
+ std::string printer_name = printer->display_name(); |
+ if (success) { |
+ PrinterPrefManagerFactory::GetForBrowserContext(profile_)->RegisterPrinter( |
+ std::move(printer)); |
+ } |
+ CallJavascriptFunction( |
+ "cr.webUIListenerCallback", base::StringValue("on-add-cups-printer"), |
+ base::FundamentalValue(success), base::StringValue(printer_name)); |
+} |
+ |
+void CupsPrintersHandler::OnAddPrinterError() { |
+ CallJavascriptFunction("cr.webUIListenerCallback", |
michaelpg
2016/09/20 20:58:42
nit: indent this and above consistently?
xdai1
2016/09/21 17:40:20
This is what "git cl format" gives me...
michaelpg
2016/09/21 19:13:23
Acknowledged.
|
+ base::StringValue("on-add-cups-printer"), |
+ base::FundamentalValue(false), base::StringValue("")); |
} |
} // namespace settings |