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); | |
Lei Zhang
2016/07/29 21:40:22
Check for failure?
| |
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; | |
Lei Zhang
2016/07/29 21:40:22
Can't you just return an empty FilePath to indicat
| |
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()->OpenUrl(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()->instance(); | |
71 if (!print_instance) { | |
72 LOG(ERROR) << "OnPrintInstanceReady called, but no print instance found"; | |
73 return; | |
74 } | |
75 | |
76 print_instance->Init(binding_.CreateInterfacePtrAndBind()); | |
77 } | |
78 | |
79 void ArcPrintService::Print(mojo::ScopedHandle pdf_data) { | |
80 if (!pdf_data.is_valid()) { | |
81 LOG(ERROR) << "handle is invalid"; | |
82 return; | |
83 } | |
84 | |
85 mojo::edk::ScopedPlatformHandle scoped_platform_handle; | |
86 MojoResult mojo_result = mojo::edk::PassWrappedPlatformHandle( | |
87 pdf_data.release().value(), &scoped_platform_handle); | |
88 if (mojo_result != MOJO_RESULT_OK) { | |
89 LOG(ERROR) << "PassWrappedPlatformHandle failed: " << mojo_result; | |
90 return; | |
91 } | |
92 | |
93 base::File file(scoped_platform_handle.release().handle); | |
94 | |
95 content::BrowserThread::PostTaskAndReplyWithResult( | |
96 content::BrowserThread::FILE, FROM_HERE, | |
97 base::Bind(&SavePdf, base::Passed(&file)), base::Bind(&OpenPdf)); | |
98 } | |
99 | |
100 } // namespace arc | |
OLD | NEW |