| 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 <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 // Query CUPS asynchronously. Post results back to UI thread. | 181 // Query CUPS asynchronously. Post results back to UI thread. |
| 182 void CupsPrintJobManagerImpl::QueryCups() { | 182 void CupsPrintJobManagerImpl::QueryCups() { |
| 183 std::vector<::printing::CupsJob> jobs = cups_connection_.GetJobs(); | 183 std::vector<::printing::CupsJob> jobs = cups_connection_.GetJobs(); |
| 184 | 184 |
| 185 content::BrowserThread::PostTask( | 185 content::BrowserThread::PostTask( |
| 186 content::BrowserThread::ID::UI, FROM_HERE, | 186 content::BrowserThread::ID::UI, FROM_HERE, |
| 187 base::Bind(&CupsPrintJobManagerImpl::UpdateJobs, | 187 base::Bind(&CupsPrintJobManagerImpl::UpdateJobs, |
| 188 weak_ptr_factory_.GetWeakPtr(), jobs)); | 188 weak_ptr_factory_.GetWeakPtr(), jobs)); |
| 189 } | 189 } |
| 190 | 190 |
| 191 // Use job information to update local job states. Update jobs that are no | 191 // Use job information to update local job states. Previously completed jobs |
| 192 // longer being reported on by CUPS. | 192 // could be in |jobs| but those are ignored as we will not emit updates for them |
| 193 // after they are completed. |
| 193 void CupsPrintJobManagerImpl::UpdateJobs( | 194 void CupsPrintJobManagerImpl::UpdateJobs( |
| 194 const std::vector<::printing::CupsJob>& jobs) { | 195 const std::vector<::printing::CupsJob>& jobs) { |
| 195 std::set<std::string> updated_jobs; | |
| 196 for (auto& job : jobs) { | 196 for (auto& job : jobs) { |
| 197 std::string key = CupsPrintJob::GetUniqueId(job.printer_id, job.id); | 197 std::string key = CupsPrintJob::GetUniqueId(job.printer_id, job.id); |
| 198 if (!base::ContainsKey(jobs_, key)) { | 198 const auto& entry = jobs_.find(key); |
| 199 LOG(WARNING) << "Unexpected print job encountered"; | 199 if (entry != jobs_.end()) { |
| 200 continue; | 200 CupsPrintJob* print_job = entry->second.get(); |
| 201 } | |
| 202 | 201 |
| 203 JobStateUpdated(jobs_[key].get(), ConvertState(job.state)); | 202 // Update a job we're tracking. |
| 204 updated_jobs.insert(key); | 203 JobStateUpdated(print_job, ConvertState(job.state)); |
| 205 } | |
| 206 | 204 |
| 207 // Cleanup completed jobs. | 205 // Cleanup completed jobs. |
| 208 auto it = jobs_.begin(); | 206 if (JobFinished(print_job->state())) { |
| 209 while (it != jobs_.end()) { | 207 jobs_.erase(entry); |
| 210 auto& entry = *it; | 208 } |
| 211 if (!base::ContainsKey(updated_jobs, entry.first)) { | |
| 212 // We are no longer receiving updates for a job. Declare it | |
| 213 // complete. | |
| 214 JobStateUpdated(entry.second.get(), | |
| 215 CupsPrintJob::State::STATE_DOCUMENT_DONE); | |
| 216 } | |
| 217 | |
| 218 CupsPrintJob* job = entry.second.get(); | |
| 219 if (JobFinished(job->state())) { | |
| 220 // Delete job since we will no longer receive events for it. | |
| 221 it = jobs_.erase(it); | |
| 222 } else { | |
| 223 it++; | |
| 224 } | 209 } |
| 225 } | 210 } |
| 226 | 211 |
| 212 // Keep polling until all jobs complete or error. |
| 227 if (!jobs_.empty()) | 213 if (!jobs_.empty()) |
| 228 ScheduleQuery(); | 214 ScheduleQuery(); |
| 229 } | 215 } |
| 230 | 216 |
| 231 void CupsPrintJobManagerImpl::JobStateUpdated(CupsPrintJob* job, | 217 void CupsPrintJobManagerImpl::JobStateUpdated(CupsPrintJob* job, |
| 232 CupsPrintJob::State new_state) { | 218 CupsPrintJob::State new_state) { |
| 233 if (job->state() == new_state) | 219 if (job->state() == new_state) |
| 234 return; | 220 return; |
| 235 | 221 |
| 236 // We don't track state transitions because some of them might be missed due | 222 // We don't track state transitions because some of them might be missed due |
| (...skipping 27 matching lines...) Expand all Loading... |
| 264 break; | 250 break; |
| 265 } | 251 } |
| 266 } | 252 } |
| 267 | 253 |
| 268 // static | 254 // static |
| 269 CupsPrintJobManager* CupsPrintJobManager::CreateInstance(Profile* profile) { | 255 CupsPrintJobManager* CupsPrintJobManager::CreateInstance(Profile* profile) { |
| 270 return new CupsPrintJobManagerImpl(profile); | 256 return new CupsPrintJobManagerImpl(profile); |
| 271 } | 257 } |
| 272 | 258 |
| 273 } // namespace chromeos | 259 } // namespace chromeos |
| OLD | NEW |