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

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

Issue 973993003: Instead of ArrayBuffer, pass blob with printerProvider.onPrintRequested (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: kalman feedback Created 5 years, 9 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>
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 82
83 private: 83 private:
84 // Callback reporting event result for an extension. Called once for each 84 // Callback reporting event result for an extension. Called once for each
85 // extension. 85 // extension.
86 PrinterProviderAPI::GetPrintersCallback callback_; 86 PrinterProviderAPI::GetPrintersCallback callback_;
87 87
88 // The list of extensions that still have to respond to the event. 88 // The list of extensions that still have to respond to the event.
89 std::set<std::string> extensions_; 89 std::set<std::string> extensions_;
90 }; 90 };
91 91
92 struct PrintRequest {
Vitaly Buka (NO REVIEWS) 2015/03/05 22:10:01 nested in PendingPrintRequests?
tbarzic 2015/03/05 23:57:36 Done.
93 PrinterProviderAPI::PrintCallback callback;
94 PrinterProviderPrintJob job;
95 };
96
92 // Keeps track of pending chrome.printerProvider.onGetPrintersRequested 97 // Keeps track of pending chrome.printerProvider.onGetPrintersRequested
93 // requests. 98 // requests.
94 class PendingGetPrintersRequests { 99 class PendingGetPrintersRequests {
95 public: 100 public:
96 PendingGetPrintersRequests(); 101 PendingGetPrintersRequests();
97 ~PendingGetPrintersRequests(); 102 ~PendingGetPrintersRequests();
98 103
99 // Adds a new request to the set of pending requests. Returns the id 104 // Adds a new request to the set of pending requests. Returns the id
100 // assigned to the request. 105 // assigned to the request.
101 int Add(const PrinterProviderAPI::GetPrintersCallback& callback); 106 int Add(const PrinterProviderAPI::GetPrintersCallback& callback);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 153
149 // Keeps track of pending chrome.printerProvider.ontPrintRequested requests 154 // Keeps track of pending chrome.printerProvider.ontPrintRequested requests
150 // for an extension. 155 // for an extension.
151 class PendingPrintRequests { 156 class PendingPrintRequests {
152 public: 157 public:
153 PendingPrintRequests(); 158 PendingPrintRequests();
154 ~PendingPrintRequests(); 159 ~PendingPrintRequests();
155 160
156 // Adds a new request to the set. Only information needed is the callback 161 // Adds a new request to the set. Only information needed is the callback
157 // associated with the request. Returns the id assigned to the request. 162 // associated with the request. Returns the id assigned to the request.
158 int Add(const PrinterProviderAPI::PrintCallback& callback); 163 int Add(const PrinterProviderPrintJob& job,
164 const PrinterProviderAPI::PrintCallback& callback);
165
166 // Gets print job associated with a request.
167 const PrinterProviderPrintJob* GetPrintJob(int request_id) const;
159 168
160 // Completes the request with the provided request id. It runs the request 169 // Completes the request with the provided request id. It runs the request
161 // callback and removes the request from the set. 170 // callback and removes the request from the set.
162 bool Complete(int request_id, bool success, const std::string& result); 171 bool Complete(int request_id, bool success, const std::string& result);
163 172
164 // Runs all pending callbacks with ERROR_FAILED and clears the set of 173 // Runs all pending callbacks with ERROR_FAILED and clears the set of
165 // pending requests. 174 // pending requests.
166 void FailAll(); 175 void FailAll();
167 176
168 private: 177 private:
169 int last_request_id_; 178 int last_request_id_;
170 std::map<int, PrinterProviderAPI::PrintCallback> pending_requests_; 179 std::map<int, PrintRequest> pending_requests_;
171 }; 180 };
172 181
173 // Implements chrome.printerProvider API events. 182 // Implements chrome.printerProvider API events.
174 class PrinterProviderAPIImpl : public PrinterProviderAPI, 183 class PrinterProviderAPIImpl : public PrinterProviderAPI,
175 public PrinterProviderInternalAPIObserver, 184 public PrinterProviderInternalAPIObserver,
176 public ExtensionRegistryObserver { 185 public ExtensionRegistryObserver {
177 public: 186 public:
178 explicit PrinterProviderAPIImpl(content::BrowserContext* browser_context); 187 explicit PrinterProviderAPIImpl(content::BrowserContext* browser_context);
179 ~PrinterProviderAPIImpl() override; 188 ~PrinterProviderAPIImpl() override;
180 189
181 private: 190 private:
182 // PrinterProviderAPI implementation: 191 // PrinterProviderAPI implementation:
183 void DispatchGetPrintersRequested( 192 void DispatchGetPrintersRequested(
184 const PrinterProviderAPI::GetPrintersCallback& callback) override; 193 const PrinterProviderAPI::GetPrintersCallback& callback) override;
185 void DispatchGetCapabilityRequested( 194 void DispatchGetCapabilityRequested(
186 const std::string& printer_id, 195 const std::string& printer_id,
187 const PrinterProviderAPI::GetCapabilityCallback& callback) override; 196 const PrinterProviderAPI::GetCapabilityCallback& callback) override;
188 void DispatchPrintRequested( 197 void DispatchPrintRequested(
189 const PrinterProviderPrintJob& job, 198 const PrinterProviderPrintJob& job,
190 const PrinterProviderAPI::PrintCallback& callback) override; 199 const PrinterProviderAPI::PrintCallback& callback) override;
200 const PrinterProviderPrintJob* GetPrintJob(const Extension* extension,
201 int request_id) const override;
191 202
192 // PrinterProviderInternalAPIObserver implementation: 203 // PrinterProviderInternalAPIObserver implementation:
193 void OnGetPrintersResult( 204 void OnGetPrintersResult(
194 const Extension* extension, 205 const Extension* extension,
195 int request_id, 206 int request_id,
196 const PrinterProviderInternalAPIObserver::PrinterInfoVector& result) 207 const PrinterProviderInternalAPIObserver::PrinterInfoVector& result)
197 override; 208 override;
198 void OnGetCapabilityResult(const Extension* extension, 209 void OnGetCapabilityResult(const Extension* extension,
199 int request_id, 210 int request_id,
200 const base::DictionaryValue& result) override; 211 const base::DictionaryValue& result) override;
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 pending_requests_.clear(); 352 pending_requests_.clear();
342 } 353 }
343 354
344 PendingPrintRequests::PendingPrintRequests() : last_request_id_(0) { 355 PendingPrintRequests::PendingPrintRequests() : last_request_id_(0) {
345 } 356 }
346 357
347 PendingPrintRequests::~PendingPrintRequests() { 358 PendingPrintRequests::~PendingPrintRequests() {
348 } 359 }
349 360
350 int PendingPrintRequests::Add( 361 int PendingPrintRequests::Add(
362 const PrinterProviderPrintJob& job,
351 const PrinterProviderAPI::PrintCallback& callback) { 363 const PrinterProviderAPI::PrintCallback& callback) {
352 pending_requests_[++last_request_id_] = callback; 364 PrintRequest request;
365 request.callback = callback;
366 request.job = job;
367 pending_requests_[++last_request_id_] = request;
353 return last_request_id_; 368 return last_request_id_;
354 } 369 }
355 370
356 bool PendingPrintRequests::Complete(int request_id, 371 bool PendingPrintRequests::Complete(int request_id,
357 bool success, 372 bool success,
358 const std::string& response) { 373 const std::string& response) {
359 auto it = pending_requests_.find(request_id); 374 auto it = pending_requests_.find(request_id);
360 if (it == pending_requests_.end()) 375 if (it == pending_requests_.end())
361 return false; 376 return false;
362 377
363 PrinterProviderAPI::PrintCallback callback = it->second; 378 PrinterProviderAPI::PrintCallback callback = it->second.callback;
364 pending_requests_.erase(it); 379 pending_requests_.erase(it);
365 380
366 callback.Run(success, response); 381 callback.Run(success, response);
367 return true; 382 return true;
368 } 383 }
369 384
385 const PrinterProviderPrintJob* PendingPrintRequests::GetPrintJob(
386 int request_id) const {
387 auto it = pending_requests_.find(request_id);
388 if (it == pending_requests_.end())
389 return nullptr;
390
391 return &it->second.job;
392 }
393
370 void PendingPrintRequests::FailAll() { 394 void PendingPrintRequests::FailAll() {
371 for (auto& request : pending_requests_) 395 for (auto& request : pending_requests_)
372 request.second.Run(false, PrinterProviderAPI::GetDefaultPrintError()); 396 request.second.callback.Run(false,
397 PrinterProviderAPI::GetDefaultPrintError());
373 pending_requests_.clear(); 398 pending_requests_.clear();
374 } 399 }
375 400
376 PrinterProviderAPIImpl::PrinterProviderAPIImpl( 401 PrinterProviderAPIImpl::PrinterProviderAPIImpl(
377 content::BrowserContext* browser_context) 402 content::BrowserContext* browser_context)
378 : browser_context_(browser_context), 403 : browser_context_(browser_context),
379 internal_api_observer_(this), 404 internal_api_observer_(this),
380 extension_registry_observer_(this) { 405 extension_registry_observer_(this) {
381 internal_api_observer_.Add( 406 internal_api_observer_.Add(
382 PrinterProviderInternalAPI::GetFactoryInstance()->Get(browser_context)); 407 PrinterProviderInternalAPI::GetFactoryInstance()->Get(browser_context));
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
475 scoped_ptr<base::Value> ticket_value(serializer.Deserialize(NULL, NULL)); 500 scoped_ptr<base::Value> ticket_value(serializer.Deserialize(NULL, NULL));
476 if (!ticket_value || 501 if (!ticket_value ||
477 !core_api::printer_provider::PrintJob::Ticket::Populate( 502 !core_api::printer_provider::PrintJob::Ticket::Populate(
478 *ticket_value, &print_job.ticket)) { 503 *ticket_value, &print_job.ticket)) {
479 callback.Run(false, 504 callback.Run(false,
480 core_api::printer_provider::ToString( 505 core_api::printer_provider::ToString(
481 core_api::printer_provider::PRINT_ERROR_INVALID_TICKET)); 506 core_api::printer_provider::PRINT_ERROR_INVALID_TICKET));
482 return; 507 return;
483 } 508 }
484 509
485 // TODO(tbarzic): Figure out how to support huge documents.
486 if (job.document_bytes->size() > PrinterProviderAPI::kMaxDocumentSize) {
487 callback.Run(false,
488 core_api::printer_provider::ToString(
489 core_api::printer_provider::PRINT_ERROR_INVALID_DATA));
490 return;
491 }
492
493 print_job.content_type = job.content_type; 510 print_job.content_type = job.content_type;
494 print_job.document = std::vector<char>( 511 int request_id = pending_print_requests_[extension_id].Add(job, callback);
495 job.document_bytes->front(),
496 job.document_bytes->front() + job.document_bytes->size());
497
498 int request_id = pending_print_requests_[extension_id].Add(callback);
499 512
500 scoped_ptr<base::ListValue> internal_args(new base::ListValue); 513 scoped_ptr<base::ListValue> internal_args(new base::ListValue);
501 // Request id is not part of the public API and it will be massaged out in 514 // Request id is not part of the public API and it will be massaged out in
502 // custom bindings. 515 // custom bindings.
503 internal_args->AppendInteger(request_id); 516 internal_args->AppendInteger(request_id);
504 internal_args->Append(print_job.ToValue().release()); 517 internal_args->Append(print_job.ToValue().release());
505 scoped_ptr<Event> event( 518 scoped_ptr<Event> event(
506 new Event(core_api::printer_provider::OnPrintRequested::kEventName, 519 new Event(core_api::printer_provider::OnPrintRequested::kEventName,
507 internal_args.Pass())); 520 internal_args.Pass()));
521 event_router->DispatchEventToExtension(extension_id, event.Pass());
522 }
508 523
509 event_router->DispatchEventToExtension(extension_id, event.Pass()); 524 const PrinterProviderPrintJob* PrinterProviderAPIImpl::GetPrintJob(
525 const Extension* extension,
526 int request_id) const {
527 auto it = pending_print_requests_.find(extension->id());
528 if (it == pending_print_requests_.end())
529 return nullptr;
530 return it->second.GetPrintJob(request_id);
510 } 531 }
511 532
512 void PrinterProviderAPIImpl::OnGetPrintersResult( 533 void PrinterProviderAPIImpl::OnGetPrintersResult(
513 const Extension* extension, 534 const Extension* extension,
514 int request_id, 535 int request_id,
515 const PrinterProviderInternalAPIObserver::PrinterInfoVector& result) { 536 const PrinterProviderInternalAPIObserver::PrinterInfoVector& result) {
516 base::ListValue printer_list; 537 base::ListValue printer_list;
517 538
518 // Update some printer description properties to better identify the extension 539 // Update some printer description properties to better identify the extension
519 // managing the printer. 540 // managing the printer.
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
595 return new PrinterProviderAPIImpl(context); 616 return new PrinterProviderAPIImpl(context);
596 } 617 }
597 618
598 // static 619 // static
599 std::string PrinterProviderAPI::GetDefaultPrintError() { 620 std::string PrinterProviderAPI::GetDefaultPrintError() {
600 return core_api::printer_provider_internal::ToString( 621 return core_api::printer_provider_internal::ToString(
601 core_api::printer_provider_internal::PRINT_ERROR_FAILED); 622 core_api::printer_provider_internal::PRINT_ERROR_FAILED);
602 } 623 }
603 624
604 } // namespace extensions 625 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698