| 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" |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 } | 82 } |
| 83 | 83 |
| 84 SkBitmap bitmap; | 84 SkBitmap bitmap; |
| 85 gfx::PNGCodec::Decode(static_cast<const unsigned char*>(data), | 85 gfx::PNGCodec::Decode(static_cast<const unsigned char*>(data), |
| 86 content_length, &bitmap); | 86 content_length, &bitmap); |
| 87 UpdateView(node_id, bitmap); | 87 UpdateView(node_id, bitmap); |
| 88 | 88 |
| 89 delete[] data; | 89 delete[] data; |
| 90 } | 90 } |
| 91 | 91 |
| 92 void UpdateView(view_manager::Id node_id, const SkBitmap& bitmap); | 92 void UpdateView(Id node_id, const SkBitmap& bitmap); |
| 93 | 93 |
| 94 int GetContentLength(const Array<String>& headers) { | 94 int GetContentLength(const Array<String>& headers) { |
| 95 for (size_t i = 0; i < headers.size(); ++i) { | 95 for (size_t i = 0; i < headers.size(); ++i) { |
| 96 base::StringTokenizer t(headers[i], ": ;="); | 96 base::StringTokenizer t(headers[i], ": ;="); |
| 97 while (t.GetNext()) { | 97 while (t.GetNext()) { |
| 98 if (!t.token_is_delim() && t.token() == "Content-Length") { | 98 if (!t.token_is_delim() && t.token() == "Content-Length") { |
| 99 while (t.GetNext()) { | 99 while (t.GetNext()) { |
| 100 if (!t.token_is_delim()) | 100 if (!t.token_is_delim()) |
| 101 return atoi(t.token().c_str()); | 101 return atoi(t.token().c_str()); |
| 102 } | 102 } |
| 103 } | 103 } |
| 104 } | 104 } |
| 105 } | 105 } |
| 106 return 0; | 106 return 0; |
| 107 } | 107 } |
| 108 | 108 |
| 109 PNGViewer* viewer_; | 109 PNGViewer* viewer_; |
| 110 | 110 |
| 111 DISALLOW_COPY_AND_ASSIGN(NavigatorImpl); | 111 DISALLOW_COPY_AND_ASSIGN(NavigatorImpl); |
| 112 }; | 112 }; |
| 113 | 113 |
| 114 class PNGViewer | 114 class PNGViewer |
| 115 : public ApplicationDelegate, | 115 : public ApplicationDelegate, |
| 116 public view_manager::ViewManagerDelegate, | 116 public ViewManagerDelegate, |
| 117 public view_manager::NodeObserver, | 117 public NodeObserver, |
| 118 public InterfaceFactoryWithContext<NavigatorImpl, PNGViewer>, | 118 public InterfaceFactoryWithContext<NavigatorImpl, PNGViewer>, |
| 119 public InterfaceFactoryWithContext<ZoomableMediaImpl, PNGViewer> { | 119 public InterfaceFactoryWithContext<ZoomableMediaImpl, PNGViewer> { |
| 120 public: | 120 public: |
| 121 PNGViewer() | 121 PNGViewer() |
| 122 : InterfaceFactoryWithContext<NavigatorImpl, PNGViewer>(this), | 122 : InterfaceFactoryWithContext<NavigatorImpl, PNGViewer>(this), |
| 123 InterfaceFactoryWithContext<ZoomableMediaImpl, PNGViewer>(this), | 123 InterfaceFactoryWithContext<ZoomableMediaImpl, PNGViewer>(this), |
| 124 content_view_(NULL), | 124 content_view_(NULL), |
| 125 root_(NULL), | 125 root_(NULL), |
| 126 zoom_percentage_(kDefaultZoomPercentage), | 126 zoom_percentage_(kDefaultZoomPercentage), |
| 127 view_manager_client_factory_(this) {} | 127 view_manager_client_factory_(this) {} |
| 128 virtual ~PNGViewer() { | 128 virtual ~PNGViewer() { |
| 129 if (root_) | 129 if (root_) |
| 130 root_->RemoveObserver(this); | 130 root_->RemoveObserver(this); |
| 131 } | 131 } |
| 132 | 132 |
| 133 void UpdateView(view_manager::Id node_id, const SkBitmap& bitmap) { | 133 void UpdateView(Id node_id, const SkBitmap& bitmap) { |
| 134 bitmap_ = bitmap; | 134 bitmap_ = bitmap; |
| 135 zoom_percentage_ = kDefaultZoomPercentage; | 135 zoom_percentage_ = kDefaultZoomPercentage; |
| 136 DrawBitmap(); | 136 DrawBitmap(); |
| 137 } | 137 } |
| 138 | 138 |
| 139 void ZoomIn() { | 139 void ZoomIn() { |
| 140 if (zoom_percentage_ >= kMaxZoomPercentage) | 140 if (zoom_percentage_ >= kMaxZoomPercentage) |
| 141 return; | 141 return; |
| 142 zoom_percentage_ += kZoomStep; | 142 zoom_percentage_ += kZoomStep; |
| 143 DrawBitmap(); | 143 DrawBitmap(); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 165 | 165 |
| 166 // Overridden from ApplicationDelegate: | 166 // Overridden from ApplicationDelegate: |
| 167 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) | 167 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) |
| 168 MOJO_OVERRIDE { | 168 MOJO_OVERRIDE { |
| 169 connection->AddService<navigation::Navigator>(this); | 169 connection->AddService<navigation::Navigator>(this); |
| 170 connection->AddService<ZoomableMedia>(this); | 170 connection->AddService<ZoomableMedia>(this); |
| 171 connection->AddService(&view_manager_client_factory_); | 171 connection->AddService(&view_manager_client_factory_); |
| 172 return true; | 172 return true; |
| 173 } | 173 } |
| 174 | 174 |
| 175 // Overridden from view_manager::ViewManagerDelegate: | 175 // Overridden from ViewManagerDelegate: |
| 176 virtual void OnRootAdded(view_manager::ViewManager* view_manager, | 176 virtual void OnRootAdded(ViewManager* view_manager, Node* root) OVERRIDE { |
| 177 view_manager::Node* root) OVERRIDE { | |
| 178 root_ = root; | 177 root_ = root; |
| 179 root_->AddObserver(this); | 178 root_->AddObserver(this); |
| 180 content_view_ = view_manager::View::Create(view_manager); | 179 content_view_ = View::Create(view_manager); |
| 181 root_->SetActiveView(content_view_); | 180 root_->SetActiveView(content_view_); |
| 182 content_view_->SetColor(SK_ColorGRAY); | 181 content_view_->SetColor(SK_ColorGRAY); |
| 183 if (!bitmap_.isNull()) | 182 if (!bitmap_.isNull()) |
| 184 DrawBitmap(); | 183 DrawBitmap(); |
| 185 } | 184 } |
| 186 virtual void OnViewManagerDisconnected( | 185 virtual void OnViewManagerDisconnected( |
| 187 view_manager::ViewManager* view_manager) OVERRIDE { | 186 ViewManager* view_manager) OVERRIDE { |
| 188 base::MessageLoop::current()->Quit(); | 187 base::MessageLoop::current()->Quit(); |
| 189 } | 188 } |
| 190 | 189 |
| 191 void DrawBitmap() { | 190 void DrawBitmap() { |
| 192 if (!content_view_) | 191 if (!content_view_) |
| 193 return; | 192 return; |
| 194 | 193 |
| 195 skia::RefPtr<SkCanvas> canvas(skia::AdoptRef(skia::CreatePlatformCanvas( | 194 skia::RefPtr<SkCanvas> canvas(skia::AdoptRef(skia::CreatePlatformCanvas( |
| 196 content_view_->node()->bounds().width(), | 195 content_view_->node()->bounds().width(), |
| 197 content_view_->node()->bounds().height(), | 196 content_view_->node()->bounds().height(), |
| 198 true))); | 197 true))); |
| 199 canvas->drawColor(SK_ColorGRAY); | 198 canvas->drawColor(SK_ColorGRAY); |
| 200 SkPaint paint; | 199 SkPaint paint; |
| 201 SkScalar scale = | 200 SkScalar scale = |
| 202 SkFloatToScalar(zoom_percentage_ * 1.0f / kDefaultZoomPercentage); | 201 SkFloatToScalar(zoom_percentage_ * 1.0f / kDefaultZoomPercentage); |
| 203 canvas->scale(scale, scale); | 202 canvas->scale(scale, scale); |
| 204 canvas->drawBitmap(bitmap_, 0, 0, &paint); | 203 canvas->drawBitmap(bitmap_, 0, 0, &paint); |
| 205 content_view_->SetContents(skia::GetTopDevice(*canvas)->accessBitmap(true)); | 204 content_view_->SetContents(skia::GetTopDevice(*canvas)->accessBitmap(true)); |
| 206 } | 205 } |
| 207 | 206 |
| 208 // NodeObserver: | 207 // NodeObserver: |
| 209 virtual void OnNodeBoundsChanged(view_manager::Node* node, | 208 virtual void OnNodeBoundsChanged(Node* node, |
| 210 const gfx::Rect& old_bounds, | 209 const gfx::Rect& old_bounds, |
| 211 const gfx::Rect& new_bounds) OVERRIDE { | 210 const gfx::Rect& new_bounds) OVERRIDE { |
| 212 DCHECK_EQ(node, root_); | 211 DCHECK_EQ(node, root_); |
| 213 DrawBitmap(); | 212 DrawBitmap(); |
| 214 } | 213 } |
| 215 virtual void OnNodeDestroyed(view_manager::Node* node) OVERRIDE { | 214 virtual void OnNodeDestroyed(Node* node) OVERRIDE { |
| 216 DCHECK_EQ(node, root_); | 215 DCHECK_EQ(node, root_); |
| 217 node->RemoveObserver(this); | 216 node->RemoveObserver(this); |
| 218 root_ = NULL; | 217 root_ = NULL; |
| 219 } | 218 } |
| 220 | 219 |
| 221 view_manager::View* content_view_; | 220 View* content_view_; |
| 222 view_manager::Node* root_; | 221 Node* root_; |
| 223 SkBitmap bitmap_; | 222 SkBitmap bitmap_; |
| 224 uint16_t zoom_percentage_; | 223 uint16_t zoom_percentage_; |
| 225 view_manager::ViewManagerClientFactory view_manager_client_factory_; | 224 ViewManagerClientFactory view_manager_client_factory_; |
| 226 | 225 |
| 227 DISALLOW_COPY_AND_ASSIGN(PNGViewer); | 226 DISALLOW_COPY_AND_ASSIGN(PNGViewer); |
| 228 }; | 227 }; |
| 229 | 228 |
| 230 void ZoomableMediaImpl::ZoomIn() { | 229 void ZoomableMediaImpl::ZoomIn() { |
| 231 viewer_->ZoomIn(); | 230 viewer_->ZoomIn(); |
| 232 } | 231 } |
| 233 | 232 |
| 234 void ZoomableMediaImpl::ZoomOut() { | 233 void ZoomableMediaImpl::ZoomOut() { |
| 235 viewer_->ZoomOut(); | 234 viewer_->ZoomOut(); |
| 236 } | 235 } |
| 237 | 236 |
| 238 void ZoomableMediaImpl::ZoomToActualSize() { | 237 void ZoomableMediaImpl::ZoomToActualSize() { |
| 239 viewer_->ZoomToActualSize(); | 238 viewer_->ZoomToActualSize(); |
| 240 } | 239 } |
| 241 | 240 |
| 242 void NavigatorImpl::UpdateView(view_manager::Id node_id, | 241 void NavigatorImpl::UpdateView(Id node_id, |
| 243 const SkBitmap& bitmap) { | 242 const SkBitmap& bitmap) { |
| 244 viewer_->UpdateView(node_id, bitmap); | 243 viewer_->UpdateView(node_id, bitmap); |
| 245 } | 244 } |
| 246 | 245 |
| 247 } // namespace examples | 246 } // namespace examples |
| 248 | 247 |
| 249 // static | 248 // static |
| 250 ApplicationDelegate* ApplicationDelegate::Create() { | 249 ApplicationDelegate* ApplicationDelegate::Create() { |
| 251 return new examples::PNGViewer; | 250 return new examples::PNGViewer; |
| 252 } | 251 } |
| 253 | 252 |
| 254 } // namespace mojo | 253 } // namespace mojo |
| OLD | NEW |