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