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 ee4b530a91dd2be296b038993afd29f3eef35a58..b3f7381f605161f457a36c11a8e073a2244199cf 100644 |
| --- a/mojo/examples/png_viewer/png_viewer.cc |
| +++ b/mojo/examples/png_viewer/png_viewer.cc |
| @@ -4,21 +4,24 @@ |
| #include <algorithm> |
| +#include "base/memory/scoped_ptr.h" |
| #include "base/message_loop/message_loop.h" |
| #include "base/strings/string_tokenizer.h" |
| #include "mojo/examples/media_viewer/media_viewer.mojom.h" |
| #include "mojo/public/c/system/main.h" |
| #include "mojo/public/cpp/application/application_connection.h" |
| #include "mojo/public/cpp/application/application_delegate.h" |
| +#include "mojo/public/cpp/application/application_impl.h" |
| #include "mojo/public/cpp/application/application_runner_chromium.h" |
| #include "mojo/public/cpp/application/interface_factory_impl.h" |
| +#include "mojo/public/cpp/application/service_provider_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" |
| @@ -32,46 +35,76 @@ namespace examples { |
| class PNGViewer; |
| -class ZoomableMediaImpl : public InterfaceImpl<ZoomableMedia> { |
| +// TODO(aa): Hook up ZoomableMedia interface again. |
| +class PNGView : public ViewManagerDelegate, |
| + public ViewObserver { |
| public: |
| - explicit ZoomableMediaImpl(PNGViewer* viewer) : viewer_(viewer) {} |
| - virtual ~ZoomableMediaImpl() {} |
| + PNGView(URLResponsePtr response, |
| + ServiceProviderImpl* exported_services, |
| + scoped_ptr<ServiceProvider> imported_services, |
| + Shell* shell) |
| + : imported_services_(imported_services.Pass()), |
| + root_(NULL), |
| + view_manager_client_factory_(shell, this), |
| + zoom_percentage_(kDefaultZoomPercentage) { |
| + exported_services->AddService(&view_manager_client_factory_); |
| + DecodePNG(response.Pass()); |
| + } |
| private: |
| - // Overridden from ZoomableMedia: |
| - virtual void ZoomIn() OVERRIDE; |
| - virtual void ZoomOut() OVERRIDE; |
| - virtual void ZoomToActualSize() OVERRIDE; |
| + static const uint16_t kMaxZoomPercentage = 400; |
| + static const uint16_t kMinZoomPercentage = 20; |
| + static const uint16_t kDefaultZoomPercentage = 100; |
| + static const uint16_t kZoomStep = 20; |
| - PNGViewer* viewer_; |
| + virtual ~PNGView() { |
| + if (root_) |
| + root_->RemoveObserver(this); |
| + } |
| - DISALLOW_COPY_AND_ASSIGN(ZoomableMediaImpl); |
| -}; |
| + // 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(); |
| + } |
| -class NavigatorImpl : public InterfaceImpl<Navigator> { |
| - public: |
| - explicit NavigatorImpl(PNGViewer* viewer) : viewer_(viewer) {} |
| - virtual ~NavigatorImpl() {} |
| + virtual void OnViewManagerDisconnected( |
| + ViewManager* view_manager) OVERRIDE { |
| + // TODO(aa): Need to figure out how shutdown works. |
| + } |
| - 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); |
| - unsigned char* data = new unsigned char[content_length]; |
| - unsigned char* buf = data; |
| + // 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_); |
| + delete this; |
| + } |
| + |
| + void DecodePNG(URLResponsePtr response) { |
| + int content_length = GetContentLength(response->headers); |
| + scoped_ptr<unsigned char[]> data(new unsigned char[content_length]); |
| + unsigned char* buf = data.get(); |
| 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) { |
| @@ -82,15 +115,47 @@ class NavigatorImpl : public InterfaceImpl<Navigator> { |
| } |
| } |
| - SkBitmap bitmap; |
| - gfx::PNGCodec::Decode(static_cast<const unsigned char*>(data), |
| - content_length, &bitmap); |
| - UpdateView(view_id, bitmap); |
| + gfx::PNGCodec::Decode(static_cast<const unsigned char*>(data.get()), |
| + content_length, &bitmap_); |
| + } |
| + |
| + void DrawBitmap() { |
| + if (!root_) |
| + return; |
| + |
| + 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)); |
| + } |
| - delete[] data; |
| + void ZoomIn() { |
| + if (zoom_percentage_ >= kMaxZoomPercentage) |
| + return; |
| + zoom_percentage_ += kZoomStep; |
| + DrawBitmap(); |
| } |
| - void UpdateView(Id view_id, const SkBitmap& bitmap); |
| + void ZoomOut() { |
| + if (zoom_percentage_ <= kMinZoomPercentage) |
| + return; |
| + zoom_percentage_ -= kZoomStep; |
| + DrawBitmap(); |
| + } |
| + |
| + void ZoomToActualSize() { |
| + if (zoom_percentage_ == kDefaultZoomPercentage) |
| + return; |
| + zoom_percentage_ = kDefaultZoomPercentage; |
| + DrawBitmap(); |
| + } |
| int GetContentLength(const Array<String>& headers) { |
| for (size_t i = 0; i < headers.size(); ++i) { |
| @@ -107,144 +172,65 @@ class NavigatorImpl : public InterfaceImpl<Navigator> { |
| return 0; |
| } |
| - PNGViewer* viewer_; |
| + SkBitmap bitmap_; |
| + scoped_ptr<ServiceProvider> imported_services_; |
| + View* root_; |
| + ViewManagerClientFactory view_manager_client_factory_; |
| + uint16_t zoom_percentage_; |
| - DISALLOW_COPY_AND_ASSIGN(NavigatorImpl); |
| + DISALLOW_COPY_AND_ASSIGN(PNGView); |
| }; |
| -class PNGViewer |
| - : public ApplicationDelegate, |
| - public ViewManagerDelegate, |
| - public ViewObserver { |
| +class ContentHandlerImpl : public InterfaceImpl<ContentHandler> { |
| 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); |
| + explicit ContentHandlerImpl(Shell* shell) |
| + : shell_(shell) { |
| } |
| + virtual ~ContentHandlerImpl() {} |
| - void UpdateView(Id view_id, const SkBitmap& bitmap) { |
| - bitmap_ = bitmap; |
| - zoom_percentage_ = kDefaultZoomPercentage; |
| - DrawBitmap(); |
| + private: |
| + // Overridden from ContentHandler: |
| + virtual void OnConnect( |
| + const mojo::String& url, |
| + URLResponsePtr response, |
| + InterfaceRequest<ServiceProvider> service_provider) OVERRIDE { |
| + ServiceProviderImpl* exported_services = new ServiceProviderImpl(); |
| + BindToRequest(exported_services, &service_provider); |
| + scoped_ptr<ServiceProvider> remote( |
| + exported_services->CreateRemoteServiceProvider()); |
| + new PNGView(response.Pass(), exported_services, remote.Pass(), shell_); |
|
darin (slow to review)
2014/09/04 21:33:51
nit: add a comment here about memory management?
Aaron Boodman
2014/09/05 02:41:31
Yeah good point -- instead, I made the ctor privat
|
| } |
| - void ZoomIn() { |
| - if (zoom_percentage_ >= kMaxZoomPercentage) |
| - return; |
| - zoom_percentage_ += kZoomStep; |
| - DrawBitmap(); |
| - } |
| + Shell* shell_; |
| - void ZoomOut() { |
| - if (zoom_percentage_ <= kMinZoomPercentage) |
| - return; |
| - zoom_percentage_ -= kZoomStep; |
| - DrawBitmap(); |
| - } |
| + DISALLOW_COPY_AND_ASSIGN(ContentHandlerImpl); |
| +}; |
| - void ZoomToActualSize() { |
| - if (zoom_percentage_ == kDefaultZoomPercentage) |
| - return; |
| - zoom_percentage_ = kDefaultZoomPercentage; |
| - DrawBitmap(); |
| +class PNGViewer : public ApplicationDelegate { |
| + public: |
| + PNGViewer() { |
| } |
| - |
| 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 void Initialize(ApplicationImpl* app) MOJO_OVERRIDE { |
| + content_handler_factory_.reset( |
|
darin (slow to review)
2014/09/04 21:33:51
food for thought...
it seems like it'd be possibl
Aaron Boodman
2014/09/04 22:31:43
I'm keeping it in mind. I wanted to flush things o
|
| + new InterfaceFactoryImplWithContext<ContentHandlerImpl, Shell>( |
| + app->shell())); |
| + } |
| // 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_); |
| + connection->AddService(content_handler_factory_.get()); |
| return true; |
| } |
| - // 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(); |
| - } |
| - |
| - void DrawBitmap() { |
| - if (!root_) |
| - return; |
| - |
| - 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)); |
| - } |
| - |
| - // 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; |
| - } |
| - |
| - InterfaceFactoryImplWithContext<NavigatorImpl, PNGViewer> navigator_factory_; |
| - InterfaceFactoryImplWithContext<ZoomableMediaImpl, PNGViewer> |
| - zoomable_media_factory_; |
| - ViewManagerClientFactory view_manager_client_factory_; |
| - |
| - View* root_; |
| - SkBitmap bitmap_; |
| - uint16_t zoom_percentage_; |
| + scoped_ptr<InterfaceFactoryImplWithContext<ContentHandlerImpl, Shell> > |
| + content_handler_factory_; |
| DISALLOW_COPY_AND_ASSIGN(PNGViewer); |
| }; |
| -void ZoomableMediaImpl::ZoomIn() { |
| - viewer_->ZoomIn(); |
| -} |
| - |
| -void ZoomableMediaImpl::ZoomOut() { |
| - viewer_->ZoomOut(); |
| -} |
| - |
| -void ZoomableMediaImpl::ZoomToActualSize() { |
| - viewer_->ZoomToActualSize(); |
| -} |
| - |
| -void NavigatorImpl::UpdateView(Id view_id, |
| - const SkBitmap& bitmap) { |
| - viewer_->UpdateView(view_id, bitmap); |
| -} |
| - |
| } // namespace examples |
| } // namespace mojo |