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 "base/file_util.h" |
| 8 #include "base/files/file.h" |
| 9 #include "base/files/file_path.h" |
| 10 #include "base/strings/stringprintf.h" |
| 11 #include "chrome/browser/download/download_prefs.h" |
| 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/profiles/profile_manager.h" |
| 14 #include "chromeos/dbus/dbus_thread_manager.h" |
| 15 #include "chromeos/login/login_state.h" |
| 16 #include "content/public/browser/browser_thread.h" |
| 17 #include "extensions/browser/extension_system.h" |
| 18 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 19 |
| 20 |
| 21 using chromeos::DBusThreadManager; |
| 22 using chromeos::LorgnetteManagerClient; |
| 23 using content::BrowserThread; |
| 24 |
| 25 namespace extensions { |
| 26 |
| 27 namespace api { |
| 28 |
| 29 DocumentScanListScannersFunction::DocumentScanListScannersFunction() {} |
| 30 |
| 31 DocumentScanListScannersFunction::~DocumentScanListScannersFunction() {} |
| 32 |
| 33 bool DocumentScanListScannersFunction::Prepare() { |
| 34 set_work_thread_id(BrowserThread::FILE); |
| 35 return true; |
| 36 } |
| 37 |
| 38 void DocumentScanListScannersFunction::AsyncWorkStart() { |
| 39 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 40 #if !defined(OS_CHROMEOS) |
| 41 results_ = document_scan::ListScanners::Results::Create( |
| 42 std::vector<linked_ptr<document_scan::ScannerInfo> >()); |
| 43 AsyncWorkCompleted(); |
| 44 #else // OS_CHROMEOS |
| 45 DBusThreadManager::Get()->GetLorgnetteManagerClient()-> |
| 46 ListScanners(base::Bind( |
| 47 &DocumentScanListScannersFunction::OnScannerListReceived, |
| 48 base::Unretained(this))); |
| 49 |
| 50 // Add a reference, which is balanced in OnScannerListReceived to keep the |
| 51 // object around and allow the callback to be invoked. |
| 52 AddRef(); |
| 53 #endif // OS_CHROMEOS |
| 54 } |
| 55 |
| 56 void DocumentScanListScannersFunction::OnScannerListReceived( |
| 57 bool succeeded, const LorgnetteManagerClient::ScannerTable &scanners) { |
| 58 std::vector<linked_ptr<document_scan::ScannerInfo> > out_scanners; |
| 59 for (LorgnetteManagerClient::ScannerTable::const_iterator iter = |
| 60 scanners.begin(); |
| 61 iter != scanners.end(); |
| 62 ++iter) { |
| 63 const std::string &name = iter->first; |
| 64 const LorgnetteManagerClient::ScannerTableEntry &entry = iter->second; |
| 65 linked_ptr<document_scan::ScannerInfo> info(new document_scan::ScannerInfo); |
| 66 info->name = name; |
| 67 LorgnetteManagerClient::ScannerTableEntry::const_iterator info_it; |
| 68 info_it = entry.find(lorgnette::kScannerPropertyManufacturer); |
| 69 if (info_it != entry.end()) { |
| 70 info->manufacturer = info_it->second; |
| 71 } |
| 72 info_it = entry.find(lorgnette::kScannerPropertyModel); |
| 73 if (info_it != entry.end()) { |
| 74 info->model = info_it->second; |
| 75 } |
| 76 info_it = entry.find(lorgnette::kScannerPropertyType); |
| 77 if (info_it != entry.end()) { |
| 78 info->type = info_it->second; |
| 79 } |
| 80 out_scanners.push_back(info); |
| 81 } |
| 82 |
| 83 results_ = document_scan::ListScanners::Results::Create(out_scanners); |
| 84 AsyncWorkCompleted(); |
| 85 |
| 86 // Balance the AddRef in AsyncWorkStart(). |
| 87 Release(); |
| 88 } |
| 89 |
| 90 bool DocumentScanListScannersFunction::Respond() { |
| 91 return true; |
| 92 } |
| 93 |
| 94 DocumentScanScanFunction::DocumentScanScanFunction() {} |
| 95 |
| 96 DocumentScanScanFunction::~DocumentScanScanFunction() {} |
| 97 |
| 98 bool DocumentScanScanFunction::Prepare() { |
| 99 set_work_thread_id(BrowserThread::FILE); |
| 100 params_ = document_scan::Scan::Params::Create(*args_); |
| 101 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 102 return true; |
| 103 } |
| 104 |
| 105 bool DocumentScanScanFunction::GetDocumentScanFilename( |
| 106 base::FilePath* filename) { |
| 107 base::FilePath directory; |
| 108 |
| 109 if (chromeos::LoginState::Get()->IsUserLoggedIn()) { |
| 110 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext( |
| 111 ProfileManager::GetActiveUserProfile()); |
| 112 directory = download_prefs->DownloadPath(); |
| 113 } else { |
| 114 if (!base::GetTempDir(&directory)) { |
| 115 LOG(ERROR) << "Failed to find temporary directory."; |
| 116 return false; |
| 117 } |
| 118 } |
| 119 |
| 120 base::Time::Exploded now; |
| 121 base::Time::Now().LocalExplode(&now); |
| 122 |
| 123 std::string basename = base::StringPrintf( |
| 124 "Document Scan %d-%02d-%02d at %02d.%02d.%02d", |
| 125 now.year, now.month, now.day_of_month, now.hour, now.minute, now.second); |
| 126 |
| 127 *filename = directory.AppendASCII(basename + ".png"); |
| 128 return true; |
| 129 } |
| 130 |
| 131 void DocumentScanScanFunction::AsyncWorkStart() { |
| 132 #if !defined(OS_CHROMEOS) |
| 133 results_ = document_scan::Scan::Results::Create(false, ""); |
| 134 AsyncWorkCompleted(); |
| 135 return; |
| 136 #endif // OS_CHROMEOS |
| 137 |
| 138 LOG(WARNING) << "Choosing scanner " << params_->scanner; |
| 139 LorgnetteManagerClient::ScanProperties properties; |
| 140 if (params_->options.mode.get()) { |
| 141 properties.mode = *params_->options.mode.get(); |
| 142 } |
| 143 |
| 144 if (params_->options.resolution_dpi.get()) { |
| 145 properties.resolution_dpi = *params_->options.resolution_dpi.get(); |
| 146 } |
| 147 |
| 148 base::FilePath scan_filename; |
| 149 if (!GetDocumentScanFilename(&scan_filename)) { |
| 150 LOG(WARNING) << "Could not find document scan directory"; |
| 151 return; |
| 152 } |
| 153 |
| 154 base::File file( |
| 155 scan_filename, |
| 156 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); |
| 157 if (!file.IsValid()) { |
| 158 LOG(WARNING) << "Could not open file " << scan_filename.value(); |
| 159 return; |
| 160 } |
| 161 |
| 162 base::PlatformFile platform_file = file.TakePlatformFile(); |
| 163 LOG(WARNING) << "ScanImage platform_file is " << platform_file; |
| 164 DBusThreadManager::Get()->GetLorgnetteManagerClient()-> |
| 165 ScanImage(params_->scanner, platform_file, properties, |
| 166 base::Bind(&DocumentScanScanFunction::ScanImageCallback, |
| 167 base::Unretained(this), |
| 168 scan_filename)); |
| 169 |
| 170 // Add a reference, which is balanced in ScanImageCallback to keep the |
| 171 // object around and allow the callback to be invoked. |
| 172 AddRef(); |
| 173 } |
| 174 |
| 175 void DocumentScanScanFunction::ScanImageCallback( |
| 176 const base::FilePath& filename, bool succeeded) { |
| 177 LOG(WARNING) << "ScanImage returns " << succeeded; |
| 178 std::string image_data; |
| 179 if (!base::ReadFileToString(filename, &image_data)) { |
| 180 LOG(WARNING) << "Unable to read image data at " << filename.value(); |
| 181 } |
| 182 results_ = document_scan::Scan::Results::Create(succeeded, image_data); |
| 183 AsyncWorkCompleted(); |
| 184 |
| 185 // Balance the AddRef in AsyncWorkStart(). |
| 186 Release(); |
| 187 } |
| 188 |
| 189 bool DocumentScanScanFunction::Respond() { |
| 190 return true; |
| 191 } |
| 192 |
| 193 } // namespace api |
| 194 |
| 195 } // namespace extensions |
OLD | NEW |