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/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/strings/string_tokenizer.h" | 8 #include "base/strings/string_tokenizer.h" |
9 #include "mojo/examples/media_viewer/media_viewer.mojom.h" | 9 #include "mojo/examples/media_viewer/media_viewer.mojom.h" |
10 #include "mojo/public/cpp/application/application_connection.h" | 10 #include "mojo/public/cpp/application/application_connection.h" |
11 #include "mojo/public/cpp/application/application_delegate.h" | 11 #include "mojo/public/cpp/application/application_delegate.h" |
12 #include "mojo/public/cpp/application/interface_factory_impl.h" | 12 #include "mojo/public/cpp/application/interface_factory_impl.h" |
13 #include "mojo/services/public/cpp/view_manager/node.h" | |
14 #include "mojo/services/public/cpp/view_manager/node_observer.h" | |
15 #include "mojo/services/public/cpp/view_manager/types.h" | 13 #include "mojo/services/public/cpp/view_manager/types.h" |
| 14 #include "mojo/services/public/cpp/view_manager/view.h" |
16 #include "mojo/services/public/cpp/view_manager/view_manager.h" | 15 #include "mojo/services/public/cpp/view_manager/view_manager.h" |
17 #include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h" | 16 #include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h" |
18 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" | 17 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" |
| 18 #include "mojo/services/public/cpp/view_manager/view_observer.h" |
19 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h" | 19 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h" |
20 #include "skia/ext/platform_canvas.h" | 20 #include "skia/ext/platform_canvas.h" |
21 #include "skia/ext/refptr.h" | 21 #include "skia/ext/refptr.h" |
22 #include "third_party/skia/include/core/SkBitmap.h" | 22 #include "third_party/skia/include/core/SkBitmap.h" |
23 #include "third_party/skia/include/core/SkCanvas.h" | 23 #include "third_party/skia/include/core/SkCanvas.h" |
24 #include "third_party/skia/include/core/SkPaint.h" | 24 #include "third_party/skia/include/core/SkPaint.h" |
25 #include "third_party/skia/include/core/SkScalar.h" | 25 #include "third_party/skia/include/core/SkScalar.h" |
26 #include "ui/gfx/codec/png_codec.h" | 26 #include "ui/gfx/codec/png_codec.h" |
27 | 27 |
28 namespace mojo { | 28 namespace mojo { |
(...skipping 18 matching lines...) Expand all Loading... |
47 }; | 47 }; |
48 | 48 |
49 class NavigatorImpl : public InterfaceImpl<Navigator> { | 49 class NavigatorImpl : public InterfaceImpl<Navigator> { |
50 public: | 50 public: |
51 explicit NavigatorImpl(PNGViewer* viewer) : viewer_(viewer) {} | 51 explicit NavigatorImpl(PNGViewer* viewer) : viewer_(viewer) {} |
52 virtual ~NavigatorImpl() {} | 52 virtual ~NavigatorImpl() {} |
53 | 53 |
54 private: | 54 private: |
55 // Overridden from Navigator: | 55 // Overridden from Navigator: |
56 virtual void Navigate( | 56 virtual void Navigate( |
57 uint32_t node_id, | 57 uint32_t view_id, |
58 NavigationDetailsPtr navigation_details, | 58 NavigationDetailsPtr navigation_details, |
59 ResponseDetailsPtr response_details) OVERRIDE { | 59 ResponseDetailsPtr response_details) OVERRIDE { |
60 int content_length = GetContentLength(response_details->response->headers); | 60 int content_length = GetContentLength(response_details->response->headers); |
61 unsigned char* data = new unsigned char[content_length]; | 61 unsigned char* data = new unsigned char[content_length]; |
62 unsigned char* buf = data; | 62 unsigned char* buf = data; |
63 uint32_t bytes_remaining = content_length; | 63 uint32_t bytes_remaining = content_length; |
64 uint32_t num_bytes = bytes_remaining; | 64 uint32_t num_bytes = bytes_remaining; |
65 while (bytes_remaining > 0) { | 65 while (bytes_remaining > 0) { |
66 MojoResult result = ReadDataRaw( | 66 MojoResult result = ReadDataRaw( |
67 response_details->response->body.get(), | 67 response_details->response->body.get(), |
68 buf, | 68 buf, |
69 &num_bytes, | 69 &num_bytes, |
70 MOJO_READ_DATA_FLAG_NONE); | 70 MOJO_READ_DATA_FLAG_NONE); |
71 if (result == MOJO_RESULT_SHOULD_WAIT) { | 71 if (result == MOJO_RESULT_SHOULD_WAIT) { |
72 Wait(response_details->response->body.get(), | 72 Wait(response_details->response->body.get(), |
73 MOJO_HANDLE_SIGNAL_READABLE, | 73 MOJO_HANDLE_SIGNAL_READABLE, |
74 MOJO_DEADLINE_INDEFINITE); | 74 MOJO_DEADLINE_INDEFINITE); |
75 } else if (result == MOJO_RESULT_OK) { | 75 } else if (result == MOJO_RESULT_OK) { |
76 buf += num_bytes; | 76 buf += num_bytes; |
77 num_bytes = bytes_remaining -= num_bytes; | 77 num_bytes = bytes_remaining -= num_bytes; |
78 } else { | 78 } else { |
79 break; | 79 break; |
80 } | 80 } |
81 } | 81 } |
82 | 82 |
83 SkBitmap bitmap; | 83 SkBitmap bitmap; |
84 gfx::PNGCodec::Decode(static_cast<const unsigned char*>(data), | 84 gfx::PNGCodec::Decode(static_cast<const unsigned char*>(data), |
85 content_length, &bitmap); | 85 content_length, &bitmap); |
86 UpdateView(node_id, bitmap); | 86 UpdateView(view_id, bitmap); |
87 | 87 |
88 delete[] data; | 88 delete[] data; |
89 } | 89 } |
90 | 90 |
91 void UpdateView(Id node_id, const SkBitmap& bitmap); | 91 void UpdateView(Id view_id, const SkBitmap& bitmap); |
92 | 92 |
93 int GetContentLength(const Array<String>& headers) { | 93 int GetContentLength(const Array<String>& headers) { |
94 for (size_t i = 0; i < headers.size(); ++i) { | 94 for (size_t i = 0; i < headers.size(); ++i) { |
95 base::StringTokenizer t(headers[i], ": ;="); | 95 base::StringTokenizer t(headers[i], ": ;="); |
96 while (t.GetNext()) { | 96 while (t.GetNext()) { |
97 if (!t.token_is_delim() && t.token() == "Content-Length") { | 97 if (!t.token_is_delim() && t.token() == "Content-Length") { |
98 while (t.GetNext()) { | 98 while (t.GetNext()) { |
99 if (!t.token_is_delim()) | 99 if (!t.token_is_delim()) |
100 return atoi(t.token().c_str()); | 100 return atoi(t.token().c_str()); |
101 } | 101 } |
102 } | 102 } |
103 } | 103 } |
104 } | 104 } |
105 return 0; | 105 return 0; |
106 } | 106 } |
107 | 107 |
108 PNGViewer* viewer_; | 108 PNGViewer* viewer_; |
109 | 109 |
110 DISALLOW_COPY_AND_ASSIGN(NavigatorImpl); | 110 DISALLOW_COPY_AND_ASSIGN(NavigatorImpl); |
111 }; | 111 }; |
112 | 112 |
113 class PNGViewer | 113 class PNGViewer |
114 : public ApplicationDelegate, | 114 : public ApplicationDelegate, |
115 public ViewManagerDelegate, | 115 public ViewManagerDelegate, |
116 public NodeObserver { | 116 public ViewObserver { |
117 public: | 117 public: |
118 PNGViewer() | 118 PNGViewer() |
119 : navigator_factory_(this), | 119 : navigator_factory_(this), |
120 zoomable_media_factory_(this), | 120 zoomable_media_factory_(this), |
121 view_manager_client_factory_(this), | 121 view_manager_client_factory_(this), |
122 root_(NULL), | 122 root_(NULL), |
123 zoom_percentage_(kDefaultZoomPercentage) {} | 123 zoom_percentage_(kDefaultZoomPercentage) {} |
124 virtual ~PNGViewer() { | 124 virtual ~PNGViewer() { |
125 if (root_) | 125 if (root_) |
126 root_->RemoveObserver(this); | 126 root_->RemoveObserver(this); |
127 } | 127 } |
128 | 128 |
129 void UpdateView(Id node_id, const SkBitmap& bitmap) { | 129 void UpdateView(Id view_id, const SkBitmap& bitmap) { |
130 bitmap_ = bitmap; | 130 bitmap_ = bitmap; |
131 zoom_percentage_ = kDefaultZoomPercentage; | 131 zoom_percentage_ = kDefaultZoomPercentage; |
132 DrawBitmap(); | 132 DrawBitmap(); |
133 } | 133 } |
134 | 134 |
135 void ZoomIn() { | 135 void ZoomIn() { |
136 if (zoom_percentage_ >= kMaxZoomPercentage) | 136 if (zoom_percentage_ >= kMaxZoomPercentage) |
137 return; | 137 return; |
138 zoom_percentage_ += kZoomStep; | 138 zoom_percentage_ += kZoomStep; |
139 DrawBitmap(); | 139 DrawBitmap(); |
(...skipping 23 matching lines...) Expand all Loading... |
163 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) | 163 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) |
164 MOJO_OVERRIDE { | 164 MOJO_OVERRIDE { |
165 connection->AddService(&navigator_factory_); | 165 connection->AddService(&navigator_factory_); |
166 connection->AddService(&zoomable_media_factory_); | 166 connection->AddService(&zoomable_media_factory_); |
167 connection->AddService(&view_manager_client_factory_); | 167 connection->AddService(&view_manager_client_factory_); |
168 return true; | 168 return true; |
169 } | 169 } |
170 | 170 |
171 // Overridden from ViewManagerDelegate: | 171 // Overridden from ViewManagerDelegate: |
172 virtual void OnEmbed(ViewManager* view_manager, | 172 virtual void OnEmbed(ViewManager* view_manager, |
173 Node* root, | 173 View* root, |
174 ServiceProviderImpl* exported_services, | 174 ServiceProviderImpl* exported_services, |
175 scoped_ptr<ServiceProvider> imported_services) OVERRIDE { | 175 scoped_ptr<ServiceProvider> imported_services) OVERRIDE { |
176 root_ = root; | 176 root_ = root; |
177 root_->AddObserver(this); | 177 root_->AddObserver(this); |
178 root_->SetColor(SK_ColorGRAY); | 178 root_->SetColor(SK_ColorGRAY); |
179 if (!bitmap_.isNull()) | 179 if (!bitmap_.isNull()) |
180 DrawBitmap(); | 180 DrawBitmap(); |
181 } | 181 } |
182 virtual void OnViewManagerDisconnected( | 182 virtual void OnViewManagerDisconnected( |
183 ViewManager* view_manager) OVERRIDE { | 183 ViewManager* view_manager) OVERRIDE { |
(...skipping 10 matching lines...) Expand all Loading... |
194 true))); | 194 true))); |
195 canvas->drawColor(SK_ColorGRAY); | 195 canvas->drawColor(SK_ColorGRAY); |
196 SkPaint paint; | 196 SkPaint paint; |
197 SkScalar scale = | 197 SkScalar scale = |
198 SkFloatToScalar(zoom_percentage_ * 1.0f / kDefaultZoomPercentage); | 198 SkFloatToScalar(zoom_percentage_ * 1.0f / kDefaultZoomPercentage); |
199 canvas->scale(scale, scale); | 199 canvas->scale(scale, scale); |
200 canvas->drawBitmap(bitmap_, 0, 0, &paint); | 200 canvas->drawBitmap(bitmap_, 0, 0, &paint); |
201 root_->SetContents(skia::GetTopDevice(*canvas)->accessBitmap(true)); | 201 root_->SetContents(skia::GetTopDevice(*canvas)->accessBitmap(true)); |
202 } | 202 } |
203 | 203 |
204 // NodeObserver: | 204 // ViewObserver: |
205 virtual void OnNodeBoundsChanged(Node* node, | 205 virtual void OnViewBoundsChanged(View* view, |
206 const gfx::Rect& old_bounds, | 206 const gfx::Rect& old_bounds, |
207 const gfx::Rect& new_bounds) OVERRIDE { | 207 const gfx::Rect& new_bounds) OVERRIDE { |
208 DCHECK_EQ(node, root_); | 208 DCHECK_EQ(view, root_); |
209 DrawBitmap(); | 209 DrawBitmap(); |
210 } | 210 } |
211 virtual void OnNodeDestroyed(Node* node) OVERRIDE { | 211 virtual void OnViewDestroyed(View* view) OVERRIDE { |
212 DCHECK_EQ(node, root_); | 212 DCHECK_EQ(view, root_); |
213 node->RemoveObserver(this); | 213 view->RemoveObserver(this); |
214 root_ = NULL; | 214 root_ = NULL; |
215 } | 215 } |
216 | 216 |
217 InterfaceFactoryImplWithContext<NavigatorImpl, PNGViewer> navigator_factory_; | 217 InterfaceFactoryImplWithContext<NavigatorImpl, PNGViewer> navigator_factory_; |
218 InterfaceFactoryImplWithContext<ZoomableMediaImpl, PNGViewer> | 218 InterfaceFactoryImplWithContext<ZoomableMediaImpl, PNGViewer> |
219 zoomable_media_factory_; | 219 zoomable_media_factory_; |
220 ViewManagerClientFactory view_manager_client_factory_; | 220 ViewManagerClientFactory view_manager_client_factory_; |
221 | 221 |
222 Node* root_; | 222 View* root_; |
223 SkBitmap bitmap_; | 223 SkBitmap bitmap_; |
224 uint16_t zoom_percentage_; | 224 uint16_t zoom_percentage_; |
225 | 225 |
226 DISALLOW_COPY_AND_ASSIGN(PNGViewer); | 226 DISALLOW_COPY_AND_ASSIGN(PNGViewer); |
227 }; | 227 }; |
228 | 228 |
229 void ZoomableMediaImpl::ZoomIn() { | 229 void ZoomableMediaImpl::ZoomIn() { |
230 viewer_->ZoomIn(); | 230 viewer_->ZoomIn(); |
231 } | 231 } |
232 | 232 |
233 void ZoomableMediaImpl::ZoomOut() { | 233 void ZoomableMediaImpl::ZoomOut() { |
234 viewer_->ZoomOut(); | 234 viewer_->ZoomOut(); |
235 } | 235 } |
236 | 236 |
237 void ZoomableMediaImpl::ZoomToActualSize() { | 237 void ZoomableMediaImpl::ZoomToActualSize() { |
238 viewer_->ZoomToActualSize(); | 238 viewer_->ZoomToActualSize(); |
239 } | 239 } |
240 | 240 |
241 void NavigatorImpl::UpdateView(Id node_id, | 241 void NavigatorImpl::UpdateView(Id view_id, |
242 const SkBitmap& bitmap) { | 242 const SkBitmap& bitmap) { |
243 viewer_->UpdateView(node_id, bitmap); | 243 viewer_->UpdateView(view_id, bitmap); |
244 } | 244 } |
245 | 245 |
246 } // namespace examples | 246 } // namespace examples |
247 | 247 |
248 // static | 248 // static |
249 ApplicationDelegate* ApplicationDelegate::Create() { | 249 ApplicationDelegate* ApplicationDelegate::Create() { |
250 return new examples::PNGViewer; | 250 return new examples::PNGViewer; |
251 } | 251 } |
252 | 252 |
253 } // namespace mojo | 253 } // namespace mojo |
OLD | NEW |