| 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 "base/files/file.h" | |
| 12 #include "base/files/file_util.h" | |
| 13 #include "base/logging.h" | |
| 14 #include "base/optional.h" | |
| 15 #include "base/threading/thread_checker.h" | |
| 16 #include "components/arc/arc_bridge_service.h" | |
| 17 #include "content/public/browser/browser_thread.h" | |
| 18 #include "mojo/edk/embedder/embedder.h" | |
| 19 #include "net/base/filename_util.h" | |
| 20 #include "url/gurl.h" | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 base::Optional<base::FilePath> SavePdf(base::File file) { | |
| 25 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE); | |
| 26 | |
| 27 base::FilePath file_path; | |
| 28 base::CreateTemporaryFile(&file_path); | |
| 29 base::File out(file_path, | |
| 30 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); | |
| 31 | |
| 32 char buf[8192]; | |
| 33 ssize_t bytes; | |
| 34 while ((bytes = file.ReadAtCurrentPos(buf, sizeof(buf))) > 0) { | |
| 35 int written = out.WriteAtCurrentPos(buf, bytes); | |
| 36 if (written < 0) { | |
| 37 LOG(ERROR) << "Error while saving PDF to a disk"; | |
| 38 return base::nullopt; | |
| 39 } | |
| 40 } | |
| 41 | |
| 42 return file_path; | |
| 43 } | |
| 44 | |
| 45 void OpenPdf(base::Optional<base::FilePath> file_path) { | |
| 46 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 47 if (!file_path) | |
| 48 return; | |
| 49 | |
| 50 GURL gurl = net::FilePathToFileURL(file_path.value()); | |
| 51 ash::WmShell::Get()->delegate()->OpenUrlFromArc(gurl); | |
| 52 // TODO(poromov) Delete file after printing. (http://crbug.com/629843) | |
| 53 } | |
| 54 | |
| 55 } // namespace | |
| 56 | |
| 57 namespace arc { | |
| 58 | |
| 59 ArcPrintService::ArcPrintService(ArcBridgeService* bridge_service) | |
| 60 : ArcService(bridge_service), binding_(this) { | |
| 61 arc_bridge_service()->print()->AddObserver(this); | |
| 62 } | |
| 63 | |
| 64 ArcPrintService::~ArcPrintService() { | |
| 65 arc_bridge_service()->print()->RemoveObserver(this); | |
| 66 } | |
| 67 | |
| 68 void ArcPrintService::OnInstanceReady() { | |
| 69 mojom::PrintInstance* print_instance = | |
| 70 arc_bridge_service()->print()->GetInstanceForMethod("Init"); | |
| 71 DCHECK(print_instance); | |
| 72 print_instance->Init(binding_.CreateInterfacePtrAndBind()); | |
| 73 } | |
| 74 | |
| 75 void ArcPrintService::Print(mojo::ScopedHandle pdf_data) { | |
| 76 if (!pdf_data.is_valid()) { | |
| 77 LOG(ERROR) << "handle is invalid"; | |
| 78 return; | |
| 79 } | |
| 80 | |
| 81 mojo::edk::ScopedPlatformHandle scoped_platform_handle; | |
| 82 MojoResult mojo_result = mojo::edk::PassWrappedPlatformHandle( | |
| 83 pdf_data.release().value(), &scoped_platform_handle); | |
| 84 if (mojo_result != MOJO_RESULT_OK) { | |
| 85 LOG(ERROR) << "PassWrappedPlatformHandle failed: " << mojo_result; | |
| 86 return; | |
| 87 } | |
| 88 | |
| 89 base::File file(scoped_platform_handle.release().handle); | |
| 90 | |
| 91 content::BrowserThread::PostTaskAndReplyWithResult( | |
| 92 content::BrowserThread::FILE, FROM_HERE, | |
| 93 base::Bind(&SavePdf, base::Passed(&file)), base::Bind(&OpenPdf)); | |
| 94 } | |
| 95 | |
| 96 } // namespace arc | |
| OLD | NEW |