1// Copyright (c) 2011 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 PRINTING_PRINTING_CONTEXT_H_
6#define PRINTING_PRINTING_CONTEXT_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/callback.h"
12#include "base/strings/string16.h"
13#include "printing/print_settings.h"
14#include "ui/gfx/native_widget_types.h"
15
16namespace base {
17class DictionaryValue;
18}
19
20namespace printing {
21
22// An abstraction of a printer context, implemented by objects that describe the
23// user selected printing context. This includes the OS-dependent UI to ask the
24// user about the print settings. Concrete implementations directly talk to the
25// printer and manage the document and page breaks.
26class PRINTING_EXPORT PrintingContext {
27 public:
28  // Printing context delegate.
29  class Delegate {
30   public:
31    Delegate() {};
32    virtual ~Delegate() {};
33
34    // Returns parent view to use for modal dialogs.
35    virtual gfx::NativeView GetParentView() = 0;
36
37    // Returns application locale.
38    virtual std::string GetAppLocale() = 0;
39  };
40
41  // Tri-state result for user behavior-dependent functions.
42  enum Result {
43    OK,
44    CANCEL,
45    FAILED,
46  };
47
48  virtual ~PrintingContext();
49
50  // Callback of AskUserForSettings, used to notify the PrintJobWorker when
51  // print settings are available.
52  typedef base::Callback<void(Result)> PrintSettingsCallback;
53
54  // Asks the user what printer and format should be used to print. Updates the
55  // context with the select device settings. The result of the call is returned
56  // in the callback. This is necessary for Linux, which only has an
57  // asynchronous printing API.
58  virtual void AskUserForSettings(int max_pages,
59                                  bool has_selection,
60                                  const PrintSettingsCallback& callback) = 0;
61
62  // Selects the user's default printer and format. Updates the context with the
63  // default device settings.
64  virtual Result UseDefaultSettings() = 0;
65
66  // Updates the context with PDF printer settings.
67  Result UsePdfSettings();
68
69  // Returns paper size to be used for PDF or Cloud Print in device units.
70  virtual gfx::Size GetPdfPaperSizeDeviceUnits() = 0;
71
72  // Updates printer settings.
73  // |external_preview| is true if pdf is going to be opened in external
74  // preview. Used by MacOS only now to open Preview.app.
75  virtual Result UpdatePrinterSettings(bool external_preview,
76                                       bool show_system_dialog) = 0;
77
78  // Updates Print Settings. |job_settings| contains all print job
79  // settings information. |ranges| has the new page range settings.
80  Result UpdatePrintSettings(const base::DictionaryValue& job_settings);
81
82  // Initializes with predefined settings.
83  virtual Result InitWithSettings(const PrintSettings& settings) = 0;
84
85  // Does platform specific setup of the printer before the printing. Signal the
86  // printer that a document is about to be spooled.
87  // Warning: This function enters a message loop. That may cause side effects
88  // like IPC message processing! Some printers have side-effects on this call
89  // like virtual printers that ask the user for the path of the saved document;
90  // for example a PDF printer.
91  virtual Result NewDocument(const base::string16& document_name) = 0;
92
93  // Starts a new page.
94  virtual Result NewPage() = 0;
95
96  // Closes the printed page.
97  virtual Result PageDone() = 0;
98
99  // Closes the printing job. After this call the object is ready to start a new
100  // document.
101  virtual Result DocumentDone() = 0;
102
103  // Cancels printing. Can be used in a multi-threaded context. Takes effect
104  // immediately.
105  virtual void Cancel() = 0;
106
107  // Releases the native printing context.
108  virtual void ReleaseContext() = 0;
109
110  // Returns the native context used to print.
111  virtual gfx::NativeDrawingContext context() const = 0;
112
113  // Creates an instance of this object. Implementers of this interface should
114  // implement this method to create an object of their implementation.
115  static scoped_ptr<PrintingContext> Create(Delegate* delegate);
116
117  void set_margin_type(MarginType type);
118
119  const PrintSettings& settings() const {
120    return settings_;
121  }
122
123 protected:
124  explicit PrintingContext(Delegate* delegate);
125
126  // Reinitializes the settings for object reuse.
127  void ResetSettings();
128
129  // Does bookkeeping when an error occurs.
130  PrintingContext::Result OnError();
131
132  // Complete print context settings.
133  PrintSettings settings_;
134
135  // Printing context delegate.
136  Delegate* delegate_;
137
138  // The dialog box has been dismissed.
139  volatile bool dialog_box_dismissed_;
140
141  // Is a print job being done.
142  volatile bool in_print_job_;
143
144  // Did the user cancel the print job.
145  volatile bool abort_printing_;
146
147 private:
148  DISALLOW_COPY_AND_ASSIGN(PrintingContext);
149};
150
151}  // namespace printing
152
153#endif  // PRINTING_PRINTING_CONTEXT_H_
154