File: print_job_manager.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (123 lines) | stat: -rw-r--r-- 3,793 bytes parent folder | download | duplicates (11)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_PRINTING_PRINT_JOB_MANAGER_H_
#define CHROME_BROWSER_PRINTING_PRINT_JOB_MANAGER_H_

#include <memory>
#include <set>
#include <vector>

#include "base/callback_list.h"
#include "base/memory/ref_counted.h"
#include "base/synchronization/lock.h"

namespace content {
struct GlobalRenderFrameHostId;
}

namespace printing {

class PrintJob;
class PrintedDocument;
class PrinterQuery;

class PrintQueriesQueue : public base::RefCountedThreadSafe<PrintQueriesQueue> {
 public:
  PrintQueriesQueue();

  PrintQueriesQueue(const PrintQueriesQueue&) = delete;
  PrintQueriesQueue& operator=(const PrintQueriesQueue&) = delete;

  // Queues a semi-initialized worker thread. Can be called from any thread.
  // Current use case is queuing from the I/O thread.
  // TODO(maruel):  Have them vanish after a timeout (~5 minutes?)
  void QueuePrinterQuery(std::unique_ptr<PrinterQuery> query);

  // Pops a queued PrinterQuery object that was previously queued.  Returns
  // nullptr if there is no query matching `document_cookie`.
  // Can be called from any thread.
  std::unique_ptr<PrinterQuery> PopPrinterQuery(int document_cookie);

  // Creates new query. Virtual so that tests can override it.
  virtual std::unique_ptr<PrinterQuery> CreatePrinterQuery(
      content::GlobalRenderFrameHostId rfh_id);

  void Shutdown();

 protected:
  // Protected for unit tests.
  virtual ~PrintQueriesQueue();

 private:
  friend class base::RefCountedThreadSafe<PrintQueriesQueue>;
  using PrinterQueries = std::vector<std::unique_ptr<PrinterQuery>>;

  // Used to serialize access to `queued_queries_`.
  base::Lock lock_;

  PrinterQueries queued_queries_;
};

class PrintJobManager {
 public:
  PrintJobManager();

  PrintJobManager(const PrintJobManager&) = delete;
  PrintJobManager& operator=(const PrintJobManager&) = delete;

  ~PrintJobManager();

  using DocDoneCallbackList = base::RepeatingCallbackList<
      void(PrintJob* job, PrintedDocument* document, int job_id)>;
  using DocDoneCallback = DocDoneCallbackList::CallbackType;

  // Call this method to be informed of DocDone events for all PrintJob
  // instances.
  // NOTE: If you need to be invoked of such events only for a specific
  // instance, you should instead observe that instance via PrintJob::Observer.
  base::CallbackListSubscription AddDocDoneCallback(DocDoneCallback callback);

  // On browser quit, we should wait to have the print job finished.
  void Shutdown();

  // Returns queries queue. Never returns NULL. Must be called on Browser UI
  // Thread. Reference could be stored and used from any thread.
  scoped_refptr<PrintQueriesQueue> queue();

  // Sets the queries queue for testing.
  void SetQueueForTest(scoped_refptr<PrintQueriesQueue> queue);

  // Invoked by PrintJob when printing is started.
  void OnStarted(PrintJob* print_job);

  // Invoked by PrintJob when a document is done.
  void OnDocDone(PrintJob* print_job, PrintedDocument* document, int job_id);

  // Invoked by PrintJob when the job is done.
  void OnJobDone(PrintJob* print_job);

  // Invoked by PrintJob when the job has failed.
  void OnFailed(PrintJob* print_job);

 private:
  using PrintJobs = std::set<scoped_refptr<PrintJob>>;

  // Stops all printing jobs. If wait_for_finish is true, tries to give jobs
  // a chance to complete before stopping them.
  void StopJobs(bool wait_for_finish);

  // Current print jobs that are active.
  PrintJobs current_jobs_;

  scoped_refptr<PrintQueriesQueue> queue_;

  DocDoneCallbackList callback_list_;

  bool is_shutdown_ = false;
};

}  // namespace printing

#endif  // CHROME_BROWSER_PRINTING_PRINT_JOB_MANAGER_H_