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 #include "url/gurl.h" |
| 16 namespace { | |
| 17 | |
| 18 // TODO(xdai): Retrieve the CUPS printers list from user preference instead. | |
| 19 void EnumeratePrintersOnBlockingPoolThread(base::ListValue* printers) { | |
| 20 scoped_refptr<printing::PrintBackend> print_backend( | |
| 21 printing::PrintBackend::CreateInstance(nullptr)); | |
| 22 | |
| 23 printing::PrinterList printer_list; | |
| 24 print_backend->EnumeratePrinters(&printer_list); | |
| 25 | |
| 26 for (const printing::PrinterBasicInfo& printer : printer_list) { | |
| 27 std::unique_ptr<base::DictionaryValue> printer_info( | |
| 28 new base::DictionaryValue); | |
| 29 printer_info->SetString("printerName", printer.printer_name); | |
| 30 printer_info->SetString("printerDescription", printer.printer_description); | |
| 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 } | |
| 38 } | |
| 39 | |
| 40 } // namespace | |
| 41 | 19 |
| 42 namespace chromeos { | 20 namespace chromeos { |
| 43 namespace settings { | 21 namespace settings { |
| 44 | 22 |
| 45 CupsPrintersHandler::CupsPrintersHandler() : weak_factory_(this) {} | 23 CupsPrintersHandler::CupsPrintersHandler(content::WebUI* webui) |
| 24 : profile_(Profile::FromWebUI(webui)), weak_factory_(this) {} | |
| 46 | 25 |
| 47 CupsPrintersHandler::~CupsPrintersHandler() {} | 26 CupsPrintersHandler::~CupsPrintersHandler() {} |
| 48 | 27 |
| 49 void CupsPrintersHandler::RegisterMessages() { | 28 void CupsPrintersHandler::RegisterMessages() { |
| 50 web_ui()->RegisterMessageCallback( | 29 web_ui()->RegisterMessageCallback( |
| 51 "getCupsPrintersList", | 30 "getCupsPrintersList", |
| 52 base::Bind(&CupsPrintersHandler::HandleGetCupsPrintersList, | 31 base::Bind(&CupsPrintersHandler::HandleGetCupsPrintersList, |
| 53 base::Unretained(this))); | 32 base::Unretained(this))); |
| 54 web_ui()->RegisterMessageCallback( | 33 web_ui()->RegisterMessageCallback( |
| 55 "updateCupsPrinter", | 34 "updateCupsPrinter", |
| 56 base::Bind(&CupsPrintersHandler::HandleUpdateCupsPrinter, | 35 base::Bind(&CupsPrintersHandler::HandleUpdateCupsPrinter, |
| 57 base::Unretained(this))); | 36 base::Unretained(this))); |
| 58 web_ui()->RegisterMessageCallback( | 37 web_ui()->RegisterMessageCallback( |
| 59 "removeCupsPrinter", | 38 "removeCupsPrinter", |
| 60 base::Bind(&CupsPrintersHandler::HandleRemoveCupsPrinter, | 39 base::Bind(&CupsPrintersHandler::HandleRemoveCupsPrinter, |
| 61 base::Unretained(this))); | 40 base::Unretained(this))); |
| 62 } | 41 } |
| 63 | 42 |
| 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( | 43 void CupsPrintersHandler::HandleGetCupsPrintersList( |
| 67 const base::ListValue* args) { | 44 const base::ListValue* args) { |
| 68 AllowJavascript(); | 45 AllowJavascript(); |
| 69 | 46 |
| 70 CHECK_EQ(1U, args->GetSize()); | 47 CHECK_EQ(1U, args->GetSize()); |
| 71 std::string callback_id; | 48 std::string callback_id; |
| 72 CHECK(args->GetString(0, &callback_id)); | 49 CHECK(args->GetString(0, &callback_id)); |
| 73 | 50 |
| 51 std::vector<std::unique_ptr<Printer>> printers = | |
| 52 PrinterPrefManagerFactory::GetForBrowserContext(profile_)->GetPrinters(); | |
| 53 | |
| 74 base::ListValue* printers_list = new base::ListValue; | 54 base::ListValue* printers_list = new base::ListValue; |
| 75 content::BrowserThread::PostBlockingPoolTaskAndReply( | 55 for (const std::unique_ptr<Printer>& printer : printers) { |
| 76 FROM_HERE, base::Bind(&EnumeratePrintersOnBlockingPoolThread, | 56 std::unique_ptr<base::DictionaryValue> printer_info = |
| 77 base::Unretained(printers_list)), | 57 base::MakeUnique<base::DictionaryValue>(); |
| 78 base::Bind(&CupsPrintersHandler::OnGetCupsPrintersList, | 58 printer_info->SetString("printerId", printer->id()); |
| 79 weak_factory_.GetWeakPtr(), callback_id, | 59 printer_info->SetString("printerName", printer->display_name()); |
| 80 base::Owned(printers_list))); | 60 printer_info->SetString("printerDescription", printer->description()); |
| 81 } | 61 printer_info->SetString("printerManufacturer", printer->manufacturer()); |
| 62 printer_info->SetString("printerModel", printer->model()); | |
| 82 | 63 |
| 83 void CupsPrintersHandler::OnGetCupsPrintersList( | 64 // Get protocol, ip address and queue from the printer's URI. |
| 84 const std::string callback_id, | 65 const GURL gurl(printer->uri()); |
| 85 base::ListValue* printers_list) { | 66 printer_info->SetString("printerAddress", gurl.host()); |
| 67 printer_info->SetString("printerProtocol", gurl.scheme()); | |
| 68 if (base::ToLowerASCII(gurl.scheme()) == "lpd") | |
| 69 printer_info->SetString("printerQueue", gurl.path().substr(1)); | |
| 70 | |
| 71 printers_list->Append(std::move(printer_info)); | |
| 72 } | |
| 73 | |
| 86 std::unique_ptr<base::DictionaryValue> response(new base::DictionaryValue()); | 74 std::unique_ptr<base::DictionaryValue> response(new base::DictionaryValue()); |
|
skau
2016/08/19 00:08:45
base::MakeUnique...
xdai1
2016/08/19 21:09:17
Done.
| |
| 87 response->Set("printerList", printers_list->DeepCopy()); | 75 response->Set("printerList", printers_list); |
| 88 ResolveJavascriptCallback(base::StringValue(callback_id), *response); | 76 ResolveJavascriptCallback(base::StringValue(callback_id), *response); |
| 89 } | 77 } |
| 90 | 78 |
| 91 void CupsPrintersHandler::HandleUpdateCupsPrinter(const base::ListValue* args) { | 79 void CupsPrintersHandler::HandleUpdateCupsPrinter(const base::ListValue* args) { |
| 92 // TODO(xdai): Implement it after https://codereview.chromium.org/2161933003/ | 80 std::string printer_id; |
| 93 // is landed. | 81 std::string printer_name; |
| 82 CHECK(args->GetString(0, &printer_id)); | |
| 83 CHECK(args->GetString(1, &printer_name)); | |
| 84 | |
| 85 std::unique_ptr<Printer> printer = base::MakeUnique<Printer>(printer_id); | |
| 86 printer->set_display_name(printer_name); | |
| 87 PrinterPrefManagerFactory::GetForBrowserContext(profile_)->RegisterPrinter( | |
| 88 std::move(printer)); | |
| 94 } | 89 } |
| 95 | 90 |
| 96 void CupsPrintersHandler::HandleRemoveCupsPrinter(const base::ListValue* args) { | 91 void CupsPrintersHandler::HandleRemoveCupsPrinter(const base::ListValue* args) { |
| 97 // TODO(xdai): Implement it after https://codereview.chromium.org/2161933003/ | 92 std::string printer_id; |
| 98 // is landed. | 93 CHECK(args->GetString(0, &printer_id)); |
| 94 PrinterPrefManagerFactory::GetForBrowserContext(profile_)->RemovePrinter( | |
| 95 printer_id); | |
| 99 } | 96 } |
| 100 | 97 |
| 101 } // namespace settings | 98 } // namespace settings |
| 102 } // namespace chromeos | 99 } // namespace chromeos |
| OLD | NEW |