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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pdf/file_writer.h ('k') | pdf/pdf.gyp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pdf/file_writer.cc
diff --git a/pdf/file_writer.cc b/pdf/file_writer.cc
new file mode 100644
index 0000000000000000000000000000000000000000..b9514c7bf27594ef7e37f809dbe85119e53dd453
--- /dev/null
+++ b/pdf/file_writer.cc
@@ -0,0 +1,89 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "pdf/file_writer.h"
+
+#include "ppapi/c/ppb_file_io.h"
+
+namespace chrome_pdf {
+
+FileWriter::FileWriter(
+ const pp::InstanceHandle& instance,
+ const std::string& filename,
+ const char* bytes,
+ size_t length)
+ : instance_(instance),
+ filename_(filename),
+ bytes_(bytes),
+ length_(length),
+ succeeded_(false),
+ offset_(0),
+ fs_(instance, PP_FILESYSTEMTYPE_ISOLATED),
+ io_(instance),
+ callback_factory_(this) {
+}
+
+void FileWriter::GetFile(
+ pp::CompletionCallbackWithOutput<pp::FileRef> callback) {
+ if (callback_) {
+ callback.Run(PP_ERROR_INPROGRESS);
+ return;
+ }
+ if (succeeded_) {
+ *(callback.output()) = file_ref_.pp_resource();
+ callback.Run(PP_OK);
+ return;
+ }
+
+ callback_.reset(new pp::CompletionCallbackWithOutput<pp::FileRef>(callback));
+ fs_.Open(length_,
+ callback_factory_.NewCallback(&FileWriter::OnFileSystemOpen));
+}
+
+void FileWriter::OnFileSystemOpen(int32_t result) {
+ if (result != PP_OK) {
+ callback_->Run(result);
+ callback_.reset();
+ return;
+ }
+ file_ref_ = pp::FileRef(fs_, filename_.c_str());
+ io_.Open(file_ref_,
+ PP_FILEOPENFLAG_CREATE | PP_FILEOPENFLAG_WRITE,
+ callback_factory_.NewCallback(&FileWriter::OnWriteComplete));
+}
+
+void FileWriter::OnWriteComplete(int32_t result) {
+ if (result != PP_OK) {
+ io_.close();
+ callback_->Run(result);
+ callback_.reset();
+ return;
+ }
+ if (offset_ == length_) {
+ io_.Flush(callback_factory_.NewCallback(&FileWriter::OnFlushComplete));
+ return;
+ }
+ offset_ += io_.Write(
+ offset_,
+ bytes_,
+ length_ - offset_,
+ callback_factory_.NewCallback(&FileWriter::OnWriteComplete));
+}
+
+void FileWriter::OnFlushComplete(int32_t result) {
+ if (result != PP_OK) {
+ io_.close();
+ callback_->Run(result);
+ callback_.reset();
+ return;
+ }
+
+ succeeded_ = true;
+ io_.Close();
+ *(callback_->output()) = file_ref_.pp_resource();
+ callback_->Run(result);
+ callback_.reset();
+}
+
+} // namespace chrome_pdf
« 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