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 | |
23 namespace arc { | |
24 | |
25 ArcPrintBridge::ArcPrintBridge(ArcBridgeService* bridge_service) | |
26 : ArcService(bridge_service), binding_(this) { | |
27 arc_bridge_service()->AddObserver(this); | |
Luis Héctor Chávez
2016/07/14 16:29:43
arc_bridge_service()->print()->AddObserver(this);
Sergey Poromov
2016/07/19 16:32:09
Done.
| |
28 } | |
29 | |
30 ArcPrintBridge::~ArcPrintBridge() { | |
31 arc_bridge_service()->RemoveObserver(this); | |
32 } | |
33 | |
34 void ArcPrintBridge::OnPrintInstanceReady() { | |
35 mojom::PrintInstance* print_instance = arc_bridge_service()->print_instance(); | |
36 if (!print_instance) { | |
37 LOG(ERROR) << "OnPrintInstanceReady called, but no print instance found"; | |
38 return; | |
39 } | |
40 | |
41 print_instance->Init(binding_.CreateInterfacePtrAndBind()); | |
42 } | |
43 | |
44 void ArcPrintBridge::Print(mojo::ScopedHandle file) { | |
45 if (!file.is_valid()) { | |
46 LOG(ERROR) << "handle is invalid"; | |
47 return; | |
48 } | |
49 | |
50 mojo::edk::ScopedPlatformHandle scoped_platform_handle; | |
51 MojoResult mojo_result = mojo::edk::PassWrappedPlatformHandle( | |
52 file.release().value(), &scoped_platform_handle); | |
53 if (mojo_result != MOJO_RESULT_OK) { | |
54 LOG(ERROR) << "PassWrappedPlatformHandle failed: " << mojo_result; | |
55 return; | |
56 } | |
57 | |
58 content::BrowserThread::PostTaskAndReply( | |
59 content::BrowserThread::FILE, FROM_HERE, | |
60 base::Bind(&ArcPrintBridge::SavePdf, this, | |
61 scoped_platform_handle.release().handle), | |
Luis Héctor Chávez
2016/07/14 16:29:43
Ideally, make the base::ScopedFD conversion in thi
Sergey Poromov
2016/07/19 16:32:09
Done.
| |
62 base::Bind(&ArcPrintBridge::OpenPdf, this)); | |
63 } | |
64 | |
65 void ArcPrintBridge::SavePdf(int handle) { | |
Luis Héctor Chávez
2016/07/14 16:29:43
This doesn't really need to be a member function.
Sergey Poromov
2016/07/19 16:32:09
Done.
| |
66 base::ScopedFD fd(handle); | |
Luis Héctor Chávez
2016/07/14 16:29:44
DCHECK_CURRENTLY_ON(content::BrowserThread::FILE);
Sergey Poromov
2016/07/19 16:32:09
Done.
| |
67 | |
68 base::CreateTemporaryFile(&file_path_); | |
69 base::File out(file_path_, | |
70 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); | |
Luis Héctor Chávez
2016/07/14 16:29:43
CreateTemporaryfile() creates the file to avoid ra
Sergey Poromov
2016/07/19 16:32:09
Yes, it doesn't work without this flag.
| |
71 | |
72 char buf[8192]; | |
73 size_t bytes; | |
74 while ((bytes = HANDLE_EINTR(read(fd.get(), buf, 8192))) > 0) { | |
75 out.WriteAtCurrentPos(buf, bytes); | |
76 } | |
77 } | |
78 | |
79 void ArcPrintBridge::OpenPdf() { | |
Luis Héctor Chávez
2016/07/14 16:29:43
This method should receive the RAII-wrapped base::
Sergey Poromov
2016/07/19 16:32:09
Unfortunately we couldn't delete the file here rig
| |
80 GURL gurl("file://" + file_path_.value()); | |
Luis Héctor Chávez
2016/07/14 16:29:43
Can you also use the constants in https://cs.chrom
Luis Héctor Chávez
2016/07/14 16:29:44
DCHECK_CURRENTLY_ON(content::BrowserThread::FILE);
Sergey Poromov
2016/07/19 16:32:09
Done.
Sergey Poromov
2016/07/19 16:32:09
Done.
| |
81 ash::WmShell::Get()->delegate()->OpenUrl(gurl); | |
82 } | |
83 | |
84 } // namespace arc | |
OLD | NEW |