OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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/chromeos/printing/cups_print_job_manager_impl.h" | 5 #include "chrome/browser/chromeos/printing/cups_print_job_manager_impl.h" |
6 | 6 |
7 #include <cups/cups.h> | 7 #include <cups/cups.h> |
| 8 #include <algorithm> |
8 #include <set> | 9 #include <set> |
9 #include <string> | 10 #include <string> |
10 #include <utility> | 11 #include <utility> |
11 #include <vector> | 12 #include <vector> |
12 | 13 |
13 #include "base/bind.h" | 14 #include "base/bind.h" |
14 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
15 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
16 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
17 #include "base/threading/sequenced_task_runner_handle.h" | 18 #include "base/threading/sequenced_task_runner_handle.h" |
18 #include "chrome/browser/chrome_notification_types.h" | 19 #include "chrome/browser/chrome_notification_types.h" |
19 #include "chrome/browser/chromeos/printing/cups_print_job.h" | 20 #include "chrome/browser/chromeos/printing/cups_print_job.h" |
20 #include "chrome/browser/chromeos/printing/printers_manager.h" | 21 #include "chrome/browser/chromeos/printing/printers_manager.h" |
21 #include "chrome/browser/chromeos/printing/printers_manager_factory.h" | 22 #include "chrome/browser/chromeos/printing/printers_manager_factory.h" |
22 #include "chrome/browser/printing/print_job.h" | 23 #include "chrome/browser/printing/print_job.h" |
23 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
24 #include "content/public/browser/browser_context.h" | 25 #include "content/public/browser/browser_context.h" |
25 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
26 #include "content/public/browser/notification_registrar.h" | 27 #include "content/public/browser/notification_registrar.h" |
27 #include "content/public/browser/notification_service.h" | 28 #include "content/public/browser/notification_service.h" |
28 #include "printing/backend/cups_connection.h" | 29 #include "printing/backend/cups_connection.h" |
29 #include "printing/printed_document.h" | 30 #include "printing/printed_document.h" |
30 | 31 |
31 namespace { | 32 namespace { |
32 | 33 |
33 // The rate in milliseconds at which we will poll CUPS for print job updates. | 34 // The rate in milliseconds at which we will poll CUPS for print job updates. |
34 const int kPollRate = 1000; | 35 const int kPollRate = 1000; |
35 | 36 |
| 37 // How long we'll wait to connect to a printer before declaring an error. |
| 38 const int kConnectingTimeout = 20; |
| 39 |
36 // Threshold for giving up on communicating with CUPS. | 40 // Threshold for giving up on communicating with CUPS. |
37 const int kRetryMax = 6; | 41 const int kRetryMax = 6; |
38 | 42 |
| 43 using State = chromeos::CupsPrintJob::State; |
| 44 using ErrorCode = chromeos::CupsPrintJob::ErrorCode; |
| 45 |
| 46 using PrinterReason = printing::PrinterStatus::PrinterReason; |
| 47 |
39 // Returns the equivalient CupsPrintJob#State from a CupsJob#JobState. | 48 // Returns the equivalient CupsPrintJob#State from a CupsJob#JobState. |
40 chromeos::CupsPrintJob::State ConvertState(printing::CupsJob::JobState state) { | 49 chromeos::CupsPrintJob::State ConvertState(printing::CupsJob::JobState state) { |
41 using cpj = chromeos::CupsPrintJob::State; | |
42 | |
43 switch (state) { | 50 switch (state) { |
44 case printing::CupsJob::PENDING: | 51 case printing::CupsJob::PENDING: |
45 return cpj::STATE_WAITING; | 52 return State::STATE_WAITING; |
46 case printing::CupsJob::HELD: | 53 case printing::CupsJob::HELD: |
47 return cpj::STATE_SUSPENDED; | 54 return State::STATE_SUSPENDED; |
48 case printing::CupsJob::PROCESSING: | 55 case printing::CupsJob::PROCESSING: |
49 return cpj::STATE_STARTED; | 56 return State::STATE_STARTED; |
50 case printing::CupsJob::CANCELED: | 57 case printing::CupsJob::CANCELED: |
51 return cpj::STATE_CANCELLED; | 58 return State::STATE_CANCELLED; |
52 case printing::CupsJob::COMPLETED: | 59 case printing::CupsJob::COMPLETED: |
53 return cpj::STATE_DOCUMENT_DONE; | 60 return State::STATE_DOCUMENT_DONE; |
54 case printing::CupsJob::STOPPED: | 61 case printing::CupsJob::STOPPED: |
55 return cpj::STATE_SUSPENDED; | 62 return State::STATE_SUSPENDED; |
56 case printing::CupsJob::ABORTED: | 63 case printing::CupsJob::ABORTED: |
57 return cpj::STATE_ERROR; | 64 return State::STATE_ERROR; |
58 case printing::CupsJob::UNKNOWN: | 65 case printing::CupsJob::UNKNOWN: |
59 break; | 66 break; |
60 } | 67 } |
61 | 68 |
62 NOTREACHED(); | 69 NOTREACHED(); |
63 | 70 |
64 return cpj::STATE_NONE; | 71 return State::STATE_NONE; |
65 } | 72 } |
66 | 73 |
67 chromeos::QueryResult QueryCups(::printing::CupsConnection* connection, | 74 chromeos::QueryResult QueryCups(::printing::CupsConnection* connection, |
68 const std::vector<std::string>& printer_ids) { | 75 const std::vector<std::string>& printer_ids) { |
69 chromeos::QueryResult result; | 76 chromeos::QueryResult result; |
70 result.success = connection->GetJobs(printer_ids, &result.queues); | 77 result.success = connection->GetJobs(printer_ids, &result.queues); |
71 return result; | 78 return result; |
72 } | 79 } |
73 | 80 |
| 81 // Returns true if |printer_status|.reasons contains |reason|. |
| 82 bool ContainsReason(const printing::PrinterStatus printer_status, |
| 83 PrinterReason::Reason reason) { |
| 84 return std::find_if(printer_status.reasons.begin(), |
| 85 printer_status.reasons.end(), |
| 86 [&reason](const PrinterReason& r) { |
| 87 return r.reason == reason; |
| 88 }) != printer_status.reasons.end(); |
| 89 } |
| 90 |
| 91 // Extracts an ErrorCode from PrinterStatus#reasons. Returns NO_ERROR if there |
| 92 // are no reasons which indicate an error. |
| 93 chromeos::CupsPrintJob::ErrorCode ErrorCodeFromReasons( |
| 94 const printing::PrinterStatus& printer_status) { |
| 95 for (const auto& reason : printer_status.reasons) { |
| 96 switch (reason.reason) { |
| 97 case PrinterReason::MEDIA_JAM: |
| 98 case PrinterReason::MEDIA_EMPTY: |
| 99 case PrinterReason::MEDIA_NEEDED: |
| 100 case PrinterReason::MEDIA_LOW: |
| 101 return chromeos::CupsPrintJob::ErrorCode::PAPER_JAM; |
| 102 case PrinterReason::TONER_EMPTY: |
| 103 case PrinterReason::TONER_LOW: |
| 104 return chromeos::CupsPrintJob::ErrorCode::OUT_OF_INK; |
| 105 default: |
| 106 break; |
| 107 } |
| 108 } |
| 109 return chromeos::CupsPrintJob::ErrorCode::NO_ERROR; |
| 110 } |
| 111 |
| 112 // Check if the job should timeout. Returns true if the job has timed out. |
| 113 bool EnforceTimeout(const printing::CupsJob& job, |
| 114 chromeos::CupsPrintJob* print_job) { |
| 115 // Check to see if we should time out. |
| 116 base::TimeDelta time_waiting = |
| 117 base::Time::Now() - base::Time::FromTimeT(job.processing_started); |
| 118 if (time_waiting > base::TimeDelta::FromSeconds(kConnectingTimeout)) { |
| 119 print_job->set_state(chromeos::CupsPrintJob::State::STATE_ERROR); |
| 120 print_job->set_error_code( |
| 121 chromeos::CupsPrintJob::ErrorCode::PRINTER_UNREACHABLE); |
| 122 return true; |
| 123 } |
| 124 |
| 125 return false; |
| 126 } |
| 127 |
| 128 // Update the current printed page. Returns true of the page has been updated. |
| 129 bool UpdateCurrentPage(const printing::CupsJob& job, |
| 130 chromeos::CupsPrintJob* print_job) { |
| 131 bool pages_updated = false; |
| 132 if (job.current_pages <= 0) { |
| 133 print_job->set_printed_page_number(0); |
| 134 print_job->set_state(State::STATE_STARTED); |
| 135 } else { |
| 136 pages_updated = job.current_pages != print_job->printed_page_number(); |
| 137 print_job->set_printed_page_number(job.current_pages); |
| 138 print_job->set_state(State::STATE_PAGE_DONE); |
| 139 } |
| 140 |
| 141 return pages_updated; |
| 142 } |
| 143 |
| 144 // Updates the state of a print job based on |printer_status| and |job|. Returns |
| 145 // true if observers need to be notified of an update. |
| 146 bool UpdatePrintJob(const printing::PrinterStatus& printer_status, |
| 147 const printing::CupsJob& job, |
| 148 chromeos::CupsPrintJob* print_job) { |
| 149 DCHECK_EQ(job.id, print_job->job_id()); |
| 150 |
| 151 State old_state = print_job->state(); |
| 152 |
| 153 bool pages_updated = false; |
| 154 switch (job.state) { |
| 155 case printing::CupsJob::PROCESSING: |
| 156 if (ContainsReason(printer_status, PrinterReason::CONNECTING_TO_DEVICE)) { |
| 157 if (EnforceTimeout(job, print_job)) { |
| 158 LOG(WARNING) << "Connecting to printer timed out"; |
| 159 // TODO(skau): Purge job from queue. |
| 160 } |
| 161 break; |
| 162 } |
| 163 pages_updated = UpdateCurrentPage(job, print_job); |
| 164 break; |
| 165 case printing::CupsJob::COMPLETED: |
| 166 DCHECK_GE(job.current_pages, print_job->total_page_number()); |
| 167 print_job->set_state(State::STATE_DOCUMENT_DONE); |
| 168 break; |
| 169 case printing::CupsJob::ABORTED: |
| 170 case printing::CupsJob::CANCELED: |
| 171 print_job->set_error_code(ErrorCodeFromReasons(printer_status)); |
| 172 // fall through |
| 173 default: |
| 174 print_job->set_state(ConvertState(job.state)); |
| 175 break; |
| 176 } |
| 177 |
| 178 return print_job->state() != old_state || pages_updated; |
| 179 } |
| 180 |
74 } // namespace | 181 } // namespace |
75 | 182 |
76 namespace chromeos { | 183 namespace chromeos { |
77 | 184 |
78 CupsPrintJobManagerImpl::CupsPrintJobManagerImpl(Profile* profile) | 185 CupsPrintJobManagerImpl::CupsPrintJobManagerImpl(Profile* profile) |
79 : CupsPrintJobManager(profile), | 186 : CupsPrintJobManager(profile), |
80 cups_connection_(GURL(), HTTP_ENCRYPT_NEVER, false), | 187 cups_connection_(GURL(), HTTP_ENCRYPT_NEVER, false), |
81 weak_ptr_factory_(this) { | 188 weak_ptr_factory_(this) { |
82 registrar_.Add(this, chrome::NOTIFICATION_PRINT_JOB_EVENT, | 189 registrar_.Add(this, chrome::NOTIFICATION_PRINT_JOB_EVENT, |
83 content::NotificationService::AllSources()); | 190 content::NotificationService::AllSources()); |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 std::vector<std::string> active_jobs; | 327 std::vector<std::string> active_jobs; |
221 for (const auto& queue : queues) { | 328 for (const auto& queue : queues) { |
222 for (auto& job : queue.jobs) { | 329 for (auto& job : queue.jobs) { |
223 std::string key = CupsPrintJob::GetUniqueId(job.printer_id, job.id); | 330 std::string key = CupsPrintJob::GetUniqueId(job.printer_id, job.id); |
224 const auto& entry = jobs_.find(key); | 331 const auto& entry = jobs_.find(key); |
225 if (entry == jobs_.end()) | 332 if (entry == jobs_.end()) |
226 continue; | 333 continue; |
227 | 334 |
228 CupsPrintJob* print_job = entry->second.get(); | 335 CupsPrintJob* print_job = entry->second.get(); |
229 | 336 |
230 // Update a job we're tracking. | 337 if (UpdatePrintJob(queue.printer_status, job, print_job)) { |
231 JobStateUpdated(print_job, ConvertState(job.state)); | 338 // The state of the job changed, notify observers. |
| 339 NotifyJobStateUpdate(print_job); |
| 340 } |
232 | 341 |
233 // Cleanup completed jobs. | 342 // Cleanup completed jobs. |
234 if (print_job->IsJobFinished()) { | 343 if (print_job->IsJobFinished()) { |
235 jobs_.erase(entry); | 344 jobs_.erase(entry); |
236 } else { | 345 } else { |
237 active_jobs.push_back(key); | 346 active_jobs.push_back(key); |
238 } | 347 } |
239 } | 348 } |
240 } | 349 } |
241 | 350 |
242 // Keep polling until all jobs complete or error. | 351 // Keep polling until all jobs complete or error. |
243 if (!active_jobs.empty()) { | 352 if (!active_jobs.empty()) { |
244 // During normal operations, we poll at the default rate. | 353 // During normal operations, we poll at the default rate. |
245 ScheduleQuery(); | 354 ScheduleQuery(); |
246 } else if (!jobs_.empty()) { | 355 } else if (!jobs_.empty()) { |
247 // We're tracking jobs that we didn't receive an update for. Something bad | 356 // We're tracking jobs that we didn't receive an update for. Something bad |
248 // has happened. | 357 // has happened. |
249 LOG(ERROR) << "Lost track of (" << jobs_.size() << ") jobs"; | 358 LOG(ERROR) << "Lost track of (" << jobs_.size() << ") jobs"; |
250 PurgeJobs(); | 359 PurgeJobs(); |
251 } | 360 } |
252 } | 361 } |
253 | 362 |
254 void CupsPrintJobManagerImpl::PurgeJobs() { | 363 void CupsPrintJobManagerImpl::PurgeJobs() { |
255 for (const auto& entry : jobs_) { | 364 for (const auto& entry : jobs_) { |
256 // Declare all lost jobs errors. | 365 // Declare all lost jobs errors. |
257 JobStateUpdated(entry.second.get(), CupsPrintJob::State::STATE_ERROR); | 366 CupsPrintJob* job = entry.second.get(); |
| 367 job->set_state(CupsPrintJob::State::STATE_ERROR); |
| 368 NotifyJobStateUpdate(job); |
258 } | 369 } |
259 | 370 |
260 jobs_.clear(); | 371 jobs_.clear(); |
261 } | 372 } |
262 | 373 |
263 void CupsPrintJobManagerImpl::JobStateUpdated(CupsPrintJob* job, | 374 void CupsPrintJobManagerImpl::NotifyJobStateUpdate(CupsPrintJob* job) { |
264 CupsPrintJob::State new_state) { | 375 switch (job->state()) { |
265 if (job->state() == new_state) | 376 case State::STATE_NONE: |
266 return; | |
267 | |
268 // We don't track state transitions because some of them might be missed due | |
269 // to how we query jobs. | |
270 job->set_state(new_state); | |
271 switch (new_state) { | |
272 case CupsPrintJob::State::STATE_NONE: | |
273 // State does not require notification. | 377 // State does not require notification. |
274 break; | 378 break; |
275 case CupsPrintJob::State::STATE_WAITING: | 379 case State::STATE_WAITING: |
276 NotifyJobUpdated(job); | 380 NotifyJobUpdated(job); |
277 break; | 381 break; |
278 case CupsPrintJob::State::STATE_STARTED: | 382 case State::STATE_STARTED: |
279 NotifyJobStarted(job); | 383 NotifyJobStarted(job); |
280 break; | 384 break; |
281 case CupsPrintJob::State::STATE_RESUMED: | 385 case State::STATE_PAGE_DONE: |
| 386 NotifyJobUpdated(job); |
| 387 break; |
| 388 case State::STATE_RESUMED: |
282 NotifyJobResumed(job); | 389 NotifyJobResumed(job); |
283 break; | 390 break; |
284 case CupsPrintJob::State::STATE_SUSPENDED: | 391 case State::STATE_SUSPENDED: |
285 NotifyJobSuspended(job); | 392 NotifyJobSuspended(job); |
286 break; | 393 break; |
287 case CupsPrintJob::State::STATE_CANCELLED: | 394 case State::STATE_CANCELLED: |
288 NotifyJobCanceled(job); | 395 NotifyJobCanceled(job); |
289 break; | 396 break; |
290 case CupsPrintJob::State::STATE_ERROR: | 397 case State::STATE_ERROR: |
291 NotifyJobError(job); | 398 NotifyJobError(job); |
292 break; | 399 break; |
293 case CupsPrintJob::State::STATE_PAGE_DONE: | 400 case State::STATE_DOCUMENT_DONE: |
294 NOTREACHED() << "CUPS does not surface this state so it's not expected"; | |
295 break; | |
296 case CupsPrintJob::State::STATE_DOCUMENT_DONE: | |
297 NotifyJobDone(job); | 401 NotifyJobDone(job); |
298 break; | 402 break; |
299 } | 403 } |
300 } | 404 } |
301 | 405 |
302 // static | 406 // static |
303 CupsPrintJobManager* CupsPrintJobManager::CreateInstance(Profile* profile) { | 407 CupsPrintJobManager* CupsPrintJobManager::CreateInstance(Profile* profile) { |
304 return new CupsPrintJobManagerImpl(profile); | 408 return new CupsPrintJobManagerImpl(profile); |
305 } | 409 } |
306 | 410 |
307 } // namespace chromeos | 411 } // namespace chromeos |
OLD | NEW |