| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/chromeos/printing/printer_info.h" |
| 6 |
| 7 #include <algorithm> |
| 8 #include <array> |
| 9 #include <string> |
| 10 |
| 11 #include "base/logging.h" |
| 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/strings/string_piece.h" |
| 14 #include "base/strings/string_util.h" |
| 15 #include "base/task_runner_util.h" |
| 16 #include "base/task_scheduler/post_task.h" |
| 17 #include "base/task_scheduler/task_traits.h" |
| 18 #include "base/version.h" |
| 19 #include "printing/backend/cups_jobs.h" |
| 20 |
| 21 namespace { |
| 22 |
| 23 const char kPdfMimeType[] = "application/pdf"; |
| 24 const char kPwgRasterMimeType[] = "image/pwg-raster"; |
| 25 |
| 26 // List of known multi-word printer manufacturers to help with make-and-model |
| 27 // string parsing. Keep in UPPER CASE as that's how matches are performed. |
| 28 const std::array<const char* const, 4> kMultiWordManufacturers{ |
| 29 {"FUJI XEROX", "KODAK FUNAI", "KONICA MINOLTA", "TEXAS INSTRUMENTS"}}; |
| 30 |
| 31 // Returns the length of the portion of |make_and_model| representing the |
| 32 // manufacturer. This is either a value from kMultiWordManufacaturers or the |
| 33 // first token. If there is only one token or less, we assume that it does not |
| 34 // represent the manufacturer and return 0. |
| 35 size_t ManufacturerLength(base::StringPiece make_and_model) { |
| 36 // TODO(crbug.com/729245): Update when better data is available. |
| 37 for (base::StringPiece multi_word_manufacturer : kMultiWordManufacturers) { |
| 38 if (base::StartsWith(make_and_model, multi_word_manufacturer, |
| 39 base::CompareCase::INSENSITIVE_ASCII)) { |
| 40 return multi_word_manufacturer.size(); |
| 41 } |
| 42 } |
| 43 |
| 44 // The position of the first space is equal to the length of the first token. |
| 45 size_t first_space = make_and_model.find(" "); |
| 46 return first_space != base::StringPiece::npos ? first_space : 0; |
| 47 } |
| 48 |
| 49 // Returns true if any of the |ipp_versions| are greater than or equal to 2.0. |
| 50 bool AllowedIpp(const std::vector<base::Version>& ipp_versions) { |
| 51 auto found = |
| 52 std::find_if(ipp_versions.begin(), ipp_versions.end(), |
| 53 [](const base::Version& version) { |
| 54 return version.IsValid() && version.components()[0] >= 2; |
| 55 }); |
| 56 |
| 57 return found != ipp_versions.end(); |
| 58 } |
| 59 |
| 60 // Returns true if |mime_type| is one of the supported types. |
| 61 bool SupportedMime(const std::string& mime_type) { |
| 62 return mime_type == kPwgRasterMimeType || mime_type == kPdfMimeType; |
| 63 } |
| 64 |
| 65 // Returns true if |formats| contains one of the supported printer description |
| 66 // languages for an autoconf printer identified by MIME type. |
| 67 bool SupportsRequiredPDLS(const std::vector<std::string>& formats) { |
| 68 auto found = std::find_if(formats.begin(), formats.end(), &SupportedMime); |
| 69 return found != formats.end(); |
| 70 } |
| 71 |
| 72 // Returns true if |info| describes a printer for which we want to attempt |
| 73 // automatic configuration. |
| 74 bool IsAutoconf(const ::printing::PrinterInfo& info) { |
| 75 return info.ipp_everywhere || (AllowedIpp(info.ipp_versions) && |
| 76 SupportsRequiredPDLS(info.document_formats)); |
| 77 } |
| 78 |
| 79 // Dispatches an IPP request to |host| to retrieve printer information. Returns |
| 80 // a nullptr if the request fails. |
| 81 std::unique_ptr<::printing::PrinterInfo> QueryPrinterImpl( |
| 82 const std::string& host, |
| 83 const int port, |
| 84 const std::string& path) { |
| 85 auto info = base::MakeUnique<::printing::PrinterInfo>(); |
| 86 if (!::printing::GetPrinterInfo(host, port, path, info.get())) { |
| 87 LOG(ERROR) << "Could not retrieve printer info"; |
| 88 return nullptr; |
| 89 } |
| 90 |
| 91 return info; |
| 92 } |
| 93 |
| 94 // Handles the request for |info|. Parses make and model information before |
| 95 // calling |callback|. |
| 96 void OnPrinterQueried(const chromeos::PrinterInfoCallback& callback, |
| 97 std::unique_ptr<::printing::PrinterInfo> info) { |
| 98 if (!info) { |
| 99 VLOG(1) << "Could not reach printer"; |
| 100 callback.Run(false, std::string(), std::string(), false); |
| 101 return; |
| 102 } |
| 103 |
| 104 base::StringPiece make_and_model(info->make_and_model); |
| 105 base::StringPiece make; |
| 106 base::StringPiece model; |
| 107 |
| 108 size_t split = ManufacturerLength(make_and_model); |
| 109 if (split != 0) { |
| 110 make = make_and_model.substr(0, split); |
| 111 model = make_and_model.substr(split + 1); |
| 112 } else { |
| 113 // If there's only one word or an empty string, use it. |
| 114 model = make_and_model; |
| 115 } |
| 116 |
| 117 callback.Run(true, make.as_string(), model.as_string(), IsAutoconf(*info)); |
| 118 } |
| 119 |
| 120 } // namespace |
| 121 |
| 122 namespace chromeos { |
| 123 |
| 124 void QueryIppPrinter(const std::string& host, |
| 125 const int port, |
| 126 const std::string& path, |
| 127 const PrinterInfoCallback& callback) { |
| 128 DCHECK(!host.empty()); |
| 129 |
| 130 // QueryPrinterImpl could block on a network call for a noticable amount of |
| 131 // time (100s of ms). Also the user is waiting on this result. Thus, run at |
| 132 // USER_VISIBLE with MayBlock. |
| 133 base::PostTaskWithTraitsAndReplyWithResult( |
| 134 FROM_HERE, |
| 135 base::TaskTraits(base::TaskPriority::USER_VISIBLE, base::MayBlock()), |
| 136 base::Bind(&QueryPrinterImpl, host, port, path), |
| 137 base::Bind(&OnPrinterQueried, callback)); |
| 138 } |
| 139 |
| 140 } // namespace chromeos |
| OLD | NEW |