| 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 <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/containers/hash_tables.h" | 11 #include "base/containers/hash_tables.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "components/bitmap_uploader/bitmap_uploader.h" | 14 #include "components/bitmap_uploader/bitmap_uploader.h" |
| 15 #include "components/mus/common/types.h" | 15 #include "components/mus/common/types.h" |
| 16 #include "components/mus/public/cpp/input_event_handler.h" | 16 #include "components/mus/public/cpp/input_event_handler.h" |
| 17 #include "components/mus/public/cpp/scoped_window_ptr.h" | 17 #include "components/mus/public/cpp/scoped_window_ptr.h" |
| 18 #include "components/mus/public/cpp/window.h" | 18 #include "components/mus/public/cpp/window.h" |
| 19 #include "components/mus/public/cpp/window_observer.h" | 19 #include "components/mus/public/cpp/window_observer.h" |
| 20 #include "components/mus/public/cpp/window_tree_connection.h" | 20 #include "components/mus/public/cpp/window_tree_connection.h" |
| 21 #include "components/mus/public/cpp/window_tree_delegate.h" | 21 #include "components/mus/public/cpp/window_tree_delegate.h" |
| 22 #include "components/mus/public/interfaces/input_events.mojom.h" | 22 #include "components/mus/public/interfaces/input_events.mojom.h" |
| 23 #include "components/mus/public/interfaces/input_key_codes.mojom.h" | 23 #include "components/mus/public/interfaces/input_key_codes.mojom.h" |
| 24 #include "components/web_view/public/interfaces/frame.mojom.h" | 24 #include "components/web_view/public/interfaces/frame.mojom.h" |
| 25 #include "mojo/common/data_pipe_utils.h" | 25 #include "mojo/common/data_pipe_utils.h" |
| 26 #include "mojo/public/c/system/main.h" | 26 #include "mojo/public/c/system/main.h" |
| 27 #include "mojo/public/cpp/bindings/binding.h" | 27 #include "mojo/public/cpp/bindings/binding.h" |
| 28 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 28 #include "mojo/services/tracing/public/cpp/tracing_impl.h" | 29 #include "mojo/services/tracing/public/cpp/tracing_impl.h" |
| 29 #include "mojo/shell/public/cpp/application_connection.h" | |
| 30 #include "mojo/shell/public/cpp/application_delegate.h" | |
| 31 #include "mojo/shell/public/cpp/application_impl.h" | 30 #include "mojo/shell/public/cpp/application_impl.h" |
| 32 #include "mojo/shell/public/cpp/application_runner.h" | 31 #include "mojo/shell/public/cpp/application_runner.h" |
| 33 #include "mojo/shell/public/cpp/connect.h" | |
| 34 #include "mojo/shell/public/cpp/interface_factory_impl.h" | 32 #include "mojo/shell/public/cpp/interface_factory_impl.h" |
| 35 #include "mojo/shell/public/cpp/service_provider_impl.h" | 33 #include "mojo/shell/public/cpp/shell_client.h" |
| 36 #include "mojo/shell/public/interfaces/content_handler.mojom.h" | 34 #include "mojo/shell/public/interfaces/content_handler.mojom.h" |
| 37 #include "mojo/shell/public/interfaces/shell.mojom.h" | 35 #include "mojo/shell/public/interfaces/shell.mojom.h" |
| 38 #include "third_party/pdfium/public/fpdf_ext.h" | 36 #include "third_party/pdfium/public/fpdf_ext.h" |
| 39 #include "third_party/pdfium/public/fpdfview.h" | 37 #include "third_party/pdfium/public/fpdfview.h" |
| 40 #include "ui/gfx/geometry/rect.h" | 38 #include "ui/gfx/geometry/rect.h" |
| 41 #include "v8/include/v8.h" | 39 #include "v8/include/v8.h" |
| 42 | 40 |
| 43 const uint32_t g_background_color = 0xFF888888; | 41 const uint32_t g_background_color = 0xFF888888; |
| 44 | 42 |
| 45 namespace pdf_viewer { | 43 namespace pdf_viewer { |
| 46 namespace { | 44 namespace { |
| 47 | 45 |
| 48 // Responsible for managing a particlar view displaying a PDF document. | 46 // Responsible for managing a particlar view displaying a PDF document. |
| 49 class PDFView : public mus::WindowTreeDelegate, | 47 class PDFView : public mus::WindowTreeDelegate, |
| 50 public mus::WindowObserver, | 48 public mus::WindowObserver, |
| 51 public mus::InputEventHandler, | 49 public mus::InputEventHandler, |
| 52 public web_view::mojom::FrameClient, | 50 public web_view::mojom::FrameClient, |
| 53 public mojo::InterfaceFactory<web_view::mojom::FrameClient> { | 51 public mojo::InterfaceFactory<web_view::mojom::FrameClient> { |
| 54 public: | 52 public: |
| 55 using DeleteCallback = base::Callback<void(PDFView*)>; | 53 using DeleteCallback = base::Callback<void(PDFView*)>; |
| 56 | 54 |
| 57 PDFView(mojo::Shell* shell, | 55 PDFView(mojo::Shell* shell, |
| 58 mojo::ApplicationConnection* connection, | 56 mojo::Connection* connection, |
| 59 FPDF_DOCUMENT doc, | 57 FPDF_DOCUMENT doc, |
| 60 const DeleteCallback& delete_callback) | 58 const DeleteCallback& delete_callback) |
| 61 : app_ref_(shell->CreateAppRefCount()), | 59 : app_ref_(shell->CreateAppRefCount()), |
| 62 doc_(doc), | 60 doc_(doc), |
| 63 current_page_(0), | 61 current_page_(0), |
| 64 page_count_(FPDF_GetPageCount(doc_)), | 62 page_count_(FPDF_GetPageCount(doc_)), |
| 65 shell_(shell), | 63 shell_(shell), |
| 66 root_(nullptr), | 64 root_(nullptr), |
| 67 frame_client_binding_(this), | 65 frame_client_binding_(this), |
| 68 delete_callback_(delete_callback) { | 66 delete_callback_(delete_callback) { |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 void HighlightFindResults(int32_t request_id, | 207 void HighlightFindResults(int32_t request_id, |
| 210 const mojo::String& search_test, | 208 const mojo::String& search_test, |
| 211 web_view::mojom::FindOptionsPtr options, | 209 web_view::mojom::FindOptionsPtr options, |
| 212 bool reset) override { | 210 bool reset) override { |
| 213 NOTIMPLEMENTED(); | 211 NOTIMPLEMENTED(); |
| 214 } | 212 } |
| 215 void StopHighlightingFindResults() override {} | 213 void StopHighlightingFindResults() override {} |
| 216 | 214 |
| 217 // mojo::InterfaceFactory<web_view::mojom::FrameClient>: | 215 // mojo::InterfaceFactory<web_view::mojom::FrameClient>: |
| 218 void Create( | 216 void Create( |
| 219 mojo::ApplicationConnection* connection, | 217 mojo::Connection* connection, |
| 220 mojo::InterfaceRequest<web_view::mojom::FrameClient> request) override { | 218 mojo::InterfaceRequest<web_view::mojom::FrameClient> request) override { |
| 221 frame_client_binding_.Bind(std::move(request)); | 219 frame_client_binding_.Bind(std::move(request)); |
| 222 } | 220 } |
| 223 | 221 |
| 224 scoped_ptr<mojo::AppRefCount> app_ref_; | 222 scoped_ptr<mojo::AppRefCount> app_ref_; |
| 225 FPDF_DOCUMENT doc_; | 223 FPDF_DOCUMENT doc_; |
| 226 int current_page_; | 224 int current_page_; |
| 227 int page_count_; | 225 int page_count_; |
| 228 | 226 |
| 229 scoped_ptr<bitmap_uploader::BitmapUploader> bitmap_uploader_; | 227 scoped_ptr<bitmap_uploader::BitmapUploader> bitmap_uploader_; |
| 230 | 228 |
| 231 mojo::Shell* shell_; | 229 mojo::Shell* shell_; |
| 232 mus::Window* root_; | 230 mus::Window* root_; |
| 233 | 231 |
| 234 web_view::mojom::FramePtr frame_; | 232 web_view::mojom::FramePtr frame_; |
| 235 mojo::Binding<web_view::mojom::FrameClient> frame_client_binding_; | 233 mojo::Binding<web_view::mojom::FrameClient> frame_client_binding_; |
| 236 DeleteCallback delete_callback_; | 234 DeleteCallback delete_callback_; |
| 237 | 235 |
| 238 DISALLOW_COPY_AND_ASSIGN(PDFView); | 236 DISALLOW_COPY_AND_ASSIGN(PDFView); |
| 239 }; | 237 }; |
| 240 | 238 |
| 241 // Responsible for managing all the views for displaying a PDF document. | 239 // Responsible for managing all the views for displaying a PDF document. |
| 242 class PDFViewerApplicationDelegate | 240 class PDFViewerApplicationDelegate |
| 243 : public mojo::ApplicationDelegate, | 241 : public mojo::ShellClient, |
| 244 public mojo::InterfaceFactory<mus::mojom::WindowTreeClient> { | 242 public mojo::InterfaceFactory<mus::mojom::WindowTreeClient> { |
| 245 public: | 243 public: |
| 246 PDFViewerApplicationDelegate( | 244 PDFViewerApplicationDelegate( |
| 247 mojo::ApplicationRequest request, | 245 mojo::ApplicationRequest request, |
| 248 mojo::URLResponsePtr response, | 246 mojo::URLResponsePtr response, |
| 249 const mojo::Callback<void()>& destruct_callback) | 247 const mojo::Callback<void()>& destruct_callback) |
| 250 : app_(this, | 248 : app_(this, |
| 251 std::move(request), | 249 std::move(request), |
| 252 base::Bind(&PDFViewerApplicationDelegate::OnTerminate, | 250 base::Bind(&PDFViewerApplicationDelegate::OnTerminate, |
| 253 base::Unretained(this))), | 251 base::Unretained(this))), |
| (...skipping 16 matching lines...) Expand all Loading... |
| 270 void FetchPDF(mojo::URLResponsePtr response) { | 268 void FetchPDF(mojo::URLResponsePtr response) { |
| 271 data_.clear(); | 269 data_.clear(); |
| 272 mojo::common::BlockingCopyToString(std::move(response->body), &data_); | 270 mojo::common::BlockingCopyToString(std::move(response->body), &data_); |
| 273 if (data_.length() >= static_cast<size_t>(std::numeric_limits<int>::max())) | 271 if (data_.length() >= static_cast<size_t>(std::numeric_limits<int>::max())) |
| 274 return; | 272 return; |
| 275 doc_ = FPDF_LoadMemDocument(data_.data(), static_cast<int>(data_.length()), | 273 doc_ = FPDF_LoadMemDocument(data_.data(), static_cast<int>(data_.length()), |
| 276 nullptr); | 274 nullptr); |
| 277 } | 275 } |
| 278 | 276 |
| 279 // Callback from the quit closure. We key off this rather than | 277 // Callback from the quit closure. We key off this rather than |
| 280 // ApplicationDelegate::Quit() as we don't want to shut down the messageloop | 278 // ShellClient::Quit() as we don't want to shut down the messageloop |
| 281 // when we quit (the messageloop is shared among multiple PDFViews). | 279 // when we quit (the messageloop is shared among multiple PDFViews). |
| 282 void OnTerminate() { delete this; } | 280 void OnTerminate() { delete this; } |
| 283 | 281 |
| 284 void OnPDFViewDestroyed(PDFView* pdf_view) { | 282 void OnPDFViewDestroyed(PDFView* pdf_view) { |
| 285 DCHECK(std::find(pdf_views_.begin(), pdf_views_.end(), pdf_view) != | 283 DCHECK(std::find(pdf_views_.begin(), pdf_views_.end(), pdf_view) != |
| 286 pdf_views_.end()); | 284 pdf_views_.end()); |
| 287 pdf_views_.erase(std::find(pdf_views_.begin(), pdf_views_.end(), pdf_view)); | 285 pdf_views_.erase(std::find(pdf_views_.begin(), pdf_views_.end(), pdf_view)); |
| 288 } | 286 } |
| 289 | 287 |
| 290 // ApplicationDelegate: | 288 // mojo::ShellClient: |
| 291 bool AcceptConnection( | 289 bool AcceptConnection(mojo::Connection* connection) override { |
| 292 mojo::ApplicationConnection* connection) override { | |
| 293 connection->AddService<mus::mojom::WindowTreeClient>(this); | 290 connection->AddService<mus::mojom::WindowTreeClient>(this); |
| 294 return true; | 291 return true; |
| 295 } | 292 } |
| 296 | 293 |
| 297 // mojo::InterfaceFactory<mus::mojom::WindowTreeClient>: | 294 // mojo::InterfaceFactory<mus::mojom::WindowTreeClient>: |
| 298 void Create( | 295 void Create( |
| 299 mojo::ApplicationConnection* connection, | 296 mojo::Connection* connection, |
| 300 mojo::InterfaceRequest<mus::mojom::WindowTreeClient> request) override { | 297 mojo::InterfaceRequest<mus::mojom::WindowTreeClient> request) override { |
| 301 PDFView* pdf_view = new PDFView( | 298 PDFView* pdf_view = new PDFView( |
| 302 &app_, connection, doc_, | 299 &app_, connection, doc_, |
| 303 base::Bind(&PDFViewerApplicationDelegate::OnPDFViewDestroyed, | 300 base::Bind(&PDFViewerApplicationDelegate::OnPDFViewDestroyed, |
| 304 base::Unretained(this))); | 301 base::Unretained(this))); |
| 305 pdf_views_.push_back(pdf_view); | 302 pdf_views_.push_back(pdf_view); |
| 306 mus::WindowTreeConnection::Create( | 303 mus::WindowTreeConnection::Create( |
| 307 pdf_view, std::move(request), | 304 pdf_view, std::move(request), |
| 308 mus::WindowTreeConnection::CreateType::DONT_WAIT_FOR_EMBED); | 305 mus::WindowTreeConnection::CreateType::DONT_WAIT_FOR_EMBED); |
| 309 } | 306 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 334 new PDFViewerApplicationDelegate(std::move(request), std::move(response), | 331 new PDFViewerApplicationDelegate(std::move(request), std::move(response), |
| 335 destruct_callback); | 332 destruct_callback); |
| 336 } | 333 } |
| 337 | 334 |
| 338 mojo::StrongBinding<mojo::shell::mojom::ContentHandler> binding_; | 335 mojo::StrongBinding<mojo::shell::mojom::ContentHandler> binding_; |
| 339 | 336 |
| 340 DISALLOW_COPY_AND_ASSIGN(ContentHandlerImpl); | 337 DISALLOW_COPY_AND_ASSIGN(ContentHandlerImpl); |
| 341 }; | 338 }; |
| 342 | 339 |
| 343 class PDFViewer | 340 class PDFViewer |
| 344 : public mojo::ApplicationDelegate, | 341 : public mojo::ShellClient, |
| 345 public mojo::InterfaceFactory<mojo::shell::mojom::ContentHandler> { | 342 public mojo::InterfaceFactory<mojo::shell::mojom::ContentHandler> { |
| 346 public: | 343 public: |
| 347 PDFViewer() { | 344 PDFViewer() { |
| 348 v8::V8::InitializeICU(); | 345 v8::V8::InitializeICU(); |
| 349 FPDF_InitLibrary(); | 346 FPDF_InitLibrary(); |
| 350 } | 347 } |
| 351 | 348 |
| 352 ~PDFViewer() override { FPDF_DestroyLibrary(); } | 349 ~PDFViewer() override { FPDF_DestroyLibrary(); } |
| 353 | 350 |
| 354 private: | 351 private: |
| 355 // ApplicationDelegate: | 352 // mojo::ShellClient: |
| 356 void Initialize(mojo::Shell* shell, const std::string& url, | 353 void Initialize(mojo::Shell* shell, const std::string& url, |
| 357 uint32_t id) override { | 354 uint32_t id) override { |
| 358 tracing_.Initialize(shell, url); | 355 tracing_.Initialize(shell, url); |
| 359 } | 356 } |
| 360 | 357 |
| 361 bool AcceptConnection( | 358 bool AcceptConnection(mojo::Connection* connection) override { |
| 362 mojo::ApplicationConnection* connection) override { | |
| 363 connection->AddService(this); | 359 connection->AddService(this); |
| 364 return true; | 360 return true; |
| 365 } | 361 } |
| 366 | 362 |
| 367 // InterfaceFactory<ContentHandler>: | 363 // InterfaceFactory<ContentHandler>: |
| 368 void Create(mojo::ApplicationConnection* connection, | 364 void Create(mojo::Connection* connection, |
| 369 mojo::InterfaceRequest<mojo::shell::mojom::ContentHandler> | 365 mojo::InterfaceRequest<mojo::shell::mojom::ContentHandler> |
| 370 request) override { | 366 request) override { |
| 371 new ContentHandlerImpl(std::move(request)); | 367 new ContentHandlerImpl(std::move(request)); |
| 372 } | 368 } |
| 373 | 369 |
| 374 mojo::TracingImpl tracing_; | 370 mojo::TracingImpl tracing_; |
| 375 | 371 |
| 376 DISALLOW_COPY_AND_ASSIGN(PDFViewer); | 372 DISALLOW_COPY_AND_ASSIGN(PDFViewer); |
| 377 }; | 373 }; |
| 378 } // namespace | 374 } // namespace |
| 379 } // namespace pdf_viewer | 375 } // namespace pdf_viewer |
| 380 | 376 |
| 381 MojoResult MojoMain(MojoHandle application_request) { | 377 MojoResult MojoMain(MojoHandle application_request) { |
| 382 mojo::ApplicationRunner runner(new pdf_viewer::PDFViewer()); | 378 mojo::ApplicationRunner runner(new pdf_viewer::PDFViewer()); |
| 383 return runner.Run(application_request); | 379 return runner.Run(application_request); |
| 384 } | 380 } |
| OLD | NEW |