OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "extensions/browser/api/printer_provider/printer_provider_api.h" | 5 #include "extensions/browser/api/printer_provider/printer_provider_api.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <set> | 8 #include <set> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 | 259 |
260 // PrinterProviderInternalAPIObserver implementation: | 260 // PrinterProviderInternalAPIObserver implementation: |
261 void OnGetPrintersResult( | 261 void OnGetPrintersResult( |
262 const Extension* extension, | 262 const Extension* extension, |
263 int request_id, | 263 int request_id, |
264 const PrinterProviderInternalAPIObserver::PrinterInfoVector& result) | 264 const PrinterProviderInternalAPIObserver::PrinterInfoVector& result) |
265 override; | 265 override; |
266 void OnGetCapabilityResult(const Extension* extension, | 266 void OnGetCapabilityResult(const Extension* extension, |
267 int request_id, | 267 int request_id, |
268 const base::DictionaryValue& result) override; | 268 const base::DictionaryValue& result) override; |
269 void OnPrintResult( | 269 void OnPrintResult(const Extension* extension, |
270 const Extension* extension, | 270 int request_id, |
271 int request_id, | 271 api::printer_provider_internal::PrintError error) override; |
272 core_api::printer_provider_internal::PrintError error) override; | |
273 void OnGetUsbPrinterInfoResult( | 272 void OnGetUsbPrinterInfoResult( |
274 const Extension* extension, | 273 const Extension* extension, |
275 int request_id, | 274 int request_id, |
276 const core_api::printer_provider::PrinterInfo* printer_info) override; | 275 const api::printer_provider::PrinterInfo* printer_info) override; |
277 | 276 |
278 // ExtensionRegistryObserver implementation: | 277 // ExtensionRegistryObserver implementation: |
279 void OnExtensionUnloaded(content::BrowserContext* browser_context, | 278 void OnExtensionUnloaded(content::BrowserContext* browser_context, |
280 const Extension* extension, | 279 const Extension* extension, |
281 UnloadedExtensionInfo::Reason reason) override; | 280 UnloadedExtensionInfo::Reason reason) override; |
282 | 281 |
283 // Called before chrome.printerProvider.onGetPrintersRequested event is | 282 // Called before chrome.printerProvider.onGetPrintersRequested event is |
284 // dispatched to an extension. It returns whether the extension is interested | 283 // dispatched to an extension. It returns whether the extension is interested |
285 // in the event. If the extension listens to the event, it's added to the set | 284 // in the event. If the extension listens to the event, it's added to the set |
286 // of |request| sources. |request| is |GetPrintersRequest| object associated | 285 // of |request| sources. |request| is |GetPrintersRequest| object associated |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
504 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context)); | 503 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context)); |
505 } | 504 } |
506 | 505 |
507 PrinterProviderAPIImpl::~PrinterProviderAPIImpl() { | 506 PrinterProviderAPIImpl::~PrinterProviderAPIImpl() { |
508 } | 507 } |
509 | 508 |
510 void PrinterProviderAPIImpl::DispatchGetPrintersRequested( | 509 void PrinterProviderAPIImpl::DispatchGetPrintersRequested( |
511 const GetPrintersCallback& callback) { | 510 const GetPrintersCallback& callback) { |
512 EventRouter* event_router = EventRouter::Get(browser_context_); | 511 EventRouter* event_router = EventRouter::Get(browser_context_); |
513 if (!event_router->HasEventListener( | 512 if (!event_router->HasEventListener( |
514 core_api::printer_provider::OnGetPrintersRequested::kEventName)) { | 513 api::printer_provider::OnGetPrintersRequested::kEventName)) { |
515 callback.Run(base::ListValue(), true /* done */); | 514 callback.Run(base::ListValue(), true /* done */); |
516 return; | 515 return; |
517 } | 516 } |
518 | 517 |
519 // |pending_get_printers_requests_| take ownership of |request| which gets | 518 // |pending_get_printers_requests_| take ownership of |request| which gets |
520 // NULLed out. Save the pointer before passing it to the requests, as it will | 519 // NULLed out. Save the pointer before passing it to the requests, as it will |
521 // be needed later on. | 520 // be needed later on. |
522 int request_id = pending_get_printers_requests_.Add(callback); | 521 int request_id = pending_get_printers_requests_.Add(callback); |
523 | 522 |
524 scoped_ptr<base::ListValue> internal_args(new base::ListValue); | 523 scoped_ptr<base::ListValue> internal_args(new base::ListValue); |
525 // Request id is not part of the public API, but it will be massaged out in | 524 // Request id is not part of the public API, but it will be massaged out in |
526 // custom bindings. | 525 // custom bindings. |
527 internal_args->AppendInteger(request_id); | 526 internal_args->AppendInteger(request_id); |
528 | 527 |
529 scoped_ptr<Event> event( | 528 scoped_ptr<Event> event( |
530 new Event(events::PRINTER_PROVIDER_ON_GET_PRINTERS_REQUESTED, | 529 new Event(events::PRINTER_PROVIDER_ON_GET_PRINTERS_REQUESTED, |
531 core_api::printer_provider::OnGetPrintersRequested::kEventName, | 530 api::printer_provider::OnGetPrintersRequested::kEventName, |
532 internal_args.Pass())); | 531 internal_args.Pass())); |
533 // This callback is called synchronously during |BroadcastEvent|, so | 532 // This callback is called synchronously during |BroadcastEvent|, so |
534 // Unretained is safe. | 533 // Unretained is safe. |
535 event->will_dispatch_callback = | 534 event->will_dispatch_callback = |
536 base::Bind(&PrinterProviderAPIImpl::WillRequestPrinters, | 535 base::Bind(&PrinterProviderAPIImpl::WillRequestPrinters, |
537 base::Unretained(this), request_id); | 536 base::Unretained(this), request_id); |
538 | 537 |
539 event_router->BroadcastEvent(event.Pass()); | 538 event_router->BroadcastEvent(event.Pass()); |
540 } | 539 } |
541 | 540 |
542 void PrinterProviderAPIImpl::DispatchGetCapabilityRequested( | 541 void PrinterProviderAPIImpl::DispatchGetCapabilityRequested( |
543 const std::string& printer_id, | 542 const std::string& printer_id, |
544 const PrinterProviderAPI::GetCapabilityCallback& callback) { | 543 const PrinterProviderAPI::GetCapabilityCallback& callback) { |
545 std::string extension_id; | 544 std::string extension_id; |
546 std::string internal_printer_id; | 545 std::string internal_printer_id; |
547 if (!ParsePrinterId(printer_id, &extension_id, &internal_printer_id)) { | 546 if (!ParsePrinterId(printer_id, &extension_id, &internal_printer_id)) { |
548 callback.Run(base::DictionaryValue()); | 547 callback.Run(base::DictionaryValue()); |
549 return; | 548 return; |
550 } | 549 } |
551 | 550 |
552 EventRouter* event_router = EventRouter::Get(browser_context_); | 551 EventRouter* event_router = EventRouter::Get(browser_context_); |
553 if (!event_router->ExtensionHasEventListener( | 552 if (!event_router->ExtensionHasEventListener( |
554 extension_id, | 553 extension_id, |
555 core_api::printer_provider::OnGetCapabilityRequested::kEventName)) { | 554 api::printer_provider::OnGetCapabilityRequested::kEventName)) { |
556 callback.Run(base::DictionaryValue()); | 555 callback.Run(base::DictionaryValue()); |
557 return; | 556 return; |
558 } | 557 } |
559 | 558 |
560 int request_id = pending_capability_requests_[extension_id].Add(callback); | 559 int request_id = pending_capability_requests_[extension_id].Add(callback); |
561 | 560 |
562 scoped_ptr<base::ListValue> internal_args(new base::ListValue); | 561 scoped_ptr<base::ListValue> internal_args(new base::ListValue); |
563 // Request id is not part of the public API, but it will be massaged out in | 562 // Request id is not part of the public API, but it will be massaged out in |
564 // custom bindings. | 563 // custom bindings. |
565 internal_args->AppendInteger(request_id); | 564 internal_args->AppendInteger(request_id); |
566 internal_args->AppendString(internal_printer_id); | 565 internal_args->AppendString(internal_printer_id); |
567 | 566 |
568 scoped_ptr<Event> event(new Event( | 567 scoped_ptr<Event> event( |
569 events::PRINTER_PROVIDER_ON_GET_CAPABILITY_REQUESTED, | 568 new Event(events::PRINTER_PROVIDER_ON_GET_CAPABILITY_REQUESTED, |
570 core_api::printer_provider::OnGetCapabilityRequested::kEventName, | 569 api::printer_provider::OnGetCapabilityRequested::kEventName, |
571 internal_args.Pass())); | 570 internal_args.Pass())); |
572 | 571 |
573 event_router->DispatchEventToExtension(extension_id, event.Pass()); | 572 event_router->DispatchEventToExtension(extension_id, event.Pass()); |
574 } | 573 } |
575 | 574 |
576 void PrinterProviderAPIImpl::DispatchPrintRequested( | 575 void PrinterProviderAPIImpl::DispatchPrintRequested( |
577 const PrinterProviderPrintJob& job, | 576 const PrinterProviderPrintJob& job, |
578 const PrinterProviderAPI::PrintCallback& callback) { | 577 const PrinterProviderAPI::PrintCallback& callback) { |
579 std::string extension_id; | 578 std::string extension_id; |
580 std::string internal_printer_id; | 579 std::string internal_printer_id; |
581 if (!ParsePrinterId(job.printer_id, &extension_id, &internal_printer_id)) { | 580 if (!ParsePrinterId(job.printer_id, &extension_id, &internal_printer_id)) { |
582 callback.Run(false, PrinterProviderAPI::GetDefaultPrintError()); | 581 callback.Run(false, PrinterProviderAPI::GetDefaultPrintError()); |
583 return; | 582 return; |
584 } | 583 } |
585 | 584 |
586 EventRouter* event_router = EventRouter::Get(browser_context_); | 585 EventRouter* event_router = EventRouter::Get(browser_context_); |
587 if (!event_router->ExtensionHasEventListener( | 586 if (!event_router->ExtensionHasEventListener( |
588 extension_id, | 587 extension_id, api::printer_provider::OnPrintRequested::kEventName)) { |
589 core_api::printer_provider::OnPrintRequested::kEventName)) { | |
590 callback.Run(false, PrinterProviderAPI::GetDefaultPrintError()); | 588 callback.Run(false, PrinterProviderAPI::GetDefaultPrintError()); |
591 return; | 589 return; |
592 } | 590 } |
593 | 591 |
594 core_api::printer_provider::PrintJob print_job; | 592 api::printer_provider::PrintJob print_job; |
595 print_job.printer_id = internal_printer_id; | 593 print_job.printer_id = internal_printer_id; |
596 | 594 |
597 JSONStringValueDeserializer deserializer(job.ticket_json); | 595 JSONStringValueDeserializer deserializer(job.ticket_json); |
598 scoped_ptr<base::Value> ticket_value(deserializer.Deserialize(NULL, NULL)); | 596 scoped_ptr<base::Value> ticket_value(deserializer.Deserialize(NULL, NULL)); |
599 if (!ticket_value || | 597 if (!ticket_value || |
600 !core_api::printer_provider::PrintJob::Ticket::Populate( | 598 !api::printer_provider::PrintJob::Ticket::Populate(*ticket_value, |
601 *ticket_value, &print_job.ticket)) { | 599 &print_job.ticket)) { |
602 callback.Run(false, | 600 callback.Run(false, api::printer_provider::ToString( |
603 core_api::printer_provider::ToString( | 601 api::printer_provider::PRINT_ERROR_INVALID_TICKET)); |
604 core_api::printer_provider::PRINT_ERROR_INVALID_TICKET)); | |
605 return; | 602 return; |
606 } | 603 } |
607 | 604 |
608 print_job.content_type = job.content_type; | 605 print_job.content_type = job.content_type; |
609 print_job.title = base::UTF16ToUTF8(job.job_title); | 606 print_job.title = base::UTF16ToUTF8(job.job_title); |
610 int request_id = pending_print_requests_[extension_id].Add(job, callback); | 607 int request_id = pending_print_requests_[extension_id].Add(job, callback); |
611 | 608 |
612 scoped_ptr<base::ListValue> internal_args(new base::ListValue); | 609 scoped_ptr<base::ListValue> internal_args(new base::ListValue); |
613 // Request id is not part of the public API and it will be massaged out in | 610 // Request id is not part of the public API and it will be massaged out in |
614 // custom bindings. | 611 // custom bindings. |
615 internal_args->AppendInteger(request_id); | 612 internal_args->AppendInteger(request_id); |
616 internal_args->Append(print_job.ToValue().release()); | 613 internal_args->Append(print_job.ToValue().release()); |
617 scoped_ptr<Event> event( | 614 scoped_ptr<Event> event( |
618 new Event(events::PRINTER_PROVIDER_ON_PRINT_REQUESTED, | 615 new Event(events::PRINTER_PROVIDER_ON_PRINT_REQUESTED, |
619 core_api::printer_provider::OnPrintRequested::kEventName, | 616 api::printer_provider::OnPrintRequested::kEventName, |
620 internal_args.Pass())); | 617 internal_args.Pass())); |
621 event_router->DispatchEventToExtension(extension_id, event.Pass()); | 618 event_router->DispatchEventToExtension(extension_id, event.Pass()); |
622 } | 619 } |
623 | 620 |
624 const PrinterProviderPrintJob* PrinterProviderAPIImpl::GetPrintJob( | 621 const PrinterProviderPrintJob* PrinterProviderAPIImpl::GetPrintJob( |
625 const Extension* extension, | 622 const Extension* extension, |
626 int request_id) const { | 623 int request_id) const { |
627 auto it = pending_print_requests_.find(extension->id()); | 624 auto it = pending_print_requests_.find(extension->id()); |
628 if (it == pending_print_requests_.end()) | 625 if (it == pending_print_requests_.end()) |
629 return nullptr; | 626 return nullptr; |
630 return it->second.GetPrintJob(request_id); | 627 return it->second.GetPrintJob(request_id); |
631 } | 628 } |
632 | 629 |
633 void PrinterProviderAPIImpl::DispatchGetUsbPrinterInfoRequested( | 630 void PrinterProviderAPIImpl::DispatchGetUsbPrinterInfoRequested( |
634 const std::string& extension_id, | 631 const std::string& extension_id, |
635 scoped_refptr<UsbDevice> device, | 632 scoped_refptr<UsbDevice> device, |
636 const PrinterProviderAPI::GetPrinterInfoCallback& callback) { | 633 const PrinterProviderAPI::GetPrinterInfoCallback& callback) { |
637 EventRouter* event_router = EventRouter::Get(browser_context_); | 634 EventRouter* event_router = EventRouter::Get(browser_context_); |
638 if (!event_router->ExtensionHasEventListener( | 635 if (!event_router->ExtensionHasEventListener( |
639 extension_id, core_api::printer_provider:: | 636 extension_id, |
640 OnGetUsbPrinterInfoRequested::kEventName)) { | 637 api::printer_provider::OnGetUsbPrinterInfoRequested::kEventName)) { |
641 callback.Run(base::DictionaryValue()); | 638 callback.Run(base::DictionaryValue()); |
642 return; | 639 return; |
643 } | 640 } |
644 | 641 |
645 int request_id = | 642 int request_id = |
646 pending_usb_printer_info_requests_[extension_id].Add(callback); | 643 pending_usb_printer_info_requests_[extension_id].Add(callback); |
647 core_api::usb::Device usb_device; | 644 api::usb::Device usb_device; |
648 usb_device.device = | 645 usb_device.device = |
649 UsbGuidMap::Get(browser_context_)->GetIdFromGuid(device->guid()); | 646 UsbGuidMap::Get(browser_context_)->GetIdFromGuid(device->guid()); |
650 usb_device.vendor_id = device->vendor_id(); | 647 usb_device.vendor_id = device->vendor_id(); |
651 usb_device.product_id = device->product_id(); | 648 usb_device.product_id = device->product_id(); |
652 | 649 |
653 scoped_ptr<base::ListValue> internal_args(new base::ListValue); | 650 scoped_ptr<base::ListValue> internal_args(new base::ListValue); |
654 // Request id is not part of the public API and it will be massaged out in | 651 // Request id is not part of the public API and it will be massaged out in |
655 // custom bindings. | 652 // custom bindings. |
656 internal_args->AppendInteger(request_id); | 653 internal_args->AppendInteger(request_id); |
657 internal_args->Append(usb_device.ToValue().release()); | 654 internal_args->Append(usb_device.ToValue().release()); |
658 scoped_ptr<Event> event(new Event( | 655 scoped_ptr<Event> event( |
659 events::PRINTER_PROVIDER_ON_GET_USB_PRINTER_INFO_REQUESTED, | 656 new Event(events::PRINTER_PROVIDER_ON_GET_USB_PRINTER_INFO_REQUESTED, |
660 core_api::printer_provider::OnGetUsbPrinterInfoRequested::kEventName, | 657 api::printer_provider::OnGetUsbPrinterInfoRequested::kEventName, |
661 internal_args.Pass())); | 658 internal_args.Pass())); |
662 event_router->DispatchEventToExtension(extension_id, event.Pass()); | 659 event_router->DispatchEventToExtension(extension_id, event.Pass()); |
663 } | 660 } |
664 | 661 |
665 void PrinterProviderAPIImpl::OnGetPrintersResult( | 662 void PrinterProviderAPIImpl::OnGetPrintersResult( |
666 const Extension* extension, | 663 const Extension* extension, |
667 int request_id, | 664 int request_id, |
668 const PrinterProviderInternalAPIObserver::PrinterInfoVector& result) { | 665 const PrinterProviderInternalAPIObserver::PrinterInfoVector& result) { |
669 base::ListValue printer_list; | 666 base::ListValue printer_list; |
670 | 667 |
671 // Update some printer description properties to better identify the extension | 668 // Update some printer description properties to better identify the extension |
(...skipping 11 matching lines...) Expand all Loading... |
683 void PrinterProviderAPIImpl::OnGetCapabilityResult( | 680 void PrinterProviderAPIImpl::OnGetCapabilityResult( |
684 const Extension* extension, | 681 const Extension* extension, |
685 int request_id, | 682 int request_id, |
686 const base::DictionaryValue& result) { | 683 const base::DictionaryValue& result) { |
687 pending_capability_requests_[extension->id()].Complete(request_id, result); | 684 pending_capability_requests_[extension->id()].Complete(request_id, result); |
688 } | 685 } |
689 | 686 |
690 void PrinterProviderAPIImpl::OnPrintResult( | 687 void PrinterProviderAPIImpl::OnPrintResult( |
691 const Extension* extension, | 688 const Extension* extension, |
692 int request_id, | 689 int request_id, |
693 core_api::printer_provider_internal::PrintError error) { | 690 api::printer_provider_internal::PrintError error) { |
694 const std::string error_str = | 691 const std::string error_str = |
695 error == core_api::printer_provider_internal::PRINT_ERROR_NONE | 692 error == api::printer_provider_internal::PRINT_ERROR_NONE |
696 ? PrinterProviderAPI::GetDefaultPrintError() | 693 ? PrinterProviderAPI::GetDefaultPrintError() |
697 : core_api::printer_provider_internal::ToString(error); | 694 : api::printer_provider_internal::ToString(error); |
698 pending_print_requests_[extension->id()].Complete( | 695 pending_print_requests_[extension->id()].Complete( |
699 request_id, error == core_api::printer_provider_internal::PRINT_ERROR_OK, | 696 request_id, error == api::printer_provider_internal::PRINT_ERROR_OK, |
700 error_str); | 697 error_str); |
701 } | 698 } |
702 | 699 |
703 void PrinterProviderAPIImpl::OnGetUsbPrinterInfoResult( | 700 void PrinterProviderAPIImpl::OnGetUsbPrinterInfoResult( |
704 const Extension* extension, | 701 const Extension* extension, |
705 int request_id, | 702 int request_id, |
706 const core_api::printer_provider::PrinterInfo* result) { | 703 const api::printer_provider::PrinterInfo* result) { |
707 if (result) { | 704 if (result) { |
708 scoped_ptr<base::DictionaryValue> printer(result->ToValue()); | 705 scoped_ptr<base::DictionaryValue> printer(result->ToValue()); |
709 UpdatePrinterWithExtensionInfo(printer.get(), extension); | 706 UpdatePrinterWithExtensionInfo(printer.get(), extension); |
710 pending_usb_printer_info_requests_[extension->id()].Complete(request_id, | 707 pending_usb_printer_info_requests_[extension->id()].Complete(request_id, |
711 *printer); | 708 *printer); |
712 } else { | 709 } else { |
713 pending_usb_printer_info_requests_[extension->id()].Complete( | 710 pending_usb_printer_info_requests_[extension->id()].Complete( |
714 request_id, base::DictionaryValue()); | 711 request_id, base::DictionaryValue()); |
715 } | 712 } |
716 } | 713 } |
(...skipping 26 matching lines...) Expand all Loading... |
743 bool PrinterProviderAPIImpl::WillRequestPrinters( | 740 bool PrinterProviderAPIImpl::WillRequestPrinters( |
744 int request_id, | 741 int request_id, |
745 content::BrowserContext* browser_context, | 742 content::BrowserContext* browser_context, |
746 const Extension* extension, | 743 const Extension* extension, |
747 base::ListValue* args) { | 744 base::ListValue* args) { |
748 if (!extension) | 745 if (!extension) |
749 return false; | 746 return false; |
750 EventRouter* event_router = EventRouter::Get(browser_context_); | 747 EventRouter* event_router = EventRouter::Get(browser_context_); |
751 if (!event_router->ExtensionHasEventListener( | 748 if (!event_router->ExtensionHasEventListener( |
752 extension->id(), | 749 extension->id(), |
753 core_api::printer_provider::OnGetPrintersRequested::kEventName)) { | 750 api::printer_provider::OnGetPrintersRequested::kEventName)) { |
754 return false; | 751 return false; |
755 } | 752 } |
756 | 753 |
757 return pending_get_printers_requests_.AddSource(request_id, extension->id()); | 754 return pending_get_printers_requests_.AddSource(request_id, extension->id()); |
758 } | 755 } |
759 | 756 |
760 } // namespace | 757 } // namespace |
761 | 758 |
762 // static | 759 // static |
763 PrinterProviderAPI* PrinterProviderAPI::Create( | 760 PrinterProviderAPI* PrinterProviderAPI::Create( |
764 content::BrowserContext* context) { | 761 content::BrowserContext* context) { |
765 return new PrinterProviderAPIImpl(context); | 762 return new PrinterProviderAPIImpl(context); |
766 } | 763 } |
767 | 764 |
768 // static | 765 // static |
769 std::string PrinterProviderAPI::GetDefaultPrintError() { | 766 std::string PrinterProviderAPI::GetDefaultPrintError() { |
770 return core_api::printer_provider_internal::ToString( | 767 return api::printer_provider_internal::ToString( |
771 core_api::printer_provider_internal::PRINT_ERROR_FAILED); | 768 api::printer_provider_internal::PRINT_ERROR_FAILED); |
772 } | 769 } |
773 | 770 |
774 } // namespace extensions | 771 } // namespace extensions |
OLD | NEW |