| 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..080679e21f1606cae219d04d9a6660f43732a1b1 100644
|
| --- a/mojo/examples/png_viewer/png_viewer.cc
|
| +++ b/mojo/examples/png_viewer/png_viewer.cc
|
| @@ -12,13 +12,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,6 +33,7 @@ namespace examples {
|
|
|
| class PNGViewer;
|
|
|
| +// TODO(aa): Make zoom work again.
|
| class ZoomableMediaImpl : public InterfaceImpl<ZoomableMedia> {
|
| public:
|
| explicit ZoomableMediaImpl(PNGViewer* viewer) : viewer_(viewer) {}
|
| @@ -48,30 +50,74 @@ class ZoomableMediaImpl : public InterfaceImpl<ZoomableMedia> {
|
| DISALLOW_COPY_AND_ASSIGN(ZoomableMediaImpl);
|
| };
|
|
|
| -class NavigatorImpl : public InterfaceImpl<Navigator> {
|
| +class PNGView : public ViewManagerDelegate,
|
| + public ViewObserver {
|
| public:
|
| - explicit NavigatorImpl(PNGViewer* viewer) : viewer_(viewer) {}
|
| - virtual ~NavigatorImpl() {}
|
| + PNGView(ContentHandlerResponsePtr 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 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 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_);
|
| + delete this;
|
| + }
|
| +
|
| + void DecodePNG(ContentHandlerResponsePtr response) {
|
| + int content_length = GetContentLength(response->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->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->response->body.get(),
|
| MOJO_HANDLE_SIGNAL_READABLE,
|
| MOJO_DEADLINE_INDEFINITE);
|
| } else if (result == MOJO_RESULT_OK) {
|
| @@ -82,50 +128,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) {
|
| @@ -155,95 +179,74 @@ 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_;
|
| + scoped_ptr<ServiceProvider> imported_services_;
|
| + View* root_;
|
| + ViewManagerClientFactory view_manager_client_factory_;
|
| + 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(ApplicationConnection* application_connection)
|
| + : application_connection_(application_connection) {
|
| }
|
| + 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,
|
| + ContentHandlerResponsePtr 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());
|
| }
|
|
|
| - InterfaceFactoryImplWithContext<NavigatorImpl, PNGViewer> navigator_factory_;
|
| - InterfaceFactoryImplWithContext<ZoomableMediaImpl, PNGViewer>
|
| - zoomable_media_factory_;
|
| - ViewManagerClientFactory view_manager_client_factory_;
|
| + ApplicationConnection* application_connection_;
|
|
|
| - 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() : content_handler_factory_(NULL) {
|
| + }
|
| + private:
|
| + // Overridden from ApplicationDelegate:
|
| + virtual bool ConfigureIncomingConnection(ApplicationConnection* connection)
|
| + MOJO_OVERRIDE {
|
| + content_handler_factory_.set_context(connection);
|
| + connection->AddService(&content_handler_factory_);
|
| + return true;
|
| + }
|
|
|
| -void ZoomableMediaImpl::ZoomToActualSize() {
|
| - viewer_->ZoomToActualSize();
|
| -}
|
| + InterfaceFactoryImplWithContext<
|
| + ContentHandlerImpl, ApplicationConnection> content_handler_factory_;
|
|
|
| -void NavigatorImpl::UpdateView(Id view_id,
|
| - const SkBitmap& bitmap) {
|
| - viewer_->UpdateView(view_id, bitmap);
|
| -}
|
| + DISALLOW_COPY_AND_ASSIGN(PNGViewer);
|
| +};
|
|
|
| } // namespace examples
|
| } // namespace mojo
|
|
|