| Index: chrome/browser/chromeos/printing/cups_print_job_manager_impl.cc
|
| diff --git a/chrome/browser/chromeos/printing/cups_print_job_manager_impl.cc b/chrome/browser/chromeos/printing/cups_print_job_manager_impl.cc
|
| index 875592b2d1b5e324994418100a637b8fd1f21dbf..468adc44817cf85085233f8c64c358a097b80695 100644
|
| --- a/chrome/browser/chromeos/printing/cups_print_job_manager_impl.cc
|
| +++ b/chrome/browser/chromeos/printing/cups_print_job_manager_impl.cc
|
| @@ -120,33 +120,14 @@ void CupsPrintJobManagerImpl::Observe(
|
| DCHECK(document);
|
| CreatePrintJob(base::UTF16ToUTF8(document->settings().device_name()),
|
| base::UTF16ToUTF8(document->settings().title()),
|
| - document->page_count());
|
| + job_details->job_id(), document->page_count());
|
| }
|
| }
|
|
|
| bool CupsPrintJobManagerImpl::CreatePrintJob(const std::string& printer_name,
|
| const std::string& title,
|
| + int job_id,
|
| int total_page_number) {
|
| - // Of the current jobs, find the new one for the printer.
|
| - ::printing::CupsJob* new_job = nullptr;
|
| - std::vector<::printing::CupsJob> cups_jobs = cups_connection_.GetJobs();
|
| - for (auto& job : cups_jobs) {
|
| - if (printer_name == job.printer_id &&
|
| - !JobFinished(ConvertState(job.state)) &&
|
| - !base::ContainsKey(jobs_,
|
| - CupsPrintJob::GetUniqueId(printer_name, job.id))) {
|
| - // We found an untracked job. It should be ours.
|
| - new_job = &job;
|
| - break;
|
| - }
|
| - }
|
| -
|
| - // The started job cannot be found in the queue.
|
| - if (!new_job) {
|
| - LOG(WARNING) << "Could not track print job.";
|
| - return false;
|
| - }
|
| -
|
| auto printer =
|
| chromeos::PrintersManagerFactory::GetForBrowserContext(profile_)
|
| ->GetPrinter(printer_name);
|
| @@ -157,25 +138,35 @@ bool CupsPrintJobManagerImpl::CreatePrintJob(const std::string& printer_name,
|
| }
|
|
|
| // Create a new print job.
|
| - auto cpj = base::MakeUnique<CupsPrintJob>(*printer, new_job->id, title,
|
| + auto cpj = base::MakeUnique<CupsPrintJob>(*printer, job_id, title,
|
| total_page_number);
|
| std::string key = cpj->GetUniqueId();
|
| jobs_[key] = std::move(cpj);
|
| - NotifyJobCreated(jobs_[key].get());
|
| + CupsPrintJob* job = jobs_[key].get();
|
| + NotifyJobCreated(job);
|
|
|
| - JobStateUpdated(jobs_[key].get(), ConvertState(new_job->state));
|
| + // Always start jobs in the waiting state.
|
| + job->set_state(CupsPrintJob::State::STATE_WAITING);
|
| + NotifyJobUpdated(job);
|
|
|
| - ScheduleQuery();
|
| + ScheduleQuery(base::TimeDelta());
|
|
|
| return true;
|
| }
|
|
|
| void CupsPrintJobManagerImpl::ScheduleQuery() {
|
| - content::BrowserThread::PostDelayedTask(
|
| - content::BrowserThread::FILE_USER_BLOCKING, FROM_HERE,
|
| - base::Bind(&CupsPrintJobManagerImpl::QueryCups,
|
| - weak_ptr_factory_.GetWeakPtr()),
|
| - base::TimeDelta::FromMilliseconds(kPollRate));
|
| + ScheduleQuery(base::TimeDelta::FromMilliseconds(kPollRate));
|
| +}
|
| +
|
| +void CupsPrintJobManagerImpl::ScheduleQuery(const base::TimeDelta& delay) {
|
| + if (!in_query_) {
|
| + in_query_ = true;
|
| + content::BrowserThread::PostDelayedTask(
|
| + content::BrowserThread::FILE_USER_BLOCKING, FROM_HERE,
|
| + base::Bind(&CupsPrintJobManagerImpl::QueryCups,
|
| + weak_ptr_factory_.GetWeakPtr()),
|
| + base::TimeDelta::FromMilliseconds(kPollRate));
|
| + }
|
| }
|
|
|
| // Query CUPS asynchronously. Post results back to UI thread.
|
| @@ -193,6 +184,9 @@ void CupsPrintJobManagerImpl::QueryCups() {
|
| // after they are completed.
|
| void CupsPrintJobManagerImpl::UpdateJobs(
|
| const std::vector<::printing::CupsJob>& jobs) {
|
| + in_query_ = false;
|
| +
|
| + std::vector<std::string> active_jobs;
|
| for (auto& job : jobs) {
|
| std::string key = CupsPrintJob::GetUniqueId(job.printer_id, job.id);
|
| const auto& entry = jobs_.find(key);
|
| @@ -205,13 +199,26 @@ void CupsPrintJobManagerImpl::UpdateJobs(
|
| // Cleanup completed jobs.
|
| if (JobFinished(print_job->state())) {
|
| jobs_.erase(entry);
|
| + } else {
|
| + active_jobs.push_back(key);
|
| }
|
| }
|
| }
|
|
|
| // Keep polling until all jobs complete or error.
|
| - if (!jobs_.empty())
|
| + if (!active_jobs.empty()) {
|
| ScheduleQuery();
|
| + } else if (!jobs_.empty()) {
|
| + // We're tracking jobs that we didn't receive an update for. Something bad
|
| + // has happened.
|
| + LOG(ERROR) << "Lost track of (" << jobs_.size() << ") jobs";
|
| + for (const auto& entry : jobs_) {
|
| + // Declare all lost jobs errors.
|
| + JobStateUpdated(entry.second.get(), CupsPrintJob::State::STATE_ERROR);
|
| + }
|
| +
|
| + jobs_.clear();
|
| + }
|
| }
|
|
|
| void CupsPrintJobManagerImpl::JobStateUpdated(CupsPrintJob* job,
|
|
|