Chromium Code Reviews| 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/public/interfaces/application/service_provider.mojom.h" | |
| 14 // TODO(aa): Ugh, we need to hook up ViewManagerClientFactory to ContentHandler somehow. | |
| 15 #include "mojo/services/public/cpp/view_manager/lib/view_manager_client_impl.h" | |
| 13 #include "mojo/services/public/cpp/view_manager/types.h" | 16 #include "mojo/services/public/cpp/view_manager/types.h" |
| 14 #include "mojo/services/public/cpp/view_manager/view.h" | 17 #include "mojo/services/public/cpp/view_manager/view.h" |
| 15 #include "mojo/services/public/cpp/view_manager/view_manager.h" | 18 #include "mojo/services/public/cpp/view_manager/view_manager.h" |
| 16 #include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h" | |
| 17 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" | 19 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" |
| 18 #include "mojo/services/public/cpp/view_manager/view_observer.h" | 20 #include "mojo/services/public/cpp/view_manager/view_observer.h" |
| 19 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h" | 21 #include "mojo/services/public/interfaces/content_handler/content_handler.mojom. h" |
| 20 #include "skia/ext/platform_canvas.h" | 22 #include "skia/ext/platform_canvas.h" |
| 21 #include "skia/ext/refptr.h" | 23 #include "skia/ext/refptr.h" |
| 22 #include "third_party/skia/include/core/SkBitmap.h" | 24 #include "third_party/skia/include/core/SkBitmap.h" |
| 23 #include "third_party/skia/include/core/SkCanvas.h" | 25 #include "third_party/skia/include/core/SkCanvas.h" |
| 24 #include "third_party/skia/include/core/SkPaint.h" | 26 #include "third_party/skia/include/core/SkPaint.h" |
| 25 #include "third_party/skia/include/core/SkScalar.h" | 27 #include "third_party/skia/include/core/SkScalar.h" |
| 26 #include "ui/gfx/codec/png_codec.h" | 28 #include "ui/gfx/codec/png_codec.h" |
| 27 | 29 |
| 28 namespace mojo { | 30 namespace mojo { |
| 29 namespace examples { | 31 namespace examples { |
| 30 | 32 |
| 31 class PNGViewer; | 33 class PNGViewer; |
| 32 | 34 |
| 35 // TODO(aa): Make zoom work again. | |
| 33 class ZoomableMediaImpl : public InterfaceImpl<ZoomableMedia> { | 36 class ZoomableMediaImpl : public InterfaceImpl<ZoomableMedia> { |
| 34 public: | 37 public: |
| 35 explicit ZoomableMediaImpl(PNGViewer* viewer) : viewer_(viewer) {} | 38 explicit ZoomableMediaImpl(PNGViewer* viewer) : viewer_(viewer) {} |
| 36 virtual ~ZoomableMediaImpl() {} | 39 virtual ~ZoomableMediaImpl() {} |
| 37 | 40 |
| 38 private: | 41 private: |
| 39 // Overridden from ZoomableMedia: | 42 // Overridden from ZoomableMedia: |
| 40 virtual void ZoomIn() OVERRIDE; | 43 virtual void ZoomIn() OVERRIDE; |
| 41 virtual void ZoomOut() OVERRIDE; | 44 virtual void ZoomOut() OVERRIDE; |
| 42 virtual void ZoomToActualSize() OVERRIDE; | 45 virtual void ZoomToActualSize() OVERRIDE; |
| 43 | 46 |
| 44 PNGViewer* viewer_; | 47 PNGViewer* viewer_; |
| 45 | 48 |
| 46 DISALLOW_COPY_AND_ASSIGN(ZoomableMediaImpl); | 49 DISALLOW_COPY_AND_ASSIGN(ZoomableMediaImpl); |
| 47 }; | 50 }; |
| 48 | 51 |
| 49 class NavigatorImpl : public InterfaceImpl<Navigator> { | 52 // One of these gets created for each incoming connection. This is the endpoint |
| 53 class PNGView | |
| 54 : public InterfaceImpl<ServiceProvider>, | |
| 55 public ViewManagerDelegate, | |
| 56 public ViewObserver { | |
| 50 public: | 57 public: |
| 51 explicit NavigatorImpl(PNGViewer* viewer) : viewer_(viewer) {} | 58 PNGView(URLResponsePtr response, ServiceProviderPtr service_provider) |
| 52 virtual ~NavigatorImpl() {} | 59 : remote_service_provider_(service_provider.Pass()), |
| 60 root_(NULL), | |
| 61 zoom_percentage_(kDefaultZoomPercentage) { | |
| 62 remote_service_provider_.set_client(this); | |
| 63 DecodePNG(response.Pass()); | |
| 64 } | |
| 53 | 65 |
| 54 private: | 66 private: |
| 55 // Overridden from Navigator: | 67 static const uint16_t kMaxZoomPercentage = 400; |
| 56 virtual void Navigate( | 68 static const uint16_t kMinZoomPercentage = 20; |
| 57 uint32_t view_id, | 69 static const uint16_t kDefaultZoomPercentage = 100; |
| 58 NavigationDetailsPtr navigation_details, | 70 static const uint16_t kZoomStep = 20; |
| 59 ResponseDetailsPtr response_details) OVERRIDE { | 71 |
| 60 int content_length = GetContentLength(response_details->response->headers); | 72 virtual ~PNGView() { |
| 73 if (root_) | |
| 74 root_->RemoveObserver(this); | |
| 75 } | |
| 76 | |
| 77 // Overridden from InterfaceImpl<ServiceProvider>: | |
| 78 virtual void OnConnectionError() OVERRIDE { | |
| 79 delete this; | |
| 80 } | |
| 81 | |
| 82 virtual void ConnectToService(const mojo::String& interface_name, | |
| 83 ScopedMessagePipeHandle handle) OVERRIDE { | |
| 84 if (interface_name.To<std::string>() == ViewManagerClient::Name_) { | |
| 85 BindToPipe(new ViewManagerClientImpl(this), handle.Pass()); | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 // Overridden from ViewManagerDelegate: | |
| 90 virtual void OnEmbed(ViewManager* view_manager, | |
| 91 View* root, | |
| 92 ServiceProviderImpl* exported_services, | |
| 93 scoped_ptr<ServiceProvider> imported_services) OVERRIDE { | |
| 94 root_ = root; | |
| 95 root_->AddObserver(this); | |
| 96 root_->SetColor(SK_ColorGRAY); | |
| 97 if (!bitmap_.isNull()) | |
| 98 DrawBitmap(); | |
| 99 } | |
| 100 | |
| 101 virtual void OnViewManagerDisconnected( | |
| 102 ViewManager* view_manager) OVERRIDE { | |
| 103 // TODO(aa): Do we even need this? | |
| 104 } | |
| 105 | |
| 106 // Overridden from ViewObserver: | |
| 107 virtual void OnViewBoundsChanged(View* view, | |
| 108 const gfx::Rect& old_bounds, | |
| 109 const gfx::Rect& new_bounds) OVERRIDE { | |
| 110 DCHECK_EQ(view, root_); | |
| 111 DrawBitmap(); | |
| 112 } | |
| 113 | |
| 114 virtual void OnViewDestroyed(View* view) OVERRIDE { | |
| 115 DCHECK_EQ(view, root_); | |
| 116 view->RemoveObserver(this); | |
| 117 root_ = NULL; | |
| 118 } | |
| 119 | |
| 120 void DecodePNG(URLResponsePtr response) { | |
| 121 int content_length = GetContentLength(response->headers); | |
| 61 unsigned char* data = new unsigned char[content_length]; | 122 unsigned char* data = new unsigned char[content_length]; |
| 62 unsigned char* buf = data; | 123 unsigned char* buf = data; |
| 63 uint32_t bytes_remaining = content_length; | 124 uint32_t bytes_remaining = content_length; |
| 64 uint32_t num_bytes = bytes_remaining; | 125 uint32_t num_bytes = bytes_remaining; |
| 65 while (bytes_remaining > 0) { | 126 while (bytes_remaining > 0) { |
| 66 MojoResult result = ReadDataRaw( | 127 MojoResult result = ReadDataRaw(response->body.get(), |
| 67 response_details->response->body.get(), | 128 buf, |
| 68 buf, | 129 &num_bytes, |
| 69 &num_bytes, | 130 MOJO_READ_DATA_FLAG_NONE); |
| 70 MOJO_READ_DATA_FLAG_NONE); | |
| 71 if (result == MOJO_RESULT_SHOULD_WAIT) { | 131 if (result == MOJO_RESULT_SHOULD_WAIT) { |
| 72 Wait(response_details->response->body.get(), | 132 Wait(response->body.get(), |
| 73 MOJO_HANDLE_SIGNAL_READABLE, | 133 MOJO_HANDLE_SIGNAL_READABLE, |
| 74 MOJO_DEADLINE_INDEFINITE); | 134 MOJO_DEADLINE_INDEFINITE); |
| 75 } else if (result == MOJO_RESULT_OK) { | 135 } else if (result == MOJO_RESULT_OK) { |
| 76 buf += num_bytes; | 136 buf += num_bytes; |
| 77 num_bytes = bytes_remaining -= num_bytes; | 137 num_bytes = bytes_remaining -= num_bytes; |
| 78 } else { | 138 } else { |
| 79 break; | 139 break; |
| 80 } | 140 } |
| 81 } | 141 } |
| 82 | 142 |
| 83 SkBitmap bitmap; | |
| 84 gfx::PNGCodec::Decode(static_cast<const unsigned char*>(data), | 143 gfx::PNGCodec::Decode(static_cast<const unsigned char*>(data), |
| 85 content_length, &bitmap); | 144 content_length, &bitmap_); |
| 86 UpdateView(view_id, bitmap); | |
| 87 | 145 |
| 146 // TODO(aa): Why is data not just on the stack? | |
| 88 delete[] data; | 147 delete[] data; |
| 89 } | 148 } |
| 90 | 149 |
| 91 void UpdateView(Id view_id, const SkBitmap& bitmap); | 150 void DrawBitmap() { |
| 151 if (!root_) | |
| 152 return; | |
| 92 | 153 |
| 93 int GetContentLength(const Array<String>& headers) { | 154 skia::RefPtr<SkCanvas> canvas(skia::AdoptRef(skia::CreatePlatformCanvas( |
| 94 for (size_t i = 0; i < headers.size(); ++i) { | 155 root_->bounds().width(), |
| 95 base::StringTokenizer t(headers[i], ": ;="); | 156 root_->bounds().height(), |
| 96 while (t.GetNext()) { | 157 true))); |
| 97 if (!t.token_is_delim() && t.token() == "Content-Length") { | 158 canvas->drawColor(SK_ColorGRAY); |
| 98 while (t.GetNext()) { | 159 SkPaint paint; |
| 99 if (!t.token_is_delim()) | 160 SkScalar scale = |
| 100 return atoi(t.token().c_str()); | 161 SkFloatToScalar(zoom_percentage_ * 1.0f / kDefaultZoomPercentage); |
| 101 } | 162 canvas->scale(scale, scale); |
| 102 } | 163 canvas->drawBitmap(bitmap_, 0, 0, &paint); |
| 103 } | 164 root_->SetContents(skia::GetTopDevice(*canvas)->accessBitmap(true)); |
| 104 } | |
| 105 return 0; | |
| 106 } | |
| 107 | |
| 108 PNGViewer* viewer_; | |
| 109 | |
| 110 DISALLOW_COPY_AND_ASSIGN(NavigatorImpl); | |
| 111 }; | |
| 112 | |
| 113 class PNGViewer | |
| 114 : public ApplicationDelegate, | |
| 115 public ViewManagerDelegate, | |
| 116 public ViewObserver { | |
| 117 public: | |
| 118 PNGViewer() | |
| 119 : navigator_factory_(this), | |
| 120 zoomable_media_factory_(this), | |
| 121 view_manager_client_factory_(this), | |
| 122 root_(NULL), | |
| 123 zoom_percentage_(kDefaultZoomPercentage) {} | |
| 124 virtual ~PNGViewer() { | |
| 125 if (root_) | |
| 126 root_->RemoveObserver(this); | |
| 127 } | 165 } |
| 128 | 166 |
| 129 void UpdateView(Id view_id, const SkBitmap& bitmap) { | 167 void UpdateView(Id view_id, const SkBitmap& bitmap) { |
| 130 bitmap_ = bitmap; | 168 bitmap_ = bitmap; |
| 131 zoom_percentage_ = kDefaultZoomPercentage; | 169 zoom_percentage_ = kDefaultZoomPercentage; |
| 132 DrawBitmap(); | 170 DrawBitmap(); |
| 133 } | 171 } |
| 134 | 172 |
| 135 void ZoomIn() { | 173 void ZoomIn() { |
| 136 if (zoom_percentage_ >= kMaxZoomPercentage) | 174 if (zoom_percentage_ >= kMaxZoomPercentage) |
| 137 return; | 175 return; |
| 138 zoom_percentage_ += kZoomStep; | 176 zoom_percentage_ += kZoomStep; |
| 139 DrawBitmap(); | 177 DrawBitmap(); |
| 140 } | 178 } |
| 141 | 179 |
| 142 void ZoomOut() { | 180 void ZoomOut() { |
| 143 if (zoom_percentage_ <= kMinZoomPercentage) | 181 if (zoom_percentage_ <= kMinZoomPercentage) |
| 144 return; | 182 return; |
| 145 zoom_percentage_ -= kZoomStep; | 183 zoom_percentage_ -= kZoomStep; |
| 146 DrawBitmap(); | 184 DrawBitmap(); |
| 147 } | 185 } |
| 148 | 186 |
| 149 void ZoomToActualSize() { | 187 void ZoomToActualSize() { |
| 150 if (zoom_percentage_ == kDefaultZoomPercentage) | 188 if (zoom_percentage_ == kDefaultZoomPercentage) |
| 151 return; | 189 return; |
| 152 zoom_percentage_ = kDefaultZoomPercentage; | 190 zoom_percentage_ = kDefaultZoomPercentage; |
| 153 DrawBitmap(); | 191 DrawBitmap(); |
| 154 } | 192 } |
| 155 | 193 |
| 194 int GetContentLength(const Array<String>& headers) { | |
| 195 for (size_t i = 0; i < headers.size(); ++i) { | |
| 196 base::StringTokenizer t(headers[i], ": ;="); | |
| 197 while (t.GetNext()) { | |
| 198 if (!t.token_is_delim() && t.token() == "Content-Length") { | |
| 199 while (t.GetNext()) { | |
| 200 if (!t.token_is_delim()) | |
| 201 return atoi(t.token().c_str()); | |
| 202 } | |
| 203 } | |
| 204 } | |
| 205 } | |
| 206 return 0; | |
| 207 } | |
| 208 | |
| 209 SkBitmap bitmap_; | |
| 210 ServiceProviderPtr remote_service_provider_; | |
| 211 View* root_; | |
| 212 uint16_t zoom_percentage_; | |
| 213 | |
| 214 DISALLOW_COPY_AND_ASSIGN(PNGView); | |
| 215 }; | |
| 216 | |
| 217 class ContentHandlerImpl : public InterfaceImpl<ContentHandler> { | |
| 218 public: | |
| 219 explicit ContentHandlerImpl() {} | |
| 220 virtual ~ContentHandlerImpl() {} | |
| 221 | |
| 156 private: | 222 private: |
| 157 static const uint16_t kMaxZoomPercentage = 400; | 223 // Overridden from ContentHandler: |
| 158 static const uint16_t kMinZoomPercentage = 20; | 224 virtual void OnConnect(const mojo::String& url, |
| 159 static const uint16_t kDefaultZoomPercentage = 100; | 225 URLResponsePtr response, |
| 160 static const uint16_t kZoomStep = 20; | 226 ServiceProviderPtr service_provider) OVERRIDE { |
|
darin (slow to review)
2014/08/20 05:16:49
We probably want to change ContentHandler to take
Aaron Boodman
2014/08/20 06:21:02
Does the BindToPipe() call I'm using in PNGView no
| |
| 227 new PNGView(response.Pass(), service_provider.Pass()); | |
| 228 } | |
| 161 | 229 |
| 230 DISALLOW_COPY_AND_ASSIGN(ContentHandlerImpl); | |
| 231 }; | |
| 232 | |
| 233 class PNGViewer : public ApplicationDelegate { | |
| 234 public: | |
| 235 PNGViewer() {} | |
| 236 private: | |
| 162 // Overridden from ApplicationDelegate: | 237 // Overridden from ApplicationDelegate: |
| 163 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) | 238 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) |
| 164 MOJO_OVERRIDE { | 239 MOJO_OVERRIDE { |
| 165 connection->AddService(&navigator_factory_); | 240 connection->AddService(&content_handler_factory_); |
| 166 connection->AddService(&zoomable_media_factory_); | |
| 167 connection->AddService(&view_manager_client_factory_); | |
| 168 return true; | 241 return true; |
| 169 } | 242 } |
| 170 | 243 |
| 171 // Overridden from ViewManagerDelegate: | 244 InterfaceFactoryImpl<ContentHandlerImpl> content_handler_factory_; |
| 172 virtual void OnEmbed(ViewManager* view_manager, | |
| 173 View* root, | |
| 174 ServiceProviderImpl* exported_services, | |
| 175 scoped_ptr<ServiceProvider> imported_services) OVERRIDE { | |
| 176 root_ = root; | |
| 177 root_->AddObserver(this); | |
| 178 root_->SetColor(SK_ColorGRAY); | |
| 179 if (!bitmap_.isNull()) | |
| 180 DrawBitmap(); | |
| 181 } | |
| 182 virtual void OnViewManagerDisconnected( | |
| 183 ViewManager* view_manager) OVERRIDE { | |
| 184 base::MessageLoop::current()->Quit(); | |
| 185 } | |
| 186 | |
| 187 void DrawBitmap() { | |
| 188 if (!root_) | |
| 189 return; | |
| 190 | |
| 191 skia::RefPtr<SkCanvas> canvas(skia::AdoptRef(skia::CreatePlatformCanvas( | |
| 192 root_->bounds().width(), | |
| 193 root_->bounds().height(), | |
| 194 true))); | |
| 195 canvas->drawColor(SK_ColorGRAY); | |
| 196 SkPaint paint; | |
| 197 SkScalar scale = | |
| 198 SkFloatToScalar(zoom_percentage_ * 1.0f / kDefaultZoomPercentage); | |
| 199 canvas->scale(scale, scale); | |
| 200 canvas->drawBitmap(bitmap_, 0, 0, &paint); | |
| 201 root_->SetContents(skia::GetTopDevice(*canvas)->accessBitmap(true)); | |
| 202 } | |
| 203 | |
| 204 // ViewObserver: | |
| 205 virtual void OnViewBoundsChanged(View* view, | |
| 206 const gfx::Rect& old_bounds, | |
| 207 const gfx::Rect& new_bounds) OVERRIDE { | |
| 208 DCHECK_EQ(view, root_); | |
| 209 DrawBitmap(); | |
| 210 } | |
| 211 virtual void OnViewDestroyed(View* view) OVERRIDE { | |
| 212 DCHECK_EQ(view, root_); | |
| 213 view->RemoveObserver(this); | |
| 214 root_ = NULL; | |
| 215 } | |
| 216 | |
| 217 InterfaceFactoryImplWithContext<NavigatorImpl, PNGViewer> navigator_factory_; | |
| 218 InterfaceFactoryImplWithContext<ZoomableMediaImpl, PNGViewer> | |
| 219 zoomable_media_factory_; | |
| 220 ViewManagerClientFactory view_manager_client_factory_; | |
| 221 | |
| 222 View* root_; | |
| 223 SkBitmap bitmap_; | |
| 224 uint16_t zoom_percentage_; | |
| 225 | 245 |
| 226 DISALLOW_COPY_AND_ASSIGN(PNGViewer); | 246 DISALLOW_COPY_AND_ASSIGN(PNGViewer); |
| 227 }; | 247 }; |
| 228 | 248 |
| 229 void ZoomableMediaImpl::ZoomIn() { | |
| 230 viewer_->ZoomIn(); | |
| 231 } | |
| 232 | |
| 233 void ZoomableMediaImpl::ZoomOut() { | |
| 234 viewer_->ZoomOut(); | |
| 235 } | |
| 236 | |
| 237 void ZoomableMediaImpl::ZoomToActualSize() { | |
| 238 viewer_->ZoomToActualSize(); | |
| 239 } | |
| 240 | |
| 241 void NavigatorImpl::UpdateView(Id view_id, | |
| 242 const SkBitmap& bitmap) { | |
| 243 viewer_->UpdateView(view_id, bitmap); | |
| 244 } | |
| 245 | |
| 246 } // namespace examples | 249 } // namespace examples |
| 247 | 250 |
| 248 // static | 251 // static |
| 249 ApplicationDelegate* ApplicationDelegate::Create() { | 252 ApplicationDelegate* ApplicationDelegate::Create() { |
| 250 return new examples::PNGViewer; | 253 return new examples::PNGViewer; |
| 251 } | 254 } |
| 252 | 255 |
| 253 } // namespace mojo | 256 } // namespace mojo |
| OLD | NEW |