Home | History | Annotate | Download | only in cloud_print
      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.
      4 
      5 #ifndef CHROME_SERVICE_CLOUD_PRINT_PRINTER_JOB_QUEUE_HANDLER_H_
      6 #define CHROME_SERVICE_CLOUD_PRINT_PRINTER_JOB_QUEUE_HANDLER_H_
      7 
      8 #include <map>
      9 #include <string>
     10 #include <utility>
     11 #include <vector>
     12 
     13 #include "base/files/file_path.h"
     14 #include "base/memory/ref_counted.h"
     15 #include "base/memory/scoped_ptr.h"
     16 #include "base/memory/weak_ptr.h"
     17 #include "base/time/time.h"
     18 #include "chrome/common/cloud_print/cloud_print_constants.h"
     19 
     20 namespace base {
     21 class DictionaryValue;
     22 }
     23 
     24 namespace cloud_print {
     25 
     26 struct JobDetails {
     27   JobDetails();
     28   ~JobDetails();
     29   void Clear();
     30   static bool ordering(const JobDetails& first, const JobDetails& second);
     31 
     32   std::string job_id_;
     33   std::string job_title_;
     34 
     35   std::string print_ticket_url_;
     36   std::string print_data_url_;
     37 
     38   std::string print_ticket_;
     39   std::string print_ticket_mime_type_;
     40   base::FilePath print_data_file_path_;
     41   std::string print_data_mime_type_;
     42 
     43   std::vector<std::string> tags_;
     44 
     45   base::TimeDelta time_remaining_;
     46 };
     47 
     48 // class containing logic for print job backoff
     49 
     50 class PrinterJobQueueHandler {
     51  public:
     52   class TimeProvider {
     53    public:
     54     virtual base::Time GetNow() = 0;
     55     virtual ~TimeProvider() {}
     56   };
     57 
     58   // PrinterJobQueueHandler takes ownership of |time_provider| and is
     59   // responsible for deleting it.
     60   explicit PrinterJobQueueHandler(TimeProvider* time_provider);
     61   PrinterJobQueueHandler();
     62   ~PrinterJobQueueHandler();
     63 
     64   // jobs will be filled with details of all jobs in the queue, sorted by time
     65   // until they are ready to print, lowest to highest. Jobs that are ready to
     66   // print will have a time_remaining_ of 0.
     67   void GetJobsFromQueue(const base::DictionaryValue* json_data,
     68                         std::vector<JobDetails>* jobs);
     69 
     70   // Marks a job fetch as failed. Returns "true" if the job will be retried.
     71   bool JobFetchFailed(const std::string& job_id);
     72 
     73   void JobDone(const std::string& job_id);
     74 
     75  private:
     76   scoped_ptr<TimeProvider> time_provider_;
     77 
     78   struct FailedJobMetadata {
     79     int retries_;
     80     base::Time last_retry_;
     81   };
     82 
     83   typedef std::map<std::string, FailedJobMetadata> FailedJobMap;
     84   typedef std::pair<std::string, FailedJobMetadata> FailedJobPair;
     85 
     86   FailedJobMap failed_job_map_;
     87 
     88   void ConstructJobDetailsFromJson(const base::DictionaryValue* json_data,
     89                                    JobDetails* details_obj);
     90   base::TimeDelta ComputeBackoffTime(const std::string& job_id);
     91 
     92   DISALLOW_COPY_AND_ASSIGN(PrinterJobQueueHandler);
     93 };
     94 
     95 }  // namespace cloud_print
     96 
     97 #endif  // CHROME_SERVICE_CLOUD_PRINT_PRINTER_JOB_QUEUE_HANDLER_H_
     98 
     99 
    100