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

Side by Side Diff: examples/pdf_viewer/pdf_viewer.cc

Issue 1157783002: Update to newer network service implementation and mojoms from monet (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 7 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
« no previous file with comments | « examples/pdf_viewer/BUILD.gn ('k') | examples/png_viewer/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/strings/string_tokenizer.h" 5 #include <string>
6
6 #include "examples/bitmap_uploader/bitmap_uploader.h" 7 #include "examples/bitmap_uploader/bitmap_uploader.h"
7 #include "mojo/application/application_runner_chromium.h" 8 #include "mojo/application/application_runner_chromium.h"
8 #include "mojo/application/content_handler_factory.h" 9 #include "mojo/application/content_handler_factory.h"
10 #include "mojo/common/data_pipe_utils.h"
9 #include "mojo/public/c/system/main.h" 11 #include "mojo/public/c/system/main.h"
10 #include "mojo/public/cpp/application/application_connection.h" 12 #include "mojo/public/cpp/application/application_connection.h"
11 #include "mojo/public/cpp/application/application_delegate.h" 13 #include "mojo/public/cpp/application/application_delegate.h"
12 #include "mojo/public/cpp/application/application_impl.h" 14 #include "mojo/public/cpp/application/application_impl.h"
13 #include "mojo/public/cpp/application/interface_factory_impl.h" 15 #include "mojo/public/cpp/application/interface_factory_impl.h"
14 #include "mojo/public/cpp/application/service_provider_impl.h" 16 #include "mojo/public/cpp/application/service_provider_impl.h"
15 #include "mojo/services/content_handler/public/interfaces/content_handler.mojom. h" 17 #include "mojo/services/content_handler/public/interfaces/content_handler.mojom. h"
16 #include "mojo/services/input_events/public/interfaces/input_events.mojom.h" 18 #include "mojo/services/input_events/public/interfaces/input_events.mojom.h"
17 #include "mojo/services/input_events/public/interfaces/input_key_codes.mojom.h" 19 #include "mojo/services/input_events/public/interfaces/input_key_codes.mojom.h"
18 #include "mojo/services/view_manager/public/cpp/types.h" 20 #include "mojo/services/view_manager/public/cpp/types.h"
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 FPDF_RenderPageBitmap(f_bitmap, page, 0, 0, width, height, 0, 0); 155 FPDF_RenderPageBitmap(f_bitmap, page, 0, 0, width, height, 0, 0);
154 FPDFBitmap_Destroy(f_bitmap); 156 FPDFBitmap_Destroy(f_bitmap);
155 157
156 FPDF_ClosePage(page); 158 FPDF_ClosePage(page);
157 159
158 embedder_data->bitmap_uploader().SetBitmap(width, height, bitmap.Pass(), 160 embedder_data->bitmap_uploader().SetBitmap(width, height, bitmap.Pass(),
159 BitmapUploader::BGRA); 161 BitmapUploader::BGRA);
160 } 162 }
161 163
162 void FetchPDF(URLResponsePtr response) { 164 void FetchPDF(URLResponsePtr response) {
163 int content_length = GetContentLength(response->headers); 165 data_.clear();
164 scoped_ptr<unsigned char[]> data; 166 mojo::common::BlockingCopyToString(response->body.Pass(), &data_);
165 data_.reset(new unsigned char[content_length]); 167 if (data_.length() >= static_cast<size_t>(std::numeric_limits<int>::max()))
166 unsigned char* buf = data_.get(); 168 return;
167 uint32_t bytes_remaining = content_length; 169 doc_ = FPDF_LoadMemDocument(data_.data(), static_cast<int>(data_.length()),
168 uint32_t num_bytes = bytes_remaining; 170 nullptr);
169 while (bytes_remaining > 0) {
170 MojoResult result = ReadDataRaw(response->body.get(), buf, &num_bytes,
171 MOJO_READ_DATA_FLAG_NONE);
172 if (result == MOJO_RESULT_SHOULD_WAIT) {
173 Wait(response->body.get(), MOJO_HANDLE_SIGNAL_READABLE,
174 MOJO_DEADLINE_INDEFINITE, nullptr);
175 } else if (result == MOJO_RESULT_OK) {
176 buf += num_bytes;
177 num_bytes = bytes_remaining -= num_bytes;
178 } else {
179 break;
180 }
181 }
182
183 doc_ = FPDF_LoadMemDocument(data_.get(), content_length, NULL);
184 page_count_ = FPDF_GetPageCount(doc_); 171 page_count_ = FPDF_GetPageCount(doc_);
185 } 172 }
186 173
187 int GetContentLength(const Array<String>& headers) { 174 std::string data_;
188 for (size_t i = 0; i < headers.size(); ++i) {
189 base::StringTokenizer t(headers[i], ": ;=");
190 while (t.GetNext()) {
191 if (!t.token_is_delim() && t.token() == "Content-Length") {
192 while (t.GetNext()) {
193 if (!t.token_is_delim())
194 return atoi(t.token().c_str());
195 }
196 }
197 }
198 }
199 return 0;
200 }
201
202 scoped_ptr<unsigned char[]> data_;
203 int current_page_; 175 int current_page_;
204 int page_count_; 176 int page_count_;
205 FPDF_DOCUMENT doc_; 177 FPDF_DOCUMENT doc_;
206 ApplicationImpl* app_; 178 ApplicationImpl* app_;
207 std::map<View*, EmbedderData*> embedder_for_roots_; 179 std::map<View*, EmbedderData*> embedder_for_roots_;
208 scoped_ptr<ViewManagerClientFactory> view_manager_client_factory_; 180 scoped_ptr<ViewManagerClientFactory> view_manager_client_factory_;
209 181
210 DISALLOW_COPY_AND_ASSIGN(PDFView); 182 DISALLOW_COPY_AND_ASSIGN(PDFView);
211 }; 183 };
212 184
(...skipping 27 matching lines...) Expand all
240 DISALLOW_COPY_AND_ASSIGN(PDFViewer); 212 DISALLOW_COPY_AND_ASSIGN(PDFViewer);
241 }; 213 };
242 214
243 } // namespace examples 215 } // namespace examples
244 } // namespace mojo 216 } // namespace mojo
245 217
246 MojoResult MojoMain(MojoHandle application_request) { 218 MojoResult MojoMain(MojoHandle application_request) {
247 mojo::ApplicationRunnerChromium runner(new mojo::examples::PDFViewer()); 219 mojo::ApplicationRunnerChromium runner(new mojo::examples::PDFViewer());
248 return runner.Run(application_request); 220 return runner.Run(application_request);
249 } 221 }
OLDNEW
« no previous file with comments | « examples/pdf_viewer/BUILD.gn ('k') | examples/png_viewer/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698