Home | History | Annotate | Download | only in printing
      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_PRINTER_QUERY_H_
      6 #define CHROME_BROWSER_PRINTING_PRINTER_QUERY_H_
      7 
      8 #include "base/callback.h"
      9 #include "base/compiler_specific.h"
     10 #include "base/memory/scoped_ptr.h"
     11 #include "chrome/browser/printing/print_job_worker_owner.h"
     12 #include "printing/print_job_constants.h"
     13 
     14 class PrintingUIWebContentsObserver;
     15 
     16 namespace base {
     17 class DictionaryValue;
     18 class MessageLoop;
     19 }
     20 
     21 namespace printing {
     22 
     23 class PrintDestinationInterface;
     24 class PrintJobWorker;
     25 
     26 // Query the printer for settings.
     27 class PrinterQuery : public PrintJobWorkerOwner {
     28  public:
     29   // GetSettings() UI parameter.
     30   enum GetSettingsAskParam {
     31     DEFAULTS,
     32     ASK_USER,
     33   };
     34 
     35   PrinterQuery();
     36 
     37   // PrintJobWorkerOwner implementation.
     38   virtual void GetSettingsDone(const PrintSettings& new_settings,
     39                                PrintingContext::Result result) OVERRIDE;
     40   virtual PrintJobWorker* DetachWorker(PrintJobWorkerOwner* new_owner) OVERRIDE;
     41   virtual base::MessageLoop* message_loop() OVERRIDE;
     42   virtual const PrintSettings& settings() const OVERRIDE;
     43   virtual int cookie() const OVERRIDE;
     44 
     45   // Initializes the printing context. It is fine to call this function multiple
     46   // times to reinitialize the settings. |web_contents_observer| can be queried
     47   // to find the owner of the print setting dialog box. It is unused when
     48   // |ask_for_user_settings| is DEFAULTS.
     49   void GetSettings(
     50       GetSettingsAskParam ask_user_for_settings,
     51       scoped_ptr<PrintingUIWebContentsObserver> web_contents_observer,
     52       int expected_page_count,
     53       bool has_selection,
     54       MarginType margin_type,
     55       const base::Closure& callback);
     56 
     57   // Updates the current settings with |new_settings| dictionary values.
     58   void SetSettings(const base::DictionaryValue& new_settings,
     59                    const base::Closure& callback);
     60 
     61   // Set a destination for the worker.
     62   void SetWorkerDestination(PrintDestinationInterface* destination);
     63 
     64   // Stops the worker thread since the client is done with this object.
     65   void StopWorker();
     66 
     67   // Returns true if a GetSettings() call is pending completion.
     68   bool is_callback_pending() const;
     69 
     70   PrintingContext::Result last_status() const { return last_status_; }
     71 
     72   // Returns if a worker thread is still associated to this instance.
     73   bool is_valid() const;
     74 
     75  private:
     76   virtual ~PrinterQuery();
     77 
     78   // Lazy create the worker thread. There is one worker thread per print job.
     79   void StartWorker(const base::Closure& callback);
     80 
     81   // Main message loop reference. Used to send notifications in the right
     82   // thread.
     83   base::MessageLoop* const io_message_loop_;
     84 
     85   // All the UI is done in a worker thread because many Win32 print functions
     86   // are blocking and enters a message loop without your consent. There is one
     87   // worker thread per print job.
     88   scoped_ptr<PrintJobWorker> worker_;
     89 
     90   // Cache of the print context settings for access in the UI thread.
     91   PrintSettings settings_;
     92 
     93   // Is the Print... dialog box currently shown.
     94   bool is_print_dialog_box_shown_;
     95 
     96   // Cookie that make this instance unique.
     97   int cookie_;
     98 
     99   // Results from the last GetSettingsDone() callback.
    100   PrintingContext::Result last_status_;
    101 
    102   // Callback waiting to be run.
    103   base::Closure callback_;
    104 
    105   DISALLOW_COPY_AND_ASSIGN(PrinterQuery);
    106 };
    107 
    108 }  // namespace printing
    109 
    110 #endif  // CHROME_BROWSER_PRINTING_PRINTER_QUERY_H_
    111