Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / service / cloud_print / printer_job_queue_handler.cc
blob8c5b2e8c33febefd1536b72097fefac79dc46a7c
1 // Copyright (c) 2013 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.
5 #include "chrome/service/cloud_print/printer_job_queue_handler.h"
7 #include <math.h>
9 #include <algorithm>
11 #include "base/values.h"
13 namespace cloud_print {
15 class TimeProviderImpl : public PrinterJobQueueHandler::TimeProvider {
16 public:
17 virtual base::Time GetNow() OVERRIDE;
20 base::Time TimeProviderImpl::GetNow() {
21 return base::Time::Now();
24 JobDetails::JobDetails() {}
26 JobDetails::~JobDetails() {}
28 void JobDetails::Clear() {
29 job_id_.clear();
30 job_title_.clear();
31 print_ticket_.clear();
32 print_data_mime_type_.clear();
33 print_data_file_path_ = base::FilePath();
34 print_data_url_.clear();
35 print_ticket_url_.clear();
36 tags_.clear();
37 time_remaining_ = base::TimeDelta();
40 // static
41 bool JobDetails::ordering(const JobDetails& first, const JobDetails& second) {
42 return first.time_remaining_ < second.time_remaining_;
45 PrinterJobQueueHandler::PrinterJobQueueHandler(TimeProvider* time_provider)
46 : time_provider_(time_provider) {}
48 PrinterJobQueueHandler::PrinterJobQueueHandler()
49 : time_provider_(new TimeProviderImpl()) {}
51 PrinterJobQueueHandler::~PrinterJobQueueHandler() {}
53 void PrinterJobQueueHandler::ConstructJobDetailsFromJson(
54 const base::DictionaryValue* job_data,
55 JobDetails* job_details) {
56 job_details->Clear();
58 job_data->GetString(kIdValue, &job_details->job_id_);
59 job_data->GetString(kTitleValue, &job_details->job_title_);
61 job_data->GetString(kTicketUrlValue, &job_details->print_ticket_url_);
62 job_data->GetString(kFileUrlValue, &job_details->print_data_url_);
64 // Get tags for print job.
65 const base::ListValue* tags = NULL;
66 if (job_data->GetList(kTagsValue, &tags)) {
67 for (size_t i = 0; i < tags->GetSize(); i++) {
68 std::string value;
69 if (tags->GetString(i, &value))
70 job_details->tags_.push_back(value);
75 base::TimeDelta PrinterJobQueueHandler::ComputeBackoffTime(
76 const std::string& job_id) {
77 FailedJobMap::const_iterator job_location = failed_job_map_.find(job_id);
78 if (job_location == failed_job_map_.end()) {
79 return base::TimeDelta();
82 base::TimeDelta backoff_time =
83 base::TimeDelta::FromSeconds(kJobFirstWaitTimeSecs);
84 backoff_time *=
85 // casting argument to double and result to uint64 to avoid compilation
86 // issues
87 static_cast<int64>(pow(
88 static_cast<long double>(kJobWaitTimeExponentialMultiplier),
89 job_location->second.retries_) + 0.5);
90 base::Time scheduled_retry =
91 job_location->second.last_retry_ + backoff_time;
92 base::Time now = time_provider_->GetNow();
93 base::TimeDelta time_remaining;
95 if (scheduled_retry < now) {
96 return base::TimeDelta();
98 return scheduled_retry - now;
101 void PrinterJobQueueHandler::GetJobsFromQueue(
102 const base::DictionaryValue* json_data,
103 std::vector<JobDetails>* jobs) {
104 std::vector<JobDetails> jobs_with_timeouts;
106 jobs->clear();
108 const base::ListValue* job_list = NULL;
109 if (!json_data->GetList(kJobListValue, &job_list)) {
110 return;
113 size_t list_size = job_list->GetSize();
114 for (size_t cur_job = 0; cur_job < list_size; cur_job++) {
115 const base::DictionaryValue* job_data = NULL;
116 if (job_list->GetDictionary(cur_job, &job_data)) {
117 JobDetails job_details_current;
118 ConstructJobDetailsFromJson(job_data, &job_details_current);
120 job_details_current.time_remaining_ =
121 ComputeBackoffTime(job_details_current.job_id_);
123 if (job_details_current.time_remaining_ == base::TimeDelta()) {
124 jobs->push_back(job_details_current);
125 } else {
126 jobs_with_timeouts.push_back(job_details_current);
131 sort(jobs_with_timeouts.begin(), jobs_with_timeouts.end(),
132 &JobDetails::ordering);
133 jobs->insert(jobs->end(), jobs_with_timeouts.begin(),
134 jobs_with_timeouts.end());
137 void PrinterJobQueueHandler::JobDone(const std::string& job_id) {
138 failed_job_map_.erase(job_id);
141 bool PrinterJobQueueHandler::JobFetchFailed(const std::string& job_id) {
142 FailedJobMetadata metadata;
143 metadata.retries_ = 0;
144 metadata.last_retry_ = time_provider_->GetNow();
146 std::pair<FailedJobMap::iterator, bool> job_found =
147 failed_job_map_.insert(FailedJobPair(job_id, metadata));
149 // If the job has already failed once, increment the number of retries.
150 // If it has failed too many times, remove it from the map and tell the caller
151 // to report a failure.
152 if (!job_found.second) {
153 if (job_found.first->second.retries_ >= kNumRetriesBeforeAbandonJob) {
154 failed_job_map_.erase(job_found.first);
155 return false;
158 job_found.first->second.retries_ += 1;
159 job_found.first->second.last_retry_ = time_provider_->GetNow();
162 return true;
165 } // namespace cloud_print