| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <string> | |
| 6 | |
| 7 #include "examples/bitmap_uploader/bitmap_uploader.h" | |
| 8 #include "mojo/application/application_runner_chromium.h" | |
| 9 #include "mojo/application/content_handler_factory.h" | |
| 10 #include "mojo/data_pipe_utils/data_pipe_utils.h" | |
| 11 #include "mojo/public/c/system/main.h" | |
| 12 #include "mojo/public/cpp/application/application_connection.h" | |
| 13 #include "mojo/public/cpp/application/application_delegate.h" | |
| 14 #include "mojo/public/cpp/application/application_impl.h" | |
| 15 #include "mojo/public/cpp/application/interface_factory_impl.h" | |
| 16 #include "mojo/public/cpp/application/service_provider_impl.h" | |
| 17 #include "mojo/services/content_handler/interfaces/content_handler.mojom.h" | |
| 18 #include "mojo/services/input_events/interfaces/input_events.mojom.h" | |
| 19 #include "mojo/services/input_events/interfaces/input_key_codes.mojom.h" | |
| 20 #include "mojo/services/view_manager/cpp/types.h" | |
| 21 #include "mojo/services/view_manager/cpp/view.h" | |
| 22 #include "mojo/services/view_manager/cpp/view_manager.h" | |
| 23 #include "mojo/services/view_manager/cpp/view_manager_client_factory.h" | |
| 24 #include "mojo/services/view_manager/cpp/view_manager_delegate.h" | |
| 25 #include "mojo/services/view_manager/cpp/view_observer.h" | |
| 26 #include "third_party/pdfium/fpdfsdk/include/fpdf_ext.h" | |
| 27 #include "third_party/pdfium/fpdfsdk/include/fpdfview.h" | |
| 28 #include "v8/include/v8.h" | |
| 29 | |
| 30 #define BACKGROUND_COLOR 0xFF888888 | |
| 31 | |
| 32 namespace mojo { | |
| 33 namespace examples { | |
| 34 | |
| 35 namespace { | |
| 36 | |
| 37 class EmbedderData { | |
| 38 public: | |
| 39 EmbedderData(Shell* shell, View* root) : bitmap_uploader_(root) { | |
| 40 bitmap_uploader_.Init(shell); | |
| 41 bitmap_uploader_.SetColor(BACKGROUND_COLOR); | |
| 42 } | |
| 43 | |
| 44 BitmapUploader& bitmap_uploader() { return bitmap_uploader_; } | |
| 45 | |
| 46 private: | |
| 47 BitmapUploader bitmap_uploader_; | |
| 48 | |
| 49 DISALLOW_COPY_AND_ASSIGN(EmbedderData); | |
| 50 }; | |
| 51 | |
| 52 } // namespace | |
| 53 | |
| 54 class PDFView : public ApplicationDelegate, | |
| 55 public ViewManagerDelegate, | |
| 56 public ViewObserver { | |
| 57 public: | |
| 58 PDFView(URLResponsePtr response) | |
| 59 : current_page_(0), page_count_(0), doc_(NULL), app_(nullptr) { | |
| 60 FetchPDF(response.Pass()); | |
| 61 } | |
| 62 | |
| 63 ~PDFView() override { | |
| 64 if (doc_) | |
| 65 FPDF_CloseDocument(doc_); | |
| 66 for (auto& roots : embedder_for_roots_) { | |
| 67 roots.first->RemoveObserver(this); | |
| 68 delete roots.second; | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 private: | |
| 73 // Overridden from ApplicationDelegate: | |
| 74 void Initialize(ApplicationImpl* app) override { | |
| 75 app_ = app; | |
| 76 view_manager_client_factory_.reset( | |
| 77 new ViewManagerClientFactory(app->shell(), this)); | |
| 78 } | |
| 79 | |
| 80 bool ConfigureIncomingConnection(ApplicationConnection* connection) override { | |
| 81 connection->AddService(view_manager_client_factory_.get()); | |
| 82 return true; | |
| 83 } | |
| 84 | |
| 85 // Overridden from ViewManagerDelegate: | |
| 86 void OnEmbed(View* root, | |
| 87 InterfaceRequest<ServiceProvider> services, | |
| 88 ServiceProviderPtr exposed_services) override { | |
| 89 DCHECK(embedder_for_roots_.find(root) == embedder_for_roots_.end()); | |
| 90 root->AddObserver(this); | |
| 91 EmbedderData* embedder_data = new EmbedderData(app_->shell(), root); | |
| 92 embedder_for_roots_[root] = embedder_data; | |
| 93 DrawBitmap(embedder_data); | |
| 94 } | |
| 95 | |
| 96 void OnViewManagerDisconnected(ViewManager* view_manager) override {} | |
| 97 | |
| 98 // Overridden from ViewObserver: | |
| 99 void OnViewBoundsChanged(View* view, | |
| 100 const Rect& old_bounds, | |
| 101 const Rect& new_bounds) override { | |
| 102 DCHECK(embedder_for_roots_.find(view) != embedder_for_roots_.end()); | |
| 103 DrawBitmap(embedder_for_roots_[view]); | |
| 104 } | |
| 105 | |
| 106 void OnViewInputEvent(View* view, const EventPtr& event) override { | |
| 107 DCHECK(embedder_for_roots_.find(view) != embedder_for_roots_.end()); | |
| 108 if (event->key_data && | |
| 109 (event->action != EventType::KEY_PRESSED || event->key_data->is_char)) { | |
| 110 return; | |
| 111 } | |
| 112 if ((event->key_data && | |
| 113 event->key_data->windows_key_code == KeyboardCode::DOWN) || | |
| 114 (event->pointer_data && event->pointer_data->vertical_wheel < 0)) { | |
| 115 if (current_page_ < (page_count_ - 1)) { | |
| 116 current_page_++; | |
| 117 DrawBitmap(embedder_for_roots_[view]); | |
| 118 } | |
| 119 } else if ((event->key_data && | |
| 120 event->key_data->windows_key_code == KeyboardCode::UP) || | |
| 121 (event->pointer_data && | |
| 122 event->pointer_data->vertical_wheel > 0)) { | |
| 123 if (current_page_ > 0) { | |
| 124 current_page_--; | |
| 125 DrawBitmap(embedder_for_roots_[view]); | |
| 126 } | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 void OnViewDestroyed(View* view) override { | |
| 131 DCHECK(embedder_for_roots_.find(view) != embedder_for_roots_.end()); | |
| 132 const auto& it = embedder_for_roots_.find(view); | |
| 133 DCHECK(it != embedder_for_roots_.end()); | |
| 134 delete it->second; | |
| 135 embedder_for_roots_.erase(it); | |
| 136 if (embedder_for_roots_.size() == 0) | |
| 137 ApplicationImpl::Terminate(); | |
| 138 } | |
| 139 | |
| 140 void DrawBitmap(EmbedderData* embedder_data) { | |
| 141 if (!doc_) | |
| 142 return; | |
| 143 | |
| 144 FPDF_PAGE page = FPDF_LoadPage(doc_, current_page_); | |
| 145 int width = static_cast<int>(FPDF_GetPageWidth(page)); | |
| 146 int height = static_cast<int>(FPDF_GetPageHeight(page)); | |
| 147 | |
| 148 scoped_ptr<std::vector<unsigned char>> bitmap; | |
| 149 bitmap.reset(new std::vector<unsigned char>); | |
| 150 bitmap->resize(width * height * 4); | |
| 151 | |
| 152 FPDF_BITMAP f_bitmap = FPDFBitmap_CreateEx(width, height, FPDFBitmap_BGRA, | |
| 153 &(*bitmap)[0], width * 4); | |
| 154 FPDFBitmap_FillRect(f_bitmap, 0, 0, width, height, 0xFFFFFFFF); | |
| 155 FPDF_RenderPageBitmap(f_bitmap, page, 0, 0, width, height, 0, 0); | |
| 156 FPDFBitmap_Destroy(f_bitmap); | |
| 157 | |
| 158 FPDF_ClosePage(page); | |
| 159 | |
| 160 embedder_data->bitmap_uploader().SetBitmap(width, height, bitmap.Pass(), | |
| 161 BitmapUploader::BGRA); | |
| 162 } | |
| 163 | |
| 164 void FetchPDF(URLResponsePtr response) { | |
| 165 data_.clear(); | |
| 166 mojo::common::BlockingCopyToString(response->body.Pass(), &data_); | |
| 167 if (data_.length() >= static_cast<size_t>(std::numeric_limits<int>::max())) | |
| 168 return; | |
| 169 doc_ = FPDF_LoadMemDocument(data_.data(), static_cast<int>(data_.length()), | |
| 170 nullptr); | |
| 171 page_count_ = FPDF_GetPageCount(doc_); | |
| 172 } | |
| 173 | |
| 174 std::string data_; | |
| 175 int current_page_; | |
| 176 int page_count_; | |
| 177 FPDF_DOCUMENT doc_; | |
| 178 ApplicationImpl* app_; | |
| 179 std::map<View*, EmbedderData*> embedder_for_roots_; | |
| 180 scoped_ptr<ViewManagerClientFactory> view_manager_client_factory_; | |
| 181 | |
| 182 DISALLOW_COPY_AND_ASSIGN(PDFView); | |
| 183 }; | |
| 184 | |
| 185 class PDFViewer : public ApplicationDelegate, | |
| 186 public ContentHandlerFactory::ManagedDelegate { | |
| 187 public: | |
| 188 PDFViewer() : content_handler_factory_(this) { | |
| 189 v8::V8::InitializeICU(); | |
| 190 FPDF_InitLibrary(); | |
| 191 } | |
| 192 | |
| 193 ~PDFViewer() override { FPDF_DestroyLibrary(); } | |
| 194 | |
| 195 private: | |
| 196 // Overridden from ApplicationDelegate: | |
| 197 bool ConfigureIncomingConnection(ApplicationConnection* connection) override { | |
| 198 connection->AddService(&content_handler_factory_); | |
| 199 return true; | |
| 200 } | |
| 201 | |
| 202 // Overridden from ContentHandlerFactory::ManagedDelegate: | |
| 203 scoped_ptr<ContentHandlerFactory::HandledApplicationHolder> | |
| 204 CreateApplication(InterfaceRequest<Application> application_request, | |
| 205 URLResponsePtr response) override { | |
| 206 return make_handled_factory_holder(new mojo::ApplicationImpl( | |
| 207 new PDFView(response.Pass()), application_request.Pass())); | |
| 208 } | |
| 209 | |
| 210 ContentHandlerFactory content_handler_factory_; | |
| 211 | |
| 212 DISALLOW_COPY_AND_ASSIGN(PDFViewer); | |
| 213 }; | |
| 214 | |
| 215 } // namespace examples | |
| 216 } // namespace mojo | |
| 217 | |
| 218 MojoResult MojoMain(MojoHandle application_request) { | |
| 219 mojo::ApplicationRunnerChromium runner(new mojo::examples::PDFViewer()); | |
| 220 return runner.Run(application_request); | |
| 221 } | |
| OLD | NEW |