Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(259)

Side by Side Diff: examples/pdf_viewer/pdf_viewer.cc

Issue 687273002: mojo: Update content handler API (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Use the new content handler mechanism for js Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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.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 : current_page_(0), page_count_(0), doc_(NULL), root_(nullptr) {
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),
51 page_count_(0),
52 doc_(NULL),
53 imported_services_(imported_services.Pass()),
54 shell_(shell),
55 root_(nullptr),
56 view_manager_client_factory_(shell, this) {
57 exported_services->AddService(&view_manager_client_factory_);
58 FetchPDF(response.Pass()); 39 FetchPDF(response.Pass());
59 } 40 }
60 41
61 virtual ~PDFView() { 42 virtual ~PDFView() {
62 if (doc_) 43 if (doc_)
63 FPDF_CloseDocument(doc_); 44 FPDF_CloseDocument(doc_);
64 if (root_) 45 if (root_)
65 root_->RemoveObserver(this); 46 root_->RemoveObserver(this);
66 } 47 }
67 48
49 private:
50 // Overridden from ApplicationDelegate:
51 virtual void Initialize(ApplicationImpl* app) override {
52 app_ = app;
53 view_manager_client_factory_.reset(
54 new ViewManagerClientFactory(app->shell(), this));
55 }
56
57 virtual bool ConfigureIncomingConnection(
58 ApplicationConnection* connection) override {
59 connection->AddService(view_manager_client_factory_.get());
60 return true;
61 }
62
68 // Overridden from ViewManagerDelegate: 63 // Overridden from ViewManagerDelegate:
69 virtual void OnEmbed(ViewManager* view_manager, 64 virtual void OnEmbed(ViewManager* view_manager,
70 View* root, 65 View* root,
71 ServiceProviderImpl* exported_services, 66 ServiceProviderImpl* exported_services,
72 scoped_ptr<ServiceProvider> imported_services) override { 67 scoped_ptr<ServiceProvider> imported_services) override {
73 root_ = root; 68 root_ = root;
74 root_->AddObserver(this); 69 root_->AddObserver(this);
75 bitmap_uploader_.reset(new BitmapUploader(root_)); 70 bitmap_uploader_.reset(new BitmapUploader(root_));
76 bitmap_uploader_->Init(shell_); 71 bitmap_uploader_->Init(app_->shell());
77 bitmap_uploader_->SetColor(BACKGROUND_COLOR); 72 bitmap_uploader_->SetColor(BACKGROUND_COLOR);
78 DrawBitmap(); 73 DrawBitmap();
79 } 74 }
80 75
81 virtual void OnViewManagerDisconnected(ViewManager* view_manager) override { 76 virtual void OnViewManagerDisconnected(ViewManager* view_manager) override {
82 } 77 }
83 78
84 // Overridden from ViewObserver: 79 // Overridden from ViewObserver:
85 virtual void OnViewBoundsChanged(View* view, 80 virtual void OnViewBoundsChanged(View* view,
86 const Rect& old_bounds, 81 const Rect& old_bounds,
(...skipping 20 matching lines...) Expand all
107 (event->wheel_data && event->wheel_data->y_offset > 0)) { 102 (event->wheel_data && event->wheel_data->y_offset > 0)) {
108 if (current_page_ > 0) { 103 if (current_page_ > 0) {
109 current_page_--; 104 current_page_--;
110 DrawBitmap(); 105 DrawBitmap();
111 } 106 }
112 } 107 }
113 } 108 }
114 109
115 virtual void OnViewDestroyed(View* view) override { 110 virtual void OnViewDestroyed(View* view) override {
116 DCHECK_EQ(view, root_); 111 DCHECK_EQ(view, root_);
117 delete this; 112 // TODO(qsr): It should not be necessary to cleanup the uploader, but it
113 // crashes if the GL context goes away.
114 bitmap_uploader_.reset();
115 ApplicationImpl::Terminate();
118 } 116 }
119 117
120 void DrawBitmap() { 118 void DrawBitmap() {
121 if (!root_ || !doc_) 119 if (!root_ || !doc_)
122 return; 120 return;
123 121
124 FPDF_PAGE page = FPDF_LoadPage(doc_, current_page_); 122 FPDF_PAGE page = FPDF_LoadPage(doc_, current_page_);
125 int width = static_cast<int>(FPDF_GetPageWidth(page)); 123 int width = static_cast<int>(FPDF_GetPageWidth(page));
126 int height = static_cast<int>(FPDF_GetPageHeight(page)); 124 int height = static_cast<int>(FPDF_GetPageHeight(page));
127 125
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 } 177 }
180 } 178 }
181 } 179 }
182 return 0; 180 return 0;
183 } 181 }
184 182
185 scoped_ptr<unsigned char[]> data_; 183 scoped_ptr<unsigned char[]> data_;
186 int current_page_; 184 int current_page_;
187 int page_count_; 185 int page_count_;
188 FPDF_DOCUMENT doc_; 186 FPDF_DOCUMENT doc_;
189 scoped_ptr<ServiceProvider> imported_services_; 187 ApplicationImpl* app_;
Aaron Boodman 2014/10/31 08:24:01 app_ needs to be initialized in ctor.
qsr 2014/10/31 12:10:44 Done.
190 Shell* shell_;
191 View* root_; 188 View* root_;
192 ViewManagerClientFactory view_manager_client_factory_; 189 scoped_ptr<ViewManagerClientFactory> view_manager_client_factory_;
193 scoped_ptr<BitmapUploader> bitmap_uploader_; 190 scoped_ptr<BitmapUploader> bitmap_uploader_;
194 191
195 DISALLOW_COPY_AND_ASSIGN(PDFView); 192 DISALLOW_COPY_AND_ASSIGN(PDFView);
196 }; 193 };
197 194
198 class ContentHandlerImpl : public InterfaceImpl<ContentHandler> { 195 class PDFViewer : public ContentHandlerDelegate {
199 public: 196 public:
200 explicit ContentHandlerImpl(Shell* shell) : shell_(shell) {} 197 PDFViewer() {
201 virtual ~ContentHandlerImpl() {} 198 v8::V8::InitializeICU();
202 199 FPDF_InitLibrary(NULL);
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 } 200 }
215 201
216 Shell* shell_;
217
218 DISALLOW_COPY_AND_ASSIGN(ContentHandlerImpl);
219 };
220
221 class PDFViewer : public ApplicationDelegate {
222 public:
223 PDFViewer() {}
224 virtual ~PDFViewer() { 202 virtual ~PDFViewer() {
225 FPDF_DestroyLibrary(); 203 FPDF_DestroyLibrary();
226 } 204 }
227 private: 205 private:
228 // Overridden from ApplicationDelegate: 206 // Overridden from ContentHandlerDelegate:
229 virtual void Initialize(ApplicationImpl* app) override { 207 virtual scoped_ptr<mojo::InterfaceImpl<mojo::Application>> CreateApplication(
Aaron Boodman 2014/10/31 08:24:01 I don't think you need mojo:: for any of these. Yo
qsr 2014/10/31 12:10:44 Well, sky isn't, and I mixed the two. Thanks.
230 content_handler_factory_.reset( 208 ShellPtr shell,
231 new InterfaceFactoryImplWithContext<ContentHandlerImpl, Shell>( 209 URLResponsePtr response) override {
232 app->shell())); 210 return make_scoped_ptr(new mojo::ApplicationImpl(
233 211 new PDFView(response.Pass()), shell.PassMessagePipe()));
234 v8::V8::InitializeICU();
235 FPDF_InitLibrary(NULL);
236 } 212 }
237 213
238 // Overridden from ApplicationDelegate:
239 virtual bool ConfigureIncomingConnection(
240 ApplicationConnection* connection) override {
241 connection->AddService(content_handler_factory_.get());
242 return true;
243 }
244
245 scoped_ptr<InterfaceFactoryImplWithContext<ContentHandlerImpl, Shell> >
246 content_handler_factory_;
247
248 DISALLOW_COPY_AND_ASSIGN(PDFViewer); 214 DISALLOW_COPY_AND_ASSIGN(PDFViewer);
249 }; 215 };
250 216
251 } // namespace examples 217 } // namespace examples
252 } // namespace mojo 218 } // namespace mojo
253 219
254 MojoResult MojoMain(MojoHandle shell_handle) { 220 MojoResult MojoMain(MojoHandle shell_handle) {
255 mojo::ApplicationRunnerChromium runner(new mojo::examples::PDFViewer); 221 return mojo::ContentHandlerRunner::Run(
256 return runner.Run(shell_handle); 222 shell_handle, make_scoped_ptr(new mojo::examples::PDFViewer));
257 } 223 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698