OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "printing/backend/cups_connection.h" | |
6 | |
7 #include <string> | |
8 #include <utility> | |
9 | |
10 #include "base/logging.h" | |
11 #include "base/memory/ptr_util.h" | |
12 #include "base/strings/stringprintf.h" | |
13 | |
14 namespace printing { | |
15 | |
16 namespace { | |
17 | |
18 const int kTimeoutMs = 3000; | |
19 | |
20 class DestinationEnumerator { | |
21 public: | |
22 DestinationEnumerator() {} | |
23 | |
24 static int cups_callback(void* user_data, unsigned flags, cups_dest_t* dest) { | |
25 cups_dest_t* copied_dest; | |
26 cupsCopyDest(dest, 0, &copied_dest); | |
27 reinterpret_cast<DestinationEnumerator*>(user_data)->store_dest( | |
28 copied_dest); | |
29 | |
30 // keep going | |
31 return 1; | |
32 } | |
33 | |
34 void store_dest(cups_dest_t* dest) { dests_.emplace_back(dest); } | |
35 | |
36 // Returns the collected destinations. | |
37 std::vector<std::unique_ptr<cups_dest_t, DestinationDeleter>>& get_dests() { | |
38 return dests_; | |
39 } | |
40 | |
41 private: | |
42 std::vector<std::unique_ptr<cups_dest_t, DestinationDeleter>> dests_; | |
43 | |
44 DISALLOW_COPY_AND_ASSIGN(DestinationEnumerator); | |
45 }; | |
46 | |
47 } // namespace | |
48 | |
49 CupsConnection::CupsConnection(const GURL& print_server_url, | |
50 http_encryption_t encryption, | |
51 bool blocking) | |
52 : print_server_url_(print_server_url), | |
53 cups_encryption_(encryption), | |
54 blocking_(blocking), | |
55 cups_http_(nullptr) {} | |
56 | |
57 CupsConnection::CupsConnection(CupsConnection&& connection) | |
58 : print_server_url_(connection.print_server_url_), | |
59 cups_encryption_(connection.cups_encryption_), | |
60 blocking_(connection.blocking_), | |
61 cups_http_(std::move(connection.cups_http_)) {} | |
62 | |
63 CupsConnection::~CupsConnection() {} | |
64 | |
65 bool CupsConnection::Connect() { | |
66 if (cups_http_) | |
67 return true; // we're already connected | |
68 | |
69 std::string host; | |
70 int port; | |
71 | |
72 if (!print_server_url_.is_empty()) { | |
73 host = print_server_url_.host(); | |
74 port = print_server_url_.IntPort(); | |
75 } else { | |
76 host = cupsServer(); | |
77 port = ippPort(); | |
78 } | |
79 | |
80 http_t* connection = | |
Lei Zhang
2016/07/21 00:46:19
Drop |connection| and just do: cups_http_.reset(..
skau
2016/07/21 20:07:30
Done.
| |
81 httpConnect2(host.c_str(), port, nullptr, AF_UNSPEC, cups_encryption_, | |
82 blocking_ ? 1 : 0, kTimeoutMs, nullptr); | |
83 | |
84 if (!connection) | |
85 return false; | |
86 | |
87 cups_http_.reset(connection); | |
88 return true; | |
89 } | |
90 | |
91 std::vector<CupsPrinter> CupsConnection::GetDests() { | |
92 if (!Connect()) { | |
93 LOG(WARNING) << "CUPS connection failed"; | |
94 return std::vector<CupsPrinter>(); | |
95 } | |
96 | |
97 DestinationEnumerator enumerator; | |
98 int success = | |
99 cupsEnumDests(CUPS_DEST_FLAGS_NONE, kTimeoutMs, | |
100 nullptr, // no cancel signal | |
101 0, // all the printers | |
102 CUPS_PRINTER_SCANNER, // except the scanners | |
103 &DestinationEnumerator::cups_callback, &enumerator); | |
104 | |
105 if (!success) { | |
106 LOG(WARNING) << "Enumerating printers failed"; | |
107 return std::vector<CupsPrinter>(); | |
108 } | |
109 | |
110 auto dests = std::move(enumerator.get_dests()); | |
111 std::vector<CupsPrinter> printers; | |
112 for (auto& dest : dests) { | |
113 printers.emplace_back(cups_http_.get(), std::move(dest), nullptr); | |
114 } | |
115 | |
116 return printers; | |
117 } | |
118 | |
119 std::unique_ptr<CupsPrinter> CupsConnection::GetPrinter( | |
120 const std::string& name) { | |
121 if (!Connect()) | |
122 return nullptr; | |
123 | |
124 cups_dest_t* dest = cupsGetNamedDest(cups_http_.get(), name.c_str(), nullptr); | |
125 if (!dest) | |
126 return nullptr; | |
127 | |
128 cups_dinfo_t* info = cupsCopyDestInfo(cups_http_.get(), dest); | |
129 return base::MakeUnique<CupsPrinter>( | |
130 cups_http_.get(), std::unique_ptr<cups_dest_t, DestinationDeleter>(dest), | |
131 std::unique_ptr<cups_dinfo_t, DestInfoDeleter>(info)); | |
132 } | |
133 | |
134 std::string CupsConnection::server_name() const { | |
135 return print_server_url_.host(); | |
136 } | |
137 | |
138 int CupsConnection::last_error() const { | |
139 return cupsLastError(); | |
140 } | |
141 | |
142 } // namespace printing | |
OLD | NEW |