Chromium Code Reviews| Index: chrome/browser/chromeos/arc/arc_print_service.cc |
| diff --git a/chrome/browser/chromeos/arc/arc_print_service.cc b/chrome/browser/chromeos/arc/arc_print_service.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1540c7bfda550fadeeee03ff2e16c25e7fc5f608 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/arc/arc_print_service.cc |
| @@ -0,0 +1,114 @@ |
| +// Copyright 2016 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/chromeos/arc/arc_print_service.h" |
| + |
| +#include <utility> |
| + |
| +#include "ash/common/shell_delegate.h" |
| +#include "ash/common/wm_shell.h" |
| +#include "ash/new_window_delegate.h" |
| +#include "ash/shell.h" |
| +#include "base/files/file.h" |
| +#include "base/files/scoped_file.h" |
| +#include "base/logging.h" |
| +#include "base/optional.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "base/threading/thread_checker.h" |
| +#include "components/arc/arc_bridge_service.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "mojo/edk/embedder/embedder.h" |
| +#include "net/base/filename_util.h" |
| +#include "url/gurl.h" |
| + |
| +namespace { |
| + |
| +class ScopedTempFile { |
| + public: |
| + ScopedTempFile() { base::CreateTemporaryFile(&file_path_); } |
| + ~ScopedTempFile() { |
| + // TODO(poromov) Delete file after printing. (http://crbug.com/629843) |
| + } |
| + |
| + const base::FilePath& file_path() const { return file_path_; } |
| + |
| + private: |
| + base::FilePath file_path_; |
| +}; |
| + |
| +base::Optional<ScopedTempFile> SavePdf(base::ScopedFD fd) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::FILE); |
| + |
| + ScopedTempFile temp_file; |
| + base::File out(temp_file.file_path(), |
| + base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); |
|
dcheng
2016/07/25 06:36:30
Btw, can we just use base::File::FLAG_DELETE_ON_CL
Sergey Poromov
2016/07/25 13:04:11
Looks like it's not yet possible to delete on clos
|
| + |
| + char buf[8192]; |
| + ssize_t bytes; |
| + while ((bytes = HANDLE_EINTR(read(fd.get(), buf, sizeof(buf)))) > 0) { |
| + int written = out.WriteAtCurrentPos(buf, bytes); |
| + if (written < 0) { |
| + LOG(ERROR) << "Error while saving PDF to a disk"; |
| + return base::nullopt; |
| + } |
| + } |
| + |
| + return base::make_optional(temp_file); |
| +} |
| + |
| +void OpenPdf(base::Optional<ScopedTempFile> file) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + if (!file) |
| + return; |
| + |
| + GURL gurl = net::FilePathToFileURL(file.value().file_path()); |
| + ash::WmShell::Get()->delegate()->OpenUrl(gurl); |
| +} |
| + |
| +} // namespace |
| + |
| +namespace arc { |
| + |
| +ArcPrintService::ArcPrintService(ArcBridgeService* bridge_service) |
| + : ArcService(bridge_service), binding_(this) { |
| + arc_bridge_service()->print()->AddObserver(this); |
| +} |
| + |
| +ArcPrintService::~ArcPrintService() { |
| + arc_bridge_service()->print()->RemoveObserver(this); |
| +} |
| + |
| +void ArcPrintService::OnInstanceReady() { |
| + mojom::PrintInstance* print_instance = |
| + arc_bridge_service()->print()->instance(); |
| + if (!print_instance) { |
| + LOG(ERROR) << "OnPrintInstanceReady called, but no print instance found"; |
| + return; |
| + } |
| + |
| + print_instance->Init(binding_.CreateInterfacePtrAndBind()); |
| +} |
| + |
| +void ArcPrintService::Print(mojo::ScopedHandle file) { |
| + if (!file.is_valid()) { |
| + LOG(ERROR) << "handle is invalid"; |
| + return; |
| + } |
| + |
| + mojo::edk::ScopedPlatformHandle scoped_platform_handle; |
| + MojoResult mojo_result = mojo::edk::PassWrappedPlatformHandle( |
| + file.release().value(), &scoped_platform_handle); |
| + if (mojo_result != MOJO_RESULT_OK) { |
| + LOG(ERROR) << "PassWrappedPlatformHandle failed: " << mojo_result; |
| + return; |
| + } |
| + |
| + base::ScopedFD fd(scoped_platform_handle.release().handle); |
| + |
| + content::BrowserThread::PostTaskAndReplyWithResult( |
| + content::BrowserThread::FILE, FROM_HERE, |
| + base::Bind(&SavePdf, base::Passed(&fd)), base::Bind(&OpenPdf)); |
| +} |
| + |
| +} // namespace arc |