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..b88468ed60f5d3f449a69e99d1e05d6683ef0abc |
--- /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) { |
dcheng
2016/07/27 16:43:36
It doesn't seem like there's any need for ScopedTe
Sergey Poromov
2016/07/27 18:24:41
lhchavez@ suggested it to avoid trashing /tmp afte
dcheng
2016/07/28 01:38:09
It's not built out, and there's no obvious need fo
Sergey Poromov
2016/07/28 14:43:00
Done.
|
+ 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]; |
+ 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 pdf_data) { |
+ if (!pdf_data.is_valid()) { |
+ LOG(ERROR) << "handle is invalid"; |
+ return; |
+ } |
+ |
+ mojo::edk::ScopedPlatformHandle scoped_platform_handle; |
+ MojoResult mojo_result = mojo::edk::PassWrappedPlatformHandle( |
+ pdf_data.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); |
dcheng
2016/07/27 16:43:36
I would suggest just creating a base::File from th
Sergey Poromov
2016/07/27 18:24:41
Done.
|
+ |
+ content::BrowserThread::PostTaskAndReplyWithResult( |
+ content::BrowserThread::FILE, FROM_HERE, |
+ base::Bind(&SavePdf, base::Passed(&fd)), base::Bind(&OpenPdf)); |
+} |
+ |
+} // namespace arc |