Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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/chromeos/arc/arc_print_service.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "ash/common/shell_delegate.h" | |
| 10 #include "ash/common/wm_shell.h" | |
| 11 #include "ash/new_window_delegate.h" | |
| 12 #include "ash/shell.h" | |
| 13 #include "base/files/file.h" | |
| 14 #include "base/files/scoped_file.h" | |
| 15 #include "base/logging.h" | |
| 16 #include "base/optional.h" | |
| 17 #include "base/strings/utf_string_conversions.h" | |
| 18 #include "base/threading/thread_checker.h" | |
| 19 #include "components/arc/arc_bridge_service.h" | |
| 20 #include "content/public/browser/browser_thread.h" | |
| 21 #include "mojo/edk/embedder/embedder.h" | |
| 22 #include "net/base/filename_util.h" | |
| 23 #include "url/gurl.h" | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 class ScopedTempFile { | |
| 28 public: | |
| 29 ScopedTempFile() { base::CreateTemporaryFile(&file_path_); } | |
| 30 ~ScopedTempFile() { | |
| 31 // TODO(poromov) Delete file after printing. (http://crbug.com/629843) | |
| 32 } | |
| 33 | |
| 34 const base::FilePath& file_path() const { return file_path_; } | |
| 35 | |
| 36 private: | |
| 37 base::FilePath file_path_; | |
| 38 }; | |
| 39 | |
| 40 base::Optional<ScopedTempFile> SavePdf(base::ScopedFD fd) { | |
| 41 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE); | |
| 42 | |
| 43 ScopedTempFile temp_file; | |
| 44 base::File out(temp_file.file_path(), | |
| 45 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
| |
| 46 | |
| 47 char buf[8192]; | |
| 48 ssize_t bytes; | |
| 49 while ((bytes = HANDLE_EINTR(read(fd.get(), buf, sizeof(buf)))) > 0) { | |
| 50 int written = out.WriteAtCurrentPos(buf, bytes); | |
| 51 if (written < 0) { | |
| 52 LOG(ERROR) << "Error while saving PDF to a disk"; | |
| 53 return base::nullopt; | |
| 54 } | |
| 55 } | |
| 56 | |
| 57 return base::make_optional(temp_file); | |
| 58 } | |
| 59 | |
| 60 void OpenPdf(base::Optional<ScopedTempFile> file) { | |
| 61 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 62 if (!file) | |
| 63 return; | |
| 64 | |
| 65 GURL gurl = net::FilePathToFileURL(file.value().file_path()); | |
| 66 ash::WmShell::Get()->delegate()->OpenUrl(gurl); | |
| 67 } | |
| 68 | |
| 69 } // namespace | |
| 70 | |
| 71 namespace arc { | |
| 72 | |
| 73 ArcPrintService::ArcPrintService(ArcBridgeService* bridge_service) | |
| 74 : ArcService(bridge_service), binding_(this) { | |
| 75 arc_bridge_service()->print()->AddObserver(this); | |
| 76 } | |
| 77 | |
| 78 ArcPrintService::~ArcPrintService() { | |
| 79 arc_bridge_service()->print()->RemoveObserver(this); | |
| 80 } | |
| 81 | |
| 82 void ArcPrintService::OnInstanceReady() { | |
| 83 mojom::PrintInstance* print_instance = | |
| 84 arc_bridge_service()->print()->instance(); | |
| 85 if (!print_instance) { | |
| 86 LOG(ERROR) << "OnPrintInstanceReady called, but no print instance found"; | |
| 87 return; | |
| 88 } | |
| 89 | |
| 90 print_instance->Init(binding_.CreateInterfacePtrAndBind()); | |
| 91 } | |
| 92 | |
| 93 void ArcPrintService::Print(mojo::ScopedHandle file) { | |
| 94 if (!file.is_valid()) { | |
| 95 LOG(ERROR) << "handle is invalid"; | |
| 96 return; | |
| 97 } | |
| 98 | |
| 99 mojo::edk::ScopedPlatformHandle scoped_platform_handle; | |
| 100 MojoResult mojo_result = mojo::edk::PassWrappedPlatformHandle( | |
| 101 file.release().value(), &scoped_platform_handle); | |
| 102 if (mojo_result != MOJO_RESULT_OK) { | |
| 103 LOG(ERROR) << "PassWrappedPlatformHandle failed: " << mojo_result; | |
| 104 return; | |
| 105 } | |
| 106 | |
| 107 base::ScopedFD fd(scoped_platform_handle.release().handle); | |
| 108 | |
| 109 content::BrowserThread::PostTaskAndReplyWithResult( | |
| 110 content::BrowserThread::FILE, FROM_HERE, | |
| 111 base::Bind(&SavePdf, base::Passed(&fd)), base::Bind(&OpenPdf)); | |
| 112 } | |
| 113 | |
| 114 } // namespace arc | |
| OLD | NEW |