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 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 | 150 |
151 // Keeps track of pending chrome.printerProvider.ontPrintRequested requests | 151 // Keeps track of pending chrome.printerProvider.ontPrintRequested requests |
152 // for an extension. | 152 // for an extension. |
153 class PendingPrintRequests { | 153 class PendingPrintRequests { |
154 public: | 154 public: |
155 PendingPrintRequests(); | 155 PendingPrintRequests(); |
156 ~PendingPrintRequests(); | 156 ~PendingPrintRequests(); |
157 | 157 |
158 // Adds a new request to the set. Only information needed is the callback | 158 // Adds a new request to the set. Only information needed is the callback |
159 // associated with the request. Returns the id assigned to the request. | 159 // associated with the request. Returns the id assigned to the request. |
160 int Add(const PrinterProviderAPI::PrintCallback& callback); | 160 int Add(const PrinterProviderPrintJob& job, |
| 161 const PrinterProviderAPI::PrintCallback& callback); |
| 162 |
| 163 // Gets print job associated with a request. |
| 164 const PrinterProviderPrintJob* GetPrintJob(int request_id) const; |
161 | 165 |
162 // Completes the request with the provided request id. It runs the request | 166 // Completes the request with the provided request id. It runs the request |
163 // callback and removes the request from the set. | 167 // callback and removes the request from the set. |
164 bool Complete(int request_id, bool success, const std::string& result); | 168 bool Complete(int request_id, bool success, const std::string& result); |
165 | 169 |
166 // Runs all pending callbacks with ERROR_FAILED and clears the set of | 170 // Runs all pending callbacks with ERROR_FAILED and clears the set of |
167 // pending requests. | 171 // pending requests. |
168 void FailAll(); | 172 void FailAll(); |
169 | 173 |
170 private: | 174 private: |
| 175 struct PrintRequest { |
| 176 PrinterProviderAPI::PrintCallback callback; |
| 177 PrinterProviderPrintJob job; |
| 178 }; |
| 179 |
171 int last_request_id_; | 180 int last_request_id_; |
172 std::map<int, PrinterProviderAPI::PrintCallback> pending_requests_; | 181 std::map<int, PrintRequest> pending_requests_; |
173 }; | 182 }; |
174 | 183 |
175 // Implements chrome.printerProvider API events. | 184 // Implements chrome.printerProvider API events. |
176 class PrinterProviderAPIImpl : public PrinterProviderAPI, | 185 class PrinterProviderAPIImpl : public PrinterProviderAPI, |
177 public PrinterProviderInternalAPIObserver, | 186 public PrinterProviderInternalAPIObserver, |
178 public ExtensionRegistryObserver { | 187 public ExtensionRegistryObserver { |
179 public: | 188 public: |
180 explicit PrinterProviderAPIImpl(content::BrowserContext* browser_context); | 189 explicit PrinterProviderAPIImpl(content::BrowserContext* browser_context); |
181 ~PrinterProviderAPIImpl() override; | 190 ~PrinterProviderAPIImpl() override; |
182 | 191 |
183 private: | 192 private: |
184 // PrinterProviderAPI implementation: | 193 // PrinterProviderAPI implementation: |
185 void DispatchGetPrintersRequested( | 194 void DispatchGetPrintersRequested( |
186 const PrinterProviderAPI::GetPrintersCallback& callback) override; | 195 const PrinterProviderAPI::GetPrintersCallback& callback) override; |
187 void DispatchGetCapabilityRequested( | 196 void DispatchGetCapabilityRequested( |
188 const std::string& printer_id, | 197 const std::string& printer_id, |
189 const PrinterProviderAPI::GetCapabilityCallback& callback) override; | 198 const PrinterProviderAPI::GetCapabilityCallback& callback) override; |
190 void DispatchPrintRequested( | 199 void DispatchPrintRequested( |
191 const PrinterProviderPrintJob& job, | 200 const PrinterProviderPrintJob& job, |
192 const PrinterProviderAPI::PrintCallback& callback) override; | 201 const PrinterProviderAPI::PrintCallback& callback) override; |
| 202 const PrinterProviderPrintJob* GetPrintJob(const Extension* extension, |
| 203 int request_id) const override; |
193 | 204 |
194 // PrinterProviderInternalAPIObserver implementation: | 205 // PrinterProviderInternalAPIObserver implementation: |
195 void OnGetPrintersResult( | 206 void OnGetPrintersResult( |
196 const Extension* extension, | 207 const Extension* extension, |
197 int request_id, | 208 int request_id, |
198 const PrinterProviderInternalAPIObserver::PrinterInfoVector& result) | 209 const PrinterProviderInternalAPIObserver::PrinterInfoVector& result) |
199 override; | 210 override; |
200 void OnGetCapabilityResult(const Extension* extension, | 211 void OnGetCapabilityResult(const Extension* extension, |
201 int request_id, | 212 int request_id, |
202 const base::DictionaryValue& result) override; | 213 const base::DictionaryValue& result) override; |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 pending_requests_.clear(); | 354 pending_requests_.clear(); |
344 } | 355 } |
345 | 356 |
346 PendingPrintRequests::PendingPrintRequests() : last_request_id_(0) { | 357 PendingPrintRequests::PendingPrintRequests() : last_request_id_(0) { |
347 } | 358 } |
348 | 359 |
349 PendingPrintRequests::~PendingPrintRequests() { | 360 PendingPrintRequests::~PendingPrintRequests() { |
350 } | 361 } |
351 | 362 |
352 int PendingPrintRequests::Add( | 363 int PendingPrintRequests::Add( |
| 364 const PrinterProviderPrintJob& job, |
353 const PrinterProviderAPI::PrintCallback& callback) { | 365 const PrinterProviderAPI::PrintCallback& callback) { |
354 pending_requests_[++last_request_id_] = callback; | 366 PrintRequest request; |
| 367 request.callback = callback; |
| 368 request.job = job; |
| 369 pending_requests_[++last_request_id_] = request; |
355 return last_request_id_; | 370 return last_request_id_; |
356 } | 371 } |
357 | 372 |
358 bool PendingPrintRequests::Complete(int request_id, | 373 bool PendingPrintRequests::Complete(int request_id, |
359 bool success, | 374 bool success, |
360 const std::string& response) { | 375 const std::string& response) { |
361 auto it = pending_requests_.find(request_id); | 376 auto it = pending_requests_.find(request_id); |
362 if (it == pending_requests_.end()) | 377 if (it == pending_requests_.end()) |
363 return false; | 378 return false; |
364 | 379 |
365 PrinterProviderAPI::PrintCallback callback = it->second; | 380 PrinterProviderAPI::PrintCallback callback = it->second.callback; |
366 pending_requests_.erase(it); | 381 pending_requests_.erase(it); |
367 | 382 |
368 callback.Run(success, response); | 383 callback.Run(success, response); |
369 return true; | 384 return true; |
370 } | 385 } |
371 | 386 |
| 387 const PrinterProviderPrintJob* PendingPrintRequests::GetPrintJob( |
| 388 int request_id) const { |
| 389 auto it = pending_requests_.find(request_id); |
| 390 if (it == pending_requests_.end()) |
| 391 return nullptr; |
| 392 |
| 393 return &it->second.job; |
| 394 } |
| 395 |
372 void PendingPrintRequests::FailAll() { | 396 void PendingPrintRequests::FailAll() { |
373 for (auto& request : pending_requests_) | 397 for (auto& request : pending_requests_) |
374 request.second.Run(false, PrinterProviderAPI::GetDefaultPrintError()); | 398 request.second.callback.Run(false, |
| 399 PrinterProviderAPI::GetDefaultPrintError()); |
375 pending_requests_.clear(); | 400 pending_requests_.clear(); |
376 } | 401 } |
377 | 402 |
378 PrinterProviderAPIImpl::PrinterProviderAPIImpl( | 403 PrinterProviderAPIImpl::PrinterProviderAPIImpl( |
379 content::BrowserContext* browser_context) | 404 content::BrowserContext* browser_context) |
380 : browser_context_(browser_context), | 405 : browser_context_(browser_context), |
381 internal_api_observer_(this), | 406 internal_api_observer_(this), |
382 extension_registry_observer_(this) { | 407 extension_registry_observer_(this) { |
383 internal_api_observer_.Add( | 408 internal_api_observer_.Add( |
384 PrinterProviderInternalAPI::GetFactoryInstance()->Get(browser_context)); | 409 PrinterProviderInternalAPI::GetFactoryInstance()->Get(browser_context)); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
477 scoped_ptr<base::Value> ticket_value(deserializer.Deserialize(NULL, NULL)); | 502 scoped_ptr<base::Value> ticket_value(deserializer.Deserialize(NULL, NULL)); |
478 if (!ticket_value || | 503 if (!ticket_value || |
479 !core_api::printer_provider::PrintJob::Ticket::Populate( | 504 !core_api::printer_provider::PrintJob::Ticket::Populate( |
480 *ticket_value, &print_job.ticket)) { | 505 *ticket_value, &print_job.ticket)) { |
481 callback.Run(false, | 506 callback.Run(false, |
482 core_api::printer_provider::ToString( | 507 core_api::printer_provider::ToString( |
483 core_api::printer_provider::PRINT_ERROR_INVALID_TICKET)); | 508 core_api::printer_provider::PRINT_ERROR_INVALID_TICKET)); |
484 return; | 509 return; |
485 } | 510 } |
486 | 511 |
487 // TODO(tbarzic): Figure out how to support huge documents. | |
488 if (job.document_bytes->size() > PrinterProviderAPI::kMaxDocumentSize) { | |
489 callback.Run(false, | |
490 core_api::printer_provider::ToString( | |
491 core_api::printer_provider::PRINT_ERROR_INVALID_DATA)); | |
492 return; | |
493 } | |
494 | |
495 print_job.content_type = job.content_type; | 512 print_job.content_type = job.content_type; |
496 print_job.document = std::vector<char>( | 513 int request_id = pending_print_requests_[extension_id].Add(job, callback); |
497 job.document_bytes->front(), | |
498 job.document_bytes->front() + job.document_bytes->size()); | |
499 | |
500 int request_id = pending_print_requests_[extension_id].Add(callback); | |
501 | 514 |
502 scoped_ptr<base::ListValue> internal_args(new base::ListValue); | 515 scoped_ptr<base::ListValue> internal_args(new base::ListValue); |
503 // Request id is not part of the public API and it will be massaged out in | 516 // Request id is not part of the public API and it will be massaged out in |
504 // custom bindings. | 517 // custom bindings. |
505 internal_args->AppendInteger(request_id); | 518 internal_args->AppendInteger(request_id); |
506 internal_args->Append(print_job.ToValue().release()); | 519 internal_args->Append(print_job.ToValue().release()); |
507 scoped_ptr<Event> event( | 520 scoped_ptr<Event> event( |
508 new Event(core_api::printer_provider::OnPrintRequested::kEventName, | 521 new Event(core_api::printer_provider::OnPrintRequested::kEventName, |
509 internal_args.Pass())); | 522 internal_args.Pass())); |
| 523 event_router->DispatchEventToExtension(extension_id, event.Pass()); |
| 524 } |
510 | 525 |
511 event_router->DispatchEventToExtension(extension_id, event.Pass()); | 526 const PrinterProviderPrintJob* PrinterProviderAPIImpl::GetPrintJob( |
| 527 const Extension* extension, |
| 528 int request_id) const { |
| 529 auto it = pending_print_requests_.find(extension->id()); |
| 530 if (it == pending_print_requests_.end()) |
| 531 return nullptr; |
| 532 return it->second.GetPrintJob(request_id); |
512 } | 533 } |
513 | 534 |
514 void PrinterProviderAPIImpl::OnGetPrintersResult( | 535 void PrinterProviderAPIImpl::OnGetPrintersResult( |
515 const Extension* extension, | 536 const Extension* extension, |
516 int request_id, | 537 int request_id, |
517 const PrinterProviderInternalAPIObserver::PrinterInfoVector& result) { | 538 const PrinterProviderInternalAPIObserver::PrinterInfoVector& result) { |
518 base::ListValue printer_list; | 539 base::ListValue printer_list; |
519 | 540 |
520 // Update some printer description properties to better identify the extension | 541 // Update some printer description properties to better identify the extension |
521 // managing the printer. | 542 // managing the printer. |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
610 return new PrinterProviderAPIImpl(context); | 631 return new PrinterProviderAPIImpl(context); |
611 } | 632 } |
612 | 633 |
613 // static | 634 // static |
614 std::string PrinterProviderAPI::GetDefaultPrintError() { | 635 std::string PrinterProviderAPI::GetDefaultPrintError() { |
615 return core_api::printer_provider_internal::ToString( | 636 return core_api::printer_provider_internal::ToString( |
616 core_api::printer_provider_internal::PRINT_ERROR_FAILED); | 637 core_api::printer_provider_internal::PRINT_ERROR_FAILED); |
617 } | 638 } |
618 | 639 |
619 } // namespace extensions | 640 } // namespace extensions |
OLD | NEW |