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 CHROME_BROWSER_UI_METRO_DRIVER_PRINT_HANDLER_H_
6#define CHROME_BROWSER_UI_METRO_DRIVER_PRINT_HANDLER_H_
7
8#include <windows.media.playto.h>
9#include <windows.graphics.printing.h>
10#include <windows.ui.core.h>
11
12#include "base/synchronization/lock.h"
13#include "base/threading/thread.h"
14#include "win8/metro_driver/print_document_source.h"
15#include "win8/metro_driver/winrt_utils.h"
16
17namespace base {
18
19class Lock;
20
21}  // namespace base
22
23namespace metro_driver {
24
25// This class handles the print aspect of the devices charm.
26class PrintHandler {
27 public:
28  PrintHandler();
29  ~PrintHandler();
30
31  HRESULT Initialize(winui::Core::ICoreWindow* window);
32
33  // Called by the exported C functions.
34  static void EnablePrinting(bool printing_enabled);
35  static void SetPageCount(size_t page_count);
36  static void AddPage(size_t page_number, IStream* metafile_stream);
37  static void ShowPrintUI();
38
39 private:
40  // Callbacks from Metro.
41  HRESULT OnPrintRequested(
42      wingfx::Printing::IPrintManager* print_mgr,
43      wingfx::Printing::IPrintTaskRequestedEventArgs* event_args);
44
45  HRESULT OnPrintTaskSourceRequest(
46      wingfx::Printing::IPrintTaskSourceRequestedArgs* args);
47
48  HRESULT OnCompleted(wingfx::Printing::IPrintTask* task,
49                      wingfx::Printing::IPrintTaskCompletedEventArgs* args);
50  // Utility methods.
51  void ClearPrintTask();
52  float GetLogicalDpi();
53
54  // Callback from Metro and entry point called on lockable thread.
55  HRESULT LogicalDpiChanged(IInspectable *sender);
56  static void OnLogicalDpiChanged(float dpi);
57
58  // Called on the lockable thread to set/release the doc.
59  static void SetPrintDocumentSource(
60      const mswr::ComPtr<PrintDocumentSource>& print_document_source);
61  static void ReleasePrintDocumentSource();
62
63  // Called on the lockable thread for the exported C functions.
64  static void OnEnablePrinting(bool printing_enabled);
65  static void OnSetPageCount(size_t page_count);
66  static void OnAddPage(size_t page_number,
67                        mswr::ComPtr<IStream> metafile_stream);
68
69  // Opens the prit device charm. Must be called from the metro thread.
70  static void OnShowPrintUI();
71
72  mswr::ComPtr<wingfx::Printing::IPrintTask> print_task_;
73  EventRegistrationToken print_requested_token_;
74  EventRegistrationToken print_completed_token_;
75  EventRegistrationToken dpi_change_token_;
76
77  mswr::ComPtr<wingfx::Printing::IPrintManager> print_manager_;
78  PrintDocumentSource::DirectXContext directx_context_;
79
80  // Hack to give access to the Print Document from the C style entry points.
81  // This will go away once we can pass a pointer to this interface down to
82  // the Chrome Browser as we send the command to print.
83  static mswr::ComPtr<PrintDocumentSource> current_document_source_;
84
85  // Another hack to enable/disable printing from an exported C function.
86  // TODO(mad): Find a better way to do this...
87  static bool printing_enabled_;
88
89  // This is also a temporary hack until we can pass down the print document
90  // to Chrome so it can call directly into it. We need to lock the access to
91  // current_document_source_.
92  static base::Lock* lock_;
93
94  // This thread is used to send blocking jobs
95  // out of threads we don't want to block.
96  static base::Thread* thread_;
97};
98
99}  // namespace metro_driver
100
101// Exported C functions for Chrome to call into the Metro module.
102extern "C" __declspec(dllexport)
103void MetroEnablePrinting(BOOL printing_enabled);
104
105extern "C" __declspec(dllexport)
106void MetroSetPrintPageCount(size_t page_count);
107
108extern "C" __declspec(dllexport)
109void MetroSetPrintPageContent(size_t current_page,
110                              void* data,
111                              size_t data_size);
112
113extern "C" __declspec(dllexport)
114void MetroShowPrintUI();
115
116#endif  // CHROME_BROWSER_UI_METRO_DRIVER_PRINT_HANDLER_H_
117