Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1548)

Unified Diff: chrome/browser/extensions/api/document_scan/document_scan_api.cc

Issue 286933006: Implement a JavaScript API for document scanning (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: API fully up to data (and functional) with respect to API submission, minus image selection API whi… Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..fd979b878fb4d79db50fd2521f2341a0593228a0
--- /dev/null
+++ b/chrome/browser/extensions/api/document_scan/document_scan_api.cc
@@ -0,0 +1,297 @@
+// 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"
+
+#include <algorithm>
+
+#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;
+using std::string;
+using std::vector;
+
+namespace {
+
+const char kImageScanFailedError[] = "Image scan failed";
+const char kScannerImageMimeTypePng[] = "image/png";
+const char kScannerNotAvailable[] = "Scanner not available";
+const char kScanFunctionNotImplementedError[] = "Scan function not implemented";
+const char kUserGestureRequiredError[] =
+ "User gesture required to perform scan";
+const char kPngImageDataUrlPrefix[] = "data:image/png;base64,";
+} // namespace
+
+namespace extensions {
+
+namespace api {
+
+#if defined(OS_CHROMEOS)
mef 2014/10/13 15:59:54 Suggest: split into 3 files: document_scan_interf
Paul Stewart 2014/10/14 19:28:31 Done, except for consistency sake with the generat
+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;
+
+ 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;
+ }
+
+ vector<string> scanned_image_data_array;
+ scanned_image_data_array.push_back(GetImageURL(scanned_image_data_));
+ callback.Run(
+ document_scan::Scan::Results::Create(
+ scanned_image_data_array, kScannerImageMimeTypePng), error_string);
+}
+
+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();
+}
+
+#else // OS_CHROMEOS
+class DocumentScanInterfaceImpl : public DocumentScanInterface {
mef 2014/10/13 15:59:54 If this is not implemented and not intended for im
Paul Stewart 2014/10/14 19:28:31 The intent is to make it possible to implement thi
+ public:
+ DocumentScanInterfaceImpl() {}
+ virtual ~DocumentScanInterfaceImpl() {}
+
+ virtual void ListScanners(
+ const ListScannersResultsCallback& callback) OVERRIDE {
+ callback.Run(vector<ScannerDescription> (), "");
+ }
+ virtual void Scan(const string& scanner_name,
+ ScanMode mode,
+ int resolution_dpi,
+ const ScanResultsCallback& callback) OVERRIDE {
+ vector<string> null_scan_results;
+ callback.Run(document_scan::Scan::Results::Create(null_scan_results, ""),
+ 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 vector<DocumentScanInterface::ScannerDescription>&
+ scanner_descriptions,
+ const string& error) {
+ vector<DocumentScanInterface::ScannerDescription>::const_iterator
+ scanner_i = scanner_descriptions.begin();
+ if (params_->options.accepted_mime_types) {
+ vector<string>& accepted_types =
+ *params_->options.accepted_mime_types.get();
+ for (; scanner_i != scanner_descriptions.end(); ++scanner_i) {
+ if (std::find(accepted_types.begin(), accepted_types.end(),
+ scanner_i->image_mime_type) != accepted_types.end()) {
+ break;
+ }
+ }
+ }
+
+ if (scanner_i == scanner_descriptions.end()) {
+ vector<string> null_scan_results;
+ results_ = document_scan::Scan::Results::Create(null_scan_results, "");
+ error_ = kScannerNotAvailable;
+ AsyncWorkCompleted();
+
+ // Balance the AddRef in AsyncWorkStart().
+ Release();
+ return;
+ }
+
+ // TODO(pstew): Display a user interface for choosing a scanner.
+ // This is where the scan mode, DPI, etc, would be specified.
+
+ document_scan_interface_->Scan(
+ scanner_i->name,
+ DocumentScanInterface::kScanModeColor,
+ 0,
+ base::Bind(&DocumentScanScanFunction::OnResultsReceived,
+ base::Unretained(this)));
+}
+
+void DocumentScanScanFunction::OnResultsReceived(
+ scoped_ptr<base::ListValue> results, const 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

Powered by Google App Engine
This is Rietveld 408576698