OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/service/cloud_print/printer_info.h" | 5 #include "chrome/service/cloud_print/print_system.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 #include <objidl.h> | 8 #include <objidl.h> |
9 #include <ocidl.h> | 9 #include <ocidl.h> |
10 #include <olectl.h> | 10 #include <olectl.h> |
11 #include <prntvpt.h> | 11 #include <prntvpt.h> |
12 #include <winspool.h> | 12 #include <winspool.h> |
13 | 13 |
14 #include "base/lock.h" | 14 #include "base/lock.h" |
15 #include "base/object_watcher.h" | 15 #include "base/object_watcher.h" |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 HRESULT hr = E_NOTIMPL; | 121 HRESULT hr = E_NOTIMPL; |
122 // TODO(sanjeevr): Implement this. | 122 // TODO(sanjeevr): Implement this. |
123 NOTIMPLEMENTED(); | 123 NOTIMPLEMENTED(); |
124 return hr; | 124 return hr; |
125 } | 125 } |
126 | 126 |
127 } // namespace | 127 } // namespace |
128 | 128 |
129 namespace cloud_print { | 129 namespace cloud_print { |
130 | 130 |
131 void EnumeratePrinters(PrinterList* printer_list) { | 131 class PrintSystemWatcherWin |
| 132 : public base::ObjectWatcher::Delegate { |
| 133 public: |
| 134 PrintSystemWatcherWin() |
| 135 : printer_(NULL), printer_change_(NULL), delegate_(NULL) { |
| 136 } |
| 137 ~PrintSystemWatcherWin() { |
| 138 Stop(); |
| 139 } |
| 140 |
| 141 class Delegate { |
| 142 public: |
| 143 virtual void OnPrinterAdded() = 0; |
| 144 virtual void OnPrinterDeleted() = 0; |
| 145 virtual void OnPrinterChanged() = 0; |
| 146 virtual void OnJobChanged() = 0; |
| 147 }; |
| 148 |
| 149 bool Start(const std::string& printer_name, Delegate* delegate) { |
| 150 delegate_ = delegate; |
| 151 // An empty printer name means watch the current server, we need to pass |
| 152 // NULL to OpenPrinter. |
| 153 LPTSTR printer_name_to_use = NULL; |
| 154 std::wstring printer_name_wide; |
| 155 if (!printer_name.empty()) { |
| 156 printer_name_wide = UTF8ToWide(printer_name); |
| 157 printer_name_to_use = const_cast<LPTSTR>(printer_name_wide.c_str()); |
| 158 } |
| 159 bool ret = false; |
| 160 OpenPrinter(printer_name_to_use, &printer_, NULL); |
| 161 if (printer_) { |
| 162 printer_change_ = FindFirstPrinterChangeNotification( |
| 163 printer_, PRINTER_CHANGE_PRINTER|PRINTER_CHANGE_JOB, 0, NULL); |
| 164 if (printer_change_) { |
| 165 ret = watcher_.StartWatching(printer_change_, this); |
| 166 } |
| 167 } |
| 168 if (!ret) { |
| 169 Stop(); |
| 170 } |
| 171 return ret; |
| 172 } |
| 173 bool Stop() { |
| 174 watcher_.StopWatching(); |
| 175 if (printer_) { |
| 176 ClosePrinter(printer_); |
| 177 printer_ = NULL; |
| 178 } |
| 179 if (printer_change_) { |
| 180 FindClosePrinterChangeNotification(printer_change_); |
| 181 printer_change_ = NULL; |
| 182 } |
| 183 return true; |
| 184 } |
| 185 |
| 186 // base::ObjectWatcher::Delegate method |
| 187 virtual void OnObjectSignaled(HANDLE object) { |
| 188 DWORD change = 0; |
| 189 FindNextPrinterChangeNotification(object, &change, NULL, NULL); |
| 190 |
| 191 if (change != ((PRINTER_CHANGE_PRINTER|PRINTER_CHANGE_JOB) & |
| 192 (~PRINTER_CHANGE_FAILED_CONNECTION_PRINTER))) { |
| 193 // For printer connections, we get spurious change notifications with |
| 194 // all flags set except PRINTER_CHANGE_FAILED_CONNECTION_PRINTER. |
| 195 // Ignore these. |
| 196 if (change & PRINTER_CHANGE_ADD_PRINTER) { |
| 197 delegate_->OnPrinterAdded(); |
| 198 } else if (change & PRINTER_CHANGE_DELETE_PRINTER) { |
| 199 delegate_->OnPrinterDeleted(); |
| 200 } else if (change & PRINTER_CHANGE_SET_PRINTER) { |
| 201 delegate_->OnPrinterChanged(); |
| 202 } |
| 203 if (change & PRINTER_CHANGE_JOB) { |
| 204 delegate_->OnJobChanged(); |
| 205 } |
| 206 } |
| 207 watcher_.StartWatching(printer_change_, this); |
| 208 } |
| 209 |
| 210 bool GetCurrentPrinterInfo(PrinterBasicInfo* printer_info) { |
| 211 DCHECK(printer_info); |
| 212 if (!printer_) |
| 213 return false; |
| 214 |
| 215 DWORD bytes_needed = 0; |
| 216 bool ret = false; |
| 217 GetPrinter(printer_, 2, NULL, 0, &bytes_needed); |
| 218 if (0 != bytes_needed) { |
| 219 scoped_ptr<BYTE> printer_info_buffer(new BYTE[bytes_needed]); |
| 220 if (GetPrinter(printer_, 2, printer_info_buffer.get(), |
| 221 bytes_needed, &bytes_needed)) { |
| 222 PRINTER_INFO_2* printer_info_win = |
| 223 reinterpret_cast<PRINTER_INFO_2*>(printer_info_buffer.get()); |
| 224 printer_info->printer_name = WideToUTF8(printer_info_win->pPrinterName); |
| 225 printer_info->printer_description = |
| 226 WideToUTF8(printer_info_win->pComment); |
| 227 printer_info->printer_status = printer_info_win->Status; |
| 228 ret = true; |
| 229 } |
| 230 } |
| 231 return ret; |
| 232 } |
| 233 |
| 234 private: |
| 235 base::ObjectWatcher watcher_; |
| 236 HANDLE printer_; // The printer being watched |
| 237 HANDLE printer_change_; // Returned by FindFirstPrinterChangeNotifier |
| 238 Delegate* delegate_; // Delegate to notify |
| 239 bool did_signal_; // DoneWaiting was called |
| 240 }; |
| 241 |
| 242 // This typedef is to workaround the issue with certain versions of |
| 243 // Visual Studio where it gets confused between multiple Delegate. |
| 244 // In this case, some compilers get confused and inherit |
| 245 // PrintSystemWin watchers from wrong Delegate, giving C2664 and C2259 errors. |
| 246 typedef PrintSystemWatcherWin::Delegate PrintSystemWatcherWinDelegate; |
| 247 |
| 248 class PrintSystemWin : public PrintSystem { |
| 249 public: |
| 250 virtual void EnumeratePrinters(PrinterList* printer_list); |
| 251 |
| 252 virtual bool GetPrinterCapsAndDefaults(const std::string& printer_name, |
| 253 PrinterCapsAndDefaults* printer_info); |
| 254 |
| 255 virtual bool ValidatePrintTicket(const std::string& printer_name, |
| 256 const std::string& print_ticket_data); |
| 257 |
| 258 virtual bool SpoolPrintJob(const std::string& print_ticket, |
| 259 const FilePath& print_data_file_path, |
| 260 const std::string& print_data_mime_type, |
| 261 const std::string& printer_name, |
| 262 const std::string& job_title, |
| 263 PlatformJobId* job_id_ret); |
| 264 |
| 265 virtual bool GetJobDetails(const std::string& printer_name, |
| 266 PlatformJobId job_id, |
| 267 PrintJobDetails *job_details); |
| 268 |
| 269 virtual bool IsValidPrinter(const std::string& printer_name); |
| 270 |
| 271 class PrintServerWatcherWin |
| 272 : public PrintSystem::PrintServerWatcher, |
| 273 public PrintSystemWatcherWinDelegate { |
| 274 public: |
| 275 PrintServerWatcherWin() {} |
| 276 |
| 277 // PrintSystem::PrintServerWatcher interface |
| 278 virtual bool StartWatching( |
| 279 PrintSystem::PrintServerWatcher::Delegate* delegate) { |
| 280 delegate_ = delegate; |
| 281 return watcher_.Start(std::string(), this); |
| 282 } |
| 283 virtual bool StopWatching() { |
| 284 bool ret = watcher_.Stop(); |
| 285 delegate_ = NULL; |
| 286 return ret; |
| 287 } |
| 288 |
| 289 // PrintSystemWatcherWin::Delegate interface |
| 290 virtual void OnPrinterAdded() { |
| 291 delegate_->OnPrinterAdded(); |
| 292 } |
| 293 virtual void OnPrinterDeleted() { |
| 294 NOTREACHED(); |
| 295 } |
| 296 virtual void OnPrinterChanged() { |
| 297 NOTREACHED(); |
| 298 } |
| 299 virtual void OnJobChanged() { |
| 300 NOTREACHED(); |
| 301 } |
| 302 |
| 303 private: |
| 304 PrintSystem::PrintServerWatcher::Delegate* delegate_; |
| 305 PrintSystemWatcherWin watcher_; |
| 306 DISALLOW_COPY_AND_ASSIGN(PrintServerWatcherWin); |
| 307 }; |
| 308 |
| 309 class PrinterWatcherWin |
| 310 : public PrintSystem::PrinterWatcher, |
| 311 public PrintSystemWatcherWinDelegate { |
| 312 public: |
| 313 explicit PrinterWatcherWin(const std::string& printer_name) |
| 314 : printer_name_(printer_name) {} |
| 315 |
| 316 // PrintSystem::PrinterWatcher interface |
| 317 virtual bool StartWatching( |
| 318 PrintSystem::PrinterWatcher::Delegate* delegate) { |
| 319 delegate_ = delegate; |
| 320 return watcher_.Start(printer_name_, this); |
| 321 } |
| 322 virtual bool StopWatching() { |
| 323 bool ret = watcher_.Stop(); |
| 324 delegate_ = NULL; |
| 325 return ret; |
| 326 } |
| 327 virtual bool GetCurrentPrinterInfo(PrinterBasicInfo* printer_info) { |
| 328 return watcher_.GetCurrentPrinterInfo(printer_info); |
| 329 } |
| 330 |
| 331 // PrintSystemWatcherWin::Delegate interface |
| 332 virtual void OnPrinterAdded() { |
| 333 NOTREACHED(); |
| 334 } |
| 335 virtual void OnPrinterDeleted() { |
| 336 delegate_->OnPrinterDeleted(); |
| 337 } |
| 338 virtual void OnPrinterChanged() { |
| 339 delegate_->OnPrinterChanged(); |
| 340 } |
| 341 virtual void OnJobChanged() { |
| 342 delegate_->OnJobChanged(); |
| 343 } |
| 344 |
| 345 private: |
| 346 std::string printer_name_; |
| 347 PrintSystem::PrinterWatcher::Delegate* delegate_; |
| 348 PrintSystemWatcherWin watcher_; |
| 349 DISALLOW_COPY_AND_ASSIGN(PrinterWatcherWin); |
| 350 }; |
| 351 |
| 352 virtual PrintSystem::PrintServerWatcher* CreatePrintServerWatcher(); |
| 353 virtual PrintSystem::PrinterWatcher* CreatePrinterWatcher( |
| 354 const std::string& printer_name); |
| 355 }; |
| 356 |
| 357 void PrintSystemWin::EnumeratePrinters(PrinterList* printer_list) { |
132 DCHECK(printer_list); | 358 DCHECK(printer_list); |
133 DWORD bytes_needed = 0; | 359 DWORD bytes_needed = 0; |
134 DWORD count_returned = 0; | 360 DWORD count_returned = 0; |
135 BOOL ret = EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, NULL, 2, | 361 BOOL ret = EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, NULL, 2, |
136 NULL, 0, &bytes_needed, &count_returned); | 362 NULL, 0, &bytes_needed, &count_returned); |
137 if (0 != bytes_needed) { | 363 if (0 != bytes_needed) { |
138 scoped_ptr<BYTE> printer_info_buffer(new BYTE[bytes_needed]); | 364 scoped_ptr<BYTE> printer_info_buffer(new BYTE[bytes_needed]); |
139 ret = EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, NULL, 2, | 365 ret = EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, NULL, 2, |
140 printer_info_buffer.get(), bytes_needed, &bytes_needed, | 366 printer_info_buffer.get(), bytes_needed, &bytes_needed, |
141 &count_returned); | 367 &count_returned); |
142 DCHECK(ret); | 368 DCHECK(ret); |
143 PRINTER_INFO_2* printer_info = | 369 PRINTER_INFO_2* printer_info = |
144 reinterpret_cast<PRINTER_INFO_2*>(printer_info_buffer.get()); | 370 reinterpret_cast<PRINTER_INFO_2*>(printer_info_buffer.get()); |
145 for (DWORD index = 0; index < count_returned; index++) { | 371 for (DWORD index = 0; index < count_returned; index++) { |
146 PrinterBasicInfo info; | 372 PrinterBasicInfo info; |
147 info.printer_name = WideToUTF8(printer_info[index].pPrinterName); | 373 info.printer_name = WideToUTF8(printer_info[index].pPrinterName); |
148 if (printer_info[index].pComment) | 374 if (printer_info[index].pComment) |
149 info.printer_description = WideToUTF8(printer_info[index].pComment); | 375 info.printer_description = WideToUTF8(printer_info[index].pComment); |
150 info.printer_status = printer_info[index].Status; | 376 info.printer_status = printer_info[index].Status; |
151 printer_list->push_back(info); | 377 printer_list->push_back(info); |
152 } | 378 } |
153 } | 379 } |
154 } | 380 } |
155 | 381 |
156 bool GetPrinterCapsAndDefaults(const std::string& printer_name, | 382 bool PrintSystemWin::GetPrinterCapsAndDefaults( |
157 PrinterCapsAndDefaults* printer_info) { | 383 const std::string& printer_name, |
| 384 PrinterCapsAndDefaults* printer_info) { |
158 if (!InitXPSModule()) { | 385 if (!InitXPSModule()) { |
159 // TODO(sanjeevr): Handle legacy proxy case (with no prntvpt.dll) | 386 // TODO(sanjeevr): Handle legacy proxy case (with no prntvpt.dll) |
160 return false; | 387 return false; |
161 } | 388 } |
162 if (!IsValidPrinter(printer_name)) { | 389 if (!IsValidPrinter(printer_name)) { |
163 return false; | 390 return false; |
164 } | 391 } |
165 DCHECK(printer_info); | 392 DCHECK(printer_info); |
166 HPTPROVIDER provider = NULL; | 393 HPTPROVIDER provider = NULL; |
167 std::wstring printer_name_wide = UTF8ToWide(printer_name); | 394 std::wstring printer_name_wide = UTF8ToWide(printer_name); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 printer_info->defaults_mime_type = "text/xml"; | 444 printer_info->defaults_mime_type = "text/xml"; |
218 } | 445 } |
219 } | 446 } |
220 ClosePrinter(printer_handle); | 447 ClosePrinter(printer_handle); |
221 } | 448 } |
222 PTCloseProvider(provider); | 449 PTCloseProvider(provider); |
223 } | 450 } |
224 return true; | 451 return true; |
225 } | 452 } |
226 | 453 |
227 bool ValidatePrintTicket(const std::string& printer_name, | 454 bool PrintSystemWin::ValidatePrintTicket( |
228 const std::string& print_ticket_data) { | 455 const std::string& printer_name, |
| 456 const std::string& print_ticket_data) { |
229 if (!InitXPSModule()) { | 457 if (!InitXPSModule()) { |
230 // TODO(sanjeevr): Handle legacy proxy case (with no prntvpt.dll) | 458 // TODO(sanjeevr): Handle legacy proxy case (with no prntvpt.dll) |
231 return false; | 459 return false; |
232 } | 460 } |
233 bool ret = false; | 461 bool ret = false; |
234 HPTPROVIDER provider = NULL; | 462 HPTPROVIDER provider = NULL; |
235 PTOpenProvider(UTF8ToWide(printer_name.c_str()).c_str(), 1, &provider); | 463 PTOpenProvider(UTF8ToWide(printer_name.c_str()).c_str(), 1, &provider); |
236 if (provider) { | 464 if (provider) { |
237 ScopedComPtr<IStream> print_ticket_stream; | 465 ScopedComPtr<IStream> print_ticket_stream; |
238 CreateStreamOnHGlobal(NULL, TRUE, print_ticket_stream.Receive()); | 466 CreateStreamOnHGlobal(NULL, TRUE, print_ticket_stream.Receive()); |
(...skipping 12 matching lines...) Expand all Loading... |
251 print_ticket_stream.get(), | 479 print_ticket_stream.get(), |
252 NULL, | 480 NULL, |
253 kPTJobScope, | 481 kPTJobScope, |
254 result_ticket_stream.get(), | 482 result_ticket_stream.get(), |
255 error.Receive())); | 483 error.Receive())); |
256 PTCloseProvider(provider); | 484 PTCloseProvider(provider); |
257 } | 485 } |
258 return ret; | 486 return ret; |
259 } | 487 } |
260 | 488 |
261 std::string GenerateProxyId() { | 489 bool PrintSystemWin::SpoolPrintJob(const std::string& print_ticket, |
262 GUID proxy_id = {0}; | 490 const FilePath& print_data_file_path, |
263 HRESULT hr = UuidCreate(&proxy_id); | 491 const std::string& print_data_mime_type, |
264 DCHECK(SUCCEEDED(hr)); | 492 const std::string& printer_name, |
265 wchar_t* proxy_id_as_string = NULL; | 493 const std::string& job_title, |
266 UuidToString(&proxy_id, reinterpret_cast<RPC_WSTR *>(&proxy_id_as_string)); | 494 PlatformJobId* job_id_ret) { |
267 DCHECK(proxy_id_as_string); | |
268 std::string ret; | |
269 WideToUTF8(proxy_id_as_string, wcslen(proxy_id_as_string), &ret); | |
270 RpcStringFree(reinterpret_cast<RPC_WSTR *>(&proxy_id_as_string)); | |
271 return ret; | |
272 } | |
273 | |
274 bool SpoolPrintJob(const std::string& print_ticket, | |
275 const FilePath& print_data_file_path, | |
276 const std::string& print_data_mime_type, | |
277 const std::string& printer_name, | |
278 const std::string& job_title, | |
279 PlatformJobId* job_id_ret) { | |
280 if (!InitXPSModule()) { | 495 if (!InitXPSModule()) { |
281 // TODO(sanjeevr): Handle legacy proxy case (with no prntvpt.dll) | 496 // TODO(sanjeevr): Handle legacy proxy case (with no prntvpt.dll) |
282 return false; | 497 return false; |
283 } | 498 } |
284 DevMode pt_dev_mode; | 499 DevMode pt_dev_mode; |
285 HRESULT hr = PrintTicketToDevMode(printer_name, print_ticket, &pt_dev_mode); | 500 HRESULT hr = PrintTicketToDevMode(printer_name, print_ticket, &pt_dev_mode); |
286 if (FAILED(hr)) { | 501 if (FAILED(hr)) { |
287 NOTREACHED(); | 502 NOTREACHED(); |
288 return false; | 503 return false; |
289 } | 504 } |
(...skipping 16 matching lines...) Expand all Loading... |
306 NOTREACHED(); | 521 NOTREACHED(); |
307 } | 522 } |
308 EndDoc(dc.Get()); | 523 EndDoc(dc.Get()); |
309 if (SUCCEEDED(hr) && job_id_ret) { | 524 if (SUCCEEDED(hr) && job_id_ret) { |
310 *job_id_ret = job_id; | 525 *job_id_ret = job_id; |
311 } | 526 } |
312 } | 527 } |
313 return SUCCEEDED(hr); | 528 return SUCCEEDED(hr); |
314 } | 529 } |
315 | 530 |
316 bool GetJobDetails(const std::string& printer_name, | 531 bool PrintSystemWin::GetJobDetails(const std::string& printer_name, |
317 PlatformJobId job_id, | 532 PlatformJobId job_id, |
318 PrintJobDetails *job_details) { | 533 PrintJobDetails *job_details) { |
319 DCHECK(job_details); | 534 DCHECK(job_details); |
320 HANDLE printer_handle = NULL; | 535 HANDLE printer_handle = NULL; |
321 std::wstring printer_name_wide = UTF8ToWide(printer_name); | 536 std::wstring printer_name_wide = UTF8ToWide(printer_name); |
322 OpenPrinter(const_cast<LPTSTR>(printer_name_wide.c_str()), &printer_handle, | 537 OpenPrinter(const_cast<LPTSTR>(printer_name_wide.c_str()), &printer_handle, |
323 NULL); | 538 NULL); |
324 DCHECK(printer_handle); | 539 DCHECK(printer_handle); |
325 bool ret = false; | 540 bool ret = false; |
326 if (printer_handle) { | 541 if (printer_handle) { |
327 DWORD bytes_needed = 0; | 542 DWORD bytes_needed = 0; |
328 GetJob(printer_handle, job_id, 1, NULL, 0, &bytes_needed); | 543 GetJob(printer_handle, job_id, 1, NULL, 0, &bytes_needed); |
(...skipping 22 matching lines...) Expand all Loading... |
351 job_details->total_pages = job_info->TotalPages; | 566 job_details->total_pages = job_info->TotalPages; |
352 job_details->pages_printed = job_info->PagesPrinted; | 567 job_details->pages_printed = job_info->PagesPrinted; |
353 ret = true; | 568 ret = true; |
354 } | 569 } |
355 } | 570 } |
356 ClosePrinter(printer_handle); | 571 ClosePrinter(printer_handle); |
357 } | 572 } |
358 return ret; | 573 return ret; |
359 } | 574 } |
360 | 575 |
361 bool IsValidPrinter(const std::string& printer_name) { | 576 bool PrintSystemWin::IsValidPrinter(const std::string& printer_name) { |
362 std::wstring printer_name_wide = UTF8ToWide(printer_name); | 577 std::wstring printer_name_wide = UTF8ToWide(printer_name); |
363 HANDLE printer_handle = NULL; | 578 HANDLE printer_handle = NULL; |
364 OpenPrinter(const_cast<LPTSTR>(printer_name_wide.c_str()), &printer_handle, | 579 OpenPrinter(const_cast<LPTSTR>(printer_name_wide.c_str()), &printer_handle, |
365 NULL); | 580 NULL); |
366 bool ret = false; | 581 bool ret = false; |
367 if (printer_handle) { | 582 if (printer_handle) { |
368 ret = true; | 583 ret = true; |
369 ClosePrinter(printer_handle); | 584 ClosePrinter(printer_handle); |
370 } | 585 } |
371 return ret; | 586 return ret; |
372 } | 587 } |
373 | 588 |
374 class PrinterChangeNotifier::NotificationState | 589 PrintSystem::PrintServerWatcher* |
375 : public base::ObjectWatcher::Delegate { | 590 PrintSystemWin::CreatePrintServerWatcher() { |
376 public: | 591 return new PrintServerWatcherWin(); |
377 NotificationState() : printer_(NULL), printer_change_(NULL), delegate_(NULL) { | |
378 } | |
379 ~NotificationState() { | |
380 Stop(); | |
381 } | |
382 bool Start(const std::string& printer_name, | |
383 PrinterChangeNotifier::Delegate* delegate) { | |
384 delegate_ = delegate; | |
385 // An empty printer name means watch the current server, we need to pass | |
386 // NULL to OpenPrinter. | |
387 LPTSTR printer_name_to_use = NULL; | |
388 std::wstring printer_name_wide; | |
389 if (!printer_name.empty()) { | |
390 printer_name_wide = UTF8ToWide(printer_name); | |
391 printer_name_to_use = const_cast<LPTSTR>(printer_name_wide.c_str()); | |
392 } | |
393 bool ret = false; | |
394 OpenPrinter(printer_name_to_use, &printer_, NULL); | |
395 if (printer_) { | |
396 printer_change_ = FindFirstPrinterChangeNotification( | |
397 printer_, PRINTER_CHANGE_PRINTER|PRINTER_CHANGE_JOB, 0, NULL); | |
398 if (printer_change_) { | |
399 ret = watcher_.StartWatching(printer_change_, this); | |
400 } | |
401 } | |
402 if (!ret) { | |
403 Stop(); | |
404 } | |
405 return ret; | |
406 } | |
407 bool Stop() { | |
408 watcher_.StopWatching(); | |
409 if (printer_) { | |
410 ClosePrinter(printer_); | |
411 printer_ = NULL; | |
412 } | |
413 if (printer_change_) { | |
414 FindClosePrinterChangeNotification(printer_change_); | |
415 printer_change_ = NULL; | |
416 } | |
417 return true; | |
418 } | |
419 | |
420 void OnObjectSignaled(HANDLE object) { | |
421 DWORD change = 0; | |
422 FindNextPrinterChangeNotification(object, &change, NULL, NULL); | |
423 | |
424 if (change != ((PRINTER_CHANGE_PRINTER|PRINTER_CHANGE_JOB) & | |
425 (~PRINTER_CHANGE_FAILED_CONNECTION_PRINTER))) { | |
426 // For printer connections, we get spurious change notifications with | |
427 // all flags set except PRINTER_CHANGE_FAILED_CONNECTION_PRINTER. | |
428 // Ignore these. | |
429 if (change & PRINTER_CHANGE_ADD_PRINTER) { | |
430 delegate_->OnPrinterAdded(); | |
431 } else if (change & PRINTER_CHANGE_DELETE_PRINTER) { | |
432 delegate_->OnPrinterDeleted(); | |
433 } else if (change & PRINTER_CHANGE_SET_PRINTER) { | |
434 delegate_->OnPrinterChanged(); | |
435 } | |
436 if (change & PRINTER_CHANGE_JOB) { | |
437 delegate_->OnJobChanged(); | |
438 } | |
439 } | |
440 watcher_.StartWatching(printer_change_, this); | |
441 } | |
442 HANDLE printer_handle() const { | |
443 return printer_; | |
444 } | |
445 private: | |
446 base::ObjectWatcher watcher_; | |
447 HANDLE printer_; // The printer being watched | |
448 HANDLE printer_change_; // Returned by FindFirstPrinterChangeNotifier | |
449 PrinterChangeNotifier::Delegate* delegate_; // Delegate to notify | |
450 bool did_signal_; // DoneWaiting was called | |
451 }; | |
452 | |
453 PrinterChangeNotifier::PrinterChangeNotifier() : state_(NULL) { | |
454 } | 592 } |
455 | 593 |
456 PrinterChangeNotifier::~PrinterChangeNotifier() { | 594 PrintSystem::PrinterWatcher* PrintSystemWin::CreatePrinterWatcher( |
457 StopWatching(); | 595 const std::string& printer_name) { |
| 596 DCHECK(!printer_name.empty()); |
| 597 return new PrinterWatcherWin(printer_name); |
458 } | 598 } |
459 | 599 |
460 bool PrinterChangeNotifier::StartWatching(const std::string& printer_name, | 600 std::string PrintSystem::GenerateProxyId() { |
461 Delegate* delegate) { | 601 GUID proxy_id = {0}; |
462 if (state_) { | 602 HRESULT hr = UuidCreate(&proxy_id); |
463 NOTREACHED(); | 603 DCHECK(SUCCEEDED(hr)); |
464 return false; | 604 wchar_t* proxy_id_as_string = NULL; |
465 } | 605 UuidToString(&proxy_id, reinterpret_cast<RPC_WSTR *>(&proxy_id_as_string)); |
466 state_ = new NotificationState; | 606 DCHECK(proxy_id_as_string); |
467 if (!state_->Start(printer_name, delegate)) { | 607 std::string ret; |
468 StopWatching(); | 608 WideToUTF8(proxy_id_as_string, wcslen(proxy_id_as_string), &ret); |
469 return false; | 609 RpcStringFree(reinterpret_cast<RPC_WSTR *>(&proxy_id_as_string)); |
470 } | 610 return ret; |
471 return true; | |
472 } | 611 } |
473 | 612 |
474 bool PrinterChangeNotifier::StopWatching() { | 613 scoped_refptr<PrintSystem> PrintSystem::CreateInstance() { |
475 if (!state_) { | 614 return new PrintSystemWin; |
476 return false; | |
477 } | |
478 state_->Stop(); | |
479 delete state_; | |
480 state_ = NULL; | |
481 return true; | |
482 } | 615 } |
483 | 616 |
484 bool PrinterChangeNotifier::GetCurrentPrinterInfo( | |
485 PrinterBasicInfo* printer_info) { | |
486 if (!state_) { | |
487 return false; | |
488 } | |
489 DCHECK(printer_info); | |
490 DWORD bytes_needed = 0; | |
491 bool ret = false; | |
492 GetPrinter(state_->printer_handle(), 2, NULL, 0, &bytes_needed); | |
493 if (0 != bytes_needed) { | |
494 scoped_ptr<BYTE> printer_info_buffer(new BYTE[bytes_needed]); | |
495 if (GetPrinter(state_->printer_handle(), 2, printer_info_buffer.get(), | |
496 bytes_needed, &bytes_needed)) { | |
497 PRINTER_INFO_2* printer_info_win = | |
498 reinterpret_cast<PRINTER_INFO_2*>(printer_info_buffer.get()); | |
499 printer_info->printer_name = WideToUTF8(printer_info_win->pPrinterName); | |
500 printer_info->printer_description = | |
501 WideToUTF8(printer_info_win->pComment); | |
502 printer_info->printer_status = printer_info_win->Status; | |
503 ret = true; | |
504 } | |
505 } | |
506 return ret; | |
507 } | |
508 } // namespace cloud_print | 617 } // namespace cloud_print |
509 | 618 |
OLD | NEW |