| 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/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "chrome/browser/chromeos/printing/printer_pref_manager_factory.h" | 14 #include "chrome/browser/chromeos/printing/printer_pref_manager_factory.h" |
| 15 #include "chrome/browser/download/download_prefs.h" | 15 #include "chrome/browser/download/download_prefs.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/ui/browser_finder.h" | 17 #include "chrome/browser/ui/browser_finder.h" |
| 18 #include "chrome/browser/ui/browser_window.h" | 18 #include "chrome/browser/ui/browser_window.h" |
| 19 #include "chrome/browser/ui/chrome_select_file_policy.h" | 19 #include "chrome/browser/ui/chrome_select_file_policy.h" |
| 20 #include "chromeos/dbus/dbus_thread_manager.h" | 20 #include "chromeos/dbus/dbus_thread_manager.h" |
| 21 #include "chromeos/dbus/debug_daemon_client.h" | 21 #include "chromeos/dbus/debug_daemon_client.h" |
| 22 #include "chromeos/printing/fake_printer_discoverer.h" |
| 22 #include "content/public/browser/browser_context.h" | 23 #include "content/public/browser/browser_context.h" |
| 23 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
| 24 #include "content/public/browser/web_ui.h" | 25 #include "content/public/browser/web_ui.h" |
| 25 #include "printing/backend/print_backend.h" | 26 #include "printing/backend/print_backend.h" |
| 26 #include "url/third_party/mozilla/url_parse.h" | 27 #include "url/third_party/mozilla/url_parse.h" |
| 27 | 28 |
| 28 namespace chromeos { | 29 namespace chromeos { |
| 29 namespace settings { | 30 namespace settings { |
| 30 | 31 |
| 31 namespace { | 32 namespace { |
| 32 | 33 |
| 33 void onRemovedPrinter(bool success) {} | 34 void OnRemovedPrinter(bool success) {} |
| 35 |
| 36 std::unique_ptr<base::DictionaryValue> GetPrinterInfo(const Printer& printer) { |
| 37 std::unique_ptr<base::DictionaryValue> printer_info = |
| 38 base::MakeUnique<base::DictionaryValue>(); |
| 39 printer_info->SetString("printerId", printer.id()); |
| 40 printer_info->SetString("printerName", printer.display_name()); |
| 41 printer_info->SetString("printerDescription", printer.description()); |
| 42 printer_info->SetString("printerManufacturer", printer.manufacturer()); |
| 43 printer_info->SetString("printerModel", printer.model()); |
| 44 |
| 45 // Get protocol, ip address and queue from the printer's URI. |
| 46 const std::string printer_uri = printer.uri(); |
| 47 url::Parsed parsed; |
| 48 url::ParseStandardURL(printer_uri.c_str(), printer_uri.length(), &parsed); |
| 49 |
| 50 std::string scheme; |
| 51 std::string host; |
| 52 std::string path; |
| 53 if (parsed.scheme.len > 0) |
| 54 scheme = std::string(printer_uri, parsed.scheme.begin, parsed.scheme.len); |
| 55 if (parsed.host.len > 0) |
| 56 host = std::string(printer_uri, parsed.host.begin, parsed.host.len); |
| 57 if (parsed.path.len > 0) |
| 58 path = std::string(printer_uri, parsed.path.begin, parsed.path.len); |
| 59 |
| 60 printer_info->SetString("printerAddress", host); |
| 61 printer_info->SetString("printerProtocol", base::ToLowerASCII(scheme)); |
| 62 if (base::ToLowerASCII(scheme) == "lpd" && !path.empty()) |
| 63 printer_info->SetString("printerQueue", path.substr(1)); |
| 64 |
| 65 return printer_info; |
| 66 } |
| 34 | 67 |
| 35 } // namespace | 68 } // namespace |
| 36 | 69 |
| 37 CupsPrintersHandler::CupsPrintersHandler(content::WebUI* webui) | 70 CupsPrintersHandler::CupsPrintersHandler(content::WebUI* webui) |
| 38 : profile_(Profile::FromWebUI(webui)), weak_factory_(this) {} | 71 : printer_discoverer_(nullptr), |
| 72 profile_(Profile::FromWebUI(webui)), |
| 73 weak_factory_(this) {} |
| 39 | 74 |
| 40 CupsPrintersHandler::~CupsPrintersHandler() {} | 75 CupsPrintersHandler::~CupsPrintersHandler() {} |
| 41 | 76 |
| 42 void CupsPrintersHandler::RegisterMessages() { | 77 void CupsPrintersHandler::RegisterMessages() { |
| 43 web_ui()->RegisterMessageCallback( | 78 web_ui()->RegisterMessageCallback( |
| 44 "getCupsPrintersList", | 79 "getCupsPrintersList", |
| 45 base::Bind(&CupsPrintersHandler::HandleGetCupsPrintersList, | 80 base::Bind(&CupsPrintersHandler::HandleGetCupsPrintersList, |
| 46 base::Unretained(this))); | 81 base::Unretained(this))); |
| 47 web_ui()->RegisterMessageCallback( | 82 web_ui()->RegisterMessageCallback( |
| 48 "updateCupsPrinter", | 83 "updateCupsPrinter", |
| 49 base::Bind(&CupsPrintersHandler::HandleUpdateCupsPrinter, | 84 base::Bind(&CupsPrintersHandler::HandleUpdateCupsPrinter, |
| 50 base::Unretained(this))); | 85 base::Unretained(this))); |
| 51 web_ui()->RegisterMessageCallback( | 86 web_ui()->RegisterMessageCallback( |
| 52 "removeCupsPrinter", | 87 "removeCupsPrinter", |
| 53 base::Bind(&CupsPrintersHandler::HandleRemoveCupsPrinter, | 88 base::Bind(&CupsPrintersHandler::HandleRemoveCupsPrinter, |
| 54 base::Unretained(this))); | 89 base::Unretained(this))); |
| 55 web_ui()->RegisterMessageCallback( | 90 web_ui()->RegisterMessageCallback( |
| 56 "addCupsPrinter", base::Bind(&CupsPrintersHandler::HandleAddCupsPrinter, | 91 "addCupsPrinter", base::Bind(&CupsPrintersHandler::HandleAddCupsPrinter, |
| 57 base::Unretained(this))); | 92 base::Unretained(this))); |
| 58 web_ui()->RegisterMessageCallback( | 93 web_ui()->RegisterMessageCallback( |
| 59 "selectPPDFile", base::Bind(&CupsPrintersHandler::HandleSelectPPDFile, | 94 "selectPPDFile", base::Bind(&CupsPrintersHandler::HandleSelectPPDFile, |
| 60 base::Unretained(this))); | 95 base::Unretained(this))); |
| 96 web_ui()->RegisterMessageCallback( |
| 97 "startDiscoveringPrinters", |
| 98 base::Bind(&CupsPrintersHandler::HandleStartDiscovery, |
| 99 base::Unretained(this))); |
| 100 web_ui()->RegisterMessageCallback( |
| 101 "stopDiscoveringPrinters", |
| 102 base::Bind(&CupsPrintersHandler::HandleStopDiscovery, |
| 103 base::Unretained(this))); |
| 61 } | 104 } |
| 62 | 105 |
| 63 void CupsPrintersHandler::HandleGetCupsPrintersList( | 106 void CupsPrintersHandler::HandleGetCupsPrintersList( |
| 64 const base::ListValue* args) { | 107 const base::ListValue* args) { |
| 65 AllowJavascript(); | 108 AllowJavascript(); |
| 66 | 109 |
| 67 CHECK_EQ(1U, args->GetSize()); | 110 CHECK_EQ(1U, args->GetSize()); |
| 68 std::string callback_id; | 111 std::string callback_id; |
| 69 CHECK(args->GetString(0, &callback_id)); | 112 CHECK(args->GetString(0, &callback_id)); |
| 70 | 113 |
| 71 std::vector<std::unique_ptr<Printer>> printers = | 114 std::vector<std::unique_ptr<Printer>> printers = |
| 72 PrinterPrefManagerFactory::GetForBrowserContext(profile_)->GetPrinters(); | 115 PrinterPrefManagerFactory::GetForBrowserContext(profile_)->GetPrinters(); |
| 73 | 116 |
| 74 base::ListValue* printers_list = new base::ListValue; | 117 base::ListValue* printers_list = new base::ListValue; |
| 75 for (const std::unique_ptr<Printer>& printer : printers) { | 118 for (const std::unique_ptr<Printer>& printer : printers) { |
| 76 std::unique_ptr<base::DictionaryValue> printer_info = | 119 std::unique_ptr<base::DictionaryValue> printer_info = |
| 77 base::MakeUnique<base::DictionaryValue>(); | 120 GetPrinterInfo(*printer.get()); |
| 78 printer_info->SetString("printerId", printer->id()); | |
| 79 printer_info->SetString("printerName", printer->display_name()); | |
| 80 printer_info->SetString("printerDescription", printer->description()); | |
| 81 printer_info->SetString("printerManufacturer", printer->manufacturer()); | |
| 82 printer_info->SetString("printerModel", printer->model()); | |
| 83 | |
| 84 // Get protocol, ip address and queue from the printer's URI. | |
| 85 const std::string printer_uri = printer->uri(); | |
| 86 url::Parsed parsed; | |
| 87 url::ParseStandardURL(printer_uri.c_str(), printer_uri.length(), &parsed); | |
| 88 | |
| 89 std::string scheme; | |
| 90 std::string host; | |
| 91 std::string path; | |
| 92 if (parsed.scheme.len > 0) | |
| 93 scheme = std::string(printer_uri, parsed.scheme.begin, parsed.scheme.len); | |
| 94 if (parsed.host.len > 0) | |
| 95 host = std::string(printer_uri, parsed.host.begin, parsed.host.len); | |
| 96 if (parsed.path.len > 0) | |
| 97 path = std::string(printer_uri, parsed.path.begin, parsed.path.len); | |
| 98 | |
| 99 printer_info->SetString("printerAddress", host); | |
| 100 printer_info->SetString("printerProtocol", base::ToLowerASCII(scheme)); | |
| 101 if (base::ToLowerASCII(scheme) == "lpd" && !path.empty()) | |
| 102 printer_info->SetString("printerQueue", path.substr(1)); | |
| 103 | |
| 104 printers_list->Append(std::move(printer_info)); | 121 printers_list->Append(std::move(printer_info)); |
| 105 } | 122 } |
| 106 | 123 |
| 107 std::unique_ptr<base::DictionaryValue> response = | 124 std::unique_ptr<base::DictionaryValue> response = |
| 108 base::MakeUnique<base::DictionaryValue>(); | 125 base::MakeUnique<base::DictionaryValue>(); |
| 109 response->Set("printerList", printers_list); | 126 response->Set("printerList", printers_list); |
| 110 ResolveJavascriptCallback(base::StringValue(callback_id), *response); | 127 ResolveJavascriptCallback(base::StringValue(callback_id), *response); |
| 111 } | 128 } |
| 112 | 129 |
| 113 void CupsPrintersHandler::HandleUpdateCupsPrinter(const base::ListValue* args) { | 130 void CupsPrintersHandler::HandleUpdateCupsPrinter(const base::ListValue* args) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 125 void CupsPrintersHandler::HandleRemoveCupsPrinter(const base::ListValue* args) { | 142 void CupsPrintersHandler::HandleRemoveCupsPrinter(const base::ListValue* args) { |
| 126 std::string printer_id; | 143 std::string printer_id; |
| 127 std::string printer_name; | 144 std::string printer_name; |
| 128 CHECK(args->GetString(0, &printer_id)); | 145 CHECK(args->GetString(0, &printer_id)); |
| 129 CHECK(args->GetString(1, &printer_name)); | 146 CHECK(args->GetString(1, &printer_name)); |
| 130 PrinterPrefManagerFactory::GetForBrowserContext(profile_)->RemovePrinter( | 147 PrinterPrefManagerFactory::GetForBrowserContext(profile_)->RemovePrinter( |
| 131 printer_id); | 148 printer_id); |
| 132 | 149 |
| 133 chromeos::DebugDaemonClient* client = | 150 chromeos::DebugDaemonClient* client = |
| 134 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient(); | 151 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient(); |
| 135 client->CupsRemovePrinter(printer_name, base::Bind(&onRemovedPrinter), | 152 client->CupsRemovePrinter(printer_name, base::Bind(&OnRemovedPrinter), |
| 136 base::Bind(&base::DoNothing)); | 153 base::Bind(&base::DoNothing)); |
| 137 } | 154 } |
| 138 | 155 |
| 139 void CupsPrintersHandler::HandleAddCupsPrinter(const base::ListValue* args) { | 156 void CupsPrintersHandler::HandleAddCupsPrinter(const base::ListValue* args) { |
| 140 AllowJavascript(); | 157 AllowJavascript(); |
| 141 | 158 |
| 142 const base::DictionaryValue* printer_dict = nullptr; | 159 const base::DictionaryValue* printer_dict = nullptr; |
| 143 CHECK(args->GetDictionary(0, &printer_dict)); | 160 CHECK(args->GetDictionary(0, &printer_dict)); |
| 144 | 161 |
| 145 std::string printer_id; | 162 std::string printer_id; |
| 146 std::string printer_name; | 163 std::string printer_name; |
| 147 std::string printer_description; | 164 std::string printer_description; |
| 148 std::string printer_manufacturer; | 165 std::string printer_manufacturer; |
| 149 std::string printer_model; | 166 std::string printer_model; |
| 150 std::string printer_address; | 167 std::string printer_address; |
| 151 std::string printer_protocol; | 168 std::string printer_protocol; |
| 152 std::string printer_queue; | 169 std::string printer_queue; |
| 153 std::string printer_ppd_path; | 170 std::string printer_ppd_path; |
| 154 CHECK(printer_dict->GetString("printerId", &printer_id)); | 171 CHECK(printer_dict->GetString("printerId", &printer_id)); |
| 155 CHECK(printer_dict->GetString("printerName", &printer_name)); | 172 CHECK(printer_dict->GetString("printerName", &printer_name)); |
| 156 CHECK(printer_dict->GetString("printerDescription", &printer_description)); | 173 CHECK(printer_dict->GetString("printerDescription", &printer_description)); |
| 157 CHECK(printer_dict->GetString("printerManufacturer", &printer_manufacturer)); | 174 CHECK(printer_dict->GetString("printerManufacturer", &printer_manufacturer)); |
| 158 CHECK(printer_dict->GetString("printerModel", &printer_model)); | 175 CHECK(printer_dict->GetString("printerModel", &printer_model)); |
| 159 CHECK(printer_dict->GetString("printerAddress", &printer_address)); | 176 CHECK(printer_dict->GetString("printerAddress", &printer_address)); |
| 160 CHECK(printer_dict->GetString("printerProtocol", &printer_protocol)); | 177 CHECK(printer_dict->GetString("printerProtocol", &printer_protocol)); |
| 161 CHECK(printer_dict->GetString("printerQueue", &printer_queue)); | 178 // printerQueue might be null for a printer whose protocol is not 'LPD'. |
| 162 CHECK(printer_dict->GetString("printerPPDPath", &printer_ppd_path)); | 179 printer_dict->GetString("printerQueue", &printer_queue); |
| 180 // printerPPDPath might be null for an auto-discovered printer. |
| 181 printer_dict->GetString("printerPPDPath", &printer_ppd_path); |
| 163 std::string printer_uri = | 182 std::string printer_uri = |
| 164 printer_protocol + "://" + printer_address + "/" + printer_queue; | 183 printer_protocol + "://" + printer_address + "/" + printer_queue; |
| 165 | 184 |
| 166 std::unique_ptr<Printer> printer = base::MakeUnique<Printer>(); | 185 std::unique_ptr<Printer> printer = base::MakeUnique<Printer>(); |
| 167 printer->set_id(printer_id); | 186 printer->set_id(printer_id); |
| 168 printer->set_display_name(printer_name); | 187 printer->set_display_name(printer_name); |
| 169 printer->set_description(printer_description); | 188 printer->set_description(printer_description); |
| 170 printer->set_manufacturer(printer_manufacturer); | 189 printer->set_manufacturer(printer_manufacturer); |
| 171 printer->set_model(printer_model); | 190 printer->set_model(printer_model); |
| 172 printer->set_uri(printer_uri); | 191 printer->set_uri(printer_uri); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 | 240 |
| 222 void CupsPrintersHandler::FileSelected(const base::FilePath& path, | 241 void CupsPrintersHandler::FileSelected(const base::FilePath& path, |
| 223 int index, | 242 int index, |
| 224 void* params) { | 243 void* params) { |
| 225 DCHECK(!webui_callback_id_.empty()); | 244 DCHECK(!webui_callback_id_.empty()); |
| 226 ResolveJavascriptCallback(base::StringValue(webui_callback_id_), | 245 ResolveJavascriptCallback(base::StringValue(webui_callback_id_), |
| 227 base::StringValue(path.value())); | 246 base::StringValue(path.value())); |
| 228 webui_callback_id_.clear(); | 247 webui_callback_id_.clear(); |
| 229 } | 248 } |
| 230 | 249 |
| 250 void CupsPrintersHandler::HandleStartDiscovery(const base::ListValue* args) { |
| 251 if (!printer_discoverer_.get()) |
| 252 printer_discoverer_ = chromeos::PrinterDiscoverer::Create(); |
| 253 |
| 254 printer_discoverer_->AddObserver(this); |
| 255 if (!printer_discoverer_->StartDiscovery()) { |
| 256 CallJavascriptFunction("cr.webUIListenerCallback", |
| 257 base::StringValue("on-printer-discovery-failed")); |
| 258 printer_discoverer_->RemoveObserver(this); |
| 259 } |
| 260 } |
| 261 |
| 262 void CupsPrintersHandler::HandleStopDiscovery(const base::ListValue* args) { |
| 263 if (printer_discoverer_.get()) { |
| 264 printer_discoverer_->RemoveObserver(this); |
| 265 printer_discoverer_->StopDiscovery(); |
| 266 printer_discoverer_.reset(); |
| 267 } |
| 268 } |
| 269 |
| 270 void CupsPrintersHandler::OnPrintersFound( |
| 271 const std::vector<Printer>& printers) { |
| 272 std::unique_ptr<base::ListValue> printers_list = |
| 273 base::MakeUnique<base::ListValue>(); |
| 274 for (const auto& printer : printers) { |
| 275 std::unique_ptr<base::DictionaryValue> printer_info = |
| 276 GetPrinterInfo(printer); |
| 277 printers_list->Append(std::move(printer_info)); |
| 278 } |
| 279 |
| 280 CallJavascriptFunction("cr.webUIListenerCallback", |
| 281 base::StringValue("on-printer-discovered"), |
| 282 *printers_list); |
| 283 } |
| 284 |
| 285 void CupsPrintersHandler::OnDiscoveryDone() { |
| 286 CallJavascriptFunction("cr.webUIListenerCallback", |
| 287 base::StringValue("on-printer-discovery-done")); |
| 288 } |
| 289 |
| 231 } // namespace settings | 290 } // namespace settings |
| 232 } // namespace chromeos | 291 } // namespace chromeos |
| OLD | NEW |