| Index: chrome/browser/extensions/api/document_scan/document_scan_api.cc
|
| diff --git a/chrome/browser/extensions/api/document_scan/document_scan_api.cc b/chrome/browser/extensions/api/document_scan/document_scan_api.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..e87afe366fd15bd33a475959c4a74c92655f6a32
|
| --- /dev/null
|
| +++ b/chrome/browser/extensions/api/document_scan/document_scan_api.cc
|
| @@ -0,0 +1,195 @@
|
| +// Copyright 2014 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "chrome/browser/extensions/api/document_scan/document_scan_api.h"
|
| +
|
| +#include "base/file_util.h"
|
| +#include "base/files/file.h"
|
| +#include "base/files/file_path.h"
|
| +#include "base/strings/stringprintf.h"
|
| +#include "chrome/browser/download/download_prefs.h"
|
| +#include "chrome/browser/profiles/profile.h"
|
| +#include "chrome/browser/profiles/profile_manager.h"
|
| +#include "chromeos/dbus/dbus_thread_manager.h"
|
| +#include "chromeos/login/login_state.h"
|
| +#include "content/public/browser/browser_thread.h"
|
| +#include "extensions/browser/extension_system.h"
|
| +#include "third_party/cros_system_api/dbus/service_constants.h"
|
| +
|
| +
|
| +using chromeos::DBusThreadManager;
|
| +using chromeos::LorgnetteManagerClient;
|
| +using content::BrowserThread;
|
| +
|
| +namespace extensions {
|
| +
|
| +namespace api {
|
| +
|
| +DocumentScanListScannersFunction::DocumentScanListScannersFunction() {}
|
| +
|
| +DocumentScanListScannersFunction::~DocumentScanListScannersFunction() {}
|
| +
|
| +bool DocumentScanListScannersFunction::Prepare() {
|
| + set_work_thread_id(BrowserThread::FILE);
|
| + return true;
|
| +}
|
| +
|
| +void DocumentScanListScannersFunction::AsyncWorkStart() {
|
| + DCHECK_CURRENTLY_ON(BrowserThread::FILE);
|
| +#if !defined(OS_CHROMEOS)
|
| + results_ = document_scan::ListScanners::Results::Create(
|
| + std::vector<linked_ptr<document_scan::ScannerInfo> >());
|
| + AsyncWorkCompleted();
|
| +#else // OS_CHROMEOS
|
| + DBusThreadManager::Get()->GetLorgnetteManagerClient()->
|
| + ListScanners(base::Bind(
|
| + &DocumentScanListScannersFunction::OnScannerListReceived,
|
| + base::Unretained(this)));
|
| +
|
| + // Add a reference, which is balanced in OnScannerListReceived to keep the
|
| + // object around and allow the callback to be invoked.
|
| + AddRef();
|
| +#endif // OS_CHROMEOS
|
| +}
|
| +
|
| +void DocumentScanListScannersFunction::OnScannerListReceived(
|
| + bool succeeded, const LorgnetteManagerClient::ScannerTable &scanners) {
|
| + std::vector<linked_ptr<document_scan::ScannerInfo> > out_scanners;
|
| + for (LorgnetteManagerClient::ScannerTable::const_iterator iter =
|
| + scanners.begin();
|
| + iter != scanners.end();
|
| + ++iter) {
|
| + const std::string &name = iter->first;
|
| + const LorgnetteManagerClient::ScannerTableEntry &entry = iter->second;
|
| + linked_ptr<document_scan::ScannerInfo> info(new document_scan::ScannerInfo);
|
| + info->name = name;
|
| + LorgnetteManagerClient::ScannerTableEntry::const_iterator info_it;
|
| + info_it = entry.find(lorgnette::kScannerPropertyManufacturer);
|
| + if (info_it != entry.end()) {
|
| + info->manufacturer = info_it->second;
|
| + }
|
| + info_it = entry.find(lorgnette::kScannerPropertyModel);
|
| + if (info_it != entry.end()) {
|
| + info->model = info_it->second;
|
| + }
|
| + info_it = entry.find(lorgnette::kScannerPropertyType);
|
| + if (info_it != entry.end()) {
|
| + info->type = info_it->second;
|
| + }
|
| + out_scanners.push_back(info);
|
| + }
|
| +
|
| + results_ = document_scan::ListScanners::Results::Create(out_scanners);
|
| + AsyncWorkCompleted();
|
| +
|
| + // Balance the AddRef in AsyncWorkStart().
|
| + Release();
|
| +}
|
| +
|
| +bool DocumentScanListScannersFunction::Respond() {
|
| + return true;
|
| +}
|
| +
|
| +DocumentScanScanFunction::DocumentScanScanFunction() {}
|
| +
|
| +DocumentScanScanFunction::~DocumentScanScanFunction() {}
|
| +
|
| +bool DocumentScanScanFunction::Prepare() {
|
| + set_work_thread_id(BrowserThread::FILE);
|
| + params_ = document_scan::Scan::Params::Create(*args_);
|
| + EXTENSION_FUNCTION_VALIDATE(params_.get());
|
| + return true;
|
| +}
|
| +
|
| +bool DocumentScanScanFunction::GetDocumentScanFilename(
|
| + base::FilePath* filename) {
|
| + base::FilePath directory;
|
| +
|
| + if (chromeos::LoginState::Get()->IsUserLoggedIn()) {
|
| + DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext(
|
| + ProfileManager::GetActiveUserProfile());
|
| + directory = download_prefs->DownloadPath();
|
| + } else {
|
| + if (!base::GetTempDir(&directory)) {
|
| + LOG(ERROR) << "Failed to find temporary directory.";
|
| + return false;
|
| + }
|
| + }
|
| +
|
| + base::Time::Exploded now;
|
| + base::Time::Now().LocalExplode(&now);
|
| +
|
| + std::string basename = base::StringPrintf(
|
| + "Document Scan %d-%02d-%02d at %02d.%02d.%02d",
|
| + now.year, now.month, now.day_of_month, now.hour, now.minute, now.second);
|
| +
|
| + *filename = directory.AppendASCII(basename + ".png");
|
| + return true;
|
| +}
|
| +
|
| +void DocumentScanScanFunction::AsyncWorkStart() {
|
| +#if !defined(OS_CHROMEOS)
|
| + results_ = document_scan::Scan::Results::Create(false, "");
|
| + AsyncWorkCompleted();
|
| + return;
|
| +#endif // OS_CHROMEOS
|
| +
|
| + LOG(WARNING) << "Choosing scanner " << params_->scanner;
|
| + LorgnetteManagerClient::ScanProperties properties;
|
| + if (params_->options.mode.get()) {
|
| + properties.mode = *params_->options.mode.get();
|
| + }
|
| +
|
| + if (params_->options.resolution_dpi.get()) {
|
| + properties.resolution_dpi = *params_->options.resolution_dpi.get();
|
| + }
|
| +
|
| + base::FilePath scan_filename;
|
| + if (!GetDocumentScanFilename(&scan_filename)) {
|
| + LOG(WARNING) << "Could not find document scan directory";
|
| + return;
|
| + }
|
| +
|
| + base::File file(
|
| + scan_filename,
|
| + base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
|
| + if (!file.IsValid()) {
|
| + LOG(WARNING) << "Could not open file " << scan_filename.value();
|
| + return;
|
| + }
|
| +
|
| + base::PlatformFile platform_file = file.TakePlatformFile();
|
| + LOG(WARNING) << "ScanImage platform_file is " << platform_file;
|
| + DBusThreadManager::Get()->GetLorgnetteManagerClient()->
|
| + ScanImage(params_->scanner, platform_file, properties,
|
| + base::Bind(&DocumentScanScanFunction::ScanImageCallback,
|
| + base::Unretained(this),
|
| + scan_filename));
|
| +
|
| + // Add a reference, which is balanced in ScanImageCallback to keep the
|
| + // object around and allow the callback to be invoked.
|
| + AddRef();
|
| +}
|
| +
|
| +void DocumentScanScanFunction::ScanImageCallback(
|
| + const base::FilePath& filename, bool succeeded) {
|
| + LOG(WARNING) << "ScanImage returns " << succeeded;
|
| + std::string image_data;
|
| + if (!base::ReadFileToString(filename, &image_data)) {
|
| + LOG(WARNING) << "Unable to read image data at " << filename.value();
|
| + }
|
| + results_ = document_scan::Scan::Results::Create(succeeded, image_data);
|
| + AsyncWorkCompleted();
|
| +
|
| + // Balance the AddRef in AsyncWorkStart().
|
| + Release();
|
| +}
|
| +
|
| +bool DocumentScanScanFunction::Respond() {
|
| + return true;
|
| +}
|
| +
|
| +} // namespace api
|
| +
|
| +} // namespace extensions
|
|
|