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 ANDROID_WEBVIEW_BROWSER_RENDER_HOST_PRINT_MANAGER_H_
6#define ANDROID_WEBVIEW_BROWSER_RENDER_HOST_PRINT_MANAGER_H_
7
8#include "content/public/browser/web_contents_observer.h"
9
10#include "base/callback_forward.h"
11#include "base/threading/non_thread_safe.h"
12
13class GURL;
14
15namespace base {
16class FileDescriptor;
17}
18
19namespace printing {
20class PrintSettings;
21}
22
23namespace android_webview {
24
25class PrintManagerDelegate {
26 public:
27  virtual ~PrintManagerDelegate() { }
28  virtual void DidExportPdf(bool success) = 0;
29  virtual bool IsCancelled() = 0;
30
31 private:
32  //  DISALLOW_COPY_AND_ASSIGN(PrintManagerDelegate);
33};
34
35// Provides RenderViewHost wrapper functionality for sending WebView-specific
36// IPC messages to the renderer and from there to WebKit.
37//
38// Note for android_webview:
39// This class manages the print (an export to PDF file essentially) task for
40// a webview. There can be at most one active print task per webview.
41class PrintManager : public content::WebContentsObserver,
42                     public base::NonThreadSafe {
43 public:
44  // To send receive messages to a RenderView we take the WebContents instance,
45  // as it internally handles RenderViewHost instances changing underneath us.
46  PrintManager(content::WebContents* contents,
47               printing::PrintSettings* settings,
48               int fd,
49               PrintManagerDelegate* delegate);
50  virtual ~PrintManager();
51
52  // Prints the current document immediately. Since the rendering is
53  // asynchronous, the actual printing will not be completed on the return of
54  // this function. Returns false if printing is impossible at the moment.
55  //
56  // Note for webview: Returns false immediately if this print manager is already
57  // busy printing.
58  bool PrintNow();
59
60 private:
61  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
62  void OnDidGetPrintedPagesCount(int cookie, int number_pages);
63  void OnDidGetDocumentCookie(int cookie);
64  void OnPrintingFailed(int cookie);
65  void OnGetDefaultPrintSettingsReply(IPC::Message* reply_msg);
66  void OnGetDefaultPrintSettings(IPC::Message* reply_msg);
67  void OnAllocateTempFileForPrinting(base::FileDescriptor* temp_file_fd,
68                                     int* sequence_number);
69  void OnTempFileForPrintingWritten(int sequence_number);
70
71  // Print Settings.
72  printing::PrintSettings* settings_;
73
74  // File descriptor to export to.
75  int fd_;
76  // Print manager delegate
77  PrintManagerDelegate* delegate_;
78  // Number of pages to print in the print job.
79  int number_pages_;
80  // The document cookie of the current PrinterQuery.
81  int cookie_;
82  // Whether a print task is pending.
83  int printing_;
84
85  DISALLOW_COPY_AND_ASSIGN(PrintManager);
86};
87
88}  // namespace android_webview
89
90#endif  // ANDROID_WEBVIEW_BROWSER_RENDER_HOST_PRINT_MANAGER_H_
91