Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / document_scan / document_scan_api.cc
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
20 }  // namespace
21
22 namespace extensions {
23
24 namespace api {
25
26 DocumentScanScanFunction::DocumentScanScanFunction()
27     : document_scan_interface_(DocumentScanInterface::CreateInstance()) {}
28
29 DocumentScanScanFunction::~DocumentScanScanFunction() {}
30
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());
35   return true;
36 }
37
38 void DocumentScanScanFunction::AsyncWorkStart() {
39   if (!user_gesture()) {
40     error_ = kUserGestureRequiredError;
41     AsyncWorkCompleted();
42     return;
43   }
44
45   // Add a reference, which is balanced in OnScannerListReceived to keep the
46   // object around and allow the callback to be invoked.
47   AddRef();
48
49   document_scan_interface_->ListScanners(
50       base::Bind(&DocumentScanScanFunction::OnScannerListReceived,
51                  base::Unretained(this)));
52 }
53
54 void DocumentScanScanFunction::OnScannerListReceived(
55     const std::vector<DocumentScanInterface::ScannerDescription>&
56         scanner_descriptions,
57     const std::string& error) {
58   std::vector<DocumentScanInterface::ScannerDescription>::const_iterator
59       scanner_i = scanner_descriptions.begin();
60
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()) {
70         break;
71       }
72     }
73   }
74
75   if (scanner_i == scanner_descriptions.end()) {
76     error_ = kScannerNotAvailable;
77     AsyncWorkCompleted();
78
79     // Balance the AddRef in AsyncWorkStart().
80     Release();
81     return;
82   }
83
84   // TODO(pstew): Call a delegate method here to select a scanner and options.
85
86   document_scan_interface_->Scan(
87       scanner_i->name,
88       DocumentScanInterface::kScanModeColor,
89       0,
90       base::Bind(&DocumentScanScanFunction::OnResultsReceived,
91                  base::Unretained(this)));
92 }
93
94 void DocumentScanScanFunction::OnResultsReceived(
95     const std::string& scanned_image,
96     const std::string& mime_type,
97     const std::string& error) {
98
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.
103
104   if (error.empty()) {
105     document_scan::ScanResults scan_results;
106     if (!scanned_image.empty()) {
107       scan_results.data_urls.push_back(scanned_image);
108     }
109     scan_results.mime_type = mime_type;
110     results_ = document_scan::Scan::Results::Create(scan_results);
111   }
112   error_ = error;
113   AsyncWorkCompleted();
114
115   // Balance the AddRef in AsyncWorkStart().
116   Release();
117 }
118
119 bool DocumentScanScanFunction::Respond() {
120   return error_.empty();
121 }
122
123 }  // namespace api
124
125 }  // namespace extensions