OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/bind.h" | 5 #include "base/bind.h" |
6 #include "base/callback.h" | 6 #include "base/callback.h" |
7 #include "base/containers/hash_tables.h" | 7 #include "base/containers/hash_tables.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "components/mus/public/cpp/types.h" | 9 #include "components/mus/public/cpp/types.h" |
10 #include "components/mus/public/cpp/view.h" | 10 #include "components/mus/public/cpp/view.h" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 | 51 |
52 void LostContext(void*) { | 52 void LostContext(void*) { |
53 DCHECK(false); | 53 DCHECK(false); |
54 } | 54 } |
55 | 55 |
56 void OnGotContentHandlerID(uint32_t content_handler_id) {} | 56 void OnGotContentHandlerID(uint32_t content_handler_id) {} |
57 | 57 |
58 // BitmapUploader is useful if you want to draw a bitmap or color in a View. | 58 // BitmapUploader is useful if you want to draw a bitmap or color in a View. |
59 class BitmapUploader : public mojo::SurfaceClient { | 59 class BitmapUploader : public mojo::SurfaceClient { |
60 public: | 60 public: |
61 explicit BitmapUploader(mus::View* view) | 61 explicit BitmapUploader(mojo::View* view) |
62 : view_(view), | 62 : view_(view), |
63 color_(g_transparent_color), | 63 color_(g_transparent_color), |
64 width_(0), | 64 width_(0), |
65 height_(0), | 65 height_(0), |
66 format_(BGRA), | 66 format_(BGRA), |
67 next_resource_id_(1u), | 67 next_resource_id_(1u), |
68 id_namespace_(0u), | 68 id_namespace_(0u), |
69 local_id_(0u), | 69 local_id_(0u), |
70 returner_binding_(this) {} | 70 returner_binding_(this) { |
| 71 } |
71 ~BitmapUploader() override { | 72 ~BitmapUploader() override { |
72 MojoGLES2DestroyContext(gles2_context_); | 73 MojoGLES2DestroyContext(gles2_context_); |
73 } | 74 } |
74 | 75 |
75 void Init(mojo::Shell* shell) { | 76 void Init(mojo::Shell* shell) { |
76 surface_ = view_->RequestSurface(); | 77 surface_ = view_->RequestSurface(); |
77 surface_->BindToThread(); | 78 surface_->BindToThread(); |
78 | 79 |
79 mojo::ServiceProviderPtr gpu_service_provider; | 80 mojo::ServiceProviderPtr gpu_service_provider; |
80 mojo::URLRequestPtr request2(mojo::URLRequest::New()); | 81 mojo::URLRequestPtr request2(mojo::URLRequest::New()); |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 mojo::ReturnedResourcePtr resource = resources[i].Pass(); | 282 mojo::ReturnedResourcePtr resource = resources[i].Pass(); |
282 DCHECK_EQ(1, resource->count); | 283 DCHECK_EQ(1, resource->count); |
283 glWaitSyncPointCHROMIUM(resource->sync_point); | 284 glWaitSyncPointCHROMIUM(resource->sync_point); |
284 uint32_t texture_id = resource_to_texture_id_map_[resource->id]; | 285 uint32_t texture_id = resource_to_texture_id_map_[resource->id]; |
285 DCHECK_NE(0u, texture_id); | 286 DCHECK_NE(0u, texture_id); |
286 resource_to_texture_id_map_.erase(resource->id); | 287 resource_to_texture_id_map_.erase(resource->id); |
287 glDeleteTextures(1, &texture_id); | 288 glDeleteTextures(1, &texture_id); |
288 } | 289 } |
289 } | 290 } |
290 | 291 |
291 mus::View* view_; | 292 mojo::View* view_; |
292 mojo::GpuPtr gpu_service_; | 293 mojo::GpuPtr gpu_service_; |
293 scoped_ptr<mus::ViewSurface> surface_; | 294 scoped_ptr<mojo::ViewSurface> surface_; |
294 MojoGLES2Context gles2_context_; | 295 MojoGLES2Context gles2_context_; |
295 | 296 |
296 mojo::Size size_; | 297 mojo::Size size_; |
297 uint32_t color_; | 298 uint32_t color_; |
298 int width_; | 299 int width_; |
299 int height_; | 300 int height_; |
300 Format format_; | 301 Format format_; |
301 scoped_ptr<std::vector<unsigned char>> bitmap_; | 302 scoped_ptr<std::vector<unsigned char>> bitmap_; |
302 uint32_t next_resource_id_; | 303 uint32_t next_resource_id_; |
303 uint32_t id_namespace_; | 304 uint32_t id_namespace_; |
304 uint32_t local_id_; | 305 uint32_t local_id_; |
305 base::hash_map<uint32_t, uint32_t> resource_to_texture_id_map_; | 306 base::hash_map<uint32_t, uint32_t> resource_to_texture_id_map_; |
306 mojo::Binding<mojo::SurfaceClient> returner_binding_; | 307 mojo::Binding<mojo::SurfaceClient> returner_binding_; |
307 | 308 |
308 DISALLOW_COPY_AND_ASSIGN(BitmapUploader); | 309 DISALLOW_COPY_AND_ASSIGN(BitmapUploader); |
309 }; | 310 }; |
310 | 311 |
311 class EmbedderData { | 312 class EmbedderData { |
312 public: | 313 public: |
313 EmbedderData(mojo::Shell* shell, mus::View* root) : bitmap_uploader_(root) { | 314 EmbedderData(mojo::Shell* shell, mojo::View* root) : bitmap_uploader_(root) { |
314 bitmap_uploader_.Init(shell); | 315 bitmap_uploader_.Init(shell); |
315 bitmap_uploader_.SetColor(g_background_color); | 316 bitmap_uploader_.SetColor(g_background_color); |
316 } | 317 } |
317 | 318 |
318 BitmapUploader& bitmap_uploader() { return bitmap_uploader_; } | 319 BitmapUploader& bitmap_uploader() { return bitmap_uploader_; } |
319 | 320 |
320 private: | 321 private: |
321 BitmapUploader bitmap_uploader_; | 322 BitmapUploader bitmap_uploader_; |
322 | 323 |
323 DISALLOW_COPY_AND_ASSIGN(EmbedderData); | 324 DISALLOW_COPY_AND_ASSIGN(EmbedderData); |
324 }; | 325 }; |
325 | 326 |
326 class PDFView : public mojo::ApplicationDelegate, | 327 class PDFView : public mojo::ApplicationDelegate, |
327 public mus::ViewTreeDelegate, | 328 public mojo::ViewTreeDelegate, |
328 public mus::ViewObserver, | 329 public mojo::ViewObserver, |
329 public mojo::InterfaceFactory<mojo::ViewTreeClient> { | 330 public mojo::InterfaceFactory<mojo::ViewTreeClient> { |
330 public: | 331 public: |
331 PDFView(mojo::InterfaceRequest<mojo::Application> request, | 332 PDFView(mojo::InterfaceRequest<mojo::Application> request, |
332 mojo::URLResponsePtr response) | 333 mojo::URLResponsePtr response) |
333 : app_(this, request.Pass(), base::Bind(&PDFView::OnTerminate, | 334 : app_(this, request.Pass(), base::Bind(&PDFView::OnTerminate, |
334 base::Unretained(this))), | 335 base::Unretained(this))), |
335 current_page_(0), page_count_(0), doc_(nullptr) { | 336 current_page_(0), page_count_(0), doc_(nullptr) { |
336 FetchPDF(response.Pass()); | 337 FetchPDF(response.Pass()); |
337 } | 338 } |
338 | 339 |
339 ~PDFView() override { | 340 ~PDFView() override { |
340 if (doc_) | 341 if (doc_) |
341 FPDF_CloseDocument(doc_); | 342 FPDF_CloseDocument(doc_); |
342 for (auto& roots : embedder_for_roots_) { | 343 for (auto& roots : embedder_for_roots_) { |
343 roots.first->RemoveObserver(this); | 344 roots.first->RemoveObserver(this); |
344 delete roots.second; | 345 delete roots.second; |
345 } | 346 } |
346 } | 347 } |
347 | 348 |
348 private: | 349 private: |
349 // Overridden from ApplicationDelegate: | 350 // Overridden from ApplicationDelegate: |
350 bool ConfigureIncomingConnection( | 351 bool ConfigureIncomingConnection( |
351 mojo::ApplicationConnection* connection) override { | 352 mojo::ApplicationConnection* connection) override { |
352 connection->AddService<mojo::ViewTreeClient>(this); | 353 connection->AddService<mojo::ViewTreeClient>(this); |
353 return true; | 354 return true; |
354 } | 355 } |
355 | 356 |
356 // Overridden from ViewTreeDelegate: | 357 // Overridden from ViewTreeDelegate: |
357 void OnEmbed(mus::View* root) override { | 358 void OnEmbed(mojo::View* root) override { |
358 DCHECK(embedder_for_roots_.find(root) == embedder_for_roots_.end()); | 359 DCHECK(embedder_for_roots_.find(root) == embedder_for_roots_.end()); |
359 root->AddObserver(this); | 360 root->AddObserver(this); |
360 EmbedderData* embedder_data = new EmbedderData(app_.shell(), root); | 361 EmbedderData* embedder_data = new EmbedderData(app_.shell(), root); |
361 embedder_for_roots_[root] = embedder_data; | 362 embedder_for_roots_[root] = embedder_data; |
362 DrawBitmap(embedder_data); | 363 DrawBitmap(embedder_data); |
363 } | 364 } |
364 | 365 |
365 void OnConnectionLost(mus::ViewTreeConnection* connection) override {} | 366 void OnConnectionLost(mojo::ViewTreeConnection* connection) override {} |
366 | 367 |
367 // Overridden from ViewObserver: | 368 // Overridden from ViewObserver: |
368 void OnViewBoundsChanged(mus::View* view, | 369 void OnViewBoundsChanged(mojo::View* view, |
369 const mojo::Rect& old_bounds, | 370 const mojo::Rect& old_bounds, |
370 const mojo::Rect& new_bounds) override { | 371 const mojo::Rect& new_bounds) override { |
371 DCHECK(embedder_for_roots_.find(view) != embedder_for_roots_.end()); | 372 DCHECK(embedder_for_roots_.find(view) != embedder_for_roots_.end()); |
372 DrawBitmap(embedder_for_roots_[view]); | 373 DrawBitmap(embedder_for_roots_[view]); |
373 } | 374 } |
374 | 375 |
375 void OnViewInputEvent(mus::View* view, const mojo::EventPtr& event) override { | 376 void OnViewInputEvent(mojo::View* view, |
| 377 const mojo::EventPtr& event) override { |
376 DCHECK(embedder_for_roots_.find(view) != embedder_for_roots_.end()); | 378 DCHECK(embedder_for_roots_.find(view) != embedder_for_roots_.end()); |
377 if (event->key_data && | 379 if (event->key_data && |
378 (event->action != mojo::EVENT_TYPE_KEY_PRESSED || | 380 (event->action != mojo::EVENT_TYPE_KEY_PRESSED || |
379 event->key_data->is_char)) { | 381 event->key_data->is_char)) { |
380 return; | 382 return; |
381 } | 383 } |
382 | 384 |
383 if ((event->key_data && | 385 if ((event->key_data && |
384 event->key_data->windows_key_code == mojo::KEYBOARD_CODE_DOWN) || | 386 event->key_data->windows_key_code == mojo::KEYBOARD_CODE_DOWN) || |
385 (event->pointer_data && event->pointer_data->vertical_wheel < 0)) { | 387 (event->pointer_data && event->pointer_data->vertical_wheel < 0)) { |
386 if (current_page_ < (page_count_ - 1)) { | 388 if (current_page_ < (page_count_ - 1)) { |
387 current_page_++; | 389 current_page_++; |
388 DrawBitmap(embedder_for_roots_[view]); | 390 DrawBitmap(embedder_for_roots_[view]); |
389 } | 391 } |
390 } else if ((event->key_data && | 392 } else if ((event->key_data && |
391 event->key_data->windows_key_code == mojo::KEYBOARD_CODE_UP) || | 393 event->key_data->windows_key_code == mojo::KEYBOARD_CODE_UP) || |
392 (event->pointer_data && | 394 (event->pointer_data && |
393 event->pointer_data->vertical_wheel > 0)) { | 395 event->pointer_data->vertical_wheel > 0)) { |
394 if (current_page_ > 0) { | 396 if (current_page_ > 0) { |
395 current_page_--; | 397 current_page_--; |
396 DrawBitmap(embedder_for_roots_[view]); | 398 DrawBitmap(embedder_for_roots_[view]); |
397 } | 399 } |
398 } | 400 } |
399 } | 401 } |
400 | 402 |
401 void OnViewDestroyed(mus::View* view) override { | 403 void OnViewDestroyed(mojo::View* view) override { |
402 DCHECK(embedder_for_roots_.find(view) != embedder_for_roots_.end()); | 404 DCHECK(embedder_for_roots_.find(view) != embedder_for_roots_.end()); |
403 const auto& it = embedder_for_roots_.find(view); | 405 const auto& it = embedder_for_roots_.find(view); |
404 DCHECK(it != embedder_for_roots_.end()); | 406 DCHECK(it != embedder_for_roots_.end()); |
405 delete it->second; | 407 delete it->second; |
406 embedder_for_roots_.erase(it); | 408 embedder_for_roots_.erase(it); |
407 if (embedder_for_roots_.size() == 0) | 409 if (embedder_for_roots_.size() == 0) |
408 app_.Quit(); | 410 app_.Quit(); |
409 } | 411 } |
410 | 412 |
411 // Overridden from mojo::InterfaceFactory<mojo::ViewTreeClient>: | 413 // Overridden from mojo::InterfaceFactory<mojo::ViewTreeClient>: |
412 void Create( | 414 void Create( |
413 mojo::ApplicationConnection* connection, | 415 mojo::ApplicationConnection* connection, |
414 mojo::InterfaceRequest<mojo::ViewTreeClient> request) override { | 416 mojo::InterfaceRequest<mojo::ViewTreeClient> request) override { |
415 mus::ViewTreeConnection::Create(this, request.Pass()); | 417 mojo::ViewTreeConnection::Create(this, request.Pass()); |
416 } | 418 } |
417 | 419 |
418 void DrawBitmap(EmbedderData* embedder_data) { | 420 void DrawBitmap(EmbedderData* embedder_data) { |
419 if (!doc_) | 421 if (!doc_) |
420 return; | 422 return; |
421 | 423 |
422 FPDF_PAGE page = FPDF_LoadPage(doc_, current_page_); | 424 FPDF_PAGE page = FPDF_LoadPage(doc_, current_page_); |
423 int width = static_cast<int>(FPDF_GetPageWidth(page)); | 425 int width = static_cast<int>(FPDF_GetPageWidth(page)); |
424 int height = static_cast<int>(FPDF_GetPageHeight(page)); | 426 int height = static_cast<int>(FPDF_GetPageHeight(page)); |
425 | 427 |
(...skipping 28 matching lines...) Expand all Loading... |
454 // when we quit (the messageloop is shared among multiple PDFViews). | 456 // when we quit (the messageloop is shared among multiple PDFViews). |
455 void OnTerminate() { | 457 void OnTerminate() { |
456 delete this; | 458 delete this; |
457 } | 459 } |
458 | 460 |
459 mojo::ApplicationImpl app_; | 461 mojo::ApplicationImpl app_; |
460 std::string data_; | 462 std::string data_; |
461 int current_page_; | 463 int current_page_; |
462 int page_count_; | 464 int page_count_; |
463 FPDF_DOCUMENT doc_; | 465 FPDF_DOCUMENT doc_; |
464 std::map<mus::View*, EmbedderData*> embedder_for_roots_; | 466 std::map<mojo::View*, EmbedderData*> embedder_for_roots_; |
465 | 467 |
466 DISALLOW_COPY_AND_ASSIGN(PDFView); | 468 DISALLOW_COPY_AND_ASSIGN(PDFView); |
467 }; | 469 }; |
468 | 470 |
469 class ContentHandlerImpl : public mojo::ContentHandler { | 471 class ContentHandlerImpl : public mojo::ContentHandler { |
470 public: | 472 public: |
471 ContentHandlerImpl(mojo::InterfaceRequest<ContentHandler> request) | 473 ContentHandlerImpl(mojo::InterfaceRequest<ContentHandler> request) |
472 : binding_(this, request.Pass()) {} | 474 : binding_(this, request.Pass()) {} |
473 ~ContentHandlerImpl() override {} | 475 ~ContentHandlerImpl() override {} |
474 | 476 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
513 DISALLOW_COPY_AND_ASSIGN(PDFViewer); | 515 DISALLOW_COPY_AND_ASSIGN(PDFViewer); |
514 }; | 516 }; |
515 | 517 |
516 } // namespace | 518 } // namespace |
517 } // namespace pdf_viewer | 519 } // namespace pdf_viewer |
518 | 520 |
519 MojoResult MojoMain(MojoHandle application_request) { | 521 MojoResult MojoMain(MojoHandle application_request) { |
520 mojo::ApplicationRunner runner(new pdf_viewer::PDFViewer()); | 522 mojo::ApplicationRunner runner(new pdf_viewer::PDFViewer()); |
521 return runner.Run(application_request); | 523 return runner.Run(application_request); |
522 } | 524 } |
OLD | NEW |