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_api.h" |
| 6 |
| 7 #if defined(OS_CHROMEOS) |
| 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 #endif // OS_CHROMEOS |
| 14 #include "content/public/browser/browser_thread.h" |
| 15 #include "extensions/browser/extension_system.h" |
| 16 #if defined(OS_CHROMEOS) |
| 17 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 18 #endif // OS_CHROMEOS |
| 19 |
| 20 using content::BrowserThread; |
| 21 |
| 22 namespace { |
| 23 |
| 24 const char kImageScanFailedError[] = "Image scan failed"; |
| 25 const char kScanFunctionNotImplementedError[] = "Scan function not implemented"; |
| 26 const char kUserGestureRequiredError[] = |
| 27 "User gesture required to perform scan"; |
| 28 |
| 29 } // namespace |
| 30 |
| 31 namespace extensions { |
| 32 |
| 33 namespace api { |
| 34 |
| 35 #if defined(OS_CHROMEOS) |
| 36 class DocumentScanInterfaceImpl : public DocumentScanInterface { |
| 37 public: |
| 38 DocumentScanInterfaceImpl() {} |
| 39 virtual ~DocumentScanInterfaceImpl() {} |
| 40 |
| 41 virtual void ListScanners(const ResultsCallback& callback) OVERRIDE; |
| 42 virtual void Scan(const document_scan::Scan::Params ¶ms, |
| 43 const ResultsCallback& callback) OVERRIDE; |
| 44 |
| 45 private: |
| 46 void OnScannerListReceived( |
| 47 bool succeeded, |
| 48 const chromeos::LorgnetteManagerClient::ScannerTable &scanners); |
| 49 void OnScanCompleted(bool succeeded); |
| 50 void OnScanDataCompleted(); |
| 51 |
| 52 ResultsCallback results_callback_; |
| 53 scoped_ptr<chromeos::PipeReaderForString> pipe_reader_; |
| 54 std::string scanned_image_data_; |
| 55 |
| 56 DISALLOW_COPY_AND_ASSIGN(DocumentScanInterfaceImpl); |
| 57 }; |
| 58 |
| 59 void DocumentScanInterfaceImpl::ListScanners( |
| 60 const ResultsCallback& callback) { |
| 61 results_callback_ = callback; |
| 62 |
| 63 chromeos::DBusThreadManager::Get()->GetLorgnetteManagerClient()-> |
| 64 ListScanners(base::Bind( |
| 65 &DocumentScanInterfaceImpl::OnScannerListReceived, |
| 66 base::Unretained(this))); |
| 67 } |
| 68 |
| 69 void DocumentScanInterfaceImpl::OnScannerListReceived( |
| 70 bool succeeded, |
| 71 const chromeos::LorgnetteManagerClient::ScannerTable &scanners) { |
| 72 std::vector<linked_ptr<document_scan::ScannerInfo> > out_scanners; |
| 73 for (chromeos::LorgnetteManagerClient::ScannerTable::const_iterator iter = |
| 74 scanners.begin(); |
| 75 iter != scanners.end(); |
| 76 ++iter) { |
| 77 const std::string &name = iter->first; |
| 78 const chromeos::LorgnetteManagerClient::ScannerTableEntry &entry = |
| 79 iter->second; |
| 80 linked_ptr<document_scan::ScannerInfo> info(new document_scan::ScannerInfo); |
| 81 info->name = name; |
| 82 chromeos::LorgnetteManagerClient::ScannerTableEntry::const_iterator info_it; |
| 83 info_it = entry.find(lorgnette::kScannerPropertyManufacturer); |
| 84 if (info_it != entry.end()) { |
| 85 info->manufacturer = info_it->second; |
| 86 } |
| 87 info_it = entry.find(lorgnette::kScannerPropertyModel); |
| 88 if (info_it != entry.end()) { |
| 89 info->model = info_it->second; |
| 90 } |
| 91 info_it = entry.find(lorgnette::kScannerPropertyType); |
| 92 if (info_it != entry.end()) { |
| 93 info->type = info_it->second; |
| 94 } |
| 95 out_scanners.push_back(info); |
| 96 } |
| 97 results_callback_.Run( |
| 98 document_scan::ListScanners::Results::Create(out_scanners), ""); |
| 99 } |
| 100 |
| 101 void DocumentScanInterfaceImpl::Scan( |
| 102 const document_scan::Scan::Params ¶ms, |
| 103 const ResultsCallback& callback) { |
| 104 results_callback_ = callback; |
| 105 |
| 106 scoped_ptr<base::ListValue> empty_results( |
| 107 document_scan::Scan::Results::Create("")); |
| 108 |
| 109 VLOG(1) << "Choosing scanner " << params.scanner; |
| 110 chromeos::LorgnetteManagerClient::ScanProperties properties; |
| 111 switch (params.options.mode) { |
| 112 case document_scan::SCAN_MODE_COLOR: |
| 113 properties.mode = lorgnette::kScanPropertyModeColor; |
| 114 break; |
| 115 |
| 116 case document_scan::SCAN_MODE_GRAY: |
| 117 properties.mode = lorgnette::kScanPropertyModeGray; |
| 118 break; |
| 119 |
| 120 case document_scan::SCAN_MODE_LINEART: |
| 121 properties.mode = lorgnette::kScanPropertyModeLineart; |
| 122 break; |
| 123 |
| 124 default: |
| 125 // Leave the mode parameter empty, thereby using the default. |
| 126 break; |
| 127 } |
| 128 |
| 129 if (params.options.resolution_dpi.get()) { |
| 130 properties.resolution_dpi = *params.options.resolution_dpi.get(); |
| 131 } |
| 132 |
| 133 scoped_refptr<base::TaskRunner> task_runner = |
| 134 base::WorkerPool::GetTaskRunner(true /* task_is_slow */); |
| 135 |
| 136 pipe_reader_.reset(new chromeos::PipeReaderForString( |
| 137 task_runner, |
| 138 base::Bind(&DocumentScanInterfaceImpl::OnScanDataCompleted, |
| 139 base::Unretained(this)))); |
| 140 base::File file = pipe_reader_->StartIO(); |
| 141 base::PlatformFile platform_file = file.TakePlatformFile(); |
| 142 VLOG(1) << "ScanImage platform_file is " << platform_file; |
| 143 chromeos::DBusThreadManager::Get()->GetLorgnetteManagerClient()-> |
| 144 ScanImage(params.scanner, platform_file, properties, |
| 145 base::Bind(&DocumentScanInterfaceImpl::OnScanCompleted, |
| 146 base::Unretained(this))); |
| 147 } |
| 148 |
| 149 void DocumentScanInterfaceImpl::OnScanCompleted(bool succeeded) { |
| 150 VLOG(1) << "ScanImage returns " << succeeded; |
| 151 if (pipe_reader_.get()) { |
| 152 pipe_reader_->OnDataReady(-1); // terminate data stream |
| 153 } |
| 154 |
| 155 std::string error_string; |
| 156 if (!succeeded) { |
| 157 error_string = kImageScanFailedError; |
| 158 } |
| 159 |
| 160 results_callback_.Run( |
| 161 document_scan::Scan::Results::Create(scanned_image_data_), error_string); |
| 162 } |
| 163 |
| 164 void DocumentScanInterfaceImpl::OnScanDataCompleted() { |
| 165 pipe_reader_->GetData(&scanned_image_data_); |
| 166 pipe_reader_.reset(); |
| 167 } |
| 168 |
| 169 #else // OS_CHROMEOS |
| 170 class DocumentScanInterfaceImpl : public DocumentScanInterface { |
| 171 public: |
| 172 DocumentScanInterfaceImpl() {} |
| 173 virtual ~DocumentScanInterfaceImpl() {} |
| 174 |
| 175 virtual void ListScanners(const ResultsCallback& callback) OVERRIDE { |
| 176 callback.Run(document_scan::ListScanners::Results::Create( |
| 177 std::vector<linked_ptr<document_scan::ScannerInfo> >()), ""); |
| 178 } |
| 179 virtual void Scan(const document_scan::Scan::Params ¶ms, |
| 180 const ResultsCallback& callback) OVERRIDE { |
| 181 callback.Run(document_scan::Scan::Results::Create(""), |
| 182 kScanFunctionNotImplementedError); |
| 183 } |
| 184 |
| 185 private: |
| 186 DISALLOW_COPY_AND_ASSIGN(DocumentScanInterfaceImpl); |
| 187 }; |
| 188 #endif // OS_CHROMEOS |
| 189 |
| 190 DocumentScanListScannersFunction::DocumentScanListScannersFunction() |
| 191 : document_scan_interface_(new DocumentScanInterfaceImpl()) {} |
| 192 |
| 193 DocumentScanListScannersFunction::~DocumentScanListScannersFunction() {} |
| 194 |
| 195 bool DocumentScanListScannersFunction::Prepare() { |
| 196 set_work_thread_id(BrowserThread::FILE); |
| 197 return true; |
| 198 } |
| 199 |
| 200 void DocumentScanListScannersFunction::AsyncWorkStart() { |
| 201 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 202 |
| 203 // Add a reference, which is balanced in OnScannerListReceived to keep the |
| 204 // object around and allow the callback to be invoked. |
| 205 AddRef(); |
| 206 |
| 207 document_scan_interface_->ListScanners( |
| 208 base::Bind(&DocumentScanListScannersFunction::OnResultsReceived, |
| 209 base::Unretained(this))); |
| 210 } |
| 211 |
| 212 void DocumentScanListScannersFunction::OnResultsReceived( |
| 213 scoped_ptr<base::ListValue> results, const std::string& error) { |
| 214 results_ = results.Pass(); |
| 215 error_ = error; |
| 216 AsyncWorkCompleted(); |
| 217 |
| 218 // Balance the AddRef in AsyncWorkStart(). |
| 219 Release(); |
| 220 } |
| 221 |
| 222 bool DocumentScanListScannersFunction::Respond() { |
| 223 return true; |
| 224 } |
| 225 |
| 226 DocumentScanScanFunction::DocumentScanScanFunction() |
| 227 : document_scan_interface_(new DocumentScanInterfaceImpl()) {} |
| 228 |
| 229 DocumentScanScanFunction::~DocumentScanScanFunction() {} |
| 230 |
| 231 bool DocumentScanScanFunction::Prepare() { |
| 232 set_work_thread_id(BrowserThread::FILE); |
| 233 params_ = document_scan::Scan::Params::Create(*args_); |
| 234 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 235 return true; |
| 236 } |
| 237 |
| 238 void DocumentScanScanFunction::AsyncWorkStart() { |
| 239 if (!user_gesture()) { |
| 240 error_ = kUserGestureRequiredError; |
| 241 AsyncWorkCompleted(); |
| 242 return; |
| 243 } |
| 244 |
| 245 // Add a reference, which is balanced in OnScannerListReceived to keep the |
| 246 // object around and allow the callback to be invoked. |
| 247 AddRef(); |
| 248 |
| 249 document_scan_interface_->Scan( |
| 250 *params_, |
| 251 base::Bind(&DocumentScanScanFunction::OnResultsReceived, |
| 252 base::Unretained(this))); |
| 253 } |
| 254 |
| 255 void DocumentScanScanFunction::OnResultsReceived( |
| 256 scoped_ptr<base::ListValue> results, const std::string& error) { |
| 257 results_ = results.Pass(); |
| 258 error_ = error; |
| 259 AsyncWorkCompleted(); |
| 260 |
| 261 // Balance the AddRef in AsyncWorkStart(). |
| 262 Release(); |
| 263 } |
| 264 |
| 265 bool DocumentScanScanFunction::Respond() { |
| 266 return error_.empty(); |
| 267 } |
| 268 |
| 269 } // namespace api |
| 270 |
| 271 } // namespace extensions |
OLD | NEW |