Chromium Code Reviews| OLD | NEW |
|---|---|
| (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; | |
|
asargent_no_longer_on_chrome
2014/10/29 23:05:21
nit: having using statements for std:: members is
Paul Stewart
2014/10/30 06:31:51
Done.
| |
| 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 error_ = kScannerNotAvailable; | |
| 73 AsyncWorkCompleted(); | |
| 74 | |
| 75 // Balance the AddRef in AsyncWorkStart(). | |
| 76 Release(); | |
| 77 return; | |
| 78 } | |
| 79 | |
| 80 // TODO(pstew): Display a user interface for choosing a scanner. | |
| 81 // This is where the scan mode, DPI, etc, would be specified. | |
| 82 | |
| 83 document_scan_interface_->Scan( | |
| 84 scanner_i->name, | |
| 85 DocumentScanInterface::kScanModeColor, | |
| 86 0, | |
| 87 base::Bind(&DocumentScanScanFunction::OnResultsReceived, | |
| 88 base::Unretained(this))); | |
| 89 } | |
| 90 | |
| 91 void DocumentScanScanFunction::OnResultsReceived( | |
| 92 const string& scanned_image, const string& mime_type, const 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 | |
| OLD | NEW |