print_job_worker.h revision 8bcbed890bc3ce4d7a057a8f32cab53fa534672e
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
58bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#ifndef CHROME_BROWSER_PRINTING_PRINT_JOB_WORKER_H_
68bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#define CHROME_BROWSER_PRINTING_PRINT_JOB_WORKER_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/ref_counted.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/weak_ptr.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/threading/thread.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "printing/page_number.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "printing/print_destination_interface.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "printing/printing_context.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "printing/print_job_constants.h"
168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)class PrintingUIWebContentsObserver;
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace base {
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class DictionaryValue;
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace printing {
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class PrintedDocument;
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class PrintedPage;
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class PrintJob;
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class PrintJobWorkerOwner;
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Worker thread code. All this code, except for the constructor, is executed in
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the worker thread. It manages the PrintingContext, which can be blocking
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// and/or run a message loop. This is the object that generates most
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// NOTIFY_PRINT_JOB_EVENT notifications, but they are generated through a
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// NotificationTask task to be executed from the right thread, the UI thread.
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// PrintJob always outlives its worker instance.
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class PrintJobWorker : public base::Thread {
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit PrintJobWorker(PrintJobWorkerOwner* owner);
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~PrintJobWorker();
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetNewOwner(PrintJobWorkerOwner* new_owner);
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set a destination for print.
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This supercedes the document's rendering destination.
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetPrintDestination(PrintDestinationInterface* destination);
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initializes the print settings. If |ask_user_for_settings| is true, a
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Print... dialog box will be shown to ask the user his preference.
498bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  void GetSettings(
508bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      bool ask_user_for_settings,
518bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      scoped_ptr<PrintingUIWebContentsObserver> web_contents_observer,
528bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      int document_page_count,
538bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      bool has_selection,
548bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      MarginType margin_type);
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set the new print settings. This function takes ownership of
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |new_settings|.
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetSettings(const base::DictionaryValue* const new_settings);
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Starts the printing loop. Every pages are printed as soon as the data is
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // available. Makes sure the new_document is the right one.
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void StartPrinting(PrintedDocument* new_document);
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Updates the printed document.
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void OnDocumentChanged(PrintedDocument* new_document);
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Dequeues waiting pages. Called when PrintJob receives a
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // NOTIFY_PRINTED_DOCUMENT_UPDATED notification. It's time to look again if
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the next page can be printed.
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void OnNewPage();
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This is the only function that can be called in a thread.
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Cancel();
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Retrieves the context for testing only.
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PrintingContext* printing_context() { return printing_context_.get(); }
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The shared NotificationService service can only be accessed from the UI
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // thread, so this class encloses the necessary information to send the
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // notification from the right thread. Most NOTIFY_PRINT_JOB_EVENT
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // notifications are sent this way, except USER_INIT_DONE, USER_INIT_CANCELED
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // and DEFAULT_INIT_DONE. These three are sent through PrintJob::InitDone().
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class NotificationTask;
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Renders a page in the printer.
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SpoolPage(PrintedPage* page);
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Closes the job since spooling is done.
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void OnDocumentDone();
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Discards the current document, the current page and cancels the printing
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // context.
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void OnFailure();
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Asks the user for print settings. Must be called on the UI thread.
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Required on Mac and Linux. Windows can display UI from non-main threads,
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // but sticks with this for consistency.
1008bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  void GetSettingsWithUI(
1018bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      scoped_ptr<PrintingUIWebContentsObserver> web_contents_observer,
1028bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      int document_page_count,
1038bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      bool has_selection);
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The callback used by PrintingContext::GetSettingsWithUI() to notify this
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // object that the print settings are set.  This is needed in order to bounce
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // back into the IO thread for GetSettingsDone().
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void GetSettingsWithUIDone(PrintingContext::Result result);
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called on the UI thread to update the print settings. This function takes
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the ownership of |new_settings|.
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void UpdatePrintSettings(const base::DictionaryValue* const new_settings);
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Reports settings back to owner_.
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void GetSettingsDone(PrintingContext::Result result);
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Use the default settings. When using GTK+ or Mac, this can still end up
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // displaying a dialog. So this needs to happen from the UI thread on these
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // systems.
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void UseDefaultSettings();
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Information about the printer setting.
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<PrintingContext> printing_context_;
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The printed document. Only has read-only access.
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<PrintedDocument> document_;
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The print destination, may be NULL.
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<PrintDestinationInterface> destination_;
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The print job owning this worker thread. It is guaranteed to outlive this
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // object.
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PrintJobWorkerOwner* owner_;
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Current page number to print.
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PageNumber page_number_;
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Used to generate a WeakPtr for callbacks.
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::WeakPtrFactory<PrintJobWorker> weak_factory_;
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(PrintJobWorker);
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace printing
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1468bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#endif  // CHROME_BROWSER_PRINTING_PRINT_JOB_WORKER_H_
147