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