Chromium Code Reviews| Index: mojo/examples/png_viewer/png_viewer.cc |
| diff --git a/mojo/examples/png_viewer/png_viewer.cc b/mojo/examples/png_viewer/png_viewer.cc |
| index 2a6c9b8ca24361bc625839d3496a3405b770fc54..5a4728ef8195988dc2ea2c2b98ed37d6bb087e07 100644 |
| --- a/mojo/examples/png_viewer/png_viewer.cc |
| +++ b/mojo/examples/png_viewer/png_viewer.cc |
| @@ -10,13 +10,15 @@ |
| #include "mojo/public/cpp/application/application_connection.h" |
| #include "mojo/public/cpp/application/application_delegate.h" |
| #include "mojo/public/cpp/application/interface_factory_impl.h" |
| +#include "mojo/public/interfaces/application/service_provider.mojom.h" |
| +// TODO(aa): Ugh, we need to hook up ViewManagerClientFactory to ContentHandler somehow. |
| +#include "mojo/services/public/cpp/view_manager/lib/view_manager_client_impl.h" |
| #include "mojo/services/public/cpp/view_manager/types.h" |
| #include "mojo/services/public/cpp/view_manager/view.h" |
| #include "mojo/services/public/cpp/view_manager/view_manager.h" |
| -#include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h" |
| #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" |
| #include "mojo/services/public/cpp/view_manager/view_observer.h" |
| -#include "mojo/services/public/interfaces/navigation/navigation.mojom.h" |
| +#include "mojo/services/public/interfaces/content_handler/content_handler.mojom.h" |
| #include "skia/ext/platform_canvas.h" |
| #include "skia/ext/refptr.h" |
| #include "third_party/skia/include/core/SkBitmap.h" |
| @@ -30,6 +32,7 @@ namespace examples { |
| class PNGViewer; |
| +// TODO(aa): Make zoom work again. |
| class ZoomableMediaImpl : public InterfaceImpl<ZoomableMedia> { |
| public: |
| explicit ZoomableMediaImpl(PNGViewer* viewer) : viewer_(viewer) {} |
| @@ -46,30 +49,87 @@ class ZoomableMediaImpl : public InterfaceImpl<ZoomableMedia> { |
| DISALLOW_COPY_AND_ASSIGN(ZoomableMediaImpl); |
| }; |
| -class NavigatorImpl : public InterfaceImpl<Navigator> { |
| +// One of these gets created for each incoming connection. This is the endpoint |
| +class PNGView |
| + : public InterfaceImpl<ServiceProvider>, |
| + public ViewManagerDelegate, |
| + public ViewObserver { |
| public: |
| - explicit NavigatorImpl(PNGViewer* viewer) : viewer_(viewer) {} |
| - virtual ~NavigatorImpl() {} |
| + PNGView(URLResponsePtr response, ServiceProviderPtr service_provider) |
| + : remote_service_provider_(service_provider.Pass()), |
| + root_(NULL), |
| + zoom_percentage_(kDefaultZoomPercentage) { |
| + remote_service_provider_.set_client(this); |
| + DecodePNG(response.Pass()); |
| + } |
| private: |
| - // Overridden from Navigator: |
| - virtual void Navigate( |
| - uint32_t view_id, |
| - NavigationDetailsPtr navigation_details, |
| - ResponseDetailsPtr response_details) OVERRIDE { |
| - int content_length = GetContentLength(response_details->response->headers); |
| + static const uint16_t kMaxZoomPercentage = 400; |
| + static const uint16_t kMinZoomPercentage = 20; |
| + static const uint16_t kDefaultZoomPercentage = 100; |
| + static const uint16_t kZoomStep = 20; |
| + |
| + virtual ~PNGView() { |
| + if (root_) |
| + root_->RemoveObserver(this); |
| + } |
| + |
| + // Overridden from InterfaceImpl<ServiceProvider>: |
| + virtual void OnConnectionError() OVERRIDE { |
| + delete this; |
| + } |
| + |
| + virtual void ConnectToService(const mojo::String& interface_name, |
| + ScopedMessagePipeHandle handle) OVERRIDE { |
| + if (interface_name.To<std::string>() == ViewManagerClient::Name_) { |
| + BindToPipe(new ViewManagerClientImpl(this), handle.Pass()); |
| + } |
| + } |
| + |
| + // Overridden from ViewManagerDelegate: |
| + virtual void OnEmbed(ViewManager* view_manager, |
| + View* root, |
| + ServiceProviderImpl* exported_services, |
| + scoped_ptr<ServiceProvider> imported_services) OVERRIDE { |
| + root_ = root; |
| + root_->AddObserver(this); |
| + root_->SetColor(SK_ColorGRAY); |
| + if (!bitmap_.isNull()) |
| + DrawBitmap(); |
| + } |
| + |
| + virtual void OnViewManagerDisconnected( |
| + ViewManager* view_manager) OVERRIDE { |
| + // TODO(aa): Do we even need this? |
| + } |
| + |
| + // Overridden from ViewObserver: |
| + virtual void OnViewBoundsChanged(View* view, |
| + const gfx::Rect& old_bounds, |
| + const gfx::Rect& new_bounds) OVERRIDE { |
| + DCHECK_EQ(view, root_); |
| + DrawBitmap(); |
| + } |
| + |
| + virtual void OnViewDestroyed(View* view) OVERRIDE { |
| + DCHECK_EQ(view, root_); |
| + view->RemoveObserver(this); |
| + root_ = NULL; |
| + } |
| + |
| + void DecodePNG(URLResponsePtr response) { |
| + int content_length = GetContentLength(response->headers); |
| unsigned char* data = new unsigned char[content_length]; |
| unsigned char* buf = data; |
| uint32_t bytes_remaining = content_length; |
| uint32_t num_bytes = bytes_remaining; |
| while (bytes_remaining > 0) { |
| - MojoResult result = ReadDataRaw( |
| - response_details->response->body.get(), |
| - buf, |
| - &num_bytes, |
| - MOJO_READ_DATA_FLAG_NONE); |
| + MojoResult result = ReadDataRaw(response->body.get(), |
| + buf, |
| + &num_bytes, |
| + MOJO_READ_DATA_FLAG_NONE); |
| if (result == MOJO_RESULT_SHOULD_WAIT) { |
| - Wait(response_details->response->body.get(), |
| + Wait(response->body.get(), |
| MOJO_HANDLE_SIGNAL_READABLE, |
| MOJO_DEADLINE_INDEFINITE); |
| } else if (result == MOJO_RESULT_OK) { |
| @@ -80,50 +140,28 @@ class NavigatorImpl : public InterfaceImpl<Navigator> { |
| } |
| } |
| - SkBitmap bitmap; |
| gfx::PNGCodec::Decode(static_cast<const unsigned char*>(data), |
| - content_length, &bitmap); |
| - UpdateView(view_id, bitmap); |
| + content_length, &bitmap_); |
| + // TODO(aa): Why is data not just on the stack? |
| delete[] data; |
| } |
| - void UpdateView(Id view_id, const SkBitmap& bitmap); |
| - |
| - int GetContentLength(const Array<String>& headers) { |
| - for (size_t i = 0; i < headers.size(); ++i) { |
| - base::StringTokenizer t(headers[i], ": ;="); |
| - while (t.GetNext()) { |
| - if (!t.token_is_delim() && t.token() == "Content-Length") { |
| - while (t.GetNext()) { |
| - if (!t.token_is_delim()) |
| - return atoi(t.token().c_str()); |
| - } |
| - } |
| - } |
| - } |
| - return 0; |
| - } |
| - |
| - PNGViewer* viewer_; |
| - |
| - DISALLOW_COPY_AND_ASSIGN(NavigatorImpl); |
| -}; |
| + void DrawBitmap() { |
| + if (!root_) |
| + return; |
| -class PNGViewer |
| - : public ApplicationDelegate, |
| - public ViewManagerDelegate, |
| - public ViewObserver { |
| - public: |
| - PNGViewer() |
| - : navigator_factory_(this), |
| - zoomable_media_factory_(this), |
| - view_manager_client_factory_(this), |
| - root_(NULL), |
| - zoom_percentage_(kDefaultZoomPercentage) {} |
| - virtual ~PNGViewer() { |
| - if (root_) |
| - root_->RemoveObserver(this); |
| + skia::RefPtr<SkCanvas> canvas(skia::AdoptRef(skia::CreatePlatformCanvas( |
| + root_->bounds().width(), |
| + root_->bounds().height(), |
| + true))); |
| + canvas->drawColor(SK_ColorGRAY); |
| + SkPaint paint; |
| + SkScalar scale = |
| + SkFloatToScalar(zoom_percentage_ * 1.0f / kDefaultZoomPercentage); |
| + canvas->scale(scale, scale); |
| + canvas->drawBitmap(bitmap_, 0, 0, &paint); |
| + root_->SetContents(skia::GetTopDevice(*canvas)->accessBitmap(true)); |
| } |
| void UpdateView(Id view_id, const SkBitmap& bitmap) { |
| @@ -153,95 +191,60 @@ class PNGViewer |
| DrawBitmap(); |
| } |
| - private: |
| - static const uint16_t kMaxZoomPercentage = 400; |
| - static const uint16_t kMinZoomPercentage = 20; |
| - static const uint16_t kDefaultZoomPercentage = 100; |
| - static const uint16_t kZoomStep = 20; |
| - |
| - // Overridden from ApplicationDelegate: |
| - virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) |
| - MOJO_OVERRIDE { |
| - connection->AddService(&navigator_factory_); |
| - connection->AddService(&zoomable_media_factory_); |
| - connection->AddService(&view_manager_client_factory_); |
| - return true; |
| + int GetContentLength(const Array<String>& headers) { |
| + for (size_t i = 0; i < headers.size(); ++i) { |
| + base::StringTokenizer t(headers[i], ": ;="); |
| + while (t.GetNext()) { |
| + if (!t.token_is_delim() && t.token() == "Content-Length") { |
| + while (t.GetNext()) { |
| + if (!t.token_is_delim()) |
| + return atoi(t.token().c_str()); |
| + } |
| + } |
| + } |
| + } |
| + return 0; |
| } |
| - // Overridden from ViewManagerDelegate: |
| - virtual void OnEmbed(ViewManager* view_manager, |
| - View* root, |
| - ServiceProviderImpl* exported_services, |
| - scoped_ptr<ServiceProvider> imported_services) OVERRIDE { |
| - root_ = root; |
| - root_->AddObserver(this); |
| - root_->SetColor(SK_ColorGRAY); |
| - if (!bitmap_.isNull()) |
| - DrawBitmap(); |
| - } |
| - virtual void OnViewManagerDisconnected( |
| - ViewManager* view_manager) OVERRIDE { |
| - base::MessageLoop::current()->Quit(); |
| - } |
| + SkBitmap bitmap_; |
| + ServiceProviderPtr remote_service_provider_; |
| + View* root_; |
| + uint16_t zoom_percentage_; |
| - void DrawBitmap() { |
| - if (!root_) |
| - return; |
| + DISALLOW_COPY_AND_ASSIGN(PNGView); |
| +}; |
| - skia::RefPtr<SkCanvas> canvas(skia::AdoptRef(skia::CreatePlatformCanvas( |
| - root_->bounds().width(), |
| - root_->bounds().height(), |
| - true))); |
| - canvas->drawColor(SK_ColorGRAY); |
| - SkPaint paint; |
| - SkScalar scale = |
| - SkFloatToScalar(zoom_percentage_ * 1.0f / kDefaultZoomPercentage); |
| - canvas->scale(scale, scale); |
| - canvas->drawBitmap(bitmap_, 0, 0, &paint); |
| - root_->SetContents(skia::GetTopDevice(*canvas)->accessBitmap(true)); |
| - } |
| +class ContentHandlerImpl : public InterfaceImpl<ContentHandler> { |
| + public: |
| + explicit ContentHandlerImpl() {} |
| + virtual ~ContentHandlerImpl() {} |
| - // ViewObserver: |
| - virtual void OnViewBoundsChanged(View* view, |
| - const gfx::Rect& old_bounds, |
| - const gfx::Rect& new_bounds) OVERRIDE { |
| - DCHECK_EQ(view, root_); |
| - DrawBitmap(); |
| - } |
| - virtual void OnViewDestroyed(View* view) OVERRIDE { |
| - DCHECK_EQ(view, root_); |
| - view->RemoveObserver(this); |
| - root_ = NULL; |
| + private: |
| + // Overridden from ContentHandler: |
| + virtual void OnConnect(const mojo::String& url, |
| + URLResponsePtr response, |
| + 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
|
| + new PNGView(response.Pass(), service_provider.Pass()); |
| } |
| - InterfaceFactoryImplWithContext<NavigatorImpl, PNGViewer> navigator_factory_; |
| - InterfaceFactoryImplWithContext<ZoomableMediaImpl, PNGViewer> |
| - zoomable_media_factory_; |
| - ViewManagerClientFactory view_manager_client_factory_; |
| - |
| - View* root_; |
| - SkBitmap bitmap_; |
| - uint16_t zoom_percentage_; |
| - |
| - DISALLOW_COPY_AND_ASSIGN(PNGViewer); |
| + DISALLOW_COPY_AND_ASSIGN(ContentHandlerImpl); |
| }; |
| -void ZoomableMediaImpl::ZoomIn() { |
| - viewer_->ZoomIn(); |
| -} |
| - |
| -void ZoomableMediaImpl::ZoomOut() { |
| - viewer_->ZoomOut(); |
| -} |
| +class PNGViewer : public ApplicationDelegate { |
| + public: |
| + PNGViewer() {} |
| + private: |
| + // Overridden from ApplicationDelegate: |
| + virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) |
| + MOJO_OVERRIDE { |
| + connection->AddService(&content_handler_factory_); |
| + return true; |
| + } |
| -void ZoomableMediaImpl::ZoomToActualSize() { |
| - viewer_->ZoomToActualSize(); |
| -} |
| + InterfaceFactoryImpl<ContentHandlerImpl> content_handler_factory_; |
| -void NavigatorImpl::UpdateView(Id view_id, |
| - const SkBitmap& bitmap) { |
| - viewer_->UpdateView(view_id, bitmap); |
| -} |
| + DISALLOW_COPY_AND_ASSIGN(PNGViewer); |
| +}; |
| } // namespace examples |