summaryrefslogtreecommitdiffstats
path: root/chromium/chrome/browser/ui/webui/settings/chromeos/cups_printers_handler.cc
diff options
context:
space:
mode:
authorAllan Sandfeld Jensen <allan.jensen@qt.io>2020-07-16 11:45:35 +0200
committerAllan Sandfeld Jensen <allan.jensen@qt.io>2020-07-17 08:59:23 +0000
commit552906b0f222c5d5dd11b9fd73829d510980461a (patch)
tree3a11e6ed0538a81dd83b20cf3a4783e297f26d91 /chromium/chrome/browser/ui/webui/settings/chromeos/cups_printers_handler.cc
parent1b05827804eaf047779b597718c03e7d38344261 (diff)
BASELINE: Update Chromium to 83.0.4103.122
Change-Id: Ie3a82f5bb0076eec2a7c6a6162326b4301ee291e Reviewed-by: Michael BrĂ¼ning <michael.bruning@qt.io>
Diffstat (limited to 'chromium/chrome/browser/ui/webui/settings/chromeos/cups_printers_handler.cc')
-rw-r--r--chromium/chrome/browser/ui/webui/settings/chromeos/cups_printers_handler.cc120
1 files changed, 57 insertions, 63 deletions
diff --git a/chromium/chrome/browser/ui/webui/settings/chromeos/cups_printers_handler.cc b/chromium/chrome/browser/ui/webui/settings/chromeos/cups_printers_handler.cc
index 74f1ab84039..fd6f8acdd11 100644
--- a/chromium/chrome/browser/ui/webui/settings/chromeos/cups_printers_handler.cc
+++ b/chromium/chrome/browser/ui/webui/settings/chromeos/cups_printers_handler.cc
@@ -14,12 +14,14 @@
#include "base/files/file_util.h"
#include "base/json/json_string_value_serializer.h"
#include "base/logging.h"
+#include "base/memory/ptr_util.h"
#include "base/metrics/histogram_macros.h"
#include "base/optional.h"
#include "base/path_service.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/task/post_task.h"
+#include "base/task/thread_pool.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
@@ -37,12 +39,11 @@
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/chrome_select_file_policy.h"
+#include "chrome/browser/ui/webui/settings/chromeos/server_printer_url_util.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/pref_names.h"
-#include "chrome/common/webui_url_constants.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/debug_daemon/debug_daemon_client.h"
-#include "chromeos/printing/ppd_cache.h"
#include "chromeos/printing/ppd_line_reader.h"
#include "chromeos/printing/printer_configuration.h"
#include "chromeos/printing/printer_translator.h"
@@ -58,6 +59,7 @@
#include "net/base/ip_endpoint.h"
#include "net/url_request/url_request_context_getter.h"
#include "printing/backend/print_backend.h"
+#include "printing/printer_status.h"
#include "url/gurl.h"
namespace chromeos {
@@ -113,8 +115,8 @@ void QueryAutoconf(const std::string& printer_uri,
// Behavior for querying a non-IPP uri is undefined and disallowed.
if (!IsIppUri(printer_uri) || !optional.has_value()) {
PRINTER_LOG(ERROR) << "Printer uri is invalid: " << printer_uri;
- std::move(callback).Run(PrinterQueryResult::UNKNOWN_FAILURE, "", "", "", {},
- false);
+ std::move(callback).Run(PrinterQueryResult::UNKNOWN_FAILURE,
+ printing::PrinterStatus(), "", "", "", {}, false);
return;
}
@@ -168,6 +170,7 @@ std::unique_ptr<chromeos::Printer> DictToPrinter(
std::string printer_make_and_model;
std::string printer_address;
std::string printer_protocol;
+ std::string print_server_uri;
if (!printer_dict.GetString("printerId", &printer_id) ||
!printer_dict.GetString("printerName", &printer_name) ||
@@ -176,7 +179,8 @@ std::unique_ptr<chromeos::Printer> DictToPrinter(
!printer_dict.GetString("printerModel", &printer_model) ||
!printer_dict.GetString("printerMakeAndModel", &printer_make_and_model) ||
!printer_dict.GetString("printerAddress", &printer_address) ||
- !printer_dict.GetString("printerProtocol", &printer_protocol)) {
+ !printer_dict.GetString("printerProtocol", &printer_protocol) ||
+ !printer_dict.GetString("printServerUri", &print_server_uri)) {
return nullptr;
}
@@ -195,6 +199,7 @@ std::unique_ptr<chromeos::Printer> DictToPrinter(
printer->set_model(printer_model);
printer->set_make_and_model(printer_make_and_model);
printer->set_uri(printer_uri);
+ printer->set_print_server_uri(print_server_uri);
return printer;
}
@@ -237,11 +242,6 @@ void SetPpdReference(const Printer::PpdReference& ppd_ref, base::Value* info) {
}
}
-bool IsFilledPpdReference(const Printer::PpdReference& ppd_ref) {
- return ppd_ref.autoconf || !ppd_ref.user_supplied_ppd_url.empty() ||
- !ppd_ref.effective_make_and_model.empty();
-}
-
Printer::PpdReference GetPpdReference(const base::Value* info) {
const char ppd_ref_pathname[] = "printerPpdReference";
auto* user_supplied_ppd_url =
@@ -267,40 +267,11 @@ Printer::PpdReference GetPpdReference(const base::Value* info) {
return ret;
}
-bool ConvertToGURL(const std::string& url, GURL* gurl) {
- *gurl = GURL(url);
- if (!gurl->is_valid()) {
- // URL is not valid.
- return false;
- }
- if (!gurl->SchemeIsHTTPOrHTTPS() && !gurl->SchemeIs("ipp") &&
- !gurl->SchemeIs("ipps")) {
- // URL has unsupported scheme; we support only: http, https, ipp, ipps.
- return false;
- }
- // Replaces ipp/ipps by http/https. IPP standard describes protocol built
- // on top of HTTP, so both types of addresses have the same meaning in the
- // context of IPP interface. Moreover, the URL must have http/https scheme
- // to pass IsStandard() test from GURL library (see "Validation of the URL
- // address" below).
- bool set_ipp_port = false;
- if (gurl->SchemeIs("ipp")) {
- set_ipp_port = (gurl->IntPort() == url::PORT_UNSPECIFIED);
- *gurl = GURL("http" + url.substr(url.find_first_of(':')));
- } else if (gurl->SchemeIs("ipps")) {
- *gurl = GURL("https" + url.substr(url.find_first_of(':')));
- }
- // The default port for ipp is 631. If the schema ipp is replaced by http
- // and the port is not explicitly defined in the url, we have to overwrite
- // the default http port with the default ipp port. For ipps we do nothing
- // because implementers use the same port for ipps and https.
- if (set_ipp_port) {
- GURL::Replacements replacement;
- replacement.SetPortStr("631");
- *gurl = gurl->ReplaceComponents(replacement);
- }
- // Validation of the URL address.
- return gurl->IsStandard();
+GURL GenerateHttpCupsServerUrl(const GURL& server_url) {
+ GURL::Replacements replacement;
+ replacement.SetSchemeStr("http");
+ replacement.SetPortStr("631");
+ return server_url.ReplaceComponents(replacement);
}
} // namespace
@@ -405,6 +376,10 @@ void CupsPrintersHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback(
"getEulaUrl", base::BindRepeating(&CupsPrintersHandler::HandleGetEulaUrl,
base::Unretained(this)));
+ web_ui()->RegisterMessageCallback(
+ "queryPrintServer",
+ base::BindRepeating(&CupsPrintersHandler::HandleQueryPrintServer,
+ base::Unretained(this)));
}
void CupsPrintersHandler::OnJavascriptAllowed() {
@@ -513,8 +488,8 @@ void CupsPrintersHandler::HandleGetPrinterInfo(const base::ListValue* args) {
if (printer_address.empty()) {
// Run the failure callback.
- OnAutoconfQueried(callback_id, PrinterQueryResult::UNKNOWN_FAILURE, "", "",
- "", {}, false);
+ OnAutoconfQueried(callback_id, PrinterQueryResult::UNKNOWN_FAILURE,
+ printing::PrinterStatus(), "", "", "", {}, false);
return;
}
@@ -541,6 +516,7 @@ void CupsPrintersHandler::OnAutoconfQueriedDiscovered(
const std::string& callback_id,
Printer printer,
PrinterQueryResult result,
+ const printing::PrinterStatus& printer_status,
const std::string& make,
const std::string& model,
const std::string& make_and_model,
@@ -587,6 +563,7 @@ void CupsPrintersHandler::OnAutoconfQueriedDiscovered(
void CupsPrintersHandler::OnAutoconfQueried(
const std::string& callback_id,
PrinterQueryResult result,
+ const printing::PrinterStatus& printer_status,
const std::string& make,
const std::string& model,
const std::string& make_and_model,
@@ -736,7 +713,7 @@ void CupsPrintersHandler::AddOrReconfigurePrinter(const base::ListValue* args,
// Check if the printer already has a valid ppd_reference.
Printer::PpdReference ppd_ref = GetPpdReference(printer_dict);
- if (IsFilledPpdReference(ppd_ref)) {
+ if (ppd_ref.IsFilled()) {
*printer->mutable_ppd_reference() = ppd_ref;
} else if (!printer_ppd_path.empty()) {
GURL tmp = net::FilePathToFileURL(base::FilePath(printer_ppd_path));
@@ -1003,9 +980,8 @@ void CupsPrintersHandler::FileSelected(const base::FilePath& path,
// VerifyPpdContents() in order to determine whether the file appears to be a
// PPD file. The task's priority is USER_BLOCKING because the this task
// updates the UI as a result of a direct user action.
- base::PostTaskAndReplyWithResult(
- FROM_HERE,
- {base::ThreadPool(), base::MayBlock(), base::TaskPriority::USER_BLOCKING},
+ base::ThreadPool::PostTaskAndReplyWithResult(
+ FROM_HERE, {base::MayBlock(), base::TaskPriority::USER_BLOCKING},
base::BindOnce(&ReadFileToStringWithMaxSize, path, kPpdMaxLineLength),
base::BindOnce(&CupsPrintersHandler::VerifyPpdContents,
weak_factory_.GetWeakPtr(), path));
@@ -1239,7 +1215,7 @@ void CupsPrintersHandler::OnGetEulaUrl(const std::string& callback_id,
return;
}
- GURL eula_url(chrome::kChromeUIOSCreditsURL + license);
+ GURL eula_url = PrinterConfigurer::GeneratePrinterEulaUrl(license);
ResolveJavascriptCallback(
base::Value(callback_id),
eula_url.is_valid() ? base::Value(eula_url.spec()) : base::Value());
@@ -1283,27 +1259,45 @@ void CupsPrintersHandler::HandleQueryPrintServer(const base::ListValue* args) {
CHECK(args->GetString(0, &callback_id));
CHECK(args->GetString(1, &server_url));
- GURL server_gurl;
- if (!ConvertToGURL(server_url, &server_gurl)) {
+ base::Optional<GURL> converted_server_url =
+ GenerateServerPrinterUrlWithValidScheme(server_url);
+ if (!converted_server_url) {
RejectJavascriptCallback(
base::Value(callback_id),
base::Value(PrintServerQueryResult::kIncorrectUrl));
return;
}
+ // Use fallback only if HasValidServerPrinterScheme is false.
+ QueryPrintServer(callback_id, converted_server_url.value(),
+ !HasValidServerPrinterScheme(GURL(server_url)));
+}
+
+void CupsPrintersHandler::QueryPrintServer(const std::string& callback_id,
+ const GURL& server_url,
+ bool should_fallback) {
server_printers_fetcher_ = std::make_unique<ServerPrintersFetcher>(
- server_gurl, "(from user)",
+ server_url, "(from user)",
base::BindRepeating(&CupsPrintersHandler::OnQueryPrintServerCompleted,
- weak_factory_.GetWeakPtr(), callback_id));
+ weak_factory_.GetWeakPtr(), callback_id,
+ should_fallback));
}
void CupsPrintersHandler::OnQueryPrintServerCompleted(
const std::string& callback_id,
+ bool should_fallback,
const ServerPrintersFetcher* sender,
const GURL& server_url,
std::vector<PrinterDetector::DetectedPrinter>&& returned_printers) {
const PrintServerQueryResult result = sender->GetLastError();
if (result != PrintServerQueryResult::kNoErrors) {
+ if (should_fallback) {
+ // Apply the fallback query.
+ QueryPrintServer(callback_id, GenerateHttpCupsServerUrl(server_url),
+ /*should_fallback=*/false);
+ return;
+ }
+
RejectJavascriptCallback(base::Value(callback_id), base::Value(result));
return;
}
@@ -1313,9 +1307,10 @@ void CupsPrintersHandler::OnQueryPrintServerCompleted(
printers_manager_->GetPrinters(PrinterClass::kSaved);
std::set<GURL> known_printers;
for (const Printer& printer : saved_printers) {
- GURL gurl;
- if (ConvertToGURL(printer.uri(), &gurl))
- known_printers.insert(gurl);
+ base::Optional<GURL> gurl =
+ GenerateServerPrinterUrlWithValidScheme(printer.uri());
+ if (gurl)
+ known_printers.insert(gurl.value());
}
// Built final list of printers and a list of current names. If "current name"
@@ -1325,11 +1320,10 @@ void CupsPrintersHandler::OnQueryPrintServerCompleted(
printers.reserve(returned_printers.size());
for (PrinterDetector::DetectedPrinter& printer : returned_printers) {
printers.push_back(std::move(printer.printer));
- GURL printer_gurl;
- if (ConvertToGURL(printers.back().uri(), &printer_gurl)) {
- if (known_printers.count(printer_gurl))
- printers.pop_back();
- }
+ base::Optional<GURL> printer_gurl =
+ GenerateServerPrinterUrlWithValidScheme(printers.back().uri());
+ if (printer_gurl && known_printers.count(printer_gurl.value()))
+ printers.pop_back();
}
// Delete fetcher object.