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