File: lorgnette_scanner_manager_util.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (102 lines) | stat: -rw-r--r-- 3,942 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/scanning/lorgnette_scanner_manager_util.h"

#include "base/strings/string_util.h"
#include "chromeos/ash/components/dbus/lorgnette/lorgnette_service.pb.h"
#include "third_party/re2/src/re2/re2.h"

namespace ash {

namespace {

// Regular expressions used to determine whether a device name contains an IPv4
// address or URL.
constexpr char kIpv4Pattern[] = R"((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))";
constexpr char kUrlPattern[] = R"((://))";

// Prefixes of backends that need to be checked for duplicate zeroconf and
// lorgnette records.
constexpr char kEpsondsNetworkPrefix[] = "epsonds:net:";
constexpr char kEpson2NetworkPrefix[] = "epson2:net:";

// Names of scanner protocol types.
constexpr char kMopriaProtocolType[] = "Mopria";
constexpr char kWsdProtocolType[] = "WSD";

}  // namespace

void ParseScannerName(const std::string& scanner_name,
                      std::string& ip_address_out,
                      ScanProtocol& protocol_out) {
  if (RE2::PartialMatch(scanner_name, kIpv4Pattern, &ip_address_out) ||
      RE2::PartialMatch(scanner_name, kUrlPattern)) {
    protocol_out = ScanProtocol::kLegacyNetwork;
    return;
  }

  protocol_out = ScanProtocol::kLegacyUsb;
}

bool MergeDuplicateScannerRecords(lorgnette::ScannerInfo* scanner_out,
                                  Scanner& zeroconf_scanner) {
  // Currently only epson2 and epsonds are detected by both lorgnette and
  // zeroconf.
  if (!scanner_out->name().starts_with(kEpson2NetworkPrefix) &&
      !scanner_out->name().starts_with(kEpsondsNetworkPrefix)) {
    return false;
  }

  auto device_names =
      zeroconf_scanner.device_names.find(ScanProtocol::kLegacyNetwork);
  if (device_names == zeroconf_scanner.device_names.end()) {
    return false;
  }
  for (ScannerDeviceName& device_name : device_names->second) {
    std::string alt_name = device_name.device_name;
    if (device_name.device_name.starts_with(kEpsondsNetworkPrefix)) {
      alt_name.replace(0, strlen(kEpsondsNetworkPrefix), kEpson2NetworkPrefix);
    } else if (device_name.device_name.starts_with(kEpson2NetworkPrefix)) {
      alt_name.replace(0, strlen(kEpson2NetworkPrefix), kEpsondsNetworkPrefix);
    }

    // epson2 and epsonds scanners can be detected through both lorgnette and
    // zeroconf.  When we get two records for the same scanner, we want to
    // prefer the lorgnette connection info because it is already validated,
    // but we want to prefer the zeroconf display strings because they match
    // the information shown in the printer settings page.  To get the desired
    // result, copy the desired fields from the zeroconf record into the
    // lorgnette record and then disable further use of the zeroconf entry.
    if (scanner_out->name() == device_name.device_name ||
        scanner_out->name() == alt_name) {
      scanner_out->set_manufacturer(zeroconf_scanner.manufacturer);
      scanner_out->set_model(zeroconf_scanner.model);
      scanner_out->set_display_name(zeroconf_scanner.display_name);
      if (!zeroconf_scanner.uuid.empty()) {
        scanner_out->set_device_uuid(zeroconf_scanner.uuid);
      }
      device_name.usable = false;
      return true;
    }
  }
  return false;
}

std::string ProtocolTypeForScanner(const lorgnette::ScannerInfo& scanner) {
  // sane-airscan implements two protocols.  For other backends, just return
  // the backend name.
  if (scanner.name().starts_with("airscan:escl:")) {
    return kMopriaProtocolType;
  } else if (scanner.name().starts_with("airscan:wsd:")) {
    return kWsdProtocolType;
  } else if (scanner.name().starts_with("ippusb:escl:")) {
    return kMopriaProtocolType;
  } else {
    return base::ToLowerASCII(
        scanner.name().substr(0, scanner.name().find(':')));
  }
}

}  // namespace ash