Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(315)

Side by Side Diff: chrome/browser/chromeos/arc/arc_print_service.cc

Issue 2115863002: Stub for ARC print Bridge (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/optional.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/threading/thread_checker.h"
19 #include "components/arc/arc_bridge_service.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "mojo/edk/embedder/embedder.h"
22 #include "net/base/filename_util.h"
23 #include "url/gurl.h"
24
25 namespace {
26
27 class ScopedTempFile {
28 public:
29 ScopedTempFile() { base::CreateTemporaryFile(&file_path_); }
30 ~ScopedTempFile() {
31 // TODO(poromov) Delete file after printing. (http://crbug.com/629843)
32 }
33
34 const base::FilePath& file_path() const { return file_path_; }
35
36 private:
37 base::FilePath file_path_;
38 };
39
40 base::Optional<ScopedTempFile> SavePdf(base::ScopedFD fd) {
dcheng 2016/07/27 16:43:36 It doesn't seem like there's any need for ScopedTe
Sergey Poromov 2016/07/27 18:24:41 lhchavez@ suggested it to avoid trashing /tmp afte
dcheng 2016/07/28 01:38:09 It's not built out, and there's no obvious need fo
Sergey Poromov 2016/07/28 14:43:00 Done.
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 ssize_t bytes;
49 while ((bytes = HANDLE_EINTR(read(fd.get(), buf, sizeof(buf)))) > 0) {
50 int written = out.WriteAtCurrentPos(buf, bytes);
51 if (written < 0) {
52 LOG(ERROR) << "Error while saving PDF to a disk";
53 return base::nullopt;
54 }
55 }
56
57 return base::make_optional(temp_file);
58 }
59
60 void OpenPdf(base::Optional<ScopedTempFile> file) {
61 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
62 if (!file)
63 return;
64
65 GURL gurl = net::FilePathToFileURL(file.value().file_path());
66 ash::WmShell::Get()->delegate()->OpenUrl(gurl);
67 }
68
69 } // namespace
70
71 namespace arc {
72
73 ArcPrintService::ArcPrintService(ArcBridgeService* bridge_service)
74 : ArcService(bridge_service), binding_(this) {
75 arc_bridge_service()->print()->AddObserver(this);
76 }
77
78 ArcPrintService::~ArcPrintService() {
79 arc_bridge_service()->print()->RemoveObserver(this);
80 }
81
82 void ArcPrintService::OnInstanceReady() {
83 mojom::PrintInstance* print_instance =
84 arc_bridge_service()->print()->instance();
85 if (!print_instance) {
86 LOG(ERROR) << "OnPrintInstanceReady called, but no print instance found";
87 return;
88 }
89
90 print_instance->Init(binding_.CreateInterfacePtrAndBind());
91 }
92
93 void ArcPrintService::Print(mojo::ScopedHandle pdf_data) {
94 if (!pdf_data.is_valid()) {
95 LOG(ERROR) << "handle is invalid";
96 return;
97 }
98
99 mojo::edk::ScopedPlatformHandle scoped_platform_handle;
100 MojoResult mojo_result = mojo::edk::PassWrappedPlatformHandle(
101 pdf_data.release().value(), &scoped_platform_handle);
102 if (mojo_result != MOJO_RESULT_OK) {
103 LOG(ERROR) << "PassWrappedPlatformHandle failed: " << mojo_result;
104 return;
105 }
106
107 base::ScopedFD fd(scoped_platform_handle.release().handle);
dcheng 2016/07/27 16:43:36 I would suggest just creating a base::File from th
Sergey Poromov 2016/07/27 18:24:41 Done.
108
109 content::BrowserThread::PostTaskAndReplyWithResult(
110 content::BrowserThread::FILE, FROM_HERE,
111 base::Bind(&SavePdf, base::Passed(&fd)), base::Bind(&OpenPdf));
112 }
113
114 } // namespace arc
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/arc/arc_print_service.h ('k') | chrome/browser/chromeos/arc/arc_service_launcher.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698