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

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

Powered by Google App Engine
This is Rietveld 408576698