Home | History | Annotate | Download | only in printing
      1 // Copyright (c) 2012 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_BROWSER_PRINTING_PRINT_JOB_MANAGER_H_
      6 #define CHROME_BROWSER_PRINTING_PRINT_JOB_MANAGER_H_
      7 
      8 #include <set>
      9 #include <vector>
     10 
     11 #include "base/logging.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "base/synchronization/lock.h"
     14 #include "base/threading/non_thread_safe.h"
     15 #include "content/public/browser/notification_observer.h"
     16 #include "content/public/browser/notification_registrar.h"
     17 #include "printing/print_destination_interface.h"
     18 
     19 namespace printing {
     20 
     21 class JobEventDetails;
     22 class PrintJob;
     23 class PrinterQuery;
     24 
     25 class PrintQueriesQueue : public base::RefCountedThreadSafe<PrintQueriesQueue> {
     26  public:
     27   PrintQueriesQueue();
     28 
     29   // Sets the print destination to be set on the next print job.
     30   void SetDestination(PrintDestinationInterface* destination);
     31 
     32   // Queues a semi-initialized worker thread. Can be called from any thread.
     33   // Current use case is queuing from the I/O thread.
     34   // TODO(maruel):  Have them vanish after a timeout (~5 minutes?)
     35   void QueuePrinterQuery(PrinterQuery* job);
     36 
     37   // Pops a queued PrintJobWorkerOwner object that was previously queued or
     38   // create new one. Can be called from any thread.
     39   scoped_refptr<PrinterQuery> PopPrinterQuery(int document_cookie);
     40 
     41   // Creates new query.
     42   scoped_refptr<PrinterQuery> CreatePrinterQuery();
     43 
     44   void Shutdown();
     45 
     46  private:
     47   friend class base::RefCountedThreadSafe<PrintQueriesQueue>;
     48   typedef std::vector<scoped_refptr<PrinterQuery> > PrinterQueries;
     49 
     50   virtual ~PrintQueriesQueue();
     51 
     52   // Used to serialize access to queued_workers_.
     53   base::Lock lock_;
     54 
     55   PrinterQueries queued_queries_;
     56 
     57   scoped_refptr<PrintDestinationInterface> destination_;
     58 
     59   DISALLOW_COPY_AND_ASSIGN(PrintQueriesQueue);
     60 };
     61 
     62 class PrintJobManager : public content::NotificationObserver {
     63  public:
     64   PrintJobManager();
     65   virtual ~PrintJobManager();
     66 
     67   // On browser quit, we should wait to have the print job finished.
     68   void Shutdown();
     69 
     70   // content::NotificationObserver
     71   virtual void Observe(int type,
     72                        const content::NotificationSource& source,
     73                        const content::NotificationDetails& details) OVERRIDE;
     74 
     75   // Returns queries queue. Never returns NULL. Must be called on Browser UI
     76   // Thread. Reference could be stored and used from any thread.
     77   scoped_refptr<PrintQueriesQueue> queue();
     78 
     79  private:
     80   typedef std::set<scoped_refptr<PrintJob> > PrintJobs;
     81 
     82   // Processes a NOTIFY_PRINT_JOB_EVENT notification.
     83   void OnPrintJobEvent(PrintJob* print_job,
     84                        const JobEventDetails& event_details);
     85 
     86   // Stops all printing jobs. If wait_for_finish is true, tries to give jobs
     87   // a chance to complete before stopping them.
     88   void StopJobs(bool wait_for_finish);
     89 
     90   content::NotificationRegistrar registrar_;
     91 
     92   // Current print jobs that are active.
     93   PrintJobs current_jobs_;
     94 
     95   scoped_refptr<PrintQueriesQueue> queue_;
     96 
     97   bool is_shutdown_;
     98 
     99   DISALLOW_COPY_AND_ASSIGN(PrintJobManager);
    100 };
    101 
    102 }  // namespace printing
    103 
    104 #endif  // CHROME_BROWSER_PRINTING_PRINT_JOB_MANAGER_H_
    105