| 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..0ec985ff1f0ff90bdd97e5fd4f0dfc3e45c8f33f 100644
|
| --- a/mojo/examples/png_viewer/png_viewer.cc
|
| +++ b/mojo/examples/png_viewer/png_viewer.cc
|
| @@ -4,6 +4,7 @@
|
|
|
| #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"
|
| @@ -12,13 +13,14 @@
|
| #include "mojo/public/cpp/application/application_delegate.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 +34,75 @@ 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)
|
| + : imported_services_(imported_services.Pass()),
|
| + root_(NULL),
|
| + view_manager_client_factory_(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 +113,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_);
|
| + }
|
|
|
| - delete[] data;
|
| + 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));
|
| }
|
|
|
| - void UpdateView(Id view_id, const SkBitmap& bitmap);
|
| + void ZoomIn() {
|
| + if (zoom_percentage_ >= kMaxZoomPercentage)
|
| + return;
|
| + zoom_percentage_ += kZoomStep;
|
| + DrawBitmap();
|
| + }
|
| +
|
| + 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 +170,60 @@ 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(ApplicationConnection* application_connection)
|
| + : application_connection_(application_connection) {
|
| }
|
| + 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(application_connection_);
|
| + BindToRequest(exported_services, &service_provider);
|
| + scoped_ptr<ServiceProvider> remote(
|
| + exported_services->CreateRemoteServiceProvider());
|
| + new PNGView(response.Pass(), exported_services, remote.Pass());
|
| }
|
|
|
| - void ZoomIn() {
|
| - if (zoom_percentage_ >= kMaxZoomPercentage)
|
| - return;
|
| - zoom_percentage_ += kZoomStep;
|
| - DrawBitmap();
|
| - }
|
| + ApplicationConnection* application_connection_;
|
|
|
| - 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() : content_handler_factory_(NULL) {
|
| }
|
| -
|
| 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_);
|
| + content_handler_factory_.set_context(connection);
|
| + connection->AddService(&content_handler_factory_);
|
| 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_;
|
| + InterfaceFactoryImplWithContext<
|
| + ContentHandlerImpl, ApplicationConnection> 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
|
|
|
|
|