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

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: Starting over. This used to be 286933006 Created 6 years, 1 month 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
14 namespace {
15
16 const char kScannerNotAvailable[] = "Scanner not available";
17 const char kUserGestureRequiredError[] =
18 "User gesture required to perform scan";
19 } // namespace
20
21 namespace extensions {
22
23 namespace api {
24
25 DocumentScanScanFunction::DocumentScanScanFunction()
26 : document_scan_interface_(DocumentScanInterface::CreateInstance()) {}
27
28 DocumentScanScanFunction::~DocumentScanScanFunction() {}
29
30 bool DocumentScanScanFunction::Prepare() {
31 set_work_thread_id(BrowserThread::FILE);
32 params_ = document_scan::Scan::Params::Create(*args_);
33 EXTENSION_FUNCTION_VALIDATE(params_.get());
34 return true;
35 }
36
37 void DocumentScanScanFunction::AsyncWorkStart() {
38 if (!user_gesture()) {
39 error_ = kUserGestureRequiredError;
40 AsyncWorkCompleted();
41 return;
42 }
43
44 // Add a reference, which is balanced in OnScannerListReceived to keep the
45 // object around and allow the callback to be invoked.
46 AddRef();
47
48 document_scan_interface_->ListScanners(
49 base::Bind(&DocumentScanScanFunction::OnScannerListReceived,
50 base::Unretained(this)));
51 }
52
53 void DocumentScanScanFunction::OnScannerListReceived(
54 const std::vector<DocumentScanInterface::ScannerDescription>&
55 scanner_descriptions,
56 const std::string& error) {
57 std::vector<DocumentScanInterface::ScannerDescription>::const_iterator
58 scanner_i = scanner_descriptions.begin();
59 if (params_->options.mime_types) {
60 std::vector<std::string>& mime_types = *params_->options.mime_types.get();
61 for (; scanner_i != scanner_descriptions.end(); ++scanner_i) {
62 if (std::find(mime_types.begin(), mime_types.end(),
63 scanner_i->image_mime_type) != mime_types.end()) {
64 break;
65 }
66 }
67 }
68
69 if (scanner_i == scanner_descriptions.end()) {
70 error_ = kScannerNotAvailable;
71 AsyncWorkCompleted();
72
73 // Balance the AddRef in AsyncWorkStart().
74 Release();
75 return;
76 }
77
78 // TODO(pstew): Display a user interface for choosing a scanner.
79 // This is where the scan mode, DPI, etc, would be specified.
80
81 document_scan_interface_->Scan(
82 scanner_i->name,
83 DocumentScanInterface::kScanModeColor,
84 0,
85 base::Bind(&DocumentScanScanFunction::OnResultsReceived,
86 base::Unretained(this)));
87 }
88
89 void DocumentScanScanFunction::OnResultsReceived(
90 const std::string& scanned_image,
91 const std::string& mime_type,
92 const std::string& error) {
93 // TODO(pstew): Display received scan in the UI and confirm that this
94 // scan should be sent to the caller. If this is a multi-page scan,
95 // provide a means for adding additional scanned images up to the
96 // requested limit.
97
98 if (error.empty()) {
99 document_scan::ScanResults scan_results;
100 if (!scanned_image.empty()) {
101 scan_results.data_urls.push_back(scanned_image);
102 }
103 scan_results.mime_type = mime_type;
104 results_ = document_scan::Scan::Results::Create(scan_results);
105 }
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