| 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/scoped_window_ptr.h" | 9 #include "components/mus/public/cpp/scoped_window_ptr.h" |
| 10 #include "components/mus/public/cpp/types.h" | 10 #include "components/mus/public/cpp/types.h" |
| (...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 502 DISALLOW_COPY_AND_ASSIGN(PDFView); | 502 DISALLOW_COPY_AND_ASSIGN(PDFView); |
| 503 }; | 503 }; |
| 504 | 504 |
| 505 // Responsible for managing all the views for displaying a PDF document. | 505 // Responsible for managing all the views for displaying a PDF document. |
| 506 class PDFViewerApplicationDelegate | 506 class PDFViewerApplicationDelegate |
| 507 : public mojo::ApplicationDelegate, | 507 : public mojo::ApplicationDelegate, |
| 508 public mojo::InterfaceFactory<mus::mojom::WindowTreeClient> { | 508 public mojo::InterfaceFactory<mus::mojom::WindowTreeClient> { |
| 509 public: | 509 public: |
| 510 PDFViewerApplicationDelegate( | 510 PDFViewerApplicationDelegate( |
| 511 mojo::InterfaceRequest<mojo::Application> request, | 511 mojo::InterfaceRequest<mojo::Application> request, |
| 512 mojo::URLResponsePtr response) | 512 mojo::URLResponsePtr response, |
| 513 const mojo::Callback<void()>& destruct_callback) |
| 513 : app_(this, | 514 : app_(this, |
| 514 request.Pass(), | 515 request.Pass(), |
| 515 base::Bind(&PDFViewerApplicationDelegate::OnTerminate, | 516 base::Bind(&PDFViewerApplicationDelegate::OnTerminate, |
| 516 base::Unretained(this))), | 517 base::Unretained(this))), |
| 517 doc_(nullptr), | 518 doc_(nullptr), |
| 518 is_destroying_(false) { | 519 is_destroying_(false), |
| 520 destruct_callback_(destruct_callback) { |
| 519 FetchPDF(response.Pass()); | 521 FetchPDF(response.Pass()); |
| 520 } | 522 } |
| 521 | 523 |
| 522 ~PDFViewerApplicationDelegate() override { | 524 ~PDFViewerApplicationDelegate() override { |
| 523 is_destroying_ = true; | 525 is_destroying_ = true; |
| 524 if (doc_) | 526 if (doc_) |
| 525 FPDF_CloseDocument(doc_); | 527 FPDF_CloseDocument(doc_); |
| 526 while (!pdf_views_.empty()) | 528 while (!pdf_views_.empty()) |
| 527 pdf_views_.front()->Close(); | 529 pdf_views_.front()->Close(); |
| 530 destruct_callback_.Run(); |
| 528 } | 531 } |
| 529 | 532 |
| 530 private: | 533 private: |
| 531 void FetchPDF(mojo::URLResponsePtr response) { | 534 void FetchPDF(mojo::URLResponsePtr response) { |
| 532 data_.clear(); | 535 data_.clear(); |
| 533 mojo::common::BlockingCopyToString(response->body.Pass(), &data_); | 536 mojo::common::BlockingCopyToString(response->body.Pass(), &data_); |
| 534 if (data_.length() >= static_cast<size_t>(std::numeric_limits<int>::max())) | 537 if (data_.length() >= static_cast<size_t>(std::numeric_limits<int>::max())) |
| 535 return; | 538 return; |
| 536 doc_ = FPDF_LoadMemDocument(data_.data(), static_cast<int>(data_.length()), | 539 doc_ = FPDF_LoadMemDocument(data_.data(), static_cast<int>(data_.length()), |
| 537 nullptr); | 540 nullptr); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 567 mus::WindowTreeConnection::Create( | 570 mus::WindowTreeConnection::Create( |
| 568 pdf_view, request.Pass(), | 571 pdf_view, request.Pass(), |
| 569 mus::WindowTreeConnection::CreateType::DONT_WAIT_FOR_EMBED); | 572 mus::WindowTreeConnection::CreateType::DONT_WAIT_FOR_EMBED); |
| 570 } | 573 } |
| 571 | 574 |
| 572 mojo::ApplicationImpl app_; | 575 mojo::ApplicationImpl app_; |
| 573 std::string data_; | 576 std::string data_; |
| 574 std::vector<PDFView*> pdf_views_; | 577 std::vector<PDFView*> pdf_views_; |
| 575 FPDF_DOCUMENT doc_; | 578 FPDF_DOCUMENT doc_; |
| 576 bool is_destroying_; | 579 bool is_destroying_; |
| 580 mojo::Callback<void()> destruct_callback_; |
| 577 | 581 |
| 578 DISALLOW_COPY_AND_ASSIGN(PDFViewerApplicationDelegate); | 582 DISALLOW_COPY_AND_ASSIGN(PDFViewerApplicationDelegate); |
| 579 }; | 583 }; |
| 580 | 584 |
| 581 class ContentHandlerImpl : public mojo::ContentHandler { | 585 class ContentHandlerImpl : public mojo::ContentHandler { |
| 582 public: | 586 public: |
| 583 ContentHandlerImpl(mojo::InterfaceRequest<ContentHandler> request) | 587 ContentHandlerImpl(mojo::InterfaceRequest<ContentHandler> request) |
| 584 : binding_(this, request.Pass()) {} | 588 : binding_(this, request.Pass()) {} |
| 585 ~ContentHandlerImpl() override {} | 589 ~ContentHandlerImpl() override {} |
| 586 | 590 |
| 587 private: | 591 private: |
| 588 // ContentHandler: | 592 // ContentHandler: |
| 589 void StartApplication(mojo::InterfaceRequest<mojo::Application> request, | 593 void StartApplication( |
| 590 mojo::URLResponsePtr response) override { | 594 mojo::InterfaceRequest<mojo::Application> request, |
| 591 new PDFViewerApplicationDelegate(request.Pass(), response.Pass()); | 595 mojo::URLResponsePtr response, |
| 596 const mojo::Callback<void()>& destruct_callback) override { |
| 597 new PDFViewerApplicationDelegate( |
| 598 request.Pass(), response.Pass(), destruct_callback); |
| 592 } | 599 } |
| 593 | 600 |
| 594 mojo::StrongBinding<mojo::ContentHandler> binding_; | 601 mojo::StrongBinding<mojo::ContentHandler> binding_; |
| 595 | 602 |
| 596 DISALLOW_COPY_AND_ASSIGN(ContentHandlerImpl); | 603 DISALLOW_COPY_AND_ASSIGN(ContentHandlerImpl); |
| 597 }; | 604 }; |
| 598 | 605 |
| 599 class PDFViewer : public mojo::ApplicationDelegate, | 606 class PDFViewer : public mojo::ApplicationDelegate, |
| 600 public mojo::InterfaceFactory<mojo::ContentHandler> { | 607 public mojo::InterfaceFactory<mojo::ContentHandler> { |
| 601 public: | 608 public: |
| (...skipping 26 matching lines...) Expand all Loading... |
| 628 | 635 |
| 629 DISALLOW_COPY_AND_ASSIGN(PDFViewer); | 636 DISALLOW_COPY_AND_ASSIGN(PDFViewer); |
| 630 }; | 637 }; |
| 631 } // namespace | 638 } // namespace |
| 632 } // namespace pdf_viewer | 639 } // namespace pdf_viewer |
| 633 | 640 |
| 634 MojoResult MojoMain(MojoHandle application_request) { | 641 MojoResult MojoMain(MojoHandle application_request) { |
| 635 mojo::ApplicationRunner runner(new pdf_viewer::PDFViewer()); | 642 mojo::ApplicationRunner runner(new pdf_viewer::PDFViewer()); |
| 636 return runner.Run(application_request); | 643 return runner.Run(application_request); |
| 637 } | 644 } |
| OLD | NEW |