| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/extensions/api/document_scan/document_scan_interface_ch
romeos.h" | |
| 6 | |
| 7 #include "base/base64.h" | |
| 8 #include "base/task_runner_util.h" | |
| 9 #include "base/threading/worker_pool.h" | |
| 10 #include "chromeos/dbus/dbus_thread_manager.h" | |
| 11 #include "chromeos/dbus/lorgnette_manager_client.h" | |
| 12 #include "third_party/cros_system_api/dbus/service_constants.h" | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 const char kImageScanFailedError[] = "Image scan failed"; | |
| 17 const char kScannerImageMimeTypePng[] = "image/png"; | |
| 18 const char kPngImageDataUrlPrefix[] = "data:image/png;base64,"; | |
| 19 | |
| 20 } // namespace | |
| 21 | |
| 22 namespace extensions { | |
| 23 | |
| 24 namespace api { | |
| 25 | |
| 26 DocumentScanInterfaceChromeos::DocumentScanInterfaceChromeos() | |
| 27 : lorgnette_manager_client_(nullptr) {} | |
| 28 | |
| 29 DocumentScanInterfaceChromeos::~DocumentScanInterfaceChromeos() {} | |
| 30 | |
| 31 void DocumentScanInterfaceChromeos::ListScanners( | |
| 32 const ListScannersResultsCallback& callback) { | |
| 33 GetLorgnetteManagerClient()->ListScanners(base::Bind( | |
| 34 &DocumentScanInterfaceChromeos::OnScannerListReceived, | |
| 35 base::Unretained(this), | |
| 36 callback)); | |
| 37 } | |
| 38 | |
| 39 void DocumentScanInterfaceChromeos::OnScannerListReceived( | |
| 40 const ListScannersResultsCallback& callback, | |
| 41 bool succeeded, | |
| 42 const chromeos::LorgnetteManagerClient::ScannerTable &scanners) { | |
| 43 std::vector<ScannerDescription> scanner_descriptions; | |
| 44 for (chromeos::LorgnetteManagerClient::ScannerTable::const_iterator iter = | |
| 45 scanners.begin(); | |
| 46 iter != scanners.end(); | |
| 47 ++iter) { | |
| 48 ScannerDescription description; | |
| 49 description.name = iter->first; | |
| 50 const chromeos::LorgnetteManagerClient::ScannerTableEntry &entry = | |
| 51 iter->second; | |
| 52 chromeos::LorgnetteManagerClient::ScannerTableEntry::const_iterator info_it; | |
| 53 info_it = entry.find(lorgnette::kScannerPropertyManufacturer); | |
| 54 if (info_it != entry.end()) { | |
| 55 description.manufacturer = info_it->second; | |
| 56 } | |
| 57 info_it = entry.find(lorgnette::kScannerPropertyModel); | |
| 58 if (info_it != entry.end()) { | |
| 59 description.model = info_it->second; | |
| 60 } | |
| 61 info_it = entry.find(lorgnette::kScannerPropertyType); | |
| 62 if (info_it != entry.end()) { | |
| 63 description.scanner_type = info_it->second; | |
| 64 } | |
| 65 description.image_mime_type = kScannerImageMimeTypePng; | |
| 66 scanner_descriptions.push_back(description); | |
| 67 } | |
| 68 callback.Run(scanner_descriptions, ""); | |
| 69 } | |
| 70 | |
| 71 void DocumentScanInterfaceChromeos::Scan(const std::string& scanner_name, | |
| 72 ScanMode mode, | |
| 73 int resolution_dpi, | |
| 74 const ScanResultsCallback& callback) { | |
| 75 VLOG(1) << "Choosing scanner " << scanner_name; | |
| 76 chromeos::LorgnetteManagerClient::ScanProperties properties; | |
| 77 switch (mode) { | |
| 78 case kScanModeColor: | |
| 79 properties.mode = lorgnette::kScanPropertyModeColor; | |
| 80 break; | |
| 81 | |
| 82 case kScanModeGray: | |
| 83 properties.mode = lorgnette::kScanPropertyModeGray; | |
| 84 break; | |
| 85 | |
| 86 case kScanModeLineart: | |
| 87 properties.mode = lorgnette::kScanPropertyModeLineart; | |
| 88 break; | |
| 89 | |
| 90 default: | |
| 91 // Leave the mode parameter empty, thereby using the default. | |
| 92 break; | |
| 93 } | |
| 94 | |
| 95 if (resolution_dpi != 0) { | |
| 96 properties.resolution_dpi = resolution_dpi; | |
| 97 } | |
| 98 | |
| 99 const bool kTasksAreSlow = true; | |
| 100 scoped_refptr<base::TaskRunner> task_runner = | |
| 101 base::WorkerPool::GetTaskRunner(kTasksAreSlow); | |
| 102 | |
| 103 pipe_reader_.reset(new chromeos::PipeReaderForString( | |
| 104 task_runner, | |
| 105 base::Bind(&DocumentScanInterfaceChromeos::OnScanDataCompleted, | |
| 106 base::Unretained(this)))); | |
| 107 base::File file = pipe_reader_->StartIO(); | |
| 108 base::PlatformFile platform_file = file.TakePlatformFile(); | |
| 109 VLOG(1) << "ScanImage platform_file is " << platform_file; | |
| 110 GetLorgnetteManagerClient()->ScanImage( | |
| 111 scanner_name, platform_file, properties, | |
| 112 base::Bind(&DocumentScanInterfaceChromeos::OnScanCompleted, | |
| 113 base::Unretained(this), | |
| 114 callback)); | |
| 115 } | |
| 116 | |
| 117 void DocumentScanInterfaceChromeos::OnScanCompleted( | |
| 118 const ScanResultsCallback &callback, bool succeeded) { | |
| 119 VLOG(1) << "ScanImage returns " << succeeded; | |
| 120 if (pipe_reader_.get()) { | |
| 121 pipe_reader_->OnDataReady(-1); // terminate data stream | |
| 122 } | |
| 123 | |
| 124 std::string error_string; | |
| 125 if (!succeeded) { | |
| 126 error_string = kImageScanFailedError; | |
| 127 } | |
| 128 | |
| 129 callback.Run(GetImageURL(scanned_image_data_), kScannerImageMimeTypePng, | |
| 130 error_string); | |
| 131 } | |
| 132 | |
| 133 std::string DocumentScanInterfaceChromeos::GetImageURL(std::string image_data) { | |
| 134 std::string image_data_base64; | |
| 135 base::Base64Encode(image_data, &image_data_base64); | |
| 136 return std::string(kPngImageDataUrlPrefix) + image_data_base64; | |
| 137 } | |
| 138 | |
| 139 void DocumentScanInterfaceChromeos::OnScanDataCompleted() { | |
| 140 pipe_reader_->GetData(&scanned_image_data_); | |
| 141 pipe_reader_.reset(); | |
| 142 } | |
| 143 | |
| 144 chromeos::LorgnetteManagerClient* | |
| 145 DocumentScanInterfaceChromeos::GetLorgnetteManagerClient() { | |
| 146 if (!lorgnette_manager_client_) { | |
| 147 lorgnette_manager_client_ = | |
| 148 chromeos::DBusThreadManager::Get()->GetLorgnetteManagerClient(); | |
| 149 CHECK(lorgnette_manager_client_); | |
| 150 } | |
| 151 return lorgnette_manager_client_; | |
| 152 } | |
| 153 | |
| 154 // static | |
| 155 DocumentScanInterface *DocumentScanInterface::CreateInstance() { | |
| 156 return new DocumentScanInterfaceChromeos(); | |
| 157 } | |
| 158 | |
| 159 } // namespace api | |
| 160 | |
| 161 } // namespace extensions | |
| OLD | NEW |