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/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. (http://crbug.com/629843) | |
31 } | |
32 | |
33 const base::FilePath& file_path() const { return file_path_; } | |
34 | |
35 private: | |
36 base::FilePath file_path_; | |
37 }; | |
38 | |
39 ScopedTempFile SavePdf(base::ScopedFD fd) { | |
40 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE); | |
41 | |
42 ScopedTempFile temp_file; | |
43 base::File out(temp_file.file_path(), | |
44 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); | |
45 | |
46 char buf[8192]; | |
47 ssize_t bytes; | |
48 while ((bytes = HANDLE_EINTR(read(fd.get(), buf, sizeof(buf)))) > 0) { | |
dcheng
2016/07/21 02:15:22
I would suggest using ReadFromFd.
Sergey Poromov
2016/07/21 13:26:03
As I understand there is no way to know how many b
| |
49 out.WriteAtCurrentPos(buf, bytes); | |
dcheng
2016/07/21 02:15:22
Also, we should probably be checking for errors
Sergey Poromov
2016/07/21 13:26:02
Done.
| |
50 } | |
51 | |
52 return temp_file; | |
53 } | |
54 | |
55 void OpenPdf(ScopedTempFile file) { | |
56 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
57 | |
58 GURL gurl(std::string(url::kFileScheme) + url::kStandardSchemeSeparator + | |
dcheng
2016/07/21 02:15:22
Please use net::FilePathToFileURL
Sergey Poromov
2016/07/21 13:26:03
Done.
| |
59 file.file_path().value()); | |
60 ash::WmShell::Get()->delegate()->OpenUrl(gurl); | |
61 } | |
62 | |
63 } // namespace | |
64 | |
65 namespace arc { | |
66 | |
67 ArcPrintService::ArcPrintService(ArcBridgeService* bridge_service) | |
68 : ArcService(bridge_service), binding_(this) { | |
69 arc_bridge_service()->print()->AddObserver(this); | |
70 } | |
71 | |
72 ArcPrintService::~ArcPrintService() { | |
73 arc_bridge_service()->print()->RemoveObserver(this); | |
74 } | |
75 | |
76 void ArcPrintService::OnInstanceReady() { | |
77 mojom::PrintInstance* print_instance = | |
78 arc_bridge_service()->print()->instance(); | |
79 if (!print_instance) { | |
80 LOG(ERROR) << "OnPrintInstanceReady called, but no print instance found"; | |
81 return; | |
82 } | |
83 | |
84 print_instance->Init(binding_.CreateInterfacePtrAndBind()); | |
85 } | |
86 | |
87 void ArcPrintService::Print(mojo::ScopedHandle file) { | |
88 if (!file.is_valid()) { | |
89 LOG(ERROR) << "handle is invalid"; | |
90 return; | |
91 } | |
92 | |
93 mojo::edk::ScopedPlatformHandle scoped_platform_handle; | |
94 MojoResult mojo_result = mojo::edk::PassWrappedPlatformHandle( | |
95 file.release().value(), &scoped_platform_handle); | |
96 if (mojo_result != MOJO_RESULT_OK) { | |
97 LOG(ERROR) << "PassWrappedPlatformHandle failed: " << mojo_result; | |
98 return; | |
99 } | |
100 | |
101 base::ScopedFD fd(scoped_platform_handle.release().handle); | |
102 | |
103 content::BrowserThread::PostTaskAndReplyWithResult( | |
104 content::BrowserThread::FILE, FROM_HERE, | |
105 base::Bind(&SavePdf, base::Passed(&fd)), base::Bind(&OpenPdf)); | |
106 } | |
107 | |
108 } // namespace arc | |
OLD | NEW |