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

Side by Side Diff: pdf/file_writer.cc

Issue 317803005: Add function to document_loader to get PDF as pp::FileRef (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 6 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2014 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 "pdf/file_writer.h"
6
7 #include "ppapi/c/ppb_file_io.h"
8
9 namespace chrome_pdf {
10
11 FileWriter::FileWriter(
12 const pp::InstanceHandle& instance,
13 const std::string& filename,
14 const char* bytes,
15 size_t length)
16 : instance_(instance),
17 filename_(filename),
18 bytes_(bytes),
19 length_(length),
20 succeeded_(false),
21 offset_(0),
22 fs_(instance, PP_FILESYSTEMTYPE_ISOLATED),
23 io_(instance),
24 callback_factory_(this) {
25 }
26
27 void FileWriter::GetFile(
28 pp::CompletionCallbackWithOutput<pp::FileRef> callback) {
29 if (callback_) {
30 callback.Run(PP_ERROR_INPROGRESS);
31 return;
32 }
33 if (succeeded_) {
34 *(callback.output()) = file_ref_.pp_resource();
35 callback.Run(PP_OK);
36 return;
37 }
38
39 callback_.reset(new pp::CompletionCallbackWithOutput<pp::FileRef>(callback));
40 fs_.Open(length_,
41 callback_factory_.NewCallback(&FileWriter::OnFileSystemOpen));
42 }
43
44 void FileWriter::OnFileSystemOpen(int32_t result) {
45 if (result != PP_OK) {
46 callback_->Run(result);
47 callback_.reset();
48 return;
49 }
50 file_ref_ = pp::FileRef(fs_, filename_.c_str());
51 io_.Open(file_ref_,
52 PP_FILEOPENFLAG_CREATE | PP_FILEOPENFLAG_WRITE,
53 callback_factory_.NewCallback(&FileWriter::OnWriteComplete));
54 }
55
56 void FileWriter::OnWriteComplete(int32_t result) {
57 if (result != PP_OK) {
58 callback_->Run(result);
Lei Zhang 2014/06/05 07:23:54 Do you need to call io_.Close() here? Same with th
raymes 2014/06/06 01:57:13 Done.
59 callback_.reset();
60 return;
61 }
62 if (offset_ == length_) {
63 io_.Flush(callback_factory_.NewCallback(&FileWriter::OnFlushComplete));
64 return;
65 }
66 offset_ += io_.Write(
67 offset_,
68 bytes_,
69 length_ - offset_,
70 callback_factory_.NewCallback(&FileWriter::OnWriteComplete));
71 }
72
73 void FileWriter::OnFlushComplete(int32_t result) {
74 if (result != PP_OK) {
75 callback_->Run(result);
76 callback_.reset();
77 return;
78 }
79
80 succeeded_ = true;
81 io_.Close();
82 *(callback_->output()) = file_ref_.pp_resource();
83 callback_->Run(result);
84 callback_.reset();
85 }
86
87 } // namespace chrome_pdf
OLDNEW
« pdf/file_writer.h ('K') | « pdf/file_writer.h ('k') | pdf/pdf.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698