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 CHROME_BROWSER_PRINTING_PRINTER_QUERY_H_
6#define CHROME_BROWSER_PRINTING_PRINTER_QUERY_H_
7#pragma once
8
9#include "base/memory/scoped_ptr.h"
10#include "chrome/browser/printing/print_job_worker_owner.h"
11#include "ui/gfx/native_widget_types.h"
12
13class CancelableTask;
14class DictionaryValue;
15class MessageLoop;
16
17namespace base {
18class Thread;
19}
20
21namespace printing {
22
23class PrintJobWorker;
24
25// Query the printer for settings.
26class PrinterQuery : public PrintJobWorkerOwner {
27 public:
28  // GetSettings() UI parameter.
29  enum GetSettingsAskParam {
30    DEFAULTS,
31    ASK_USER,
32  };
33
34  PrinterQuery();
35
36  // PrintJobWorkerOwner
37  virtual void GetSettingsDone(const PrintSettings& new_settings,
38                               PrintingContext::Result result);
39  virtual PrintJobWorker* DetachWorker(PrintJobWorkerOwner* new_owner);
40  virtual MessageLoop* message_loop();
41  virtual const PrintSettings& settings() const;
42  virtual int cookie() const;
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                   bool use_overlays,
53                   CancelableTask* callback);
54
55  // Updates the current settings with |new_settings| dictionary values.
56  void SetSettings(const DictionaryValue& new_settings,
57                   CancelableTask* callback);
58
59  // Stops the worker thread since the client is done with this object.
60  void StopWorker();
61
62  // Returns true if a GetSettings() call is pending completion.
63  bool is_callback_pending() const;
64
65  PrintingContext::Result last_status() const { return last_status_; }
66
67  // Returns if a worker thread is still associated to this instance.
68  bool is_valid() const;
69
70 private:
71  virtual ~PrinterQuery();
72
73  // Lazy create the worker thread. There is one worker thread per print job.
74  // Returns true, if worker thread exists or has been created.
75  bool StartWorker(CancelableTask* callback);
76
77  // Main message loop reference. Used to send notifications in the right
78  // thread.
79  MessageLoop* const io_message_loop_;
80
81  // All the UI is done in a worker thread because many Win32 print functions
82  // are blocking and enters a message loop without your consent. There is one
83  // worker thread per print job.
84  scoped_ptr<PrintJobWorker> worker_;
85
86  // Cache of the print context settings for access in the UI thread.
87  PrintSettings settings_;
88
89  // Is the Print... dialog box currently shown.
90  bool is_print_dialog_box_shown_;
91
92  // Cookie that make this instance unique.
93  int cookie_;
94
95  // Results from the last GetSettingsDone() callback.
96  PrintingContext::Result last_status_;
97
98  // Task waiting to be executed.
99  scoped_ptr<CancelableTask> callback_;
100
101  DISALLOW_COPY_AND_ASSIGN(PrinterQuery);
102};
103
104}  // namespace printing
105
106#endif  // CHROME_BROWSER_PRINTING_PRINTER_QUERY_H_
107