| 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..c41ed17816b76cf7432425f0508507829c797676 100644
|
| --- a/chrome/browser/ui/webui/settings/chromeos/cups_printers_handler.cc
|
| +++ b/chrome/browser/ui/webui/settings/chromeos/cups_printers_handler.cc
|
| @@ -4,20 +4,49 @@
|
|
|
| #include "chrome/browser/ui/webui/settings/chromeos/cups_printers_handler.h"
|
|
|
| +#include <algorithm>
|
| #include <memory>
|
|
|
| #include "base/bind.h"
|
| #include "base/memory/ptr_util.h"
|
| #include "base/strings/string_util.h"
|
| +#include "base/strings/stringprintf.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"
|
| #include "url/third_party/mozilla/url_parse.h"
|
|
|
| namespace chromeos {
|
| +
|
| +namespace {
|
| +
|
| +struct IdMatches {
|
| + std::string id;
|
| +
|
| + bool operator()(const std::unique_ptr<Printer>& printer) {
|
| + return printer->id() == id;
|
| + }
|
| +};
|
| +
|
| +void OnPrinterAdded(bool success) {
|
| + VLOG(1) << "Printer added " << success;
|
| +}
|
| +
|
| +void OnPrinterRemoved(bool success) {
|
| + VLOG(1) << "Printer removed " << success;
|
| +}
|
| +
|
| +void OperationError(const std::string& label) {
|
| + VLOG(1) << "Operation " << label << " encountered an error";
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| namespace settings {
|
|
|
| CupsPrintersHandler::CupsPrintersHandler(content::WebUI* webui)
|
| @@ -38,6 +67,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(
|
| @@ -93,20 +125,86 @@ void CupsPrintersHandler::HandleGetCupsPrintersList(
|
| void CupsPrintersHandler::HandleUpdateCupsPrinter(const base::ListValue* args) {
|
| std::string printer_id;
|
| std::string printer_name;
|
| + std::string printer_address;
|
| CHECK(args->GetString(0, &printer_id));
|
| CHECK(args->GetString(1, &printer_name));
|
| + CHECK(args->GetString(2, &printer_address));
|
| +
|
| + chromeos::PrinterPrefManager* prefs =
|
| + PrinterPrefManagerFactory::GetForBrowserContext(profile_);
|
| + auto printers = prefs->GetPrinters();
|
| + IdMatches matcher;
|
| + matcher.id = printer_id;
|
| + auto found = std::find_if(printers.begin(), printers.end(), matcher);
|
| + DCHECK(found != printers.end()) << "Printer not found";
|
|
|
| - std::unique_ptr<Printer> printer = base::MakeUnique<Printer>(printer_id);
|
| + std::unique_ptr<Printer>& printer = *found;
|
| printer->set_display_name(printer_name);
|
| - PrinterPrefManagerFactory::GetForBrowserContext(profile_)->RegisterPrinter(
|
| - std::move(printer));
|
| + prefs->RegisterPrinter(std::move(*found));
|
| +
|
| + chromeos::DebugDaemonClient::AddPrinterCallback callback =
|
| + base::Bind(&OnPrinterAdded);
|
| + base::Closure error_callback = base::Bind(&OperationError, "PRINTER_ADDED");
|
| +
|
| + chromeos::DebugDaemonClient* client =
|
| + chromeos::DBusThreadManager::Get()->GetDebugDaemonClient();
|
| + // TODO(skau): switch to real ppd values when file is availalble.
|
| + client->CupsAddPrinter(printer_name, // name
|
| + base::StringPrintf("ipp://%s/", printer_address.c_str()), // uri
|
| + "", // ppd name
|
| + true, // ipp everywhere
|
| + callback, error_callback);
|
| }
|
|
|
| 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::RemovePrinterCallback callback =
|
| + base::Bind(&OnPrinterRemoved);
|
| + base::Closure error_callback = base::Bind(&OperationError, "PRINTER_REMOVED");
|
| +
|
| + chromeos::DebugDaemonClient* client =
|
| + chromeos::DBusThreadManager::Get()->GetDebugDaemonClient();
|
| + client->CupsRemovePrinter(printer_name, callback, error_callback);
|
| +}
|
| +
|
| +void CupsPrintersHandler::HandleAddCupsPrinter(const base::ListValue* args) {
|
| + 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::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_protocol + "://" + printer_address + "/" +
|
| + printer_queue);
|
| +
|
| + PrinterPrefManagerFactory::GetForBrowserContext(profile_)->RegisterPrinter(
|
| + std::move(printer));
|
| }
|
|
|
| } // namespace settings
|
|
|