| 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..113016e8c69ec1886e0be054c645c871b1734c4c
|
| --- /dev/null
|
| +++ b/chrome/browser/extensions/api/document_scan/document_scan_api.cc
|
| @@ -0,0 +1,267 @@
|
| +// 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 kScannerImageMimeTypePng[] = "image/png";
|
| +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 ListScannersResultsCallback& callback) OVERRIDE;
|
| + virtual void Scan(const std::string& scanner_name,
|
| + const document_scan::Scan::Params ¶ms,
|
| + const ScanResultsCallback& callback) OVERRIDE;
|
| +
|
| + private:
|
| + void OnScannerListReceived(
|
| + bool succeeded,
|
| + const chromeos::LorgnetteManagerClient::ScannerTable &scanners);
|
| + void OnScanCompleted(bool succeeded);
|
| + void OnScanDataCompleted();
|
| +
|
| + ListScannersResultsCallback list_scanners_results_callback_;
|
| + ScanResultsCallback scan_results_callback_;
|
| + scoped_ptr<chromeos::PipeReaderForString> pipe_reader_;
|
| + std::string scanned_image_data_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(DocumentScanInterfaceImpl);
|
| +};
|
| +
|
| +void DocumentScanInterfaceImpl::ListScanners(
|
| + const ListScannersResultsCallback& callback) {
|
| + list_scannerrs_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<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(info);
|
| + }
|
| + list_scanners_results_callback_.Run(scanner_descriptions, "");
|
| +}
|
| +
|
| +void DocumentScanInterfaceImpl::Scan(
|
| + const std::string& scanner_name,
|
| + const document_scan::Scan::Params ¶ms,
|
| + const ResultsCallback& callback) {
|
| + scan_results_callback_ = callback;
|
| +
|
| + scoped_ptr<base::ListValue> empty_results(
|
| + document_scan::Scan::Results::Create(""));
|
| +
|
| + VLOG(1) << "Choosing scanner " << scanner_name;
|
| + 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();
|
| + }
|
| +
|
| + 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)));
|
| +}
|
| +
|
| +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;
|
| + }
|
| +
|
| + scan_results_callback_.Run(
|
| + document_scan::Scan::Results::Create(
|
| + scanned_image_data_, kScannerImageMimeTypePng), 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 ListScannersResultsCallback& callback) OVERRIDE {
|
| + callback.Run(std::vector<ScannerDescription> (), "");
|
| + }
|
| + virtual void Scan(const std::string& scanner_name,
|
| + const document_scan::Scan::Params ¶ms,
|
| + const ScanResultsCallback& callback) OVERRIDE {
|
| + callback.Run(document_scan::Scan::Results::Create(""),
|
| + kScanFunctionNotImplementedError);
|
| + }
|
| +
|
| + private:
|
| + DISALLOW_COPY_AND_ASSIGN(DocumentScanInterfaceImpl);
|
| +};
|
| +#endif // OS_CHROMEOS
|
| +
|
| +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_->ListScanners(
|
| + base::Bind(&DocumentScanScanFunction::OnScannerListReceived,
|
| + base::Unretained(this)));
|
| +}
|
| +
|
| +void DocumentScanScanFunction::OnScannerListReceived(
|
| + const std::vector<ScannerDescription>& scanner_descriptions,
|
| + const std::string& error) {
|
| + if (!scanner_descriptions.size()) {
|
| + results_ = document_scan::Scan::Results::Create(""));
|
| + error_ = kScannerNotAvailable;
|
| + AsyncWorkCompleted();
|
| +
|
| + // Balance the AddRef in AsyncWorkStart().
|
| + Release();
|
| + return;
|
| + }
|
| +
|
| + // TODO(pstew): Display a user interface for choosing a scanner.
|
| +
|
| + document_scan_interface_->Scan(
|
| + scanner_descriptions[0].name,
|
| + *params_,
|
| + base::Bind(&DocumentScanScanFunction::OnResultsReceived,
|
| + base::Unretained(this)));
|
| +}
|
| +
|
| +void DocumentScanScanFunction::OnResultsReceived(
|
| + scoped_ptr<base::ListValue> results, const std::string& error) {
|
| + // TODO(pstew): Display receied scan in the UI and confirm that this
|
| + // scan should be sent to the caller. If this is a multi-page scan,
|
| + // provide a means for adding additional scanned images up to the
|
| + // requested limit.
|
| + results_ = results.Pass();
|
| + error_ = error;
|
| + AsyncWorkCompleted();
|
| +
|
| + // Balance the AddRef in AsyncWorkStart().
|
| + Release();
|
| +}
|
| +
|
| +bool DocumentScanScanFunction::Respond() {
|
| + return error_.empty();
|
| +}
|
| +
|
| +} // namespace api
|
| +
|
| +} // namespace extensions
|
|
|