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 "components/arc/print/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 "mojo/edk/embedder/embedder.h" | |
| 20 #include "url/gurl.h" | |
| 21 | |
| 22 namespace arc { | |
| 23 | |
| 24 ArcPrintBridge::ArcPrintBridge(ArcBridgeService* bridge_service) | |
| 25 : ArcService(bridge_service), binding_(this) { | |
| 26 arc_bridge_service()->AddObserver(this); | |
| 27 } | |
| 28 | |
| 29 ArcPrintBridge::~ArcPrintBridge() { | |
| 30 arc_bridge_service()->RemoveObserver(this); | |
| 31 } | |
| 32 | |
| 33 void ArcPrintBridge::OnPrintInstanceReady() { | |
| 34 mojom::PrintInstance* print_instance = arc_bridge_service()->print_instance(); | |
| 35 if (!print_instance) { | |
| 36 LOG(ERROR) << "OnPrintInstanceReady called, but no print instance found"; | |
| 37 return; | |
| 38 } | |
| 39 | |
| 40 print_instance->Init(binding_.CreateInterfacePtrAndBind()); | |
| 41 } | |
| 42 | |
| 43 void ArcPrintBridge::Print(mojo::ScopedHandle file) { | |
|
Luis Héctor Chávez
2016/07/11 18:27:53
This function runs in the UI thread, which shouldn
Sergey Poromov
2016/07/13 16:58:42
Done.
| |
| 44 LOG(ERROR) << "Print called!!! " << file.get().value(); | |
|
Luis Héctor Chávez
2016/07/11 18:27:53
Try to keep debugging statements to a minimum. If
Sergey Poromov
2016/07/13 16:58:42
Done.
| |
| 45 | |
| 46 if (!file.is_valid()) { | |
| 47 LOG(ERROR) << "handle is invalid"; | |
| 48 return; | |
| 49 } | |
| 50 | |
| 51 mojo::edk::ScopedPlatformHandle scoped_platform_handle; | |
| 52 MojoResult mojo_result = mojo::edk::PassWrappedPlatformHandle( | |
| 53 file.release().value(), &scoped_platform_handle); | |
| 54 if (mojo_result != MOJO_RESULT_OK) { | |
| 55 LOG(ERROR) << "PassWrappedPlatformHandle failed: " << mojo_result; | |
| 56 return; | |
| 57 } | |
| 58 | |
| 59 base::ScopedFD fd(scoped_platform_handle.release().handle); | |
| 60 | |
| 61 base::FileDescriptor fn(fd.release(), true); | |
|
Luis Héctor Chávez
2016/07/11 18:27:53
This seems unnecessary. You can use |fd.get()| ins
Sergey Poromov
2016/07/13 16:58:42
Done.
| |
| 62 LOG(ERROR) << "File opened"; | |
| 63 base::File fl(fn.fd); | |
|
Luis Héctor Chávez
2016/07/11 18:27:53
This also feels unnecessary. Plus it will double-c
Sergey Poromov
2016/07/13 16:58:42
Done.
| |
| 64 LOG(ERROR) << "File length" << fl.GetLength(); | |
|
Luis Héctor Chávez
2016/07/11 18:27:53
Huh, I was not expecting this would work with pipe
Sergey Poromov
2016/07/13 16:58:42
Done.
| |
| 65 | |
| 66 base::FilePath filePath(FILE_PATH_LITERAL("/tmp/test.pdf")); | |
|
Luis Héctor Chávez
2016/07/11 18:27:53
Can you use base::CreateTemporaryFile instead? Als
Sergey Poromov
2016/07/13 16:58:42
Done.
| |
| 67 base::File out(filePath, | |
| 68 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); | |
| 69 | |
| 70 char buf[8192]; | |
| 71 int bytes; | |
|
Luis Héctor Chávez
2016/07/11 18:27:53
size_t
Sergey Poromov
2016/07/13 16:58:42
Done.
| |
| 72 while ((bytes = read(fn.fd, buf, 8192)) > 0) { | |
|
Luis Héctor Chávez
2016/07/11 18:27:53
Try to model this after base::ReadFromFD. This doe
Sergey Poromov
2016/07/13 16:58:42
ReadFromFD doesn't return amount of read bytes. We
| |
| 73 LOG(ERROR) << "READ " << bytes; | |
| 74 out.WriteAtCurrentPos(buf, bytes); | |
| 75 } | |
| 76 | |
| 77 LOG(ERROR) << "Result code: " << errno; | |
|
Luis Héctor Chávez
2016/07/11 18:27:53
VPLOG(2)? That'll convert errno into its string re
Sergey Poromov
2016/07/13 16:58:42
Done.
| |
| 78 | |
| 79 out.Flush(); | |
| 80 out.Close(); | |
|
Luis Héctor Chávez
2016/07/11 18:27:53
You can just do
{
base::File out(...);
// Wri
Sergey Poromov
2016/07/13 16:58:42
Done.
| |
| 81 fd.release(); | |
|
Luis Héctor Chávez
2016/07/11 18:27:53
This is unnecessary.
Sergey Poromov
2016/07/13 16:58:42
Done.
| |
| 82 | |
| 83 GURL gurl("file:///tmp/test.pdf"); | |
| 84 ash::WmShell::Get()->delegate()->OpenUrl(gurl); | |
| 85 | |
| 86 } | |
| 87 | |
| 88 } // namespace arc | |
| OLD | NEW |