| Index: chrome/browser/extensions/api/document_scan/document_scan_api.cc
|
| diff --git a/chrome/browser/extensions/api/document_scan/document_scan_api.cc b/chrome/browser/extensions/api/document_scan/document_scan_api.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..45859e10894ce3c7546c9a4b35ef2189f4a7e492
|
| --- /dev/null
|
| +++ b/chrome/browser/extensions/api/document_scan/document_scan_api.cc
|
| @@ -0,0 +1,271 @@
|
| +// 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_api.h"
|
| +
|
| +#if defined(OS_CHROMEOS)
|
| +#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"
|
| +#endif // OS_CHROMEOS
|
| +#include "content/public/browser/browser_thread.h"
|
| +#include "extensions/browser/extension_system.h"
|
| +#if defined(OS_CHROMEOS)
|
| +#include "third_party/cros_system_api/dbus/service_constants.h"
|
| +#endif // OS_CHROMEOS
|
| +
|
| +using content::BrowserThread;
|
| +
|
| +namespace {
|
| +
|
| +const char kImageScanFailedError[] = "Image scan failed";
|
| +const char kScanFunctionNotImplementedError[] = "Scan function not implemented";
|
| +const char kUserGestureRequiredError[] =
|
| + "User gesture required to perform scan";
|
| +
|
| +} // namespace
|
| +
|
| +namespace extensions {
|
| +
|
| +namespace api {
|
| +
|
| +#if defined(OS_CHROMEOS)
|
| +class DocumentScanInterfaceImpl : public DocumentScanInterface {
|
| + public:
|
| + DocumentScanInterfaceImpl() {}
|
| + virtual ~DocumentScanInterfaceImpl() {}
|
| +
|
| + virtual void ListScanners(const ResultsCallback& callback) OVERRIDE;
|
| + virtual void Scan(const document_scan::Scan::Params ¶ms,
|
| + const ResultsCallback& callback) OVERRIDE;
|
| +
|
| + private:
|
| + void OnScannerListReceived(
|
| + bool succeeded,
|
| + const chromeos::LorgnetteManagerClient::ScannerTable &scanners);
|
| + void OnScanCompleted(bool succeeded);
|
| + void OnScanDataCompleted();
|
| +
|
| + ResultsCallback results_callback_;
|
| + scoped_ptr<chromeos::PipeReaderForString> pipe_reader_;
|
| + std::string scanned_image_data_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(DocumentScanInterfaceImpl);
|
| +};
|
| +
|
| +void DocumentScanInterfaceImpl::ListScanners(
|
| + const ResultsCallback& callback) {
|
| + results_callback_ = callback;
|
| +
|
| + chromeos::DBusThreadManager::Get()->GetLorgnetteManagerClient()->
|
| + ListScanners(base::Bind(
|
| + &DocumentScanInterfaceImpl::OnScannerListReceived,
|
| + base::Unretained(this)));
|
| +}
|
| +
|
| +void DocumentScanInterfaceImpl::OnScannerListReceived(
|
| + bool succeeded,
|
| + const chromeos::LorgnetteManagerClient::ScannerTable &scanners) {
|
| + std::vector<linked_ptr<document_scan::ScannerInfo> > out_scanners;
|
| + for (chromeos::LorgnetteManagerClient::ScannerTable::const_iterator iter =
|
| + scanners.begin();
|
| + iter != scanners.end();
|
| + ++iter) {
|
| + const std::string &name = iter->first;
|
| + const chromeos::LorgnetteManagerClient::ScannerTableEntry &entry =
|
| + iter->second;
|
| + linked_ptr<document_scan::ScannerInfo> info(new document_scan::ScannerInfo);
|
| + info->name = name;
|
| + chromeos::LorgnetteManagerClient::ScannerTableEntry::const_iterator info_it;
|
| + info_it = entry.find(lorgnette::kScannerPropertyManufacturer);
|
| + if (info_it != entry.end()) {
|
| + info->manufacturer = info_it->second;
|
| + }
|
| + info_it = entry.find(lorgnette::kScannerPropertyModel);
|
| + if (info_it != entry.end()) {
|
| + info->model = info_it->second;
|
| + }
|
| + info_it = entry.find(lorgnette::kScannerPropertyType);
|
| + if (info_it != entry.end()) {
|
| + info->type = info_it->second;
|
| + }
|
| + out_scanners.push_back(info);
|
| + }
|
| + results_callback_.Run(
|
| + document_scan::ListScanners::Results::Create(out_scanners), "");
|
| +}
|
| +
|
| +void DocumentScanInterfaceImpl::Scan(
|
| + const document_scan::Scan::Params ¶ms,
|
| + const ResultsCallback& callback) {
|
| + results_callback_ = callback;
|
| +
|
| + scoped_ptr<base::ListValue> empty_results(
|
| + document_scan::Scan::Results::Create(""));
|
| +
|
| + VLOG(1) << "Choosing scanner " << params.scanner;
|
| + chromeos::LorgnetteManagerClient::ScanProperties properties;
|
| + switch (params.options.mode) {
|
| + case document_scan::SCAN_MODE_COLOR:
|
| + properties.mode = lorgnette::kScanPropertyModeColor;
|
| + break;
|
| +
|
| + case document_scan::SCAN_MODE_GRAY:
|
| + properties.mode = lorgnette::kScanPropertyModeGray;
|
| + break;
|
| +
|
| + case document_scan::SCAN_MODE_LINEART:
|
| + properties.mode = lorgnette::kScanPropertyModeLineart;
|
| + break;
|
| +
|
| + default:
|
| + // Leave the mode parameter empty, thereby using the default.
|
| + break;
|
| + }
|
| +
|
| + if (params.options.resolution_dpi.get()) {
|
| + properties.resolution_dpi = *params.options.resolution_dpi.get();
|
| + }
|
| +
|
| + scoped_refptr<base::TaskRunner> task_runner =
|
| + base::WorkerPool::GetTaskRunner(true /* task_is_slow */);
|
| +
|
| + 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(params.scanner, platform_file, properties,
|
| + base::Bind(&DocumentScanInterfaceImpl::OnScanCompleted,
|
| + base::Unretained(this)));
|
| +}
|
| +
|
| +void DocumentScanInterfaceImpl::OnScanCompleted(bool succeeded) {
|
| + VLOG(1) << "ScanImage returns " << succeeded;
|
| + if (pipe_reader_.get()) {
|
| + pipe_reader_->OnDataReady(-1); // terminate data stream
|
| + }
|
| +
|
| + std::string error_string;
|
| + if (!succeeded) {
|
| + error_string = kImageScanFailedError;
|
| + }
|
| +
|
| + results_callback_.Run(
|
| + document_scan::Scan::Results::Create(scanned_image_data_), error_string);
|
| +}
|
| +
|
| +void DocumentScanInterfaceImpl::OnScanDataCompleted() {
|
| + pipe_reader_->GetData(&scanned_image_data_);
|
| + pipe_reader_.reset();
|
| +}
|
| +
|
| +#else // OS_CHROMEOS
|
| +class DocumentScanInterfaceImpl : public DocumentScanInterface {
|
| + public:
|
| + DocumentScanInterfaceImpl() {}
|
| + virtual ~DocumentScanInterfaceImpl() {}
|
| +
|
| + virtual void ListScanners(const ResultsCallback& callback) OVERRIDE {
|
| + callback.Run(document_scan::ListScanners::Results::Create(
|
| + std::vector<linked_ptr<document_scan::ScannerInfo> >()), "");
|
| + }
|
| + virtual void Scan(const document_scan::Scan::Params ¶ms,
|
| + const ResultsCallback& callback) OVERRIDE {
|
| + callback.Run(document_scan::Scan::Results::Create(""),
|
| + kScanFunctionNotImplementedError);
|
| + }
|
| +
|
| + private:
|
| + DISALLOW_COPY_AND_ASSIGN(DocumentScanInterfaceImpl);
|
| +};
|
| +#endif // OS_CHROMEOS
|
| +
|
| +DocumentScanListScannersFunction::DocumentScanListScannersFunction()
|
| + : document_scan_interface_(new DocumentScanInterfaceImpl()) {}
|
| +
|
| +DocumentScanListScannersFunction::~DocumentScanListScannersFunction() {}
|
| +
|
| +bool DocumentScanListScannersFunction::Prepare() {
|
| + set_work_thread_id(BrowserThread::FILE);
|
| + return true;
|
| +}
|
| +
|
| +void DocumentScanListScannersFunction::AsyncWorkStart() {
|
| + DCHECK_CURRENTLY_ON(BrowserThread::FILE);
|
| +
|
| + // Add a reference, which is balanced in OnScannerListReceived to keep the
|
| + // object around and allow the callback to be invoked.
|
| + AddRef();
|
| +
|
| + document_scan_interface_->ListScanners(
|
| + base::Bind(&DocumentScanListScannersFunction::OnResultsReceived,
|
| + base::Unretained(this)));
|
| +}
|
| +
|
| +void DocumentScanListScannersFunction::OnResultsReceived(
|
| + scoped_ptr<base::ListValue> results, const std::string& error) {
|
| + results_ = results.Pass();
|
| + error_ = error;
|
| + AsyncWorkCompleted();
|
| +
|
| + // Balance the AddRef in AsyncWorkStart().
|
| + Release();
|
| +}
|
| +
|
| +bool DocumentScanListScannersFunction::Respond() {
|
| + return true;
|
| +}
|
| +
|
| +DocumentScanScanFunction::DocumentScanScanFunction()
|
| + : document_scan_interface_(new DocumentScanInterfaceImpl()) {}
|
| +
|
| +DocumentScanScanFunction::~DocumentScanScanFunction() {}
|
| +
|
| +bool DocumentScanScanFunction::Prepare() {
|
| + set_work_thread_id(BrowserThread::FILE);
|
| + params_ = document_scan::Scan::Params::Create(*args_);
|
| + EXTENSION_FUNCTION_VALIDATE(params_.get());
|
| + return true;
|
| +}
|
| +
|
| +void DocumentScanScanFunction::AsyncWorkStart() {
|
| + if (!user_gesture()) {
|
| + error_ = kUserGestureRequiredError;
|
| + AsyncWorkCompleted();
|
| + return;
|
| + }
|
| +
|
| + // Add a reference, which is balanced in OnScannerListReceived to keep the
|
| + // object around and allow the callback to be invoked.
|
| + AddRef();
|
| +
|
| + document_scan_interface_->Scan(
|
| + *params_,
|
| + base::Bind(&DocumentScanScanFunction::OnResultsReceived,
|
| + base::Unretained(this)));
|
| +}
|
| +
|
| +void DocumentScanScanFunction::OnResultsReceived(
|
| + scoped_ptr<base::ListValue> results, const std::string& error) {
|
| + results_ = results.Pass();
|
| + error_ = error;
|
| + AsyncWorkCompleted();
|
| +
|
| + // Balance the AddRef in AsyncWorkStart().
|
| + Release();
|
| +}
|
| +
|
| +bool DocumentScanScanFunction::Respond() {
|
| + return error_.empty();
|
| +}
|
| +
|
| +} // namespace api
|
| +
|
| +} // namespace extensions
|
|
|