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 103 104 105 106
|
// Copyright 2016 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/printing/cups_print_job_manager.h"
#include <algorithm>
#include "base/metrics/histogram_macros.h"
#include "chrome/browser/ash/printing/cups_print_job.h"
#include "chrome/browser/ash/printing/cups_print_job_notification_manager.h"
namespace ash {
CupsPrintJobManager::CupsPrintJobManager(Profile* profile) : profile_(profile) {
notification_manager_ =
std::make_unique<CupsPrintJobNotificationManager>(profile, this);
}
CupsPrintJobManager::~CupsPrintJobManager() = default;
void CupsPrintJobManager::Shutdown() {}
void CupsPrintJobManager::AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}
void CupsPrintJobManager::RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}
void CupsPrintJobManager::NotifyJobCreated(base::WeakPtr<CupsPrintJob> job) {
for (Observer& observer : observers_)
observer.OnPrintJobCreated(job);
}
void CupsPrintJobManager::NotifyJobStarted(base::WeakPtr<CupsPrintJob> job) {
DCHECK(job);
print_job_start_times_[job->GetUniqueId()] = base::TimeTicks::Now();
for (Observer& observer : observers_)
observer.OnPrintJobStarted(job);
}
void CupsPrintJobManager::NotifyJobUpdated(base::WeakPtr<CupsPrintJob> job) {
for (Observer& observer : observers_)
observer.OnPrintJobUpdated(job);
}
void CupsPrintJobManager::NotifyJobResumed(base::WeakPtr<CupsPrintJob> job) {
for (Observer& observer : observers_)
observer.OnPrintJobResumed(job);
}
void CupsPrintJobManager::NotifyJobSuspended(base::WeakPtr<CupsPrintJob> job) {
for (Observer& observer : observers_)
observer.OnPrintJobSuspended(job);
}
void CupsPrintJobManager::NotifyJobCanceled(base::WeakPtr<CupsPrintJob> job) {
RecordJobDuration(job);
for (Observer& observer : observers_)
observer.OnPrintJobCancelled(job);
}
void CupsPrintJobManager::NotifyJobFailed(base::WeakPtr<CupsPrintJob> job) {
for (Observer& observer : observers_)
observer.OnPrintJobError(job);
}
void CupsPrintJobManager::NotifyJobDone(base::WeakPtr<CupsPrintJob> job) {
RecordJobDuration(job);
for (Observer& observer : observers_)
observer.OnPrintJobDone(job);
}
// TODO(http://crbug.com/883966): In some instances, Chrome doesn't receive an
// error state from the printer. For that reason, the job duration is currently
// recorded for done and cancelled print jobs without accounting for the added
// time a job may spend in a suspended or error state.
void CupsPrintJobManager::RecordJobDuration(base::WeakPtr<CupsPrintJob> job) {
DCHECK(job);
auto it = print_job_start_times_.find(job->GetUniqueId());
if (it == print_job_start_times_.end())
return;
base::TimeDelta duration = base::TimeTicks::Now() - it->second;
switch (job->state()) {
case CupsPrintJob::State::STATE_DOCUMENT_DONE:
UMA_HISTOGRAM_LONG_TIMES_100("Printing.CUPS.JobDuration.JobDone",
duration);
break;
case CupsPrintJob::State::STATE_CANCELLED:
UMA_HISTOGRAM_LONG_TIMES_100("Printing.CUPS.JobDuration.JobCancelled",
duration);
break;
default:
break;
}
print_job_start_times_.erase(job->GetUniqueId());
}
} // namespace ash
|