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 <utility> | 7 #include <utility> |
| 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/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/json/json_string_value_serializer.h" | 12 #include "base/json/json_string_value_serializer.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
| 15 #include "base/path_service.h" | 15 #include "base/path_service.h" |
| 16 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 17 #include "base/threading/sequenced_task_runner_handle.h" | 17 #include "base/threading/sequenced_task_runner_handle.h" |
| 18 #include "base/values.h" | 18 #include "base/values.h" |
| 19 #include "chrome/browser/browser_process.h" | 19 #include "chrome/browser/browser_process.h" |
| 20 #include "chrome/browser/chromeos/printing/ppd_provider_factory.h" | 20 #include "chrome/browser/chromeos/printing/ppd_provider_factory.h" |
| 21 #include "chrome/browser/chromeos/printing/printer_configurer.h" | 21 #include "chrome/browser/chromeos/printing/printer_configurer.h" |
| 22 #include "chrome/browser/chromeos/printing/printer_discoverer.h" | 22 #include "chrome/browser/chromeos/printing/printer_discoverer.h" |
| 23 #include "chrome/browser/chromeos/printing/printer_info.h" | |
| 23 #include "chrome/browser/chromeos/printing/printers_manager_factory.h" | 24 #include "chrome/browser/chromeos/printing/printers_manager_factory.h" |
| 24 #include "chrome/browser/download/download_prefs.h" | 25 #include "chrome/browser/download/download_prefs.h" |
| 25 #include "chrome/browser/profiles/profile.h" | 26 #include "chrome/browser/profiles/profile.h" |
| 26 #include "chrome/browser/ui/browser_finder.h" | 27 #include "chrome/browser/ui/browser_finder.h" |
| 27 #include "chrome/browser/ui/browser_window.h" | 28 #include "chrome/browser/ui/browser_window.h" |
| 28 #include "chrome/browser/ui/chrome_select_file_policy.h" | 29 #include "chrome/browser/ui/chrome_select_file_policy.h" |
| 29 #include "chrome/common/chrome_paths.h" | 30 #include "chrome/common/chrome_paths.h" |
| 30 #include "chromeos/dbus/dbus_thread_manager.h" | 31 #include "chromeos/dbus/dbus_thread_manager.h" |
| 31 #include "chromeos/dbus/debug_daemon_client.h" | 32 #include "chromeos/dbus/debug_daemon_client.h" |
| 32 #include "chromeos/printing/ppd_cache.h" | 33 #include "chromeos/printing/ppd_cache.h" |
| 33 #include "chromeos/printing/ppd_provider.h" | 34 #include "chromeos/printing/ppd_provider.h" |
| 34 #include "content/public/browser/browser_context.h" | 35 #include "content/public/browser/browser_context.h" |
| 35 #include "content/public/browser/browser_thread.h" | 36 #include "content/public/browser/browser_thread.h" |
| 36 #include "content/public/browser/web_ui.h" | 37 #include "content/public/browser/web_ui.h" |
| 37 #include "google_apis/google_api_keys.h" | 38 #include "google_apis/google_api_keys.h" |
| 38 #include "net/base/filename_util.h" | 39 #include "net/base/filename_util.h" |
| 39 #include "net/url_request/url_request_context_getter.h" | 40 #include "net/url_request/url_request_context_getter.h" |
| 40 #include "printing/backend/print_backend.h" | 41 #include "printing/backend/print_backend.h" |
| 41 #include "url/third_party/mozilla/url_parse.h" | 42 #include "url/third_party/mozilla/url_parse.h" |
| 42 | 43 |
| 43 namespace chromeos { | 44 namespace chromeos { |
| 44 namespace settings { | 45 namespace settings { |
| 45 | 46 |
| 46 namespace { | 47 namespace { |
| 47 | 48 |
| 49 const char kIppScheme[] = "ipp"; | |
| 50 const char kIppsScheme[] = "ipps"; | |
| 51 | |
| 48 void OnRemovedPrinter(bool success) {} | 52 void OnRemovedPrinter(bool success) {} |
| 49 | 53 |
| 50 std::unique_ptr<base::DictionaryValue> GetPrinterInfo(const Printer& printer) { | 54 std::unique_ptr<base::DictionaryValue> GetPrinterInfo(const Printer& printer) { |
| 51 std::unique_ptr<base::DictionaryValue> printer_info = | 55 std::unique_ptr<base::DictionaryValue> printer_info = |
| 52 base::MakeUnique<base::DictionaryValue>(); | 56 base::MakeUnique<base::DictionaryValue>(); |
| 53 printer_info->SetString("printerId", printer.id()); | 57 printer_info->SetString("printerId", printer.id()); |
| 54 printer_info->SetString("printerName", printer.display_name()); | 58 printer_info->SetString("printerName", printer.display_name()); |
| 55 printer_info->SetString("printerDescription", printer.description()); | 59 printer_info->SetString("printerDescription", printer.description()); |
| 56 printer_info->SetString("printerManufacturer", printer.manufacturer()); | 60 printer_info->SetString("printerManufacturer", printer.manufacturer()); |
| 57 printer_info->SetString("printerModel", printer.model()); | 61 printer_info->SetString("printerModel", printer.model()); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 109 base::Bind(&CupsPrintersHandler::HandleUpdateCupsPrinter, | 113 base::Bind(&CupsPrintersHandler::HandleUpdateCupsPrinter, |
| 110 base::Unretained(this))); | 114 base::Unretained(this))); |
| 111 web_ui()->RegisterMessageCallback( | 115 web_ui()->RegisterMessageCallback( |
| 112 "removeCupsPrinter", | 116 "removeCupsPrinter", |
| 113 base::Bind(&CupsPrintersHandler::HandleRemoveCupsPrinter, | 117 base::Bind(&CupsPrintersHandler::HandleRemoveCupsPrinter, |
| 114 base::Unretained(this))); | 118 base::Unretained(this))); |
| 115 web_ui()->RegisterMessageCallback( | 119 web_ui()->RegisterMessageCallback( |
| 116 "addCupsPrinter", base::Bind(&CupsPrintersHandler::HandleAddCupsPrinter, | 120 "addCupsPrinter", base::Bind(&CupsPrintersHandler::HandleAddCupsPrinter, |
| 117 base::Unretained(this))); | 121 base::Unretained(this))); |
| 118 web_ui()->RegisterMessageCallback( | 122 web_ui()->RegisterMessageCallback( |
| 123 "getPrinterInfo", base::Bind(&CupsPrintersHandler::HandleGetPrinterInfo, | |
| 124 base::Unretained(this))); | |
| 125 web_ui()->RegisterMessageCallback( | |
| 119 "getCupsPrinterManufacturersList", | 126 "getCupsPrinterManufacturersList", |
| 120 base::Bind(&CupsPrintersHandler::HandleGetCupsPrinterManufacturers, | 127 base::Bind(&CupsPrintersHandler::HandleGetCupsPrinterManufacturers, |
| 121 base::Unretained(this))); | 128 base::Unretained(this))); |
| 122 web_ui()->RegisterMessageCallback( | 129 web_ui()->RegisterMessageCallback( |
| 123 "getCupsPrinterModelsList", | 130 "getCupsPrinterModelsList", |
| 124 base::Bind(&CupsPrintersHandler::HandleGetCupsPrinterModels, | 131 base::Bind(&CupsPrintersHandler::HandleGetCupsPrinterModels, |
| 125 base::Unretained(this))); | 132 base::Unretained(this))); |
| 126 web_ui()->RegisterMessageCallback( | 133 web_ui()->RegisterMessageCallback( |
| 127 "selectPPDFile", base::Bind(&CupsPrintersHandler::HandleSelectPPDFile, | 134 "selectPPDFile", base::Bind(&CupsPrintersHandler::HandleSelectPPDFile, |
| 128 base::Unretained(this))); | 135 base::Unretained(this))); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 178 CHECK(args->GetString(1, &printer_name)); | 185 CHECK(args->GetString(1, &printer_name)); |
| 179 PrintersManagerFactory::GetForBrowserContext(profile_)->RemovePrinter( | 186 PrintersManagerFactory::GetForBrowserContext(profile_)->RemovePrinter( |
| 180 printer_id); | 187 printer_id); |
| 181 | 188 |
| 182 chromeos::DebugDaemonClient* client = | 189 chromeos::DebugDaemonClient* client = |
| 183 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient(); | 190 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient(); |
| 184 client->CupsRemovePrinter(printer_name, base::Bind(&OnRemovedPrinter), | 191 client->CupsRemovePrinter(printer_name, base::Bind(&OnRemovedPrinter), |
| 185 base::Bind(&base::DoNothing)); | 192 base::Bind(&base::DoNothing)); |
| 186 } | 193 } |
| 187 | 194 |
| 195 void CupsPrintersHandler::HandleGetPrinterInfo(const base::ListValue* args) { | |
| 196 DCHECK(args); | |
| 197 std::string callback_id; | |
| 198 if (!args->GetString(0, &callback_id)) { | |
| 199 NOTREACHED() << "Expected request for a promise"; | |
| 200 return; | |
| 201 } | |
| 202 | |
| 203 const base::DictionaryValue* printer_dict = nullptr; | |
| 204 if (!args->GetDictionary(1, &printer_dict)) { | |
| 205 NOTREACHED() << "Dictionary missing"; | |
| 206 return; | |
| 207 } | |
| 208 | |
| 209 AllowJavascript(); | |
| 210 | |
| 211 std::string printer_address; | |
| 212 if (!printer_dict->GetString("printerAddress", &printer_address)) { | |
| 213 NOTREACHED() << "Address missing"; | |
| 214 return; | |
| 215 } | |
| 216 | |
| 217 if (printer_address.empty()) { | |
| 218 // Run the failure callback. | |
|
Carlson
2017/05/31 16:58:52
I'm confused about when you do and don't run the f
skau
2017/06/01 21:50:29
We run the failure callback for expected failures.
| |
| 219 OnPrinterInfo(callback_id, false, "", "", false); | |
| 220 return; | |
| 221 } | |
| 222 | |
| 223 std::string printer_queue; | |
| 224 printer_dict->GetString("printerQueue", &printer_queue); | |
| 225 | |
| 226 std::string printer_protocol; | |
| 227 if (!printer_dict->GetString("printerProtocol", &printer_protocol)) { | |
| 228 NOTREACHED() << "Protocol missing"; | |
| 229 return; | |
| 230 } | |
| 231 | |
| 232 std::string printer_uri = printer_protocol + url::kStandardSchemeSeparator + | |
| 233 printer_address + "/" + printer_queue; | |
|
Carlson
2017/05/31 16:58:52
I don't understand why you assemble a uri and then
skau
2017/06/01 21:50:29
It is just for the port so the whole uri doesn't n
skau
2017/06/07 23:55:39
So, I was wrong. If there's no scheme (aka protoc
| |
| 234 const char* uri_ptr = printer_uri.c_str(); | |
| 235 url::Parsed parsed; | |
| 236 url::ParseStandardURL(uri_ptr, printer_uri.length(), &parsed); | |
| 237 base::StringPiece host(&uri_ptr[parsed.host.begin], parsed.host.len); | |
| 238 base::StringPiece path(&uri_ptr[parsed.path.begin], parsed.path.len); | |
| 239 | |
| 240 int port = ParsePort(uri_ptr, parsed.port); | |
| 241 if (port == url::SpecialPort::PORT_UNSPECIFIED || | |
| 242 port == url::SpecialPort::PORT_INVALID) { | |
| 243 // imply port from protocol | |
| 244 if (printer_protocol == kIppScheme) { | |
| 245 port = 631; | |
| 246 } else if (printer_protocol == kIppsScheme) { | |
| 247 // ipps is ipp over https so it uses the https port. | |
| 248 port = 443; | |
| 249 } else { | |
| 250 NOTREACHED() << "Unrecognized protocol. Port was not set."; | |
| 251 } | |
| 252 } | |
| 253 | |
| 254 ::chromeos::QueryIppPrinter( | |
| 255 host.as_string(), port, path.as_string(), | |
| 256 base::Bind(&CupsPrintersHandler::OnPrinterInfo, | |
| 257 weak_factory_.GetWeakPtr(), callback_id)); | |
| 258 } | |
| 259 | |
| 188 void CupsPrintersHandler::HandleAddCupsPrinter(const base::ListValue* args) { | 260 void CupsPrintersHandler::HandleAddCupsPrinter(const base::ListValue* args) { |
| 189 AllowJavascript(); | 261 AllowJavascript(); |
| 190 | 262 |
| 191 const base::DictionaryValue* printer_dict = nullptr; | 263 const base::DictionaryValue* printer_dict = nullptr; |
| 192 CHECK(args->GetDictionary(0, &printer_dict)); | 264 CHECK(args->GetDictionary(0, &printer_dict)); |
| 193 | 265 |
| 194 std::string printer_id; | 266 std::string printer_id; |
| 195 std::string printer_name; | 267 std::string printer_name; |
| 196 std::string printer_description; | 268 std::string printer_description; |
| 197 std::string printer_manufacturer; | 269 std::string printer_manufacturer; |
| 198 std::string printer_model; | 270 std::string printer_model; |
| 199 std::string printer_address; | 271 std::string printer_address; |
| 200 std::string printer_protocol; | 272 std::string printer_protocol; |
| 201 std::string printer_queue; | 273 std::string printer_queue; |
| 202 std::string printer_ppd_path; | 274 std::string printer_ppd_path; |
| 203 CHECK(printer_dict->GetString("printerId", &printer_id)); | 275 CHECK(printer_dict->GetString("printerId", &printer_id)); |
| 204 CHECK(printer_dict->GetString("printerName", &printer_name)); | 276 CHECK(printer_dict->GetString("printerName", &printer_name)); |
| 205 CHECK(printer_dict->GetString("printerDescription", &printer_description)); | 277 CHECK(printer_dict->GetString("printerDescription", &printer_description)); |
| 206 CHECK(printer_dict->GetString("printerManufacturer", &printer_manufacturer)); | 278 CHECK(printer_dict->GetString("printerManufacturer", &printer_manufacturer)); |
| 207 CHECK(printer_dict->GetString("printerModel", &printer_model)); | 279 CHECK(printer_dict->GetString("printerModel", &printer_model)); |
| 208 CHECK(printer_dict->GetString("printerAddress", &printer_address)); | 280 CHECK(printer_dict->GetString("printerAddress", &printer_address)); |
| 209 CHECK(printer_dict->GetString("printerProtocol", &printer_protocol)); | 281 CHECK(printer_dict->GetString("printerProtocol", &printer_protocol)); |
| 282 | |
| 210 // printerQueue might be null for a printer whose protocol is not 'LPD'. | 283 // printerQueue might be null for a printer whose protocol is not 'LPD'. |
| 211 printer_dict->GetString("printerQueue", &printer_queue); | 284 printer_dict->GetString("printerQueue", &printer_queue); |
| 212 | 285 |
| 213 // printerPPDPath might be null for an auto-discovered printer. | 286 std::string printer_uri = |
| 214 printer_dict->GetString("printerPPDPath", &printer_ppd_path); | 287 printer_protocol + url::kStandardSchemeSeparator + printer_address; |
| 215 std::string printer_uri = printer_protocol + "://" + printer_address; | |
| 216 if (!printer_queue.empty()) { | 288 if (!printer_queue.empty()) { |
| 217 printer_uri += "/" + printer_queue; | 289 printer_uri += "/" + printer_queue; |
| 218 } | 290 } |
| 219 | 291 |
| 292 // printerPPDPath might be null for an auto-discovered printer. | |
| 293 printer_dict->GetString("printerPPDPath", &printer_ppd_path); | |
| 294 | |
| 220 std::unique_ptr<Printer> printer = base::MakeUnique<Printer>(printer_id); | 295 std::unique_ptr<Printer> printer = base::MakeUnique<Printer>(printer_id); |
| 221 printer->set_display_name(printer_name); | 296 printer->set_display_name(printer_name); |
| 222 printer->set_description(printer_description); | 297 printer->set_description(printer_description); |
| 223 printer->set_manufacturer(printer_manufacturer); | 298 printer->set_manufacturer(printer_manufacturer); |
| 224 printer->set_model(printer_model); | 299 printer->set_model(printer_model); |
| 225 printer->set_uri(printer_uri); | 300 printer->set_uri(printer_uri); |
| 226 | 301 |
| 227 // Verify a valid ppd path is present. | 302 bool autoconf = false; |
| 228 if (!printer_ppd_path.empty()) { | 303 printer_dict->GetBoolean("autoconf", &autoconf); |
| 304 | |
| 305 // Verify that the printer is autoconf or a valid ppd path is present. | |
| 306 if (autoconf) { | |
| 307 printer->mutable_ppd_reference()->autoconf = true; | |
| 308 } else if (!printer_ppd_path.empty()) { | |
| 229 GURL tmp = net::FilePathToFileURL(base::FilePath(printer_ppd_path)); | 309 GURL tmp = net::FilePathToFileURL(base::FilePath(printer_ppd_path)); |
| 230 if (!tmp.is_valid()) { | 310 if (!tmp.is_valid()) { |
| 231 LOG(ERROR) << "Invalid ppd path: " << printer_ppd_path; | 311 LOG(ERROR) << "Invalid ppd path: " << printer_ppd_path; |
| 232 OnAddPrinterError(); | 312 OnAddPrinterError(); |
| 233 return; | 313 return; |
| 234 } | 314 } |
| 235 printer->mutable_ppd_reference()->user_supplied_ppd_url = tmp.spec(); | 315 printer->mutable_ppd_reference()->user_supplied_ppd_url = tmp.spec(); |
| 236 } else if (!printer_manufacturer.empty() && !printer_model.empty()) { | 316 } else if (!printer_manufacturer.empty() && !printer_model.empty()) { |
| 237 // Using the manufacturer and model, get a ppd reference. | 317 // Using the manufacturer and model, get a ppd reference. |
| 238 if (!ppd_provider_->GetPpdReference(printer_manufacturer, printer_model, | 318 if (!ppd_provider_->GetPpdReference(printer_manufacturer, printer_model, |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 412 } | 492 } |
| 413 | 493 |
| 414 FireWebUIListener("on-printer-discovered", *printers_list); | 494 FireWebUIListener("on-printer-discovered", *printers_list); |
| 415 } | 495 } |
| 416 | 496 |
| 417 void CupsPrintersHandler::OnDiscoveryInitialScanDone(int printer_count) { | 497 void CupsPrintersHandler::OnDiscoveryInitialScanDone(int printer_count) { |
| 418 UMA_HISTOGRAM_COUNTS_100("Printing.CUPS.PrintersDiscovered", printer_count); | 498 UMA_HISTOGRAM_COUNTS_100("Printing.CUPS.PrintersDiscovered", printer_count); |
| 419 FireWebUIListener("on-printer-discovery-done"); | 499 FireWebUIListener("on-printer-discovery-done"); |
| 420 } | 500 } |
| 421 | 501 |
| 502 void CupsPrintersHandler::OnPrinterInfo(const std::string& callback_id, | |
| 503 bool success, | |
| 504 const std::string& make, | |
| 505 const std::string& model, | |
| 506 bool ipp_everywhere) { | |
| 507 if (!success) { | |
| 508 base::DictionaryValue reject; | |
| 509 reject.SetString("message", "Querying printer failed"); | |
| 510 RejectJavascriptCallback(base::Value(callback_id), reject); | |
| 511 return; | |
| 512 } | |
| 513 | |
| 514 base::DictionaryValue info; | |
| 515 info.SetString("manufacturer", make); | |
| 516 info.SetString("model", model); | |
| 517 info.SetBoolean("autoconf", ipp_everywhere); | |
| 518 ResolveJavascriptCallback(base::Value(callback_id), info); | |
| 519 } | |
| 520 | |
| 422 } // namespace settings | 521 } // namespace settings |
| 423 } // namespace chromeos | 522 } // namespace chromeos |
| OLD | NEW |