| OLD | NEW |
| 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 "base/strings/string_tokenizer.h" |
| 6 #include "examples/bitmap_uploader/bitmap_uploader.h" | 6 #include "examples/bitmap_uploader/bitmap_uploader.h" |
| 7 #include "mojo/application/application_runner_chromium.h" | 7 #include "mojo/application/application_runner_chromium.h" |
| 8 #include "mojo/application/content_handler_factory.h" |
| 8 #include "mojo/public/c/system/main.h" | 9 #include "mojo/public/c/system/main.h" |
| 9 #include "mojo/public/cpp/application/application_connection.h" | 10 #include "mojo/public/cpp/application/application_connection.h" |
| 10 #include "mojo/public/cpp/application/application_delegate.h" | 11 #include "mojo/public/cpp/application/application_delegate.h" |
| 11 #include "mojo/public/cpp/application/application_impl.h" | 12 #include "mojo/public/cpp/application/application_impl.h" |
| 12 #include "mojo/public/cpp/application/interface_factory_impl.h" | 13 #include "mojo/public/cpp/application/interface_factory_impl.h" |
| 13 #include "mojo/public/cpp/application/service_provider_impl.h" | 14 #include "mojo/public/cpp/application/service_provider_impl.h" |
| 14 #include "mojo/services/public/cpp/view_manager/types.h" | 15 #include "mojo/services/public/cpp/view_manager/types.h" |
| 15 #include "mojo/services/public/cpp/view_manager/view.h" | 16 #include "mojo/services/public/cpp/view_manager/view.h" |
| 16 #include "mojo/services/public/cpp/view_manager/view_manager.h" | 17 #include "mojo/services/public/cpp/view_manager/view_manager.h" |
| 17 #include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h" | 18 #include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h" |
| 18 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" | 19 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" |
| 19 #include "mojo/services/public/cpp/view_manager/view_observer.h" | 20 #include "mojo/services/public/cpp/view_manager/view_observer.h" |
| 20 #include "mojo/services/public/interfaces/content_handler/content_handler.mojom.
h" | 21 #include "mojo/services/public/interfaces/content_handler/content_handler.mojom.
h" |
| 21 #include "mojo/services/public/interfaces/input_events/input_events.mojom.h" | 22 #include "mojo/services/public/interfaces/input_events/input_events.mojom.h" |
| 22 #include "mojo/services/public/interfaces/input_events/input_key_codes.mojom.h" | 23 #include "mojo/services/public/interfaces/input_events/input_key_codes.mojom.h" |
| 23 #include "third_party/pdfium/fpdfsdk/include/fpdf_ext.h" | 24 #include "third_party/pdfium/fpdfsdk/include/fpdf_ext.h" |
| 24 #include "third_party/pdfium/fpdfsdk/include/fpdfview.h" | 25 #include "third_party/pdfium/fpdfsdk/include/fpdfview.h" |
| 25 #include "v8/include/v8.h" | 26 #include "v8/include/v8.h" |
| 26 | 27 |
| 27 #define BACKGROUND_COLOR 0xFF888888 | 28 #define BACKGROUND_COLOR 0xFF888888 |
| 28 | 29 |
| 29 namespace mojo { | 30 namespace mojo { |
| 30 namespace examples { | 31 namespace examples { |
| 31 | 32 |
| 32 class PDFViewer; | 33 class PDFView : public ApplicationDelegate, |
| 33 | 34 public ViewManagerDelegate, |
| 34 class PDFView : public ViewManagerDelegate, public ViewObserver { | 35 public ViewObserver { |
| 35 public: | 36 public: |
| 36 static void Spawn(URLResponsePtr response, | 37 PDFView(URLResponsePtr response) |
| 37 ServiceProviderImpl* exported_services, | |
| 38 scoped_ptr<ServiceProvider> imported_services, | |
| 39 Shell* shell) { | |
| 40 // PDFView deletes itself when its View is destroyed. | |
| 41 new PDFView( | |
| 42 response.Pass(), exported_services, imported_services.Pass(), shell); | |
| 43 } | |
| 44 | |
| 45 private: | |
| 46 PDFView(URLResponsePtr response, | |
| 47 ServiceProviderImpl* exported_services, | |
| 48 scoped_ptr<ServiceProvider> imported_services, | |
| 49 Shell* shell) | |
| 50 : current_page_(0), | 38 : current_page_(0), |
| 51 page_count_(0), | 39 page_count_(0), |
| 52 doc_(NULL), | 40 doc_(NULL), |
| 53 imported_services_(imported_services.Pass()), | 41 app_(nullptr), |
| 54 shell_(shell), | 42 root_(nullptr) { |
| 55 root_(nullptr), | |
| 56 view_manager_client_factory_(shell, this) { | |
| 57 exported_services->AddService(&view_manager_client_factory_); | |
| 58 FetchPDF(response.Pass()); | 43 FetchPDF(response.Pass()); |
| 59 } | 44 } |
| 60 | 45 |
| 61 virtual ~PDFView() { | 46 virtual ~PDFView() { |
| 62 if (doc_) | 47 if (doc_) |
| 63 FPDF_CloseDocument(doc_); | 48 FPDF_CloseDocument(doc_); |
| 64 if (root_) | 49 if (root_) |
| 65 root_->RemoveObserver(this); | 50 root_->RemoveObserver(this); |
| 66 } | 51 } |
| 67 | 52 |
| 53 private: |
| 54 // Overridden from ApplicationDelegate: |
| 55 virtual void Initialize(ApplicationImpl* app) override { |
| 56 app_ = app; |
| 57 view_manager_client_factory_.reset( |
| 58 new ViewManagerClientFactory(app->shell(), this)); |
| 59 } |
| 60 |
| 61 virtual bool ConfigureIncomingConnection( |
| 62 ApplicationConnection* connection) override { |
| 63 connection->AddService(view_manager_client_factory_.get()); |
| 64 return true; |
| 65 } |
| 66 |
| 68 // Overridden from ViewManagerDelegate: | 67 // Overridden from ViewManagerDelegate: |
| 69 virtual void OnEmbed(ViewManager* view_manager, | 68 virtual void OnEmbed(ViewManager* view_manager, |
| 70 View* root, | 69 View* root, |
| 71 ServiceProviderImpl* exported_services, | 70 ServiceProviderImpl* exported_services, |
| 72 scoped_ptr<ServiceProvider> imported_services) override { | 71 scoped_ptr<ServiceProvider> imported_services) override { |
| 73 root_ = root; | 72 root_ = root; |
| 74 root_->AddObserver(this); | 73 root_->AddObserver(this); |
| 75 bitmap_uploader_.reset(new BitmapUploader(root_)); | 74 bitmap_uploader_.reset(new BitmapUploader(root_)); |
| 76 bitmap_uploader_->Init(shell_); | 75 bitmap_uploader_->Init(app_->shell()); |
| 77 bitmap_uploader_->SetColor(BACKGROUND_COLOR); | 76 bitmap_uploader_->SetColor(BACKGROUND_COLOR); |
| 78 DrawBitmap(); | 77 DrawBitmap(); |
| 79 } | 78 } |
| 80 | 79 |
| 81 virtual void OnViewManagerDisconnected(ViewManager* view_manager) override { | 80 virtual void OnViewManagerDisconnected(ViewManager* view_manager) override { |
| 82 } | 81 } |
| 83 | 82 |
| 84 // Overridden from ViewObserver: | 83 // Overridden from ViewObserver: |
| 85 virtual void OnViewBoundsChanged(View* view, | 84 virtual void OnViewBoundsChanged(View* view, |
| 86 const Rect& old_bounds, | 85 const Rect& old_bounds, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 107 (event->wheel_data && event->wheel_data->y_offset > 0)) { | 106 (event->wheel_data && event->wheel_data->y_offset > 0)) { |
| 108 if (current_page_ > 0) { | 107 if (current_page_ > 0) { |
| 109 current_page_--; | 108 current_page_--; |
| 110 DrawBitmap(); | 109 DrawBitmap(); |
| 111 } | 110 } |
| 112 } | 111 } |
| 113 } | 112 } |
| 114 | 113 |
| 115 virtual void OnViewDestroyed(View* view) override { | 114 virtual void OnViewDestroyed(View* view) override { |
| 116 DCHECK_EQ(view, root_); | 115 DCHECK_EQ(view, root_); |
| 117 delete this; | 116 // TODO(qsr): It should not be necessary to cleanup the uploader, but it |
| 117 // crashes if the GL context goes away. |
| 118 bitmap_uploader_.reset(); |
| 119 ApplicationImpl::Terminate(); |
| 118 } | 120 } |
| 119 | 121 |
| 120 void DrawBitmap() { | 122 void DrawBitmap() { |
| 121 if (!root_ || !doc_) | 123 if (!root_ || !doc_) |
| 122 return; | 124 return; |
| 123 | 125 |
| 124 FPDF_PAGE page = FPDF_LoadPage(doc_, current_page_); | 126 FPDF_PAGE page = FPDF_LoadPage(doc_, current_page_); |
| 125 int width = static_cast<int>(FPDF_GetPageWidth(page)); | 127 int width = static_cast<int>(FPDF_GetPageWidth(page)); |
| 126 int height = static_cast<int>(FPDF_GetPageHeight(page)); | 128 int height = static_cast<int>(FPDF_GetPageHeight(page)); |
| 127 | 129 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 } | 181 } |
| 180 } | 182 } |
| 181 } | 183 } |
| 182 return 0; | 184 return 0; |
| 183 } | 185 } |
| 184 | 186 |
| 185 scoped_ptr<unsigned char[]> data_; | 187 scoped_ptr<unsigned char[]> data_; |
| 186 int current_page_; | 188 int current_page_; |
| 187 int page_count_; | 189 int page_count_; |
| 188 FPDF_DOCUMENT doc_; | 190 FPDF_DOCUMENT doc_; |
| 189 scoped_ptr<ServiceProvider> imported_services_; | 191 ApplicationImpl* app_; |
| 190 Shell* shell_; | |
| 191 View* root_; | 192 View* root_; |
| 192 ViewManagerClientFactory view_manager_client_factory_; | 193 scoped_ptr<ViewManagerClientFactory> view_manager_client_factory_; |
| 193 scoped_ptr<BitmapUploader> bitmap_uploader_; | 194 scoped_ptr<BitmapUploader> bitmap_uploader_; |
| 194 | 195 |
| 195 DISALLOW_COPY_AND_ASSIGN(PDFView); | 196 DISALLOW_COPY_AND_ASSIGN(PDFView); |
| 196 }; | 197 }; |
| 197 | 198 |
| 198 class ContentHandlerImpl : public InterfaceImpl<ContentHandler> { | 199 class PDFViewer : public ApplicationDelegate, |
| 200 public ContentHandlerFactory::Delegate { |
| 199 public: | 201 public: |
| 200 explicit ContentHandlerImpl(Shell* shell) : shell_(shell) {} | 202 PDFViewer() : content_handler_factory_(this) { |
| 201 virtual ~ContentHandlerImpl() {} | |
| 202 | |
| 203 private: | |
| 204 // Overridden from ContentHandler: | |
| 205 virtual void OnConnect( | |
| 206 const mojo::String& requestor_url, | |
| 207 URLResponsePtr response, | |
| 208 InterfaceRequest<ServiceProvider> service_provider) override { | |
| 209 ServiceProviderImpl* exported_services = new ServiceProviderImpl(); | |
| 210 BindToRequest(exported_services, &service_provider); | |
| 211 scoped_ptr<ServiceProvider> remote( | |
| 212 exported_services->CreateRemoteServiceProvider()); | |
| 213 PDFView::Spawn(response.Pass(), exported_services, remote.Pass(), shell_); | |
| 214 } | |
| 215 | |
| 216 Shell* shell_; | |
| 217 | |
| 218 DISALLOW_COPY_AND_ASSIGN(ContentHandlerImpl); | |
| 219 }; | |
| 220 | |
| 221 class PDFViewer : public ApplicationDelegate { | |
| 222 public: | |
| 223 PDFViewer() {} | |
| 224 virtual ~PDFViewer() { | |
| 225 FPDF_DestroyLibrary(); | |
| 226 } | |
| 227 private: | |
| 228 // Overridden from ApplicationDelegate: | |
| 229 virtual void Initialize(ApplicationImpl* app) override { | |
| 230 content_handler_factory_.reset( | |
| 231 new InterfaceFactoryImplWithContext<ContentHandlerImpl, Shell>( | |
| 232 app->shell())); | |
| 233 | |
| 234 v8::V8::InitializeICU(); | 203 v8::V8::InitializeICU(); |
| 235 FPDF_InitLibrary(NULL); | 204 FPDF_InitLibrary(NULL); |
| 236 } | 205 } |
| 237 | 206 |
| 207 virtual ~PDFViewer() { FPDF_DestroyLibrary(); } |
| 208 |
| 209 private: |
| 238 // Overridden from ApplicationDelegate: | 210 // Overridden from ApplicationDelegate: |
| 239 virtual bool ConfigureIncomingConnection( | 211 virtual bool ConfigureIncomingConnection( |
| 240 ApplicationConnection* connection) override { | 212 ApplicationConnection* connection) override { |
| 241 connection->AddService(content_handler_factory_.get()); | 213 connection->AddService(&content_handler_factory_); |
| 242 return true; | 214 return true; |
| 243 } | 215 } |
| 244 | 216 |
| 245 scoped_ptr<InterfaceFactoryImplWithContext<ContentHandlerImpl, Shell> > | 217 // Overridden from ContentHandlerFactory::Delegate: |
| 246 content_handler_factory_; | 218 virtual scoped_ptr<ContentHandlerFactory::HandledApplicationHolder> |
| 219 CreateApplication(ShellPtr shell, URLResponsePtr response) override { |
| 220 return make_handled_factory_holder(new mojo::ApplicationImpl( |
| 221 new PDFView(response.Pass()), shell.PassMessagePipe())); |
| 222 } |
| 223 |
| 224 ContentHandlerFactory content_handler_factory_; |
| 247 | 225 |
| 248 DISALLOW_COPY_AND_ASSIGN(PDFViewer); | 226 DISALLOW_COPY_AND_ASSIGN(PDFViewer); |
| 249 }; | 227 }; |
| 250 | 228 |
| 251 } // namespace examples | 229 } // namespace examples |
| 252 } // namespace mojo | 230 } // namespace mojo |
| 253 | 231 |
| 254 MojoResult MojoMain(MojoHandle shell_handle) { | 232 MojoResult MojoMain(MojoHandle shell_handle) { |
| 255 mojo::ApplicationRunnerChromium runner(new mojo::examples::PDFViewer); | 233 mojo::ApplicationRunnerChromium runner(new mojo::examples::PDFViewer()); |
| 256 return runner.Run(shell_handle); | 234 return runner.Run(shell_handle); |
| 257 } | 235 } |
| OLD | NEW |