Index: examples/pdf_viewer/pdf_viewer.cc |
diff --git a/examples/pdf_viewer/pdf_viewer.cc b/examples/pdf_viewer/pdf_viewer.cc |
index 0e6a72cc396f822f24d06c35524ab4173562498d..21e38d7cfacb10c658fce76c69416af36cfae10b 100644 |
--- a/examples/pdf_viewer/pdf_viewer.cc |
+++ b/examples/pdf_viewer/pdf_viewer.cc |
@@ -2,10 +2,12 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "base/strings/string_tokenizer.h" |
+#include <string> |
+ |
#include "examples/bitmap_uploader/bitmap_uploader.h" |
#include "mojo/application/application_runner_chromium.h" |
#include "mojo/application/content_handler_factory.h" |
+#include "mojo/common/data_pipe_utils.h" |
#include "mojo/public/c/system/main.h" |
#include "mojo/public/cpp/application/application_connection.h" |
#include "mojo/public/cpp/application/application_delegate.h" |
@@ -160,46 +162,16 @@ class PDFView : public ApplicationDelegate, |
} |
void FetchPDF(URLResponsePtr response) { |
- int content_length = GetContentLength(response->headers); |
- scoped_ptr<unsigned char[]> data; |
- data_.reset(new unsigned char[content_length]); |
- unsigned char* buf = data_.get(); |
- uint32_t bytes_remaining = content_length; |
- uint32_t num_bytes = bytes_remaining; |
- while (bytes_remaining > 0) { |
- MojoResult result = ReadDataRaw(response->body.get(), buf, &num_bytes, |
- MOJO_READ_DATA_FLAG_NONE); |
- if (result == MOJO_RESULT_SHOULD_WAIT) { |
- Wait(response->body.get(), MOJO_HANDLE_SIGNAL_READABLE, |
- MOJO_DEADLINE_INDEFINITE, nullptr); |
- } else if (result == MOJO_RESULT_OK) { |
- buf += num_bytes; |
- num_bytes = bytes_remaining -= num_bytes; |
- } else { |
- break; |
- } |
- } |
- |
- doc_ = FPDF_LoadMemDocument(data_.get(), content_length, NULL); |
+ data_.clear(); |
+ mojo::common::BlockingCopyToString(response->body.Pass(), &data_); |
+ if (data_.length() >= static_cast<size_t>(std::numeric_limits<int>::max())) |
+ return; |
+ doc_ = FPDF_LoadMemDocument(data_.data(), static_cast<int>(data_.length()), |
+ nullptr); |
page_count_ = FPDF_GetPageCount(doc_); |
} |
- int GetContentLength(const Array<String>& headers) { |
- for (size_t i = 0; i < headers.size(); ++i) { |
- base::StringTokenizer t(headers[i], ": ;="); |
- while (t.GetNext()) { |
- if (!t.token_is_delim() && t.token() == "Content-Length") { |
- while (t.GetNext()) { |
- if (!t.token_is_delim()) |
- return atoi(t.token().c_str()); |
- } |
- } |
- } |
- } |
- return 0; |
- } |
- |
- scoped_ptr<unsigned char[]> data_; |
+ std::string data_; |
int current_page_; |
int page_count_; |
FPDF_DOCUMENT doc_; |