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 "extensions/browser/api/document_scan/document_scan_interface_chromeos.h"
7 #include "base/base64.h"
9 #include "chromeos/dbus/dbus_thread_manager.h"
10 #include "chromeos/dbus/lorgnette_manager_client.h"
11 #include "third_party/cros_system_api/dbus/service_constants.h"
15 const char kImageScanFailedError
[] = "Image scan failed";
16 const char kScannerImageMimeTypePng
[] = "image/png";
17 const char kPngImageDataUrlPrefix
[] = "data:image/png;base64,";
21 namespace extensions
{
25 DocumentScanInterfaceChromeos::DocumentScanInterfaceChromeos()
26 : lorgnette_manager_client_(nullptr) {
29 DocumentScanInterfaceChromeos::~DocumentScanInterfaceChromeos() {
32 void DocumentScanInterfaceChromeos::ListScanners(
33 const ListScannersResultsCallback
& callback
) {
34 GetLorgnetteManagerClient()->ListScanners(
35 base::Bind(&DocumentScanInterfaceChromeos::OnScannerListReceived
,
36 base::Unretained(this), callback
));
39 void DocumentScanInterfaceChromeos::OnScannerListReceived(
40 const ListScannersResultsCallback
& callback
,
42 const chromeos::LorgnetteManagerClient::ScannerTable
& scanners
) {
43 std::vector
<ScannerDescription
> scanner_descriptions
;
44 for (const auto& scanner
: scanners
) {
45 ScannerDescription description
;
46 description
.name
= scanner
.first
;
47 const auto& entry
= scanner
.second
;
48 auto info_it
= entry
.find(lorgnette::kScannerPropertyManufacturer
);
49 if (info_it
!= entry
.end()) {
50 description
.manufacturer
= info_it
->second
;
52 info_it
= entry
.find(lorgnette::kScannerPropertyModel
);
53 if (info_it
!= entry
.end()) {
54 description
.model
= info_it
->second
;
56 info_it
= entry
.find(lorgnette::kScannerPropertyType
);
57 if (info_it
!= entry
.end()) {
58 description
.scanner_type
= info_it
->second
;
60 description
.image_mime_type
= kScannerImageMimeTypePng
;
61 scanner_descriptions
.push_back(description
);
63 const std::string kNoError
;
64 callback
.Run(scanner_descriptions
, kNoError
);
67 void DocumentScanInterfaceChromeos::Scan(const std::string
& scanner_name
,
70 const ScanResultsCallback
& callback
) {
71 VLOG(1) << "Choosing scanner " << scanner_name
;
72 chromeos::LorgnetteManagerClient::ScanProperties properties
;
75 properties
.mode
= lorgnette::kScanPropertyModeColor
;
79 properties
.mode
= lorgnette::kScanPropertyModeGray
;
82 case kScanModeLineart
:
83 properties
.mode
= lorgnette::kScanPropertyModeLineart
;
87 if (resolution_dpi
!= 0) {
88 properties
.resolution_dpi
= resolution_dpi
;
91 GetLorgnetteManagerClient()->ScanImageToString(
92 scanner_name
, properties
,
93 base::Bind(&DocumentScanInterfaceChromeos::OnScanCompleted
,
94 base::Unretained(this), callback
));
97 void DocumentScanInterfaceChromeos::OnScanCompleted(
98 const ScanResultsCallback
& callback
,
100 const std::string
& image_data
) {
101 VLOG(1) << "ScanImage returns " << succeeded
;
102 std::string error_string
;
104 error_string
= kImageScanFailedError
;
107 std::string image_base64
;
108 base::Base64Encode(image_data
, &image_base64
);
109 std::string
image_url(std::string(kPngImageDataUrlPrefix
) + image_base64
);
111 callback
.Run(image_url
, kScannerImageMimeTypePng
, error_string
);
114 chromeos::LorgnetteManagerClient
*
115 DocumentScanInterfaceChromeos::GetLorgnetteManagerClient() {
116 if (!lorgnette_manager_client_
) {
117 lorgnette_manager_client_
=
118 chromeos::DBusThreadManager::Get()->GetLorgnetteManagerClient();
119 CHECK(lorgnette_manager_client_
);
121 return lorgnette_manager_client_
;
125 DocumentScanInterface
* DocumentScanInterface::CreateInstance() {
126 return new DocumentScanInterfaceChromeos();
131 } // namespace extensions