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   // Updates the context with PDF printer settings.
     55   Result UsePdfSettings();
     56 
     57   // Returns paper size to be used for PDF or Cloud Print in device units.
     58   virtual gfx::Size GetPdfPaperSizeDeviceUnits() = 0;
     59 
     60   // Updates printer settings.
     61   // |external_preview| is true if pdf is going to be opened in external
     62   // preview. Used by MacOS only now to open Preview.app.
     63   virtual Result UpdatePrinterSettings(bool external_preview) = 0;
     64 
     65   // Updates Print Settings. |job_settings| contains all print job
     66   // settings information. |ranges| has the new page range settings.
     67   Result UpdatePrintSettings(const base::DictionaryValue& job_settings);
     68 
     69   // Initializes with predefined settings.
     70   virtual Result InitWithSettings(const PrintSettings& settings) = 0;
     71 
     72   // Does platform specific setup of the printer before the printing. Signal the
     73   // printer that a document is about to be spooled.
     74   // Warning: This function enters a message loop. That may cause side effects
     75   // like IPC message processing! Some printers have side-effects on this call
     76   // like virtual printers that ask the user for the path of the saved document;
     77   // for example a PDF printer.
     78   virtual Result NewDocument(const base::string16& document_name) = 0;
     79 
     80   // Starts a new page.
     81   virtual Result NewPage() = 0;
     82 
     83   // Closes the printed page.
     84   virtual Result PageDone() = 0;
     85 
     86   // Closes the printing job. After this call the object is ready to start a new
     87   // document.
     88   virtual Result DocumentDone() = 0;
     89 
     90   // Cancels printing. Can be used in a multi-threaded context. Takes effect
     91   // immediately.
     92   virtual void Cancel() = 0;
     93 
     94   // Releases the native printing context.
     95   virtual void ReleaseContext() = 0;
     96 
     97   // Returns the native context used to print.
     98   virtual gfx::NativeDrawingContext context() const = 0;
     99 
    100   // Creates an instance of this object. Implementers of this interface should
    101   // implement this method to create an object of their implementation. The
    102   // caller owns the returned object.
    103   static PrintingContext* Create(const std::string& app_locale);
    104 
    105   void set_margin_type(MarginType type);
    106 
    107   const PrintSettings& settings() const {
    108     return settings_;
    109   }
    110 
    111  protected:
    112   explicit PrintingContext(const std::string& app_locale);
    113 
    114   // Reinitializes the settings for object reuse.
    115   void ResetSettings();
    116 
    117   // Does bookkeeping when an error occurs.
    118   PrintingContext::Result OnError();
    119 
    120   // Complete print context settings.
    121   PrintSettings settings_;
    122 
    123   // The dialog box has been dismissed.
    124   volatile bool dialog_box_dismissed_;
    125 
    126   // Is a print job being done.
    127   volatile bool in_print_job_;
    128 
    129   // Did the user cancel the print job.
    130   volatile bool abort_printing_;
    131 
    132   // The application locale.
    133   std::string app_locale_;
    134 
    135  private:
    136   DISALLOW_COPY_AND_ASSIGN(PrintingContext);
    137 };
    138 
    139 }  // namespace printing
    140 
    141 #endif  // PRINTING_PRINTING_CONTEXT_H_
    142