Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(124)

Side by Side Diff: printing/backend/print_backend_win.cc

Issue 9569029: Improved resource management (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Review Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/service/cloud_print/print_system_win.cc ('k') | printing/printing_context_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 error.Receive()); 124 error.Receive());
125 DCHECK(SUCCEEDED(hr)); 125 DCHECK(SUCCEEDED(hr));
126 if (FAILED(hr)) { 126 if (FAILED(hr)) {
127 return false; 127 return false;
128 } 128 }
129 hr = StreamOnHGlobalToString(print_capabilities_stream.get(), 129 hr = StreamOnHGlobalToString(print_capabilities_stream.get(),
130 &printer_info->printer_capabilities); 130 &printer_info->printer_capabilities);
131 DCHECK(SUCCEEDED(hr)); 131 DCHECK(SUCCEEDED(hr));
132 printer_info->caps_mime_type = "text/xml"; 132 printer_info->caps_mime_type = "text/xml";
133 } 133 }
134 // TODO(sanjeevr): Add ScopedPrinterHandle 134 ScopedPrinterHandle printer_handle;
135 HANDLE printer_handle = NULL; 135 OpenPrinter(const_cast<LPTSTR>(printer_name_wide.c_str()),
136 OpenPrinter(const_cast<LPTSTR>(printer_name_wide.c_str()), &printer_handle, 136 printer_handle.Receive(), NULL);
137 NULL);
138 DCHECK(printer_handle); 137 DCHECK(printer_handle);
139 if (printer_handle) { 138 if (printer_handle.IsValid()) {
140 LONG devmode_size = DocumentProperties( 139 LONG devmode_size = DocumentProperties(
141 NULL, printer_handle, const_cast<LPTSTR>(printer_name_wide.c_str()), 140 NULL, printer_handle, const_cast<LPTSTR>(printer_name_wide.c_str()),
142 NULL, NULL, 0); 141 NULL, NULL, 0);
143 if (devmode_size <= 0) 142 if (devmode_size <= 0)
144 return false; 143 return false;
145 scoped_array<BYTE> devmode_out_buffer(new BYTE[devmode_size]); 144 scoped_array<BYTE> devmode_out_buffer(new BYTE[devmode_size]);
146 DEVMODE* devmode_out = 145 DEVMODE* devmode_out =
147 reinterpret_cast<DEVMODE*>(devmode_out_buffer.get()); 146 reinterpret_cast<DEVMODE*>(devmode_out_buffer.get());
148 DocumentProperties( 147 DocumentProperties(
149 NULL, printer_handle, const_cast<LPTSTR>(printer_name_wide.c_str()), 148 NULL, printer_handle, const_cast<LPTSTR>(printer_name_wide.c_str()),
150 devmode_out, NULL, DM_OUT_BUFFER); 149 devmode_out, NULL, DM_OUT_BUFFER);
151 base::win::ScopedComPtr<IStream> printer_defaults_stream; 150 base::win::ScopedComPtr<IStream> printer_defaults_stream;
152 hr = CreateStreamOnHGlobal(NULL, TRUE, 151 hr = CreateStreamOnHGlobal(NULL, TRUE,
153 printer_defaults_stream.Receive()); 152 printer_defaults_stream.Receive());
154 DCHECK(SUCCEEDED(hr)); 153 DCHECK(SUCCEEDED(hr));
155 if (printer_defaults_stream) { 154 if (printer_defaults_stream) {
156 hr = XPSModule::ConvertDevModeToPrintTicket(provider, 155 hr = XPSModule::ConvertDevModeToPrintTicket(provider,
157 devmode_size, 156 devmode_size,
158 devmode_out, 157 devmode_out,
159 kPTJobScope, 158 kPTJobScope,
160 printer_defaults_stream); 159 printer_defaults_stream);
161 DCHECK(SUCCEEDED(hr)); 160 DCHECK(SUCCEEDED(hr));
162 if (SUCCEEDED(hr)) { 161 if (SUCCEEDED(hr)) {
163 hr = StreamOnHGlobalToString(printer_defaults_stream.get(), 162 hr = StreamOnHGlobalToString(printer_defaults_stream.get(),
164 &printer_info->printer_defaults); 163 &printer_info->printer_defaults);
165 DCHECK(SUCCEEDED(hr)); 164 DCHECK(SUCCEEDED(hr));
166 printer_info->defaults_mime_type = "text/xml"; 165 printer_info->defaults_mime_type = "text/xml";
167 } 166 }
168 } 167 }
169 ClosePrinter(printer_handle);
170 } 168 }
171 XPSModule::CloseProvider(provider); 169 XPSModule::CloseProvider(provider);
172 } 170 }
173 return true; 171 return true;
174 } 172 }
175 173
176 bool PrintBackendWin::IsValidPrinter(const std::string& printer_name) { 174 bool PrintBackendWin::IsValidPrinter(const std::string& printer_name) {
177 std::wstring printer_name_wide = UTF8ToWide(printer_name); 175 std::wstring printer_name_wide = UTF8ToWide(printer_name);
178 HANDLE printer_handle = NULL; 176 ScopedPrinterHandle printer_handle;
179 OpenPrinter(const_cast<LPTSTR>(printer_name_wide.c_str()), &printer_handle, 177 OpenPrinter(const_cast<LPTSTR>(printer_name_wide.c_str()),
180 NULL); 178 printer_handle.Receive(), NULL);
181 bool ret = false; 179 return printer_handle.IsValid();
182 if (printer_handle) {
183 ret = true;
184 ClosePrinter(printer_handle);
185 }
186 return ret;
187 } 180 }
188 181
189 scoped_refptr<PrintBackend> PrintBackend::CreateInstance( 182 scoped_refptr<PrintBackend> PrintBackend::CreateInstance(
190 const base::DictionaryValue* print_backend_settings) { 183 const base::DictionaryValue* print_backend_settings) {
191 return new PrintBackendWin; 184 return new PrintBackendWin;
192 } 185 }
193 186
194 } // namespace printing 187 } // namespace printing
OLDNEW
« no previous file with comments | « chrome/service/cloud_print/print_system_win.cc ('k') | printing/printing_context_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698