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

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
« no previous file with comments | « pdf/file_writer.h ('k') | pdf/pdf.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 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 io_.close();
59 callback_->Run(result);
60 callback_.reset();
61 return;
62 }
63 if (offset_ == length_) {
64 io_.Flush(callback_factory_.NewCallback(&FileWriter::OnFlushComplete));
65 return;
66 }
67 offset_ += io_.Write(
68 offset_,
69 bytes_,
70 length_ - offset_,
71 callback_factory_.NewCallback(&FileWriter::OnWriteComplete));
72 }
73
74 void FileWriter::OnFlushComplete(int32_t result) {
75 if (result != PP_OK) {
76 io_.close();
77 callback_->Run(result);
78 callback_.reset();
79 return;
80 }
81
82 succeeded_ = true;
83 io_.Close();
84 *(callback_->output()) = file_ref_.pp_resource();
85 callback_->Run(result);
86 callback_.reset();
87 }
88
89 } // namespace chrome_pdf
OLDNEW
« no previous file with comments | « pdf/file_writer.h ('k') | pdf/pdf.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698