printer_query.h 3.6 KB

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