Chromium Code Reviews| Index: chrome/browser/chromeos/arc/arc_print_bridge.cc |
| diff --git a/chrome/browser/chromeos/arc/arc_print_bridge.cc b/chrome/browser/chromeos/arc/arc_print_bridge.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..fc2306aa8e0d998a8f563f8c63fe56d77b2d63be |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/arc/arc_print_bridge.cc |
| @@ -0,0 +1,109 @@ |
| +// 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_bridge.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/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 "url/gurl.h" |
| +#include "url/url_constants.h" |
| + |
| +namespace { |
| + |
| +class ScopedTempFile { |
| + public: |
| + ScopedTempFile() { base::CreateTemporaryFile(&file_path_); } |
| + ~ScopedTempFile() { |
| + // TODO(poromov) Delete file after printing. |
|
Luis Héctor Chávez
2016/07/19 16:47:43
nit: Expand this comment mentioning that this is j
Sergey Poromov
2016/07/20 15:08:16
Done.
|
| + // base::DeleteFile(file_path_, false); |
|
Luis Héctor Chávez
2016/07/19 16:47:43
nit: There should not be any commented code.
Sergey Poromov
2016/07/20 15:08:16
Done.
|
| + } |
| + |
| + const base::FilePath& file_path() const { return file_path_; } |
| + |
| + private: |
| + base::FilePath file_path_; |
| +}; |
| + |
| +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); |
| + |
| + char buf[8192]; |
| + size_t bytes; |
|
Luis Héctor Chávez
2016/07/19 16:47:43
welp, sorry, this should have been ssize_t.
Sergey Poromov
2016/07/20 15:08:16
Done.
|
| + while ((bytes = HANDLE_EINTR(read(fd.get(), buf, 8192))) > 0) { |
|
Luis Héctor Chávez
2016/07/19 16:47:43
nit: s/8192/sizeof(buf)/
Sergey Poromov
2016/07/20 15:08:16
Done.
|
| + out.WriteAtCurrentPos(buf, bytes); |
| + } |
| + |
| + return temp_file; |
| +} |
| + |
| +void OpenPdf(ScopedTempFile file) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + |
| + GURL gurl(std::string(url::kFileScheme) + url::kStandardSchemeSeparator + |
| + file.file_path().value()); |
| + ash::WmShell::Get()->delegate()->OpenUrl(gurl); |
| +} |
| + |
| +} // namespace |
| + |
| +namespace arc { |
| + |
| +ArcPrintBridge::ArcPrintBridge(ArcBridgeService* bridge_service) |
| + : ArcService(bridge_service), binding_(this) { |
| + arc_bridge_service()->print()->AddObserver(this); |
| +} |
| + |
| +ArcPrintBridge::~ArcPrintBridge() { |
| + arc_bridge_service()->print()->RemoveObserver(this); |
| +} |
| + |
| +void ArcPrintBridge::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 ArcPrintBridge::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 |