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 "chrome/browser/ui/webui/print_preview/extension_printer_handler.h" | 5 #include "chrome/browser/ui/webui/print_preview/extension_printer_handler.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/files/file.h" | 12 #include "base/files/file.h" |
13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
14 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
15 #include "base/location.h" | 15 #include "base/location.h" |
16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
17 #include "base/memory/ref_counted_memory.h" | 17 #include "base/memory/ref_counted_memory.h" |
18 #include "base/strings/string_split.h" | 18 #include "base/strings/string_split.h" |
19 #include "base/task_runner_util.h" | 19 #include "base/task_scheduler/post_task.h" |
20 #include "chrome/browser/printing/pwg_raster_converter.h" | 20 #include "chrome/browser/printing/pwg_raster_converter.h" |
21 #include "components/cloud_devices/common/cloud_device_description.h" | 21 #include "components/cloud_devices/common/cloud_device_description.h" |
22 #include "components/cloud_devices/common/printer_description.h" | 22 #include "components/cloud_devices/common/printer_description.h" |
23 #include "device/base/device_client.h" | 23 #include "device/base/device_client.h" |
24 #include "device/usb/usb_device.h" | 24 #include "device/usb/usb_device.h" |
25 #include "device/usb/usb_service.h" | 25 #include "device/usb/usb_service.h" |
26 #include "extensions/browser/api/device_permissions_manager.h" | 26 #include "extensions/browser/api/device_permissions_manager.h" |
27 #include "extensions/browser/api/printer_provider/printer_provider_api.h" | 27 #include "extensions/browser/api/printer_provider/printer_provider_api.h" |
28 #include "extensions/browser/api/printer_provider/printer_provider_api_factory.h
" | 28 #include "extensions/browser/api/printer_provider/printer_provider_api_factory.h
" |
29 #include "extensions/browser/api/printer_provider/printer_provider_print_job.h" | 29 #include "extensions/browser/api/printer_provider/printer_provider_print_job.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 UpdateJobFileInfoOnWorkerThread( | 62 UpdateJobFileInfoOnWorkerThread( |
63 const base::FilePath& raster_path, | 63 const base::FilePath& raster_path, |
64 std::unique_ptr<extensions::PrinterProviderPrintJob> job) { | 64 std::unique_ptr<extensions::PrinterProviderPrintJob> job) { |
65 if (base::GetFileInfo(raster_path, &job->file_info)) | 65 if (base::GetFileInfo(raster_path, &job->file_info)) |
66 job->document_path = raster_path; | 66 job->document_path = raster_path; |
67 return job; | 67 return job; |
68 } | 68 } |
69 | 69 |
70 // Callback to PWG raster conversion. | 70 // Callback to PWG raster conversion. |
71 // Posts a task to update print job with info about file containing converted | 71 // Posts a task to update print job with info about file containing converted |
72 // PWG raster data. The task is posted to |slow_task_runner|. | 72 // PWG raster data. |
73 void UpdateJobFileInfo( | 73 void UpdateJobFileInfo( |
74 std::unique_ptr<extensions::PrinterProviderPrintJob> job, | 74 std::unique_ptr<extensions::PrinterProviderPrintJob> job, |
75 const scoped_refptr<base::TaskRunner>& slow_task_runner, | |
76 const ExtensionPrinterHandler::PrintJobCallback& callback, | 75 const ExtensionPrinterHandler::PrintJobCallback& callback, |
77 bool success, | 76 bool success, |
78 const base::FilePath& pwg_file_path) { | 77 const base::FilePath& pwg_file_path) { |
79 if (!success) { | 78 if (!success) { |
80 callback.Run(std::move(job)); | 79 callback.Run(std::move(job)); |
81 return; | 80 return; |
82 } | 81 } |
83 | 82 |
84 base::PostTaskAndReplyWithResult( | 83 base::PostTaskWithTraitsAndReplyWithResult( |
85 slow_task_runner.get(), FROM_HERE, | 84 FROM_HERE, base::TaskTraits() |
| 85 .WithShutdownBehavior( |
| 86 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) |
| 87 .MayBlock(), |
86 base::Bind(&UpdateJobFileInfoOnWorkerThread, pwg_file_path, | 88 base::Bind(&UpdateJobFileInfoOnWorkerThread, pwg_file_path, |
87 base::Passed(&job)), | 89 base::Passed(&job)), |
88 callback); | 90 callback); |
89 } | 91 } |
90 | 92 |
91 bool HasUsbPrinterProviderPermissions(const Extension* extension) { | 93 bool HasUsbPrinterProviderPermissions(const Extension* extension) { |
92 return extension->permissions_data() && | 94 return extension->permissions_data() && |
93 extension->permissions_data()->HasAPIPermission( | 95 extension->permissions_data()->HasAPIPermission( |
94 extensions::APIPermission::kPrinterProvider) && | 96 extensions::APIPermission::kPrinterProvider) && |
95 extension->permissions_data()->HasAPIPermission( | 97 extension->permissions_data()->HasAPIPermission( |
(...skipping 19 matching lines...) Expand all Loading... |
115 return false; | 117 return false; |
116 | 118 |
117 *extension_id = components[1]; | 119 *extension_id = components[1]; |
118 *device_guid = components[2]; | 120 *device_guid = components[2]; |
119 return true; | 121 return true; |
120 } | 122 } |
121 | 123 |
122 } // namespace | 124 } // namespace |
123 | 125 |
124 ExtensionPrinterHandler::ExtensionPrinterHandler( | 126 ExtensionPrinterHandler::ExtensionPrinterHandler( |
125 content::BrowserContext* browser_context, | 127 content::BrowserContext* browser_context) |
126 const scoped_refptr<base::TaskRunner>& slow_task_runner) | 128 : browser_context_(browser_context), weak_ptr_factory_(this) {} |
127 : browser_context_(browser_context), | |
128 slow_task_runner_(slow_task_runner), | |
129 weak_ptr_factory_(this) { | |
130 } | |
131 | 129 |
132 ExtensionPrinterHandler::~ExtensionPrinterHandler() { | 130 ExtensionPrinterHandler::~ExtensionPrinterHandler() { |
133 } | 131 } |
134 | 132 |
135 void ExtensionPrinterHandler::Reset() { | 133 void ExtensionPrinterHandler::Reset() { |
136 // TODO(tbarzic): Keep track of pending request ids issued by |this| and | 134 // TODO(tbarzic): Keep track of pending request ids issued by |this| and |
137 // cancel them from here. | 135 // cancel them from here. |
138 pending_enumeration_count_ = 0; | 136 pending_enumeration_count_ = 0; |
139 weak_ptr_factory_.InvalidateWeakPtrs(); | 137 weak_ptr_factory_.InvalidateWeakPtrs(); |
140 } | 138 } |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 const gfx::Size& page_size, | 265 const gfx::Size& page_size, |
268 std::unique_ptr<extensions::PrinterProviderPrintJob> job, | 266 std::unique_ptr<extensions::PrinterProviderPrintJob> job, |
269 const ExtensionPrinterHandler::PrintJobCallback& callback) { | 267 const ExtensionPrinterHandler::PrintJobCallback& callback) { |
270 if (!pwg_raster_converter_) { | 268 if (!pwg_raster_converter_) { |
271 pwg_raster_converter_ = PWGRasterConverter::CreateDefault(); | 269 pwg_raster_converter_ = PWGRasterConverter::CreateDefault(); |
272 } | 270 } |
273 pwg_raster_converter_->Start( | 271 pwg_raster_converter_->Start( |
274 data.get(), | 272 data.get(), |
275 PWGRasterConverter::GetConversionSettings(printer_description, page_size), | 273 PWGRasterConverter::GetConversionSettings(printer_description, page_size), |
276 PWGRasterConverter::GetBitmapSettings(printer_description, ticket), | 274 PWGRasterConverter::GetBitmapSettings(printer_description, ticket), |
277 base::Bind(&UpdateJobFileInfo, base::Passed(&job), slow_task_runner_, | 275 base::Bind(&UpdateJobFileInfo, base::Passed(&job), callback)); |
278 callback)); | |
279 } | 276 } |
280 | 277 |
281 void ExtensionPrinterHandler::DispatchPrintJob( | 278 void ExtensionPrinterHandler::DispatchPrintJob( |
282 const PrinterHandler::PrintCallback& callback, | 279 const PrinterHandler::PrintCallback& callback, |
283 std::unique_ptr<extensions::PrinterProviderPrintJob> print_job) { | 280 std::unique_ptr<extensions::PrinterProviderPrintJob> print_job) { |
284 if (print_job->document_path.empty() && !print_job->document_bytes) { | 281 if (print_job->document_path.empty() && !print_job->document_bytes) { |
285 WrapPrintCallback(callback, false, kInvalidDataPrintError); | 282 WrapPrintCallback(callback, false, kInvalidDataPrintError); |
286 return; | 283 return; |
287 } | 284 } |
288 | 285 |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
367 .Set("provisional", true) | 364 .Set("provisional", true) |
368 .Build()); | 365 .Build()); |
369 } | 366 } |
370 } | 367 } |
371 } | 368 } |
372 | 369 |
373 DCHECK_GT(pending_enumeration_count_, 0); | 370 DCHECK_GT(pending_enumeration_count_, 0); |
374 pending_enumeration_count_--; | 371 pending_enumeration_count_--; |
375 callback.Run(*printer_list.Build().get(), pending_enumeration_count_ == 0); | 372 callback.Run(*printer_list.Build().get(), pending_enumeration_count_ == 0); |
376 } | 373 } |
OLD | NEW |