Chromium Code Reviews| 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..fdf627b69bdc9f615513d05748f180944f47143a |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/document_scan/document_scan_api.cc |
| @@ -0,0 +1,119 @@ |
| +// 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> |
| + |
| +#include "content/public/browser/browser_thread.h" |
| +#include "extensions/browser/extension_system.h" |
| + |
| +using content::BrowserThread; |
| +using std::string; |
| +using std::vector; |
| + |
| +namespace { |
| + |
| +const char kScannerNotAvailable[] = "Scanner not available"; |
| +const char kUserGestureRequiredError[] = |
| + "User gesture required to perform scan"; |
| +} // namespace |
| + |
| +namespace extensions { |
| + |
| +namespace api { |
| + |
| +DocumentScanScanFunction::DocumentScanScanFunction() |
| + : document_scan_interface_(DocumentScanInterface::CreateInstance()) {} |
| + |
| +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.mime_types) { |
| + vector<string>& mime_types = *params_->options.mime_types.get(); |
| + for (; scanner_i != scanner_descriptions.end(); ++scanner_i) { |
| + if (std::find(mime_types.begin(), mime_types.end(), |
| + scanner_i->image_mime_type) != mime_types.end()) { |
| + break; |
| + } |
| + } |
| + } |
| + |
| + if (scanner_i == scanner_descriptions.end()) { |
| + results_ = document_scan::Scan::Results::Create( |
| + document_scan::ScanResults()); |
| + 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( |
| + const string& scanned_image, const string& error) { |
| + // TODO(pstew): Display receied scan in the UI and confirm that this |
|
asargent_no_longer_on_chrome
2014/10/23 22:15:57
typo: "receied"
|
| + // 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. |
| + |
| + document_scan::ScanResults scan_results; |
| + if (!scanned_image.empty()) { |
| + scan_results.data_urls.push_back(scanned_image); |
| + } |
| + scan_results.mime_type = document_scan_interface_->GetImageMimeType(); |
| + results_ = document_scan::Scan::Results::Create(scan_results); |
| + error_ = error; |
| + AsyncWorkCompleted(); |
| + |
| + // Balance the AddRef in AsyncWorkStart(). |
| + Release(); |
| +} |
| + |
| +bool DocumentScanScanFunction::Respond() { |
| + return error_.empty(); |
| +} |
| + |
| +} // namespace api |
| + |
| +} // namespace extensions |