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_bridge.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/strings/utf_string_conversions.h" | |
| 17 #include "base/threading/thread_checker.h" | |
| 18 #include "components/arc/arc_bridge_service.h" | |
| 19 #include "content/public/browser/browser_thread.h" | |
| 20 #include "mojo/edk/embedder/embedder.h" | |
| 21 #include "url/gurl.h" | |
| 22 #include "url/url_constants.h" | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 class ScopedTempFile { | |
| 27 public: | |
| 28 ScopedTempFile() { base::CreateTemporaryFile(&file_path_); } | |
| 29 ~ScopedTempFile() { | |
| 30 // 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.
| |
| 31 // 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.
| |
| 32 } | |
| 33 | |
| 34 const base::FilePath& file_path() const { return file_path_; } | |
| 35 | |
| 36 private: | |
| 37 base::FilePath file_path_; | |
| 38 }; | |
| 39 | |
| 40 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); | |
| 46 | |
| 47 char buf[8192]; | |
| 48 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.
| |
| 49 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.
| |
| 50 out.WriteAtCurrentPos(buf, bytes); | |
| 51 } | |
| 52 | |
| 53 return temp_file; | |
| 54 } | |
| 55 | |
| 56 void OpenPdf(ScopedTempFile file) { | |
| 57 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 58 | |
| 59 GURL gurl(std::string(url::kFileScheme) + url::kStandardSchemeSeparator + | |
| 60 file.file_path().value()); | |
| 61 ash::WmShell::Get()->delegate()->OpenUrl(gurl); | |
| 62 } | |
| 63 | |
| 64 } // namespace | |
| 65 | |
| 66 namespace arc { | |
| 67 | |
| 68 ArcPrintBridge::ArcPrintBridge(ArcBridgeService* bridge_service) | |
| 69 : ArcService(bridge_service), binding_(this) { | |
| 70 arc_bridge_service()->print()->AddObserver(this); | |
| 71 } | |
| 72 | |
| 73 ArcPrintBridge::~ArcPrintBridge() { | |
| 74 arc_bridge_service()->print()->RemoveObserver(this); | |
| 75 } | |
| 76 | |
| 77 void ArcPrintBridge::OnInstanceReady() { | |
| 78 mojom::PrintInstance* print_instance = | |
| 79 arc_bridge_service()->print()->instance(); | |
| 80 if (!print_instance) { | |
| 81 LOG(ERROR) << "OnPrintInstanceReady called, but no print instance found"; | |
| 82 return; | |
| 83 } | |
| 84 | |
| 85 print_instance->Init(binding_.CreateInterfacePtrAndBind()); | |
| 86 } | |
| 87 | |
| 88 void ArcPrintBridge::Print(mojo::ScopedHandle file) { | |
| 89 if (!file.is_valid()) { | |
| 90 LOG(ERROR) << "handle is invalid"; | |
| 91 return; | |
| 92 } | |
| 93 | |
| 94 mojo::edk::ScopedPlatformHandle scoped_platform_handle; | |
| 95 MojoResult mojo_result = mojo::edk::PassWrappedPlatformHandle( | |
| 96 file.release().value(), &scoped_platform_handle); | |
| 97 if (mojo_result != MOJO_RESULT_OK) { | |
| 98 LOG(ERROR) << "PassWrappedPlatformHandle failed: " << mojo_result; | |
| 99 return; | |
| 100 } | |
| 101 | |
| 102 base::ScopedFD fd(scoped_platform_handle.release().handle); | |
| 103 | |
| 104 content::BrowserThread::PostTaskAndReplyWithResult( | |
| 105 content::BrowserThread::FILE, FROM_HERE, | |
| 106 base::Bind(&SavePdf, base::Passed(&fd)), base::Bind(&OpenPdf)); | |
| 107 } | |
| 108 | |
| 109 } // namespace arc | |
| OLD | NEW |