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

Side by Side 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: Address kalman@'s comments. Still need tests. 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 unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "chrome/browser/extensions/api/document_scan/document_scan_api.h"
6
7 #include <algorithm>
8
9 #include "content/public/browser/browser_thread.h"
10 #include "extensions/browser/extension_system.h"
11
12 using content::BrowserThread;
13 using std::string;
14 using std::vector;
15
16 namespace {
17
18 const char kScannerNotAvailable[] = "Scanner not available";
19 const char kUserGestureRequiredError[] =
20 "User gesture required to perform scan";
21 } // namespace
22
23 namespace extensions {
24
25 namespace api {
26
27 DocumentScanScanFunction::DocumentScanScanFunction()
28 : document_scan_interface_(DocumentScanInterface::CreateInstance()) {}
29
30 DocumentScanScanFunction::~DocumentScanScanFunction() {}
31
32 bool DocumentScanScanFunction::Prepare() {
33 set_work_thread_id(BrowserThread::FILE);
34 params_ = document_scan::Scan::Params::Create(*args_);
35 EXTENSION_FUNCTION_VALIDATE(params_.get());
36 return true;
37 }
38
39 void DocumentScanScanFunction::AsyncWorkStart() {
40 if (!user_gesture()) {
41 error_ = kUserGestureRequiredError;
42 AsyncWorkCompleted();
43 return;
44 }
45
46 // Add a reference, which is balanced in OnScannerListReceived to keep the
47 // object around and allow the callback to be invoked.
48 AddRef();
49
50 document_scan_interface_->ListScanners(
51 base::Bind(&DocumentScanScanFunction::OnScannerListReceived,
52 base::Unretained(this)));
53 }
54
55 void DocumentScanScanFunction::OnScannerListReceived(
56 const vector<DocumentScanInterface::ScannerDescription>&
57 scanner_descriptions,
58 const string& error) {
59 vector<DocumentScanInterface::ScannerDescription>::const_iterator
60 scanner_i = scanner_descriptions.begin();
61 if (params_->options.mime_types) {
62 vector<string>& mime_types = *params_->options.mime_types.get();
63 for (; scanner_i != scanner_descriptions.end(); ++scanner_i) {
64 if (std::find(mime_types.begin(), mime_types.end(),
65 scanner_i->image_mime_type) != mime_types.end()) {
66 break;
67 }
68 }
69 }
70
71 if (scanner_i == scanner_descriptions.end()) {
72 results_ = document_scan::Scan::Results::Create(
73 document_scan::ScanResults());
74 error_ = kScannerNotAvailable;
75 AsyncWorkCompleted();
76
77 // Balance the AddRef in AsyncWorkStart().
78 Release();
79 return;
80 }
81
82 // TODO(pstew): Display a user interface for choosing a scanner.
83 // This is where the scan mode, DPI, etc, would be specified.
84
85 document_scan_interface_->Scan(
86 scanner_i->name,
87 DocumentScanInterface::kScanModeColor,
88 0,
89 base::Bind(&DocumentScanScanFunction::OnResultsReceived,
90 base::Unretained(this)));
91 }
92
93 void DocumentScanScanFunction::OnResultsReceived(
94 const string& scanned_image, const string& error) {
95 // 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"
96 // scan should be sent to the caller. If this is a multi-page scan,
97 // provide a means for adding additional scanned images up to the
98 // requested limit.
99
100 document_scan::ScanResults scan_results;
101 if (!scanned_image.empty()) {
102 scan_results.data_urls.push_back(scanned_image);
103 }
104 scan_results.mime_type = document_scan_interface_->GetImageMimeType();
105 results_ = document_scan::Scan::Results::Create(scan_results);
106 error_ = error;
107 AsyncWorkCompleted();
108
109 // Balance the AddRef in AsyncWorkStart().
110 Release();
111 }
112
113 bool DocumentScanScanFunction::Respond() {
114 return error_.empty();
115 }
116
117 } // namespace api
118
119 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698