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