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