1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/api/document_scan/document_scan_api.h"
9 #include "content/public/browser/browser_thread.h"
10 #include "extensions/browser/extension_system.h"
12 using content::BrowserThread;
16 const char kScannerNotAvailable[] = "Scanner not available";
17 const char kUserGestureRequiredError[] =
18 "User gesture required to perform scan";
22 namespace extensions {
26 DocumentScanScanFunction::DocumentScanScanFunction()
27 : document_scan_interface_(DocumentScanInterface::CreateInstance()) {}
29 DocumentScanScanFunction::~DocumentScanScanFunction() {}
31 bool DocumentScanScanFunction::Prepare() {
32 set_work_thread_id(BrowserThread::FILE);
33 params_ = document_scan::Scan::Params::Create(*args_);
34 EXTENSION_FUNCTION_VALIDATE(params_.get());
38 void DocumentScanScanFunction::AsyncWorkStart() {
39 if (!user_gesture()) {
40 error_ = kUserGestureRequiredError;
45 // Add a reference, which is balanced in OnScannerListReceived to keep the
46 // object around and allow the callback to be invoked.
49 document_scan_interface_->ListScanners(
50 base::Bind(&DocumentScanScanFunction::OnScannerListReceived,
51 base::Unretained(this)));
54 void DocumentScanScanFunction::OnScannerListReceived(
55 const std::vector<DocumentScanInterface::ScannerDescription>&
57 const std::string& error) {
58 std::vector<DocumentScanInterface::ScannerDescription>::const_iterator
59 scanner_i = scanner_descriptions.begin();
61 // If no |scanner_descriptions| is empty, this is an error. If no
62 // MIME types are specified, the first scanner is chosen. If MIME
63 // types are specified, the first scanner that supports one of these
64 // MIME types is selected.
65 if (params_->options.mime_types) {
66 std::vector<std::string>& mime_types = *params_->options.mime_types.get();
67 for (; scanner_i != scanner_descriptions.end(); ++scanner_i) {
68 if (std::find(mime_types.begin(), mime_types.end(),
69 scanner_i->image_mime_type) != mime_types.end()) {
75 if (scanner_i == scanner_descriptions.end()) {
76 error_ = kScannerNotAvailable;
79 // Balance the AddRef in AsyncWorkStart().
84 // TODO(pstew): Call a delegate method here to select a scanner and options.
86 document_scan_interface_->Scan(
88 DocumentScanInterface::kScanModeColor,
90 base::Bind(&DocumentScanScanFunction::OnResultsReceived,
91 base::Unretained(this)));
94 void DocumentScanScanFunction::OnResultsReceived(
95 const std::string& scanned_image,
96 const std::string& mime_type,
97 const std::string& error) {
99 // TODO(pstew): Enlist a delegate to display received scan in the UI
100 // and confirm that this scan should be sent to the caller. If this
101 // is a multi-page scan, provide a means for adding additional scanned
102 // images up to the requested limit.
105 document_scan::ScanResults scan_results;
106 if (!scanned_image.empty()) {
107 scan_results.data_urls.push_back(scanned_image);
109 scan_results.mime_type = mime_type;
110 results_ = document_scan::Scan::Results::Create(scan_results);
113 AsyncWorkCompleted();
115 // Balance the AddRef in AsyncWorkStart().
119 bool DocumentScanScanFunction::Respond() {
120 return error_.empty();
125 } // namespace extensions