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 <algorithm> | 5 #include <algorithm> |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/strings/string_tokenizer.h" | 10 #include "base/strings/string_tokenizer.h" |
11 #include "examples/bitmap_uploader/bitmap_uploader.h" | 11 #include "examples/bitmap_uploader/bitmap_uploader.h" |
12 #include "examples/media_viewer/media_viewer.mojom.h" | 12 #include "examples/media_viewer/media_viewer.mojom.h" |
13 #include "mojo/application/application_runner_chromium.h" | 13 #include "mojo/application/application_runner_chromium.h" |
| 14 #include "mojo/application/content_handler_factory.h" |
14 #include "mojo/public/c/system/main.h" | 15 #include "mojo/public/c/system/main.h" |
15 #include "mojo/public/cpp/application/application_connection.h" | 16 #include "mojo/public/cpp/application/application_connection.h" |
16 #include "mojo/public/cpp/application/application_delegate.h" | 17 #include "mojo/public/cpp/application/application_delegate.h" |
17 #include "mojo/public/cpp/application/application_impl.h" | 18 #include "mojo/public/cpp/application/application_impl.h" |
18 #include "mojo/public/cpp/application/interface_factory_impl.h" | 19 #include "mojo/public/cpp/application/interface_factory_impl.h" |
19 #include "mojo/public/cpp/application/service_provider_impl.h" | 20 #include "mojo/public/cpp/application/service_provider_impl.h" |
20 #include "mojo/services/public/cpp/view_manager/types.h" | 21 #include "mojo/services/public/cpp/view_manager/types.h" |
21 #include "mojo/services/public/cpp/view_manager/view.h" | 22 #include "mojo/services/public/cpp/view_manager/view.h" |
22 #include "mojo/services/public/cpp/view_manager/view_manager.h" | 23 #include "mojo/services/public/cpp/view_manager/view_manager.h" |
23 #include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h" | 24 #include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h" |
24 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" | 25 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" |
25 #include "mojo/services/public/cpp/view_manager/view_observer.h" | 26 #include "mojo/services/public/cpp/view_manager/view_observer.h" |
26 #include "mojo/services/public/interfaces/content_handler/content_handler.mojom.
h" | 27 #include "mojo/services/public/interfaces/content_handler/content_handler.mojom.
h" |
27 #include "third_party/skia/include/core/SkColor.h" | 28 #include "third_party/skia/include/core/SkColor.h" |
28 #include "ui/gfx/codec/png_codec.h" | 29 #include "ui/gfx/codec/png_codec.h" |
29 | 30 |
30 namespace mojo { | 31 namespace mojo { |
31 namespace examples { | 32 namespace examples { |
32 | 33 |
33 class PNGViewer; | |
34 | |
35 // TODO(aa): Hook up ZoomableMedia interface again. | 34 // TODO(aa): Hook up ZoomableMedia interface again. |
36 class PNGView : public ViewManagerDelegate, public ViewObserver { | 35 class PNGView : public ApplicationDelegate, |
| 36 public ViewManagerDelegate, |
| 37 public ViewObserver { |
37 public: | 38 public: |
38 static void Spawn(URLResponsePtr response, | 39 PNGView(URLResponsePtr response) |
39 ServiceProviderImpl* exported_services, | |
40 scoped_ptr<ServiceProvider> imported_services, | |
41 Shell* shell) { | |
42 // PNGView deletes itself when its View is destroyed. | |
43 new PNGView( | |
44 response.Pass(), exported_services, imported_services.Pass(), shell); | |
45 } | |
46 | |
47 private: | |
48 static const uint16_t kMaxZoomPercentage = 400; | |
49 static const uint16_t kMinZoomPercentage = 20; | |
50 static const uint16_t kDefaultZoomPercentage = 100; | |
51 static const uint16_t kZoomStep = 20; | |
52 | |
53 PNGView(URLResponsePtr response, | |
54 ServiceProviderImpl* exported_services, | |
55 scoped_ptr<ServiceProvider> imported_services, | |
56 Shell* shell) | |
57 : width_(0), | 40 : width_(0), |
58 height_(0), | 41 height_(0), |
59 imported_services_(imported_services.Pass()), | 42 app_(nullptr), |
60 shell_(shell), | |
61 root_(nullptr), | 43 root_(nullptr), |
62 view_manager_client_factory_(shell, this), | |
63 zoom_percentage_(kDefaultZoomPercentage) { | 44 zoom_percentage_(kDefaultZoomPercentage) { |
64 exported_services->AddService(&view_manager_client_factory_); | |
65 DecodePNG(response.Pass()); | 45 DecodePNG(response.Pass()); |
66 } | 46 } |
67 | 47 |
68 virtual ~PNGView() { | 48 virtual ~PNGView() { |
69 if (root_) | 49 if (root_) |
70 root_->RemoveObserver(this); | 50 root_->RemoveObserver(this); |
71 } | 51 } |
72 | 52 |
| 53 private: |
| 54 static const uint16_t kMaxZoomPercentage = 400; |
| 55 static const uint16_t kMinZoomPercentage = 20; |
| 56 static const uint16_t kDefaultZoomPercentage = 100; |
| 57 static const uint16_t kZoomStep = 20; |
| 58 |
| 59 // Overridden from ApplicationDelegate: |
| 60 virtual void Initialize(ApplicationImpl* app) override { |
| 61 app_ = app; |
| 62 view_manager_client_factory_.reset( |
| 63 new ViewManagerClientFactory(app->shell(), this)); |
| 64 } |
| 65 |
| 66 // Overridden from ApplicationDelegate: |
| 67 virtual bool ConfigureIncomingConnection( |
| 68 ApplicationConnection* connection) override { |
| 69 connection->AddService(view_manager_client_factory_.get()); |
| 70 return true; |
| 71 } |
| 72 |
73 // Overridden from ViewManagerDelegate: | 73 // Overridden from ViewManagerDelegate: |
74 virtual void OnEmbed(ViewManager* view_manager, | 74 virtual void OnEmbed(ViewManager* view_manager, |
75 View* root, | 75 View* root, |
76 ServiceProviderImpl* exported_services, | 76 ServiceProviderImpl* exported_services, |
77 scoped_ptr<ServiceProvider> imported_services) override { | 77 scoped_ptr<ServiceProvider> imported_services) override { |
| 78 // TODO(qsr): The same view should be embeddable on multiple views. |
| 79 DCHECK(!root_); |
78 root_ = root; | 80 root_ = root; |
79 root_->AddObserver(this); | 81 root_->AddObserver(this); |
80 bitmap_uploader_.reset(new BitmapUploader(root_)); | 82 bitmap_uploader_.reset(new BitmapUploader(root_)); |
81 bitmap_uploader_->Init(shell_); | 83 bitmap_uploader_->Init(app_->shell()); |
82 bitmap_uploader_->SetColor(SK_ColorGRAY); | 84 bitmap_uploader_->SetColor(SK_ColorGRAY); |
83 if (bitmap_.get()) | 85 if (bitmap_.get()) |
84 DrawBitmap(); | 86 DrawBitmap(); |
85 } | 87 } |
86 | 88 |
87 virtual void OnViewManagerDisconnected(ViewManager* view_manager) override { | 89 virtual void OnViewManagerDisconnected(ViewManager* view_manager) override { |
88 // TODO(aa): Need to figure out how shutdown works. | 90 // TODO(aa): Need to figure out how shutdown works. |
89 } | 91 } |
90 | 92 |
91 // Overridden from ViewObserver: | 93 // Overridden from ViewObserver: |
92 virtual void OnViewBoundsChanged(View* view, | 94 virtual void OnViewBoundsChanged(View* view, |
93 const Rect& old_bounds, | 95 const Rect& old_bounds, |
94 const Rect& new_bounds) override { | 96 const Rect& new_bounds) override { |
95 DCHECK_EQ(view, root_); | 97 DCHECK_EQ(view, root_); |
96 DrawBitmap(); | 98 DrawBitmap(); |
97 } | 99 } |
98 | 100 |
99 virtual void OnViewDestroyed(View* view) override { | 101 virtual void OnViewDestroyed(View* view) override { |
100 DCHECK_EQ(view, root_); | 102 DCHECK_EQ(view, root_); |
101 delete this; | 103 // TODO(qsr): It should not be necessary to cleanup the uploader, but it |
| 104 // crashes if the GL context goes away. |
| 105 bitmap_uploader_.reset(); |
| 106 ApplicationImpl::Terminate(); |
| 107 } |
| 108 |
| 109 void DrawBitmap() { |
| 110 if (!root_) |
| 111 return; |
| 112 |
| 113 bitmap_uploader_->SetBitmap( |
| 114 width_, height_, bitmap_.Pass(), BitmapUploader::BGRA); |
| 115 } |
| 116 |
| 117 void ZoomIn() { |
| 118 if (zoom_percentage_ >= kMaxZoomPercentage) |
| 119 return; |
| 120 zoom_percentage_ += kZoomStep; |
| 121 DrawBitmap(); |
| 122 } |
| 123 |
| 124 void ZoomOut() { |
| 125 if (zoom_percentage_ <= kMinZoomPercentage) |
| 126 return; |
| 127 zoom_percentage_ -= kZoomStep; |
| 128 DrawBitmap(); |
| 129 } |
| 130 |
| 131 void ZoomToActualSize() { |
| 132 if (zoom_percentage_ == kDefaultZoomPercentage) |
| 133 return; |
| 134 zoom_percentage_ = kDefaultZoomPercentage; |
| 135 DrawBitmap(); |
102 } | 136 } |
103 | 137 |
104 void DecodePNG(URLResponsePtr response) { | 138 void DecodePNG(URLResponsePtr response) { |
105 int content_length = GetContentLength(response->headers); | 139 int content_length = GetContentLength(response->headers); |
106 scoped_ptr<unsigned char[]> data(new unsigned char[content_length]); | 140 scoped_ptr<unsigned char[]> data(new unsigned char[content_length]); |
107 unsigned char* buf = data.get(); | 141 unsigned char* buf = data.get(); |
108 uint32_t bytes_remaining = content_length; | 142 uint32_t bytes_remaining = content_length; |
109 uint32_t num_bytes = bytes_remaining; | 143 uint32_t num_bytes = bytes_remaining; |
110 while (bytes_remaining > 0) { | 144 while (bytes_remaining > 0) { |
111 MojoResult result = ReadDataRaw( | 145 MojoResult result = ReadDataRaw( |
(...skipping 12 matching lines...) Expand all Loading... |
124 | 158 |
125 bitmap_.reset(new std::vector<unsigned char>); | 159 bitmap_.reset(new std::vector<unsigned char>); |
126 gfx::PNGCodec::Decode(static_cast<const unsigned char*>(data.get()), | 160 gfx::PNGCodec::Decode(static_cast<const unsigned char*>(data.get()), |
127 content_length, | 161 content_length, |
128 gfx::PNGCodec::FORMAT_BGRA, | 162 gfx::PNGCodec::FORMAT_BGRA, |
129 bitmap_.get(), | 163 bitmap_.get(), |
130 &width_, | 164 &width_, |
131 &height_); | 165 &height_); |
132 } | 166 } |
133 | 167 |
134 void DrawBitmap() { | |
135 if (!root_) | |
136 return; | |
137 | |
138 | |
139 bitmap_uploader_->SetBitmap(width_, height_, bitmap_.Pass(), | |
140 BitmapUploader::BGRA); | |
141 } | |
142 | |
143 void ZoomIn() { | |
144 if (zoom_percentage_ >= kMaxZoomPercentage) | |
145 return; | |
146 zoom_percentage_ += kZoomStep; | |
147 DrawBitmap(); | |
148 } | |
149 | |
150 void ZoomOut() { | |
151 if (zoom_percentage_ <= kMinZoomPercentage) | |
152 return; | |
153 zoom_percentage_ -= kZoomStep; | |
154 DrawBitmap(); | |
155 } | |
156 | |
157 void ZoomToActualSize() { | |
158 if (zoom_percentage_ == kDefaultZoomPercentage) | |
159 return; | |
160 zoom_percentage_ = kDefaultZoomPercentage; | |
161 DrawBitmap(); | |
162 } | |
163 | |
164 int GetContentLength(const Array<String>& headers) { | 168 int GetContentLength(const Array<String>& headers) { |
165 for (size_t i = 0; i < headers.size(); ++i) { | 169 for (size_t i = 0; i < headers.size(); ++i) { |
166 base::StringTokenizer t(headers[i], ": ;="); | 170 base::StringTokenizer t(headers[i], ": ;="); |
167 while (t.GetNext()) { | 171 while (t.GetNext()) { |
168 if (!t.token_is_delim() && t.token() == "Content-Length") { | 172 if (!t.token_is_delim() && t.token() == "Content-Length") { |
169 while (t.GetNext()) { | 173 while (t.GetNext()) { |
170 if (!t.token_is_delim()) | 174 if (!t.token_is_delim()) |
171 return atoi(t.token().c_str()); | 175 return atoi(t.token().c_str()); |
172 } | 176 } |
173 } | 177 } |
174 } | 178 } |
175 } | 179 } |
176 return 0; | 180 return 0; |
177 } | 181 } |
178 | 182 |
179 int width_; | 183 int width_; |
180 int height_; | 184 int height_; |
181 scoped_ptr<std::vector<unsigned char>> bitmap_; | 185 scoped_ptr<std::vector<unsigned char>> bitmap_; |
182 scoped_ptr<ServiceProvider> imported_services_; | 186 ApplicationImpl* app_; |
183 Shell* shell_; | |
184 View* root_; | 187 View* root_; |
185 ViewManagerClientFactory view_manager_client_factory_; | 188 scoped_ptr<ViewManagerClientFactory> view_manager_client_factory_; |
186 uint16_t zoom_percentage_; | 189 uint16_t zoom_percentage_; |
187 scoped_ptr<BitmapUploader> bitmap_uploader_; | 190 scoped_ptr<BitmapUploader> bitmap_uploader_; |
188 | 191 |
189 DISALLOW_COPY_AND_ASSIGN(PNGView); | 192 DISALLOW_COPY_AND_ASSIGN(PNGView); |
190 }; | 193 }; |
191 | 194 |
192 class ContentHandlerImpl : public InterfaceImpl<ContentHandler> { | 195 class PNGViewer : public ApplicationDelegate, |
| 196 public ContentHandlerFactory::Delegate { |
193 public: | 197 public: |
194 explicit ContentHandlerImpl(Shell* shell) : shell_(shell) {} | 198 PNGViewer() : content_handler_factory_(this) {} |
195 virtual ~ContentHandlerImpl() {} | |
196 | 199 |
197 private: | 200 private: |
198 // Overridden from ContentHandler: | |
199 virtual void OnConnect( | |
200 const mojo::String& requestor_url, | |
201 URLResponsePtr response, | |
202 InterfaceRequest<ServiceProvider> service_provider) override { | |
203 ServiceProviderImpl* exported_services = new ServiceProviderImpl(); | |
204 BindToRequest(exported_services, &service_provider); | |
205 scoped_ptr<ServiceProvider> remote( | |
206 exported_services->CreateRemoteServiceProvider()); | |
207 PNGView::Spawn(response.Pass(), exported_services, remote.Pass(), shell_); | |
208 } | |
209 | |
210 Shell* shell_; | |
211 | |
212 DISALLOW_COPY_AND_ASSIGN(ContentHandlerImpl); | |
213 }; | |
214 | |
215 class PNGViewer : public ApplicationDelegate { | |
216 public: | |
217 PNGViewer() {} | |
218 private: | |
219 // Overridden from ApplicationDelegate: | |
220 virtual void Initialize(ApplicationImpl* app) override { | |
221 content_handler_factory_.reset( | |
222 new InterfaceFactoryImplWithContext<ContentHandlerImpl, Shell>( | |
223 app->shell())); | |
224 } | |
225 | |
226 // Overridden from ApplicationDelegate: | 201 // Overridden from ApplicationDelegate: |
227 virtual bool ConfigureIncomingConnection( | 202 virtual bool ConfigureIncomingConnection( |
228 ApplicationConnection* connection) override { | 203 ApplicationConnection* connection) override { |
229 connection->AddService(content_handler_factory_.get()); | 204 connection->AddService(&content_handler_factory_); |
230 return true; | 205 return true; |
231 } | 206 } |
232 | 207 |
233 scoped_ptr<InterfaceFactoryImplWithContext<ContentHandlerImpl, Shell> > | 208 // Overridden from ContentHandlerFactory::Delegate: |
234 content_handler_factory_; | 209 virtual scoped_ptr<ContentHandlerFactory::HandledApplicationHolder> |
| 210 CreateApplication(ShellPtr shell, URLResponsePtr response) override { |
| 211 return make_handled_factory_holder(new mojo::ApplicationImpl( |
| 212 new PNGView(response.Pass()), shell.PassMessagePipe())); |
| 213 } |
| 214 |
| 215 ContentHandlerFactory content_handler_factory_; |
235 | 216 |
236 DISALLOW_COPY_AND_ASSIGN(PNGViewer); | 217 DISALLOW_COPY_AND_ASSIGN(PNGViewer); |
237 }; | 218 }; |
238 | 219 |
239 } // namespace examples | 220 } // namespace examples |
240 } // namespace mojo | 221 } // namespace mojo |
241 | 222 |
242 MojoResult MojoMain(MojoHandle shell_handle) { | 223 MojoResult MojoMain(MojoHandle shell_handle) { |
243 mojo::ApplicationRunnerChromium runner(new mojo::examples::PNGViewer); | 224 mojo::ApplicationRunnerChromium runner(new mojo::examples::PNGViewer()); |
244 return runner.Run(shell_handle); | 225 return runner.Run(shell_handle); |
245 } | 226 } |
OLD | NEW |