| Index: chrome/browser/extensions/api/document_scan/document_scan_interface_chromeos.cc
|
| diff --git a/chrome/browser/extensions/api/document_scan/document_scan_interface_chromeos.cc b/chrome/browser/extensions/api/document_scan/document_scan_interface_chromeos.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..cfa3ed86daa422471346ac4845ea698900eb3c1c
|
| --- /dev/null
|
| +++ b/chrome/browser/extensions/api/document_scan/document_scan_interface_chromeos.cc
|
| @@ -0,0 +1,182 @@
|
| +// Copyright 2014 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "chrome/browser/extensions/api/document_scan/document_scan_interface.h"
|
| +
|
| +#include "base/base64.h"
|
| +#include "base/task_runner_util.h"
|
| +#include "base/threading/worker_pool.h"
|
| +#include "chromeos/dbus/dbus_thread_manager.h"
|
| +#include "chromeos/dbus/lorgnette_manager_client.h"
|
| +#include "chromeos/dbus/pipe_reader.h"
|
| +#include "third_party/cros_system_api/dbus/service_constants.h"
|
| +
|
| +using std::string;
|
| +using std::vector;
|
| +
|
| +namespace {
|
| +
|
| +const char kImageScanFailedError[] = "Image scan failed";
|
| +const char kScannerImageMimeTypePng[] = "image/png";
|
| +const char kPngImageDataUrlPrefix[] = "data:image/png;base64,";
|
| +
|
| +} // namespace
|
| +
|
| +namespace extensions {
|
| +
|
| +namespace api {
|
| +
|
| +class DocumentScanInterfaceImpl : public DocumentScanInterface {
|
| + public:
|
| + DocumentScanInterfaceImpl() {}
|
| + virtual ~DocumentScanInterfaceImpl() {}
|
| +
|
| + virtual void ListScanners(
|
| + const ListScannersResultsCallback& callback) OVERRIDE;
|
| + virtual void Scan(const string& scanner_name,
|
| + ScanMode mode,
|
| + int resolution_dpi,
|
| + const ScanResultsCallback& callback) OVERRIDE;
|
| + virtual string GetImageMimeType() const OVERRIDE;
|
| +
|
| + private:
|
| + void OnScannerListReceived(
|
| + const ListScannersResultsCallback& callback,
|
| + bool succeeded,
|
| + const chromeos::LorgnetteManagerClient::ScannerTable &scanners);
|
| + void OnScanCompleted(const ScanResultsCallback &callback, bool succeeded);
|
| + void OnScanDataCompleted();
|
| + string GetImageURL(string image_data);
|
| +
|
| + scoped_ptr<chromeos::PipeReaderForString> pipe_reader_;
|
| + string scanned_image_data_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(DocumentScanInterfaceImpl);
|
| +};
|
| +
|
| +void DocumentScanInterfaceImpl::ListScanners(
|
| + const ListScannersResultsCallback& callback) {
|
| + chromeos::DBusThreadManager::Get()->GetLorgnetteManagerClient()->
|
| + ListScanners(base::Bind(
|
| + &DocumentScanInterfaceImpl::OnScannerListReceived,
|
| + base::Unretained(this),
|
| + callback));
|
| +}
|
| +
|
| +void DocumentScanInterfaceImpl::OnScannerListReceived(
|
| + const ListScannersResultsCallback& callback,
|
| + bool succeeded,
|
| + const chromeos::LorgnetteManagerClient::ScannerTable &scanners) {
|
| + vector<ScannerDescription> scanner_descriptions;
|
| + for (chromeos::LorgnetteManagerClient::ScannerTable::const_iterator iter =
|
| + scanners.begin();
|
| + iter != scanners.end();
|
| + ++iter) {
|
| + ScannerDescription description;
|
| + description.name = iter->first;
|
| + const chromeos::LorgnetteManagerClient::ScannerTableEntry &entry =
|
| + iter->second;
|
| + chromeos::LorgnetteManagerClient::ScannerTableEntry::const_iterator info_it;
|
| + info_it = entry.find(lorgnette::kScannerPropertyManufacturer);
|
| + if (info_it != entry.end()) {
|
| + description.manufacturer = info_it->second;
|
| + }
|
| + info_it = entry.find(lorgnette::kScannerPropertyModel);
|
| + if (info_it != entry.end()) {
|
| + description.model = info_it->second;
|
| + }
|
| + info_it = entry.find(lorgnette::kScannerPropertyType);
|
| + if (info_it != entry.end()) {
|
| + description.scanner_type = info_it->second;
|
| + }
|
| + description.image_mime_type = kScannerImageMimeTypePng;
|
| + scanner_descriptions.push_back(description);
|
| + }
|
| + callback.Run(scanner_descriptions, "");
|
| +}
|
| +
|
| +void DocumentScanInterfaceImpl::Scan(const string& scanner_name,
|
| + ScanMode mode,
|
| + int resolution_dpi,
|
| + const ScanResultsCallback& callback) {
|
| + VLOG(1) << "Choosing scanner " << scanner_name;
|
| + chromeos::LorgnetteManagerClient::ScanProperties properties;
|
| + switch (mode) {
|
| + case kScanModeColor:
|
| + properties.mode = lorgnette::kScanPropertyModeColor;
|
| + break;
|
| +
|
| + case kScanModeGray:
|
| + properties.mode = lorgnette::kScanPropertyModeGray;
|
| + break;
|
| +
|
| + case kScanModeLineart:
|
| + properties.mode = lorgnette::kScanPropertyModeLineart;
|
| + break;
|
| +
|
| + default:
|
| + // Leave the mode parameter empty, thereby using the default.
|
| + break;
|
| + }
|
| +
|
| + if (resolution_dpi != 0) {
|
| + properties.resolution_dpi = resolution_dpi;
|
| + }
|
| +
|
| + const bool kTasksAreSlow = true;
|
| + scoped_refptr<base::TaskRunner> task_runner =
|
| + base::WorkerPool::GetTaskRunner(kTasksAreSlow);
|
| +
|
| + pipe_reader_.reset(new chromeos::PipeReaderForString(
|
| + task_runner,
|
| + base::Bind(&DocumentScanInterfaceImpl::OnScanDataCompleted,
|
| + base::Unretained(this))));
|
| + base::File file = pipe_reader_->StartIO();
|
| + base::PlatformFile platform_file = file.TakePlatformFile();
|
| + VLOG(1) << "ScanImage platform_file is " << platform_file;
|
| + chromeos::DBusThreadManager::Get()->GetLorgnetteManagerClient()->
|
| + ScanImage(scanner_name, platform_file, properties,
|
| + base::Bind(&DocumentScanInterfaceImpl::OnScanCompleted,
|
| + base::Unretained(this),
|
| + callback));
|
| +}
|
| +
|
| +void DocumentScanInterfaceImpl::OnScanCompleted(
|
| + const ScanResultsCallback &callback, bool succeeded) {
|
| + VLOG(1) << "ScanImage returns " << succeeded;
|
| + if (pipe_reader_.get()) {
|
| + pipe_reader_->OnDataReady(-1); // terminate data stream
|
| + }
|
| +
|
| + string error_string;
|
| + if (!succeeded) {
|
| + error_string = kImageScanFailedError;
|
| + }
|
| +
|
| + callback.Run(GetImageURL(scanned_image_data_), error_string);
|
| +}
|
| +
|
| +string DocumentScanInterfaceImpl::GetImageMimeType() const {
|
| + return kScannerImageMimeTypePng;
|
| +}
|
| +
|
| +string DocumentScanInterfaceImpl::GetImageURL(string image_data) {
|
| + string image_data_base64;
|
| + base::Base64Encode(image_data, &image_data_base64);
|
| + return string(kPngImageDataUrlPrefix) + image_data_base64;
|
| +}
|
| +
|
| +void DocumentScanInterfaceImpl::OnScanDataCompleted() {
|
| + pipe_reader_->GetData(&scanned_image_data_);
|
| + pipe_reader_.reset();
|
| +}
|
| +
|
| +// static
|
| +DocumentScanInterface *DocumentScanInterface::CreateInstance() {
|
| + return new DocumentScanInterfaceImpl();
|
| +}
|
| +
|
| +} // namespace api
|
| +
|
| +} // namespace extensions
|
|
|