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

Side by Side Diff: extensions/browser/api/printer_provider/printer_provider_api.cc

Issue 1148383002: Add onGetUsbPrinterInfoRequested event to printerProvider API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased. Created 5 years, 6 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
OLDNEW
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>
11 11
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/i18n/rtl.h" 13 #include "base/i18n/rtl.h"
14 #include "base/json/json_string_value_serializer.h" 14 #include "base/json/json_string_value_serializer.h"
15 #include "base/macros.h" 15 #include "base/macros.h"
16 #include "base/memory/ref_counted_memory.h"
17 #include "base/scoped_observer.h" 16 #include "base/scoped_observer.h"
18 #include "base/strings/string16.h" 17 #include "base/strings/string16.h"
19 #include "base/strings/utf_string_conversions.h" 18 #include "base/strings/utf_string_conversions.h"
20 #include "base/values.h" 19 #include "base/values.h"
20 #include "device/usb/usb_device.h"
21 #include "extensions/browser/api/printer_provider/printer_provider_print_job.h" 21 #include "extensions/browser/api/printer_provider/printer_provider_print_job.h"
22 #include "extensions/browser/api/printer_provider_internal/printer_provider_inte rnal_api.h" 22 #include "extensions/browser/api/printer_provider_internal/printer_provider_inte rnal_api.h"
23 #include "extensions/browser/api/printer_provider_internal/printer_provider_inte rnal_api_observer.h" 23 #include "extensions/browser/api/printer_provider_internal/printer_provider_inte rnal_api_observer.h"
24 #include "extensions/browser/api/usb/usb_guid_map.h"
24 #include "extensions/browser/event_router.h" 25 #include "extensions/browser/event_router.h"
25 #include "extensions/browser/extension_registry.h" 26 #include "extensions/browser/extension_registry.h"
26 #include "extensions/browser/extension_registry_observer.h" 27 #include "extensions/browser/extension_registry_observer.h"
27 #include "extensions/common/api/printer_provider.h" 28 #include "extensions/common/api/printer_provider.h"
28 #include "extensions/common/api/printer_provider_internal.h" 29 #include "extensions/common/api/printer_provider_internal.h"
30 #include "extensions/common/api/usb.h"
29 #include "extensions/common/extension.h" 31 #include "extensions/common/extension.h"
30 32
33 using device::UsbDevice;
34
31 namespace extensions { 35 namespace extensions {
32 36
33 namespace { 37 namespace {
34 38
35 // The separator between extension id and the extension's internal printer id 39 // The separator between extension id and the extension's internal printer id
36 // used when generating a printer id unique across extensions. 40 // used when generating a printer id unique across extensions.
37 const char kPrinterIdSeparator = ':'; 41 const char kPrinterIdSeparator = ':';
38 42
39 // Given an extension ID and an ID of a printer reported by the extension, it 43 // Given an extension ID and an ID of a printer reported by the extension, it
40 // generates a ID for the printer unique across extensions (assuming that the 44 // generates a ID for the printer unique across extensions (assuming that the
(...skipping 13 matching lines...) Expand all
54 std::string* extension_id, 58 std::string* extension_id,
55 std::string* internal_printer_id) { 59 std::string* internal_printer_id) {
56 size_t separator = printer_id.find_first_of(kPrinterIdSeparator); 60 size_t separator = printer_id.find_first_of(kPrinterIdSeparator);
57 if (separator == std::string::npos) 61 if (separator == std::string::npos)
58 return false; 62 return false;
59 *extension_id = printer_id.substr(0, separator); 63 *extension_id = printer_id.substr(0, separator);
60 *internal_printer_id = printer_id.substr(separator + 1); 64 *internal_printer_id = printer_id.substr(separator + 1);
61 return true; 65 return true;
62 } 66 }
63 67
68 void UpdatePrinterWithExtensionInfo(base::DictionaryValue* printer,
69 const Extension* extension) {
70 std::string internal_printer_id;
71 CHECK(printer->GetString("id", &internal_printer_id));
72 printer->SetString("id",
73 GeneratePrinterId(extension->id(), internal_printer_id));
74 printer->SetString("extensionId", extension->id());
75 printer->SetString("extensionName", extension->name());
76
77 base::string16 printer_name;
78 if (printer->GetString("name", &printer_name) &&
79 base::i18n::AdjustStringForLocaleDirection(&printer_name)) {
80 printer->SetString("name", printer_name);
81 }
82
83 base::string16 printer_description;
84 if (printer->GetString("description", &printer_description) &&
85 base::i18n::AdjustStringForLocaleDirection(&printer_description)) {
86 printer->SetString("description", printer_description);
87 }
88 }
89
64 // Holds information about a pending onGetPrintersRequested request; 90 // Holds information about a pending onGetPrintersRequested request;
65 // in particular, the list of extensions to which the event was dispatched but 91 // in particular, the list of extensions to which the event was dispatched but
66 // which haven't yet responded, and the |GetPrinters| callback associated with 92 // which haven't yet responded, and the |GetPrinters| callback associated with
67 // the event. 93 // the event.
68 class GetPrintersRequest { 94 class GetPrintersRequest {
69 public: 95 public:
70 explicit GetPrintersRequest( 96 explicit GetPrintersRequest(
71 const PrinterProviderAPI::GetPrintersCallback& callback); 97 const PrinterProviderAPI::GetPrintersCallback& callback);
72 ~GetPrintersRequest(); 98 ~GetPrintersRequest();
73 99
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 // values reported by the extension. 133 // values reported by the extension.
108 bool CompleteForExtension(const std::string& extension_id, 134 bool CompleteForExtension(const std::string& extension_id,
109 int request_id, 135 int request_id,
110 const base::ListValue& result); 136 const base::ListValue& result);
111 137
112 // Runs callbacks for the extension for all requests that are waiting for a 138 // Runs callbacks for the extension for all requests that are waiting for a
113 // response from the extension with the provided extension id. Callbacks are 139 // response from the extension with the provided extension id. Callbacks are
114 // called as if the extension reported empty set of printers. 140 // called as if the extension reported empty set of printers.
115 void FailAllForExtension(const std::string& extension_id); 141 void FailAllForExtension(const std::string& extension_id);
116 142
117 // Adds an extension id to the list of the extensions that need to respond 143 // Adds an extension id to the list of the extensions that need to respond to
118 // to the event. 144 // the event.
119 bool AddSource(int request_id, const std::string& extension_id); 145 bool AddSource(int request_id, const std::string& extension_id);
120 146
121 private: 147 private:
122 int last_request_id_; 148 int last_request_id_;
123 std::map<int, GetPrintersRequest> pending_requests_; 149 std::map<int, GetPrintersRequest> pending_requests_;
124 150
125 DISALLOW_COPY_AND_ASSIGN(PendingGetPrintersRequests); 151 DISALLOW_COPY_AND_ASSIGN(PendingGetPrintersRequests);
126 }; 152 };
127 153
128 // Keeps track of pending chrome.printerProvider.onGetCapabilityRequested 154 // Keeps track of pending chrome.printerProvider.onGetCapabilityRequested
(...skipping 13 matching lines...) Expand all
142 168
143 // Runs all pending callbacks with empty capability value and clears the 169 // Runs all pending callbacks with empty capability value and clears the
144 // set of pending requests. 170 // set of pending requests.
145 void FailAll(); 171 void FailAll();
146 172
147 private: 173 private:
148 int last_request_id_; 174 int last_request_id_;
149 std::map<int, PrinterProviderAPI::GetCapabilityCallback> pending_requests_; 175 std::map<int, PrinterProviderAPI::GetCapabilityCallback> pending_requests_;
150 }; 176 };
151 177
152 // Keeps track of pending chrome.printerProvider.ontPrintRequested requests 178 // Keeps track of pending chrome.printerProvider.onPrintRequested requests
153 // for an extension. 179 // for an extension.
154 class PendingPrintRequests { 180 class PendingPrintRequests {
155 public: 181 public:
156 PendingPrintRequests(); 182 PendingPrintRequests();
157 ~PendingPrintRequests(); 183 ~PendingPrintRequests();
158 184
159 // Adds a new request to the set. Only information needed is the callback 185 // Adds a new request to the set. Only information needed is the callback
160 // associated with the request. Returns the id assigned to the request. 186 // associated with the request. Returns the id assigned to the request.
161 int Add(const PrinterProviderPrintJob& job, 187 int Add(const PrinterProviderPrintJob& job,
162 const PrinterProviderAPI::PrintCallback& callback); 188 const PrinterProviderAPI::PrintCallback& callback);
(...skipping 12 matching lines...) Expand all
175 private: 201 private:
176 struct PrintRequest { 202 struct PrintRequest {
177 PrinterProviderAPI::PrintCallback callback; 203 PrinterProviderAPI::PrintCallback callback;
178 PrinterProviderPrintJob job; 204 PrinterProviderPrintJob job;
179 }; 205 };
180 206
181 int last_request_id_; 207 int last_request_id_;
182 std::map<int, PrintRequest> pending_requests_; 208 std::map<int, PrintRequest> pending_requests_;
183 }; 209 };
184 210
211 // Keeps track of pending chrome.printerProvider.onGetUsbPrinterInfoRequested
212 // requests for an extension.
213 class PendingUsbPrinterInfoRequests {
214 public:
215 PendingUsbPrinterInfoRequests();
216 ~PendingUsbPrinterInfoRequests();
217
218 // Adds a new request to the set. Only information needed is the callback
219 // associated with the request. Returns the id assigned to the request.
220 int Add(const PrinterProviderAPI::GetPrinterInfoCallback& callback);
221
222 // Completes the request with the provided request id. It runs the request
223 // callback and removes the request from the set.
224 void Complete(int request_id, const base::DictionaryValue& printer_info);
225
226 // Runs all pending callbacks with empty capability value and clears the
227 // set of pending requests.
228 void FailAll();
229
230 private:
231 int last_request_id_ = 0;
232 std::map<int, PrinterProviderAPI::GetPrinterInfoCallback> pending_requests_;
233 };
234
185 // Implements chrome.printerProvider API events. 235 // Implements chrome.printerProvider API events.
186 class PrinterProviderAPIImpl : public PrinterProviderAPI, 236 class PrinterProviderAPIImpl : public PrinterProviderAPI,
187 public PrinterProviderInternalAPIObserver, 237 public PrinterProviderInternalAPIObserver,
188 public ExtensionRegistryObserver { 238 public ExtensionRegistryObserver {
189 public: 239 public:
190 explicit PrinterProviderAPIImpl(content::BrowserContext* browser_context); 240 explicit PrinterProviderAPIImpl(content::BrowserContext* browser_context);
191 ~PrinterProviderAPIImpl() override; 241 ~PrinterProviderAPIImpl() override;
192 242
193 private: 243 private:
194 // PrinterProviderAPI implementation: 244 // PrinterProviderAPI implementation:
195 void DispatchGetPrintersRequested( 245 void DispatchGetPrintersRequested(
196 const PrinterProviderAPI::GetPrintersCallback& callback) override; 246 const PrinterProviderAPI::GetPrintersCallback& callback) override;
197 void DispatchGetCapabilityRequested( 247 void DispatchGetCapabilityRequested(
198 const std::string& printer_id, 248 const std::string& printer_id,
199 const PrinterProviderAPI::GetCapabilityCallback& callback) override; 249 const PrinterProviderAPI::GetCapabilityCallback& callback) override;
200 void DispatchPrintRequested( 250 void DispatchPrintRequested(
201 const PrinterProviderPrintJob& job, 251 const PrinterProviderPrintJob& job,
202 const PrinterProviderAPI::PrintCallback& callback) override; 252 const PrinterProviderAPI::PrintCallback& callback) override;
203 const PrinterProviderPrintJob* GetPrintJob(const Extension* extension, 253 const PrinterProviderPrintJob* GetPrintJob(const Extension* extension,
204 int request_id) const override; 254 int request_id) const override;
255 void DispatchGetUsbPrinterInfoRequested(
256 const std::string& extension_id,
257 scoped_refptr<UsbDevice> device,
258 const PrinterProviderAPI::GetPrinterInfoCallback& callback) override;
205 259
206 // PrinterProviderInternalAPIObserver implementation: 260 // PrinterProviderInternalAPIObserver implementation:
207 void OnGetPrintersResult( 261 void OnGetPrintersResult(
208 const Extension* extension, 262 const Extension* extension,
209 int request_id, 263 int request_id,
210 const PrinterProviderInternalAPIObserver::PrinterInfoVector& result) 264 const PrinterProviderInternalAPIObserver::PrinterInfoVector& result)
211 override; 265 override;
212 void OnGetCapabilityResult(const Extension* extension, 266 void OnGetCapabilityResult(const Extension* extension,
213 int request_id, 267 int request_id,
214 const base::DictionaryValue& result) override; 268 const base::DictionaryValue& result) override;
215 void OnPrintResult( 269 void OnPrintResult(
216 const Extension* extension, 270 const Extension* extension,
217 int request_id, 271 int request_id,
218 core_api::printer_provider_internal::PrintError error) override; 272 core_api::printer_provider_internal::PrintError error) override;
273 void OnGetUsbPrinterInfoResult(
274 const Extension* extension,
275 int request_id,
276 const core_api::printer_provider::PrinterInfo* printer_info) override;
219 277
220 // ExtensionRegistryObserver implementation: 278 // ExtensionRegistryObserver implementation:
221 void OnExtensionUnloaded(content::BrowserContext* browser_context, 279 void OnExtensionUnloaded(content::BrowserContext* browser_context,
222 const Extension* extension, 280 const Extension* extension,
223 UnloadedExtensionInfo::Reason reason) override; 281 UnloadedExtensionInfo::Reason reason) override;
224 282
225 // Called before chrome.printerProvider.onGetPrintersRequested event is 283 // Called before chrome.printerProvider.onGetPrintersRequested event is
226 // dispatched to an extension. It returns whether the extension is interested 284 // dispatched to an extension. It returns whether the extension is interested
227 // in the event. If the extension listens to the event, it's added to the set 285 // in the event. If the extension listens to the event, it's added to the set
228 // of |request| sources. |request| is |GetPrintersRequest| object associated 286 // of |request| sources. |request| is |GetPrintersRequest| object associated
229 // with the event. 287 // with the event.
230 bool WillRequestPrinters(int request_id, 288 bool WillRequestPrinters(int request_id,
231 content::BrowserContext* browser_context, 289 content::BrowserContext* browser_context,
232 const Extension* extension, 290 const Extension* extension,
233 base::ListValue* args); 291 base::ListValue* args);
234 292
235 content::BrowserContext* browser_context_; 293 content::BrowserContext* browser_context_;
236 294
237 PendingGetPrintersRequests pending_get_printers_requests_; 295 PendingGetPrintersRequests pending_get_printers_requests_;
238 296
239 std::map<std::string, PendingPrintRequests> pending_print_requests_; 297 std::map<std::string, PendingPrintRequests> pending_print_requests_;
240 298
241 std::map<std::string, PendingGetCapabilityRequests> 299 std::map<std::string, PendingGetCapabilityRequests>
242 pending_capability_requests_; 300 pending_capability_requests_;
243 301
302 std::map<std::string, PendingUsbPrinterInfoRequests>
303 pending_usb_printer_info_requests_;
304
244 ScopedObserver<PrinterProviderInternalAPI, PrinterProviderInternalAPIObserver> 305 ScopedObserver<PrinterProviderInternalAPI, PrinterProviderInternalAPIObserver>
245 internal_api_observer_; 306 internal_api_observer_;
246 307
247 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> 308 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
248 extension_registry_observer_; 309 extension_registry_observer_;
249 310
250 DISALLOW_COPY_AND_ASSIGN(PrinterProviderAPIImpl); 311 DISALLOW_COPY_AND_ASSIGN(PrinterProviderAPIImpl);
251 }; 312 };
252 313
253 GetPrintersRequest::GetPrintersRequest( 314 GetPrintersRequest::GetPrintersRequest(
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 return &it->second.job; 455 return &it->second.job;
395 } 456 }
396 457
397 void PendingPrintRequests::FailAll() { 458 void PendingPrintRequests::FailAll() {
398 for (auto& request : pending_requests_) 459 for (auto& request : pending_requests_)
399 request.second.callback.Run(false, 460 request.second.callback.Run(false,
400 PrinterProviderAPI::GetDefaultPrintError()); 461 PrinterProviderAPI::GetDefaultPrintError());
401 pending_requests_.clear(); 462 pending_requests_.clear();
402 } 463 }
403 464
465 PendingUsbPrinterInfoRequests::PendingUsbPrinterInfoRequests() {
466 }
467
468 PendingUsbPrinterInfoRequests::~PendingUsbPrinterInfoRequests() {
469 }
470
471 int PendingUsbPrinterInfoRequests::Add(
472 const PrinterProviderAPI::GetPrinterInfoCallback& callback) {
473 pending_requests_[++last_request_id_] = callback;
474 return last_request_id_;
475 }
476
477 void PendingUsbPrinterInfoRequests::Complete(
478 int request_id,
479 const base::DictionaryValue& printer_info) {
480 auto it = pending_requests_.find(request_id);
481 if (it == pending_requests_.end())
482 return;
483
484 PrinterProviderAPI::GetPrinterInfoCallback callback = it->second;
485 pending_requests_.erase(it);
486
487 callback.Run(printer_info);
488 }
489
490 void PendingUsbPrinterInfoRequests::FailAll() {
491 for (auto& request : pending_requests_) {
492 request.second.Run(base::DictionaryValue());
493 }
494 pending_requests_.clear();
495 }
496
404 PrinterProviderAPIImpl::PrinterProviderAPIImpl( 497 PrinterProviderAPIImpl::PrinterProviderAPIImpl(
405 content::BrowserContext* browser_context) 498 content::BrowserContext* browser_context)
406 : browser_context_(browser_context), 499 : browser_context_(browser_context),
407 internal_api_observer_(this), 500 internal_api_observer_(this),
408 extension_registry_observer_(this) { 501 extension_registry_observer_(this) {
409 internal_api_observer_.Add( 502 internal_api_observer_.Add(
410 PrinterProviderInternalAPI::GetFactoryInstance()->Get(browser_context)); 503 PrinterProviderInternalAPI::GetFactoryInstance()->Get(browser_context));
411 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context)); 504 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context));
412 } 505 }
413 506
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
527 620
528 const PrinterProviderPrintJob* PrinterProviderAPIImpl::GetPrintJob( 621 const PrinterProviderPrintJob* PrinterProviderAPIImpl::GetPrintJob(
529 const Extension* extension, 622 const Extension* extension,
530 int request_id) const { 623 int request_id) const {
531 auto it = pending_print_requests_.find(extension->id()); 624 auto it = pending_print_requests_.find(extension->id());
532 if (it == pending_print_requests_.end()) 625 if (it == pending_print_requests_.end())
533 return nullptr; 626 return nullptr;
534 return it->second.GetPrintJob(request_id); 627 return it->second.GetPrintJob(request_id);
535 } 628 }
536 629
630 void PrinterProviderAPIImpl::DispatchGetUsbPrinterInfoRequested(
631 const std::string& extension_id,
632 scoped_refptr<UsbDevice> device,
633 const PrinterProviderAPI::GetPrinterInfoCallback& callback) {
634 EventRouter* event_router = EventRouter::Get(browser_context_);
635 if (!event_router->ExtensionHasEventListener(
636 extension_id, core_api::printer_provider::
637 OnGetUsbPrinterInfoRequested::kEventName)) {
638 callback.Run(base::DictionaryValue());
639 return;
640 }
641
642 int request_id =
643 pending_usb_printer_info_requests_[extension_id].Add(callback);
644 core_api::usb::Device usb_device;
645 usb_device.device =
646 UsbGuidMap::Get(browser_context_)->GetIdFromGuid(device->guid());
647 usb_device.vendor_id = device->vendor_id();
648 usb_device.product_id = device->product_id();
649
650 scoped_ptr<base::ListValue> internal_args(new base::ListValue);
651 // Request id is not part of the public API and it will be massaged out in
652 // custom bindings.
653 internal_args->AppendInteger(request_id);
654 internal_args->Append(usb_device.ToValue().release());
655 scoped_ptr<Event> event(new Event(
656 core_api::printer_provider::OnGetUsbPrinterInfoRequested::kEventName,
657 internal_args.Pass()));
658 event_router->DispatchEventToExtension(extension_id, event.Pass());
659 }
660
537 void PrinterProviderAPIImpl::OnGetPrintersResult( 661 void PrinterProviderAPIImpl::OnGetPrintersResult(
538 const Extension* extension, 662 const Extension* extension,
539 int request_id, 663 int request_id,
540 const PrinterProviderInternalAPIObserver::PrinterInfoVector& result) { 664 const PrinterProviderInternalAPIObserver::PrinterInfoVector& result) {
541 base::ListValue printer_list; 665 base::ListValue printer_list;
542 666
543 // Update some printer description properties to better identify the extension 667 // Update some printer description properties to better identify the extension
544 // managing the printer. 668 // managing the printer.
545 for (size_t i = 0; i < result.size(); ++i) { 669 for (size_t i = 0; i < result.size(); ++i) {
546 scoped_ptr<base::DictionaryValue> printer(result[i]->ToValue()); 670 scoped_ptr<base::DictionaryValue> printer(result[i]->ToValue());
547 std::string internal_printer_id; 671 UpdatePrinterWithExtensionInfo(printer.get(), extension);
548 CHECK(printer->GetString("id", &internal_printer_id)); 672 printer_list.Append(printer.Pass());
549 printer->SetString("id",
550 GeneratePrinterId(extension->id(), internal_printer_id));
551 printer->SetString("extensionId", extension->id());
552 printer->SetString("extensionName", extension->name());
553
554 base::string16 printer_name;
555 if (printer->GetString("name", &printer_name) &&
556 base::i18n::AdjustStringForLocaleDirection(&printer_name)) {
557 printer->SetString("name", printer_name);
558 }
559
560 base::string16 printer_description;
561 if (printer->GetString("description", &printer_description) &&
562 base::i18n::AdjustStringForLocaleDirection(&printer_description)) {
563 printer->SetString("description", printer_description);
564 }
565
566 printer_list.Append(printer.release());
567 } 673 }
568 674
569 pending_get_printers_requests_.CompleteForExtension(extension->id(), 675 pending_get_printers_requests_.CompleteForExtension(extension->id(),
570 request_id, printer_list); 676 request_id, printer_list);
571 } 677 }
572 678
573 void PrinterProviderAPIImpl::OnGetCapabilityResult( 679 void PrinterProviderAPIImpl::OnGetCapabilityResult(
574 const Extension* extension, 680 const Extension* extension,
575 int request_id, 681 int request_id,
576 const base::DictionaryValue& result) { 682 const base::DictionaryValue& result) {
577 pending_capability_requests_[extension->id()].Complete(request_id, result); 683 pending_capability_requests_[extension->id()].Complete(request_id, result);
578 } 684 }
579 685
580 void PrinterProviderAPIImpl::OnPrintResult( 686 void PrinterProviderAPIImpl::OnPrintResult(
581 const Extension* extension, 687 const Extension* extension,
582 int request_id, 688 int request_id,
583 core_api::printer_provider_internal::PrintError error) { 689 core_api::printer_provider_internal::PrintError error) {
584 const std::string error_str = 690 const std::string error_str =
585 error == core_api::printer_provider_internal::PRINT_ERROR_NONE 691 error == core_api::printer_provider_internal::PRINT_ERROR_NONE
586 ? PrinterProviderAPI::GetDefaultPrintError() 692 ? PrinterProviderAPI::GetDefaultPrintError()
587 : core_api::printer_provider_internal::ToString(error); 693 : core_api::printer_provider_internal::ToString(error);
588 pending_print_requests_[extension->id()].Complete( 694 pending_print_requests_[extension->id()].Complete(
589 request_id, error == core_api::printer_provider_internal::PRINT_ERROR_OK, 695 request_id, error == core_api::printer_provider_internal::PRINT_ERROR_OK,
590 error_str); 696 error_str);
591 } 697 }
592 698
699 void PrinterProviderAPIImpl::OnGetUsbPrinterInfoResult(
700 const Extension* extension,
701 int request_id,
702 const core_api::printer_provider::PrinterInfo* result) {
703 if (result) {
704 scoped_ptr<base::DictionaryValue> printer(result->ToValue());
705 UpdatePrinterWithExtensionInfo(printer.get(), extension);
706 pending_usb_printer_info_requests_[extension->id()].Complete(request_id,
707 *printer);
708 } else {
709 pending_usb_printer_info_requests_[extension->id()].Complete(
710 request_id, base::DictionaryValue());
711 }
712 }
713
593 void PrinterProviderAPIImpl::OnExtensionUnloaded( 714 void PrinterProviderAPIImpl::OnExtensionUnloaded(
594 content::BrowserContext* browser_context, 715 content::BrowserContext* browser_context,
595 const Extension* extension, 716 const Extension* extension,
596 UnloadedExtensionInfo::Reason reason) { 717 UnloadedExtensionInfo::Reason reason) {
597 pending_get_printers_requests_.FailAllForExtension(extension->id()); 718 pending_get_printers_requests_.FailAllForExtension(extension->id());
598 719
599 auto print_it = pending_print_requests_.find(extension->id()); 720 auto print_it = pending_print_requests_.find(extension->id());
600 if (print_it != pending_print_requests_.end()) { 721 if (print_it != pending_print_requests_.end()) {
601 print_it->second.FailAll(); 722 print_it->second.FailAll();
602 pending_print_requests_.erase(print_it); 723 pending_print_requests_.erase(print_it);
603 } 724 }
604 725
605 auto capability_it = pending_capability_requests_.find(extension->id()); 726 auto capability_it = pending_capability_requests_.find(extension->id());
606 if (capability_it != pending_capability_requests_.end()) { 727 if (capability_it != pending_capability_requests_.end()) {
607 capability_it->second.FailAll(); 728 capability_it->second.FailAll();
608 pending_capability_requests_.erase(capability_it); 729 pending_capability_requests_.erase(capability_it);
609 } 730 }
731
732 auto usb_it = pending_usb_printer_info_requests_.find(extension->id());
733 if (usb_it != pending_usb_printer_info_requests_.end()) {
734 usb_it->second.FailAll();
735 pending_usb_printer_info_requests_.erase(usb_it);
736 }
610 } 737 }
611 738
612 bool PrinterProviderAPIImpl::WillRequestPrinters( 739 bool PrinterProviderAPIImpl::WillRequestPrinters(
613 int request_id, 740 int request_id,
614 content::BrowserContext* browser_context, 741 content::BrowserContext* browser_context,
615 const Extension* extension, 742 const Extension* extension,
616 base::ListValue* args) { 743 base::ListValue* args) {
617 if (!extension) 744 if (!extension)
618 return false; 745 return false;
619 EventRouter* event_router = EventRouter::Get(browser_context_); 746 EventRouter* event_router = EventRouter::Get(browser_context_);
(...skipping 14 matching lines...) Expand all
634 return new PrinterProviderAPIImpl(context); 761 return new PrinterProviderAPIImpl(context);
635 } 762 }
636 763
637 // static 764 // static
638 std::string PrinterProviderAPI::GetDefaultPrintError() { 765 std::string PrinterProviderAPI::GetDefaultPrintError() {
639 return core_api::printer_provider_internal::ToString( 766 return core_api::printer_provider_internal::ToString(
640 core_api::printer_provider_internal::PRINT_ERROR_FAILED); 767 core_api::printer_provider_internal::PRINT_ERROR_FAILED);
641 } 768 }
642 769
643 } // namespace extensions 770 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698