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.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 "chromeos/dbus/pipe_reader.h" |
| 13 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 14 |
| 15 using std::string; |
| 16 using std::vector; |
| 17 |
| 18 namespace { |
| 19 |
| 20 const char kImageScanFailedError[] = "Image scan failed"; |
| 21 const char kScannerImageMimeTypePng[] = "image/png"; |
| 22 const char kPngImageDataUrlPrefix[] = "data:image/png;base64,"; |
| 23 |
| 24 } // namespace |
| 25 |
| 26 namespace extensions { |
| 27 |
| 28 namespace api { |
| 29 |
| 30 class DocumentScanInterfaceImpl : public DocumentScanInterface { |
| 31 public: |
| 32 DocumentScanInterfaceImpl() {} |
| 33 virtual ~DocumentScanInterfaceImpl() {} |
| 34 |
| 35 virtual void ListScanners( |
| 36 const ListScannersResultsCallback& callback) OVERRIDE; |
| 37 virtual void Scan(const string& scanner_name, |
| 38 ScanMode mode, |
| 39 int resolution_dpi, |
| 40 const ScanResultsCallback& callback) OVERRIDE; |
| 41 virtual string GetImageMimeType() const OVERRIDE; |
| 42 |
| 43 private: |
| 44 void OnScannerListReceived( |
| 45 const ListScannersResultsCallback& callback, |
| 46 bool succeeded, |
| 47 const chromeos::LorgnetteManagerClient::ScannerTable &scanners); |
| 48 void OnScanCompleted(const ScanResultsCallback &callback, bool succeeded); |
| 49 void OnScanDataCompleted(); |
| 50 string GetImageURL(string image_data); |
| 51 |
| 52 scoped_ptr<chromeos::PipeReaderForString> pipe_reader_; |
| 53 string scanned_image_data_; |
| 54 |
| 55 DISALLOW_COPY_AND_ASSIGN(DocumentScanInterfaceImpl); |
| 56 }; |
| 57 |
| 58 void DocumentScanInterfaceImpl::ListScanners( |
| 59 const ListScannersResultsCallback& callback) { |
| 60 chromeos::DBusThreadManager::Get()->GetLorgnetteManagerClient()-> |
| 61 ListScanners(base::Bind( |
| 62 &DocumentScanInterfaceImpl::OnScannerListReceived, |
| 63 base::Unretained(this), |
| 64 callback)); |
| 65 } |
| 66 |
| 67 void DocumentScanInterfaceImpl::OnScannerListReceived( |
| 68 const ListScannersResultsCallback& callback, |
| 69 bool succeeded, |
| 70 const chromeos::LorgnetteManagerClient::ScannerTable &scanners) { |
| 71 vector<ScannerDescription> scanner_descriptions; |
| 72 for (chromeos::LorgnetteManagerClient::ScannerTable::const_iterator iter = |
| 73 scanners.begin(); |
| 74 iter != scanners.end(); |
| 75 ++iter) { |
| 76 ScannerDescription description; |
| 77 description.name = iter->first; |
| 78 const chromeos::LorgnetteManagerClient::ScannerTableEntry &entry = |
| 79 iter->second; |
| 80 chromeos::LorgnetteManagerClient::ScannerTableEntry::const_iterator info_it; |
| 81 info_it = entry.find(lorgnette::kScannerPropertyManufacturer); |
| 82 if (info_it != entry.end()) { |
| 83 description.manufacturer = info_it->second; |
| 84 } |
| 85 info_it = entry.find(lorgnette::kScannerPropertyModel); |
| 86 if (info_it != entry.end()) { |
| 87 description.model = info_it->second; |
| 88 } |
| 89 info_it = entry.find(lorgnette::kScannerPropertyType); |
| 90 if (info_it != entry.end()) { |
| 91 description.scanner_type = info_it->second; |
| 92 } |
| 93 description.image_mime_type = kScannerImageMimeTypePng; |
| 94 scanner_descriptions.push_back(description); |
| 95 } |
| 96 callback.Run(scanner_descriptions, ""); |
| 97 } |
| 98 |
| 99 void DocumentScanInterfaceImpl::Scan(const string& scanner_name, |
| 100 ScanMode mode, |
| 101 int resolution_dpi, |
| 102 const ScanResultsCallback& callback) { |
| 103 VLOG(1) << "Choosing scanner " << scanner_name; |
| 104 chromeos::LorgnetteManagerClient::ScanProperties properties; |
| 105 switch (mode) { |
| 106 case kScanModeColor: |
| 107 properties.mode = lorgnette::kScanPropertyModeColor; |
| 108 break; |
| 109 |
| 110 case kScanModeGray: |
| 111 properties.mode = lorgnette::kScanPropertyModeGray; |
| 112 break; |
| 113 |
| 114 case kScanModeLineart: |
| 115 properties.mode = lorgnette::kScanPropertyModeLineart; |
| 116 break; |
| 117 |
| 118 default: |
| 119 // Leave the mode parameter empty, thereby using the default. |
| 120 break; |
| 121 } |
| 122 |
| 123 if (resolution_dpi != 0) { |
| 124 properties.resolution_dpi = resolution_dpi; |
| 125 } |
| 126 |
| 127 const bool kTasksAreSlow = true; |
| 128 scoped_refptr<base::TaskRunner> task_runner = |
| 129 base::WorkerPool::GetTaskRunner(kTasksAreSlow); |
| 130 |
| 131 pipe_reader_.reset(new chromeos::PipeReaderForString( |
| 132 task_runner, |
| 133 base::Bind(&DocumentScanInterfaceImpl::OnScanDataCompleted, |
| 134 base::Unretained(this)))); |
| 135 base::File file = pipe_reader_->StartIO(); |
| 136 base::PlatformFile platform_file = file.TakePlatformFile(); |
| 137 VLOG(1) << "ScanImage platform_file is " << platform_file; |
| 138 chromeos::DBusThreadManager::Get()->GetLorgnetteManagerClient()-> |
| 139 ScanImage(scanner_name, platform_file, properties, |
| 140 base::Bind(&DocumentScanInterfaceImpl::OnScanCompleted, |
| 141 base::Unretained(this), |
| 142 callback)); |
| 143 } |
| 144 |
| 145 void DocumentScanInterfaceImpl::OnScanCompleted( |
| 146 const ScanResultsCallback &callback, bool succeeded) { |
| 147 VLOG(1) << "ScanImage returns " << succeeded; |
| 148 if (pipe_reader_.get()) { |
| 149 pipe_reader_->OnDataReady(-1); // terminate data stream |
| 150 } |
| 151 |
| 152 string error_string; |
| 153 if (!succeeded) { |
| 154 error_string = kImageScanFailedError; |
| 155 } |
| 156 |
| 157 callback.Run(GetImageURL(scanned_image_data_), error_string); |
| 158 } |
| 159 |
| 160 string DocumentScanInterfaceImpl::GetImageMimeType() const { |
| 161 return kScannerImageMimeTypePng; |
| 162 } |
| 163 |
| 164 string DocumentScanInterfaceImpl::GetImageURL(string image_data) { |
| 165 string image_data_base64; |
| 166 base::Base64Encode(image_data, &image_data_base64); |
| 167 return string(kPngImageDataUrlPrefix) + image_data_base64; |
| 168 } |
| 169 |
| 170 void DocumentScanInterfaceImpl::OnScanDataCompleted() { |
| 171 pipe_reader_->GetData(&scanned_image_data_); |
| 172 pipe_reader_.reset(); |
| 173 } |
| 174 |
| 175 // static |
| 176 DocumentScanInterface *DocumentScanInterface::CreateInstance() { |
| 177 return new DocumentScanInterfaceImpl(); |
| 178 } |
| 179 |
| 180 } // namespace api |
| 181 |
| 182 } // namespace extensions |
OLD | NEW |