OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "printing/backend/print_backend.h" | 5 #include "printing/backend/print_backend.h" |
6 | 6 |
7 #include <objidl.h> | 7 #include <objidl.h> |
8 #include <winspool.h> | 8 #include <winspool.h> |
9 | 9 |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/string_piece.h" | 11 #include "base/string_piece.h" |
12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
13 #include "base/win/scoped_bstr.h" | 13 #include "base/win/scoped_bstr.h" |
14 #include "base/win/scoped_comptr.h" | 14 #include "base/win/scoped_comptr.h" |
15 #include "base/win/scoped_hglobal.h" | 15 #include "base/win/scoped_hglobal.h" |
16 #include "printing/backend/print_backend_consts.h" | 16 #include "printing/backend/print_backend_consts.h" |
17 #include "printing/backend/win_helper.h" | 17 #include "printing/backend/win_helper.h" |
18 | 18 |
19 using base::win::ScopedBstr; | |
20 using base::win::ScopedComPtr; | |
21 | |
22 namespace { | 19 namespace { |
23 | 20 |
24 HRESULT StreamOnHGlobalToString(IStream* stream, std::string* out) { | 21 HRESULT StreamOnHGlobalToString(IStream* stream, std::string* out) { |
25 DCHECK(stream); | 22 DCHECK(stream); |
26 DCHECK(out); | 23 DCHECK(out); |
27 HGLOBAL hdata = NULL; | 24 HGLOBAL hdata = NULL; |
28 HRESULT hr = GetHGlobalFromStream(stream, &hdata); | 25 HRESULT hr = GetHGlobalFromStream(stream, &hdata); |
29 if (SUCCEEDED(hr)) { | 26 if (SUCCEEDED(hr)) { |
30 DCHECK(hdata); | 27 DCHECK(hdata); |
31 base::win::ScopedHGlobal<char> locked_data(hdata); | 28 base::win::ScopedHGlobal<char> locked_data(hdata); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 } | 101 } |
105 if (!IsValidPrinter(printer_name)) { | 102 if (!IsValidPrinter(printer_name)) { |
106 return false; | 103 return false; |
107 } | 104 } |
108 DCHECK(printer_info); | 105 DCHECK(printer_info); |
109 HPTPROVIDER provider = NULL; | 106 HPTPROVIDER provider = NULL; |
110 std::wstring printer_name_wide = UTF8ToWide(printer_name); | 107 std::wstring printer_name_wide = UTF8ToWide(printer_name); |
111 HRESULT hr = XPSModule::OpenProvider(printer_name_wide, 1, &provider); | 108 HRESULT hr = XPSModule::OpenProvider(printer_name_wide, 1, &provider); |
112 DCHECK(SUCCEEDED(hr)); | 109 DCHECK(SUCCEEDED(hr)); |
113 if (provider) { | 110 if (provider) { |
114 ScopedComPtr<IStream> print_capabilities_stream; | 111 base::win::ScopedComPtr<IStream> print_capabilities_stream; |
115 hr = CreateStreamOnHGlobal(NULL, TRUE, | 112 hr = CreateStreamOnHGlobal(NULL, TRUE, |
116 print_capabilities_stream.Receive()); | 113 print_capabilities_stream.Receive()); |
117 DCHECK(SUCCEEDED(hr)); | 114 DCHECK(SUCCEEDED(hr)); |
118 if (print_capabilities_stream) { | 115 if (print_capabilities_stream) { |
119 ScopedBstr error; | 116 base::win::ScopedBstr error; |
120 hr = XPSModule::GetPrintCapabilities(provider, | 117 hr = XPSModule::GetPrintCapabilities(provider, |
121 NULL, | 118 NULL, |
122 print_capabilities_stream, | 119 print_capabilities_stream, |
123 error.Receive()); | 120 error.Receive()); |
124 DCHECK(SUCCEEDED(hr)); | 121 DCHECK(SUCCEEDED(hr)); |
125 if (FAILED(hr)) { | 122 if (FAILED(hr)) { |
126 return false; | 123 return false; |
127 } | 124 } |
128 hr = StreamOnHGlobalToString(print_capabilities_stream.get(), | 125 hr = StreamOnHGlobalToString(print_capabilities_stream.get(), |
129 &printer_info->printer_capabilities); | 126 &printer_info->printer_capabilities); |
130 DCHECK(SUCCEEDED(hr)); | 127 DCHECK(SUCCEEDED(hr)); |
131 printer_info->caps_mime_type = "text/xml"; | 128 printer_info->caps_mime_type = "text/xml"; |
132 } | 129 } |
133 // TODO(sanjeevr): Add ScopedPrinterHandle | 130 // TODO(sanjeevr): Add ScopedPrinterHandle |
134 HANDLE printer_handle = NULL; | 131 HANDLE printer_handle = NULL; |
135 OpenPrinter(const_cast<LPTSTR>(printer_name_wide.c_str()), &printer_handle, | 132 OpenPrinter(const_cast<LPTSTR>(printer_name_wide.c_str()), &printer_handle, |
136 NULL); | 133 NULL); |
137 DCHECK(printer_handle); | 134 DCHECK(printer_handle); |
138 if (printer_handle) { | 135 if (printer_handle) { |
139 DWORD devmode_size = DocumentProperties( | 136 DWORD devmode_size = DocumentProperties( |
140 NULL, printer_handle, const_cast<LPTSTR>(printer_name_wide.c_str()), | 137 NULL, printer_handle, const_cast<LPTSTR>(printer_name_wide.c_str()), |
141 NULL, NULL, 0); | 138 NULL, NULL, 0); |
142 DCHECK_NE(0U, devmode_size); | 139 DCHECK_NE(0U, devmode_size); |
143 scoped_ptr<BYTE> devmode_out_buffer(new BYTE[devmode_size]); | 140 scoped_ptr<BYTE> devmode_out_buffer(new BYTE[devmode_size]); |
144 DEVMODE* devmode_out = | 141 DEVMODE* devmode_out = |
145 reinterpret_cast<DEVMODE*>(devmode_out_buffer.get()); | 142 reinterpret_cast<DEVMODE*>(devmode_out_buffer.get()); |
146 DocumentProperties( | 143 DocumentProperties( |
147 NULL, printer_handle, const_cast<LPTSTR>(printer_name_wide.c_str()), | 144 NULL, printer_handle, const_cast<LPTSTR>(printer_name_wide.c_str()), |
148 devmode_out, NULL, DM_OUT_BUFFER); | 145 devmode_out, NULL, DM_OUT_BUFFER); |
149 ScopedComPtr<IStream> printer_defaults_stream; | 146 base::win::ScopedComPtr<IStream> printer_defaults_stream; |
150 hr = CreateStreamOnHGlobal(NULL, TRUE, | 147 hr = CreateStreamOnHGlobal(NULL, TRUE, |
151 printer_defaults_stream.Receive()); | 148 printer_defaults_stream.Receive()); |
152 DCHECK(SUCCEEDED(hr)); | 149 DCHECK(SUCCEEDED(hr)); |
153 if (printer_defaults_stream) { | 150 if (printer_defaults_stream) { |
154 hr = XPSModule::ConvertDevModeToPrintTicket(provider, | 151 hr = XPSModule::ConvertDevModeToPrintTicket(provider, |
155 devmode_size, | 152 devmode_size, |
156 devmode_out, | 153 devmode_out, |
157 kPTJobScope, | 154 kPTJobScope, |
158 printer_defaults_stream); | 155 printer_defaults_stream); |
159 DCHECK(SUCCEEDED(hr)); | 156 DCHECK(SUCCEEDED(hr)); |
(...skipping 23 matching lines...) Expand all Loading... |
183 } | 180 } |
184 return ret; | 181 return ret; |
185 } | 182 } |
186 | 183 |
187 scoped_refptr<PrintBackend> PrintBackend::CreateInstance( | 184 scoped_refptr<PrintBackend> PrintBackend::CreateInstance( |
188 const DictionaryValue* print_backend_settings) { | 185 const DictionaryValue* print_backend_settings) { |
189 return new PrintBackendWin; | 186 return new PrintBackendWin; |
190 } | 187 } |
191 | 188 |
192 } // namespace printing | 189 } // namespace printing |
OLD | NEW |