1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/browser/api/printer_provider/printer_provider_api.h"
#include <stddef.h>
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include "base/functional/bind.h"
#include "base/i18n/rtl.h"
#include "base/location.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observation.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/time/time.h"
#include "base/values.h"
#include "components/device_event_log/device_event_log.h"
#include "extensions/browser/api/printer_provider/printer_provider_internal_api.h"
#include "extensions/browser/api/printer_provider/printer_provider_internal_api_observer.h"
#include "extensions/browser/api/printer_provider/printer_provider_print_job.h"
#include "extensions/browser/api/usb/usb_device_manager.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_registry_observer.h"
#include "extensions/browser/unloaded_extension_reason.h"
#include "extensions/common/api/printer_provider.h"
#include "extensions/common/api/printer_provider_internal.h"
#include "extensions/common/api/usb.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_id.h"
#include "extensions/common/mojom/context_type.mojom.h"
#include "extensions/common/mojom/event_dispatcher.mojom-forward.h"
namespace extensions {
namespace {
// The separator between extension id and the extension's internal printer id
// used when generating a printer id unique across extensions.
const char kPrinterIdSeparator = ':';
// Given an extension ID and an ID of a printer reported by the extension, it
// generates a ID for the printer unique across extensions (assuming that the
// printer id is unique in the extension's space).
std::string GeneratePrinterId(const ExtensionId& extension_id,
const std::string& internal_printer_id) {
std::string result = extension_id;
result.append(1, kPrinterIdSeparator);
result.append(internal_printer_id);
return result;
}
// Parses an ID created using |GeneratePrinterId| to it's components:
// the extension ID and the printer ID internal to the extension.
// Returns whenter the ID was succesfully parsed.
bool ParsePrinterId(const std::string& printer_id,
std::string* extension_id,
std::string* internal_printer_id) {
size_t separator = printer_id.find_first_of(kPrinterIdSeparator);
if (separator == std::string::npos)
return false;
*extension_id = printer_id.substr(0, separator);
*internal_printer_id = printer_id.substr(separator + 1);
return true;
}
void UpdatePrinterWithExtensionInfo(base::Value::Dict* printer,
const Extension* extension) {
std::string* internal_printer_id = printer->FindString("id");
CHECK(internal_printer_id);
printer->Set("id", GeneratePrinterId(extension->id(), *internal_printer_id));
printer->Set("extensionId", extension->id());
printer->Set("extensionName", extension->name());
std::string* printer_name = printer->FindString("name");
if (printer_name) {
std::u16string u16_printer_name = base::UTF8ToUTF16(*printer_name);
if (base::i18n::AdjustStringForLocaleDirection(&u16_printer_name))
printer->Set("name", u16_printer_name);
}
std::string* printer_description = printer->FindString("description");
if (printer_description) {
std::u16string u16_printer_description =
base::UTF8ToUTF16(*printer_description);
if (base::i18n::AdjustStringForLocaleDirection(&u16_printer_description))
printer->Set("description", u16_printer_description);
}
}
// Holds information about a pending onGetPrintersRequested request;
// in particular, the list of extensions to which the event was dispatched but
// which haven't yet responded, and the |GetPrinters| callback associated with
// the event.
class GetPrintersRequest {
public:
explicit GetPrintersRequest(
const PrinterProviderAPI::GetPrintersCallback& callback);
~GetPrintersRequest();
// Adds an extension id to the list of the extensions that need to respond
// to the event.
void AddSource(const ExtensionId& extension_id);
// Whether all extensions have responded to the event.
bool IsDone() const;
// Runs the callback for an extension and removes the extension from the
// list of extensions that still have to respond to the event.
void ReportForExtension(const ExtensionId& extension_id,
base::Value::List printers);
private:
// Callback reporting event result for an extension. Called once for each
// extension.
PrinterProviderAPI::GetPrintersCallback callback_;
// The list of extensions that still have to respond to the event.
std::set<std::string> extensions_;
};
// Keeps track of pending chrome.printerProvider.onGetPrintersRequested
// requests.
class PendingGetPrintersRequests {
public:
PendingGetPrintersRequests();
PendingGetPrintersRequests(const PendingGetPrintersRequests&) = delete;
PendingGetPrintersRequests& operator=(const PendingGetPrintersRequests&) =
delete;
~PendingGetPrintersRequests();
// Adds a new request to the set of pending requests. Returns the id
// assigned to the request.
int Add(const PrinterProviderAPI::GetPrintersCallback& callback);
// Completes a request for an extension. It runs the request callback with
// values reported by the extension.
bool CompleteForExtension(const ExtensionId& extension_id,
int request_id,
base::Value::List result);
// Runs callbacks for the extension for all requests that are waiting for a
// response from the extension with the provided extension id. Callbacks are
// called as if the extension reported empty set of printers.
void FailAllForExtension(const ExtensionId& extension_id);
// Adds an extension id to the list of the extensions that need to respond to
// the event.
bool AddSource(int request_id, const ExtensionId& extension_id);
private:
int last_request_id_;
std::map<int, GetPrintersRequest> pending_requests_;
};
// Keeps track of pending chrome.printerProvider.onGetCapabilityRequested
// requests for an extension.
class PendingGetCapabilityRequests {
public:
static constexpr base::TimeDelta kGetCapabilityTimeout = base::Seconds(20);
PendingGetCapabilityRequests();
~PendingGetCapabilityRequests();
// Adds a new request to the set. Only information needed is the callback
// associated with the request. Returns the id assigned to the request.
int Add(PrinterProviderAPI::GetCapabilityCallback callback);
// Completes the request with the provided request id. It runs the request
// callback and removes the request from the set.
void Complete(int request_id, base::Value::Dict result);
// Runs all pending callbacks with empty capability value and clears the
// set of pending requests.
void FailAll();
private:
int last_request_id_;
std::map<int, PrinterProviderAPI::GetCapabilityCallback> pending_requests_;
base::WeakPtrFactory<PendingGetCapabilityRequests> weak_factory_{this};
};
constexpr base::TimeDelta PendingGetCapabilityRequests::kGetCapabilityTimeout;
// Keeps track of pending chrome.printerProvider.onPrintRequested requests
// for an extension.
class PendingPrintRequests {
public:
PendingPrintRequests();
~PendingPrintRequests();
// Adds a new request to the set. Only information needed is the callback
// associated with the request. Returns the id assigned to the request.
int Add(PrinterProviderPrintJob job,
PrinterProviderAPI::PrintCallback callback);
// Gets print job associated with a request.
const PrinterProviderPrintJob* GetPrintJob(int request_id) const;
// Completes the request with the provided request id. It runs the request
// callback and removes the request from the set.
bool Complete(int request_id,
api::printer_provider_internal::PrintError error);
// Runs all pending callbacks with ERROR_FAILED and clears the set of
// pending requests.
void FailAll();
private:
struct PrintRequest {
PrinterProviderAPI::PrintCallback callback;
PrinterProviderPrintJob job;
};
int last_request_id_;
std::map<int, PrintRequest> pending_requests_;
};
// Keeps track of pending chrome.printerProvider.onGetUsbPrinterInfoRequested
// requests for an extension.
class PendingUsbPrinterInfoRequests {
public:
PendingUsbPrinterInfoRequests();
~PendingUsbPrinterInfoRequests();
// Adds a new request to the set. Only information needed is the callback
// associated with the request. Returns the id assigned to the request.
int Add(PrinterProviderAPI::GetPrinterInfoCallback callback);
// Completes the request with the provided request id. It runs the request
// callback and removes the request from the set.
void Complete(int request_id, base::Value::Dict printer_info);
// Runs all pending callbacks with empty capability value and clears the
// set of pending requests.
void FailAll();
private:
int last_request_id_ = 0;
std::map<int, PrinterProviderAPI::GetPrinterInfoCallback> pending_requests_;
};
// Implements chrome.printerProvider API events.
class PrinterProviderAPIImpl : public PrinterProviderAPI,
public PrinterProviderInternalAPIObserver,
public ExtensionRegistryObserver {
public:
explicit PrinterProviderAPIImpl(content::BrowserContext* browser_context);
PrinterProviderAPIImpl(const PrinterProviderAPIImpl&) = delete;
PrinterProviderAPIImpl& operator=(const PrinterProviderAPIImpl&) = delete;
~PrinterProviderAPIImpl() override;
private:
// PrinterProviderAPI implementation:
void DispatchGetPrintersRequested(
const GetPrintersCallback& callback) override;
void DispatchGetCapabilityRequested(const std::string& printer_id,
GetCapabilityCallback callback) override;
void DispatchPrintRequested(PrinterProviderPrintJob job,
PrintCallback callback) override;
const PrinterProviderPrintJob* GetPrintJob(const Extension* extension,
int request_id) const override;
void DispatchGetUsbPrinterInfoRequested(
const ExtensionId& extension_id,
const device::mojom::UsbDeviceInfo& device,
GetPrinterInfoCallback callback) override;
// PrinterProviderInternalAPIObserver implementation:
void OnGetPrintersResult(
const Extension* extension,
int request_id,
const PrinterProviderInternalAPIObserver::PrinterInfoVector& result)
override;
void OnGetCapabilityResult(const Extension* extension,
int request_id,
base::Value::Dict result) override;
void OnPrintResult(const Extension* extension,
int request_id,
api::printer_provider_internal::PrintError error) override;
void OnGetUsbPrinterInfoResult(
const Extension* extension,
int request_id,
const api::printer_provider::PrinterInfo* printer_info) override;
// ExtensionRegistryObserver implementation:
void OnExtensionUnloaded(content::BrowserContext* browser_context,
const Extension* extension,
UnloadedExtensionReason reason) override;
// Called before chrome.printerProvider.onGetPrintersRequested event is
// dispatched to an extension. It returns whether the extension is interested
// in the event. If the extension listens to the event, it's added to the set
// of |request| sources. |request| is |GetPrintersRequest| object associated
// with the event.
bool WillRequestPrinters(
int request_id,
content::BrowserContext* browser_context,
mojom::ContextType target_context,
const Extension* extension,
const base::Value::Dict* listener_filter,
std::optional<base::Value::List>& event_args_out,
mojom::EventFilteringInfoPtr& event_filtering_info_out);
raw_ptr<content::BrowserContext> browser_context_;
PendingGetPrintersRequests pending_get_printers_requests_;
std::map<std::string, PendingPrintRequests> pending_print_requests_;
std::map<std::string, PendingGetCapabilityRequests>
pending_capability_requests_;
std::map<std::string, PendingUsbPrinterInfoRequests>
pending_usb_printer_info_requests_;
base::ScopedObservation<PrinterProviderInternalAPI,
PrinterProviderInternalAPIObserver>
internal_api_observation_{this};
base::ScopedObservation<ExtensionRegistry, ExtensionRegistryObserver>
extension_registry_observation_{this};
};
GetPrintersRequest::GetPrintersRequest(
const PrinterProviderAPI::GetPrintersCallback& callback)
: callback_(callback) {
}
GetPrintersRequest::~GetPrintersRequest() {
}
void GetPrintersRequest::AddSource(const ExtensionId& extension_id) {
extensions_.insert(extension_id);
}
bool GetPrintersRequest::IsDone() const {
return extensions_.empty();
}
void GetPrintersRequest::ReportForExtension(const ExtensionId& extension_id,
base::Value::List printers) {
if (extensions_.erase(extension_id) > 0)
callback_.Run(std::move(printers), IsDone());
}
PendingGetPrintersRequests::PendingGetPrintersRequests() : last_request_id_(0) {
}
PendingGetPrintersRequests::~PendingGetPrintersRequests() {
}
int PendingGetPrintersRequests::Add(
const PrinterProviderAPI::GetPrintersCallback& callback) {
pending_requests_.insert(
std::make_pair(++last_request_id_, GetPrintersRequest(callback)));
return last_request_id_;
}
bool PendingGetPrintersRequests::CompleteForExtension(
const ExtensionId& extension_id,
int request_id,
base::Value::List result) {
auto it = pending_requests_.find(request_id);
if (it == pending_requests_.end())
return false;
it->second.ReportForExtension(extension_id, std::move(result));
if (it->second.IsDone()) {
pending_requests_.erase(it);
}
return true;
}
void PendingGetPrintersRequests::FailAllForExtension(
const ExtensionId& extension_id) {
auto it = pending_requests_.begin();
while (it != pending_requests_.end()) {
int request_id = it->first;
// |it| may get deleted during |CompleteForExtension|, so progress it to the
// next item before calling the method.
++it;
CompleteForExtension(extension_id, request_id, base::Value::List());
}
}
bool PendingGetPrintersRequests::AddSource(int request_id,
const ExtensionId& extension_id) {
auto it = pending_requests_.find(request_id);
if (it == pending_requests_.end())
return false;
it->second.AddSource(extension_id);
return true;
}
PendingGetCapabilityRequests::PendingGetCapabilityRequests()
: last_request_id_(0) {
}
PendingGetCapabilityRequests::~PendingGetCapabilityRequests() {
}
int PendingGetCapabilityRequests::Add(
PrinterProviderAPI::GetCapabilityCallback callback) {
pending_requests_[++last_request_id_] = std::move(callback);
// Abort the request after the timeout is exceeded.
base::SequencedTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE,
base::BindOnce(&PendingGetCapabilityRequests::Complete,
weak_factory_.GetWeakPtr(), last_request_id_,
base::Value::Dict()),
kGetCapabilityTimeout);
return last_request_id_;
}
void PendingGetCapabilityRequests::Complete(int request_id,
base::Value::Dict response) {
auto it = pending_requests_.find(request_id);
if (it == pending_requests_.end())
return;
PrinterProviderAPI::GetCapabilityCallback callback = std::move(it->second);
pending_requests_.erase(it);
std::move(callback).Run(std::move(response));
}
void PendingGetCapabilityRequests::FailAll() {
for (auto& request : pending_requests_)
std::move(request.second).Run(base::Value::Dict());
pending_requests_.clear();
}
PendingPrintRequests::PendingPrintRequests() : last_request_id_(0) {
}
PendingPrintRequests::~PendingPrintRequests() {
}
int PendingPrintRequests::Add(PrinterProviderPrintJob job,
PrinterProviderAPI::PrintCallback callback) {
PrintRequest request;
request.callback = std::move(callback);
request.job = std::move(job);
pending_requests_[++last_request_id_] = std::move(request);
return last_request_id_;
}
bool PendingPrintRequests::Complete(
int request_id,
api::printer_provider_internal::PrintError error) {
auto it = pending_requests_.find(request_id);
if (it == pending_requests_.end())
return false;
PrinterProviderAPI::PrintCallback callback = std::move(it->second.callback);
pending_requests_.erase(it);
base::Value error_value;
if (error != api::printer_provider_internal::PrintError::kOk) {
const std::string error_str =
error == api::printer_provider_internal::PrintError::kNone
? PrinterProviderAPI::GetDefaultPrintError()
: api::printer_provider_internal::ToString(error);
error_value = base::Value(error_str);
}
std::move(callback).Run(error_value);
return true;
}
const PrinterProviderPrintJob* PendingPrintRequests::GetPrintJob(
int request_id) const {
auto it = pending_requests_.find(request_id);
if (it == pending_requests_.end())
return nullptr;
return &it->second.job;
}
void PendingPrintRequests::FailAll() {
for (auto& request : pending_requests_) {
std::move(request.second.callback)
.Run(base::Value(PrinterProviderAPI::GetDefaultPrintError()));
}
pending_requests_.clear();
}
PendingUsbPrinterInfoRequests::PendingUsbPrinterInfoRequests() {
}
PendingUsbPrinterInfoRequests::~PendingUsbPrinterInfoRequests() {
}
int PendingUsbPrinterInfoRequests::Add(
PrinterProviderAPI::GetPrinterInfoCallback callback) {
pending_requests_[++last_request_id_] = std::move(callback);
return last_request_id_;
}
void PendingUsbPrinterInfoRequests::Complete(int request_id,
base::Value::Dict printer_info) {
auto it = pending_requests_.find(request_id);
if (it == pending_requests_.end())
return;
PrinterProviderAPI::GetPrinterInfoCallback callback = std::move(it->second);
pending_requests_.erase(it);
std::move(callback).Run(std::move(printer_info));
}
void PendingUsbPrinterInfoRequests::FailAll() {
for (auto& request : pending_requests_) {
std::move(request.second).Run(base::Value::Dict());
}
pending_requests_.clear();
}
PrinterProviderAPIImpl::PrinterProviderAPIImpl(
content::BrowserContext* browser_context)
: browser_context_(browser_context) {
internal_api_observation_.Observe(
PrinterProviderInternalAPI::GetFactoryInstance()->Get(browser_context));
extension_registry_observation_.Observe(
ExtensionRegistry::Get(browser_context));
}
PrinterProviderAPIImpl::~PrinterProviderAPIImpl() {
}
void PrinterProviderAPIImpl::DispatchGetPrintersRequested(
const GetPrintersCallback& callback) {
EventRouter* event_router = EventRouter::Get(browser_context_);
if (!event_router->HasEventListener(
api::printer_provider::OnGetPrintersRequested::kEventName)) {
callback.Run(base::Value::List(), /*done=*/true);
return;
}
// |pending_get_printers_requests_| take ownership of |request| which gets
// NULLed out. Save the pointer before passing it to the requests, as it will
// be needed later on.
int request_id = pending_get_printers_requests_.Add(callback);
base::Value::List internal_args;
// Request id is not part of the public API, but it will be massaged out in
// custom bindings.
internal_args.Append(request_id);
auto event = std::make_unique<Event>(
events::PRINTER_PROVIDER_ON_GET_PRINTERS_REQUESTED,
api::printer_provider::OnGetPrintersRequested::kEventName,
std::move(internal_args));
// This callback is called synchronously during |BroadcastEvent|, so
// Unretained is safe.
event->will_dispatch_callback =
base::BindRepeating(&PrinterProviderAPIImpl::WillRequestPrinters,
base::Unretained(this), request_id);
event_router->BroadcastEvent(std::move(event));
}
void PrinterProviderAPIImpl::DispatchGetCapabilityRequested(
const std::string& printer_id,
GetCapabilityCallback callback) {
ExtensionId extension_id;
std::string internal_printer_id;
if (!ParsePrinterId(printer_id, &extension_id, &internal_printer_id)) {
std::move(callback).Run(base::Value::Dict());
return;
}
EventRouter* event_router = EventRouter::Get(browser_context_);
if (!event_router->ExtensionHasEventListener(
extension_id,
api::printer_provider::OnGetCapabilityRequested::kEventName)) {
std::move(callback).Run(base::Value::Dict());
return;
}
int request_id =
pending_capability_requests_[extension_id].Add(std::move(callback));
base::Value::List internal_args;
// Request id is not part of the public API, but it will be massaged out in
// custom bindings.
internal_args.Append(request_id);
internal_args.Append(internal_printer_id);
std::unique_ptr<Event> event(
new Event(events::PRINTER_PROVIDER_ON_GET_CAPABILITY_REQUESTED,
api::printer_provider::OnGetCapabilityRequested::kEventName,
std::move(internal_args)));
event_router->DispatchEventToExtension(extension_id, std::move(event));
}
void PrinterProviderAPIImpl::DispatchPrintRequested(PrinterProviderPrintJob job,
PrintCallback callback) {
ExtensionId extension_id;
std::string internal_printer_id;
if (!ParsePrinterId(job.printer_id, &extension_id, &internal_printer_id)) {
std::move(callback).Run(base::Value(GetDefaultPrintError()));
return;
}
EventRouter* event_router = EventRouter::Get(browser_context_);
if (!event_router->ExtensionHasEventListener(
extension_id, api::printer_provider::OnPrintRequested::kEventName)) {
std::move(callback).Run(base::Value(GetDefaultPrintError()));
return;
}
api::printer_provider::PrintJob print_job;
print_job.printer_id = internal_printer_id;
if (auto ticket =
api::printer_provider::PrintJob::Ticket::FromValue(job.ticket);
!ticket) {
std::move(callback).Run(base::Value(api::printer_provider::ToString(
api::printer_provider::PrintError::kInvalidTicket)));
return;
} else {
print_job.ticket = std::move(ticket).value();
}
print_job.content_type = job.content_type;
print_job.title = base::UTF16ToUTF8(job.job_title);
int request_id = pending_print_requests_[extension_id].Add(
std::move(job), std::move(callback));
base::Value::List internal_args;
// Request id is not part of the public API and it will be massaged out in
// custom bindings.
internal_args.Append(request_id);
internal_args.Append(print_job.ToValue());
auto event = std::make_unique<Event>(
events::PRINTER_PROVIDER_ON_PRINT_REQUESTED,
api::printer_provider::OnPrintRequested::kEventName,
std::move(internal_args));
event_router->DispatchEventToExtension(extension_id, std::move(event));
}
const PrinterProviderPrintJob* PrinterProviderAPIImpl::GetPrintJob(
const Extension* extension,
int request_id) const {
auto it = pending_print_requests_.find(extension->id());
if (it == pending_print_requests_.end())
return nullptr;
return it->second.GetPrintJob(request_id);
}
void PrinterProviderAPIImpl::DispatchGetUsbPrinterInfoRequested(
const ExtensionId& extension_id,
const device::mojom::UsbDeviceInfo& device,
GetPrinterInfoCallback callback) {
EventRouter* event_router = EventRouter::Get(browser_context_);
if (!event_router->ExtensionHasEventListener(
extension_id,
api::printer_provider::OnGetUsbPrinterInfoRequested::kEventName)) {
std::move(callback).Run(base::Value::Dict());
return;
}
int request_id =
pending_usb_printer_info_requests_[extension_id].Add(std::move(callback));
api::usb::Device api_device;
UsbDeviceManager::Get(browser_context_)->GetApiDevice(device, &api_device);
base::Value::List internal_args;
// Request id is not part of the public API and it will be massaged out in
// custom bindings.
internal_args.Append(request_id);
internal_args.Append(api_device.ToValue());
auto event = std::make_unique<Event>(
events::PRINTER_PROVIDER_ON_GET_USB_PRINTER_INFO_REQUESTED,
api::printer_provider::OnGetUsbPrinterInfoRequested::kEventName,
std::move(internal_args));
event_router->DispatchEventToExtension(extension_id, std::move(event));
}
void PrinterProviderAPIImpl::OnGetPrintersResult(
const Extension* extension,
int request_id,
const PrinterProviderInternalAPIObserver::PrinterInfoVector& result) {
base::Value::List printer_list;
// Update some printer description properties to better identify the extension
// managing the printer.
for (const api::printer_provider::PrinterInfo& p : result) {
base::Value::Dict printer(p.ToValue());
UpdatePrinterWithExtensionInfo(&printer, extension);
printer_list.Append(std::move(printer));
}
PRINTER_LOG(DEBUG) << "Notifying extensionID=" << extension->id()
<< " of OnGetPrinters request=" << request_id
<< " completed with printers=" << printer_list;
pending_get_printers_requests_.CompleteForExtension(
extension->id(), request_id, std::move(printer_list));
}
void PrinterProviderAPIImpl::OnGetCapabilityResult(const Extension* extension,
int request_id,
base::Value::Dict result) {
PRINTER_LOG(DEBUG) << "Notifying extensionID=" << extension->id()
<< " that OnGetCapabilility request id=" << request_id
<< " completed with capabilities=" << result;
pending_capability_requests_[extension->id()].Complete(request_id,
std::move(result));
}
void PrinterProviderAPIImpl::OnPrintResult(
const Extension* extension,
int request_id,
api::printer_provider_internal::PrintError error) {
PRINTER_LOG(DEBUG) << "Notifying extensionID=" << extension->id()
<< " that OnPrint request id=" << request_id
<< " has completed with status="
<< api::printer_provider_internal::ToString(error);
pending_print_requests_[extension->id()].Complete(request_id, error);
}
void PrinterProviderAPIImpl::OnGetUsbPrinterInfoResult(
const Extension* extension,
int request_id,
const api::printer_provider::PrinterInfo* result) {
if (result) {
base::Value::Dict printer(result->ToValue());
UpdatePrinterWithExtensionInfo(&printer, extension);
PRINTER_LOG(DEBUG) << "Notifying extensionID=" << extension->id()
<< " from request=" << request_id << " that "
<< result->name << " is a USB connected printer";
pending_usb_printer_info_requests_[extension->id()].Complete(
request_id, std::move(printer));
} else {
PRINTER_LOG(DEBUG) << "Notifying extensionID=" << extension->id()
<< " from request=" << request_id
<< " that there are no USB connected printers";
pending_usb_printer_info_requests_[extension->id()].Complete(
request_id, base::Value::Dict());
}
}
void PrinterProviderAPIImpl::OnExtensionUnloaded(
content::BrowserContext* browser_context,
const Extension* extension,
UnloadedExtensionReason reason) {
PRINTER_LOG(DEBUG) << "Unloading Extension: Name=" << extension->name()
<< " Version=" << extension->VersionString()
<< " extensionID=" << extension->id()
<< " for reason=" << base::ToString(reason);
pending_get_printers_requests_.FailAllForExtension(extension->id());
auto print_it = pending_print_requests_.find(extension->id());
if (print_it != pending_print_requests_.end()) {
print_it->second.FailAll();
pending_print_requests_.erase(print_it);
}
auto capability_it = pending_capability_requests_.find(extension->id());
if (capability_it != pending_capability_requests_.end()) {
capability_it->second.FailAll();
pending_capability_requests_.erase(capability_it);
}
auto usb_it = pending_usb_printer_info_requests_.find(extension->id());
if (usb_it != pending_usb_printer_info_requests_.end()) {
usb_it->second.FailAll();
pending_usb_printer_info_requests_.erase(usb_it);
}
}
bool PrinterProviderAPIImpl::WillRequestPrinters(
int request_id,
content::BrowserContext* browser_context,
mojom::ContextType target_context,
const Extension* extension,
const base::Value::Dict* listener_filter,
std::optional<base::Value::List>& event_args_out,
mojom::EventFilteringInfoPtr& event_filtering_info_out) {
if (!extension)
return false;
EventRouter* event_router = EventRouter::Get(browser_context_);
if (!event_router->ExtensionHasEventListener(
extension->id(),
api::printer_provider::OnGetPrintersRequested::kEventName)) {
return false;
}
return pending_get_printers_requests_.AddSource(request_id, extension->id());
}
} // namespace
// static
std::unique_ptr<PrinterProviderAPI> PrinterProviderAPI::Create(
content::BrowserContext* context) {
return std::make_unique<PrinterProviderAPIImpl>(context);
}
// static
std::string PrinterProviderAPI::GetDefaultPrintError() {
return api::printer_provider_internal::ToString(
api::printer_provider_internal::PrintError::kFailed);
}
} // namespace extensions
|