Chromium Code Reviews| Index: mojo/examples/image_viewer/image_viewer.cc |
| diff --git a/mojo/examples/image_viewer/image_viewer.cc b/mojo/examples/image_viewer/image_viewer.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..73c4c1264249ae7f6bc2f0e73705286cf4225d70 |
| --- /dev/null |
| +++ b/mojo/examples/image_viewer/image_viewer.cc |
| @@ -0,0 +1,137 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <algorithm> |
| + |
| +#include "base/strings/string_tokenizer.h" |
| +#include "mojo/public/cpp/application/application.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_delegate.h" |
| +#include "mojo/services/public/cpp/view_manager/view_manager_types.h" |
| +#include "mojo/services/public/cpp/view_manager/view_tree_node.h" |
| +#include "mojo/services/public/interfaces/launcher/launcher.mojom.h" |
| +#include "third_party/skia/include/core/SkBitmap.h" |
| +#include "ui/gfx/codec/png_codec.h" |
| + |
| +namespace mojo { |
| +namespace examples { |
| + |
| +class ImageViewer; |
| + |
| +class LaunchableConnection : public InterfaceImpl<launcher::Launchable> { |
| + public: |
| + explicit LaunchableConnection(ImageViewer* viewer) : viewer_(viewer) {} |
| + virtual ~LaunchableConnection() {} |
| + |
| + private: |
| + // Overridden from launcher::Launchable: |
| + virtual void OnLaunch(URLResponsePtr response, |
| + ScopedDataPipeConsumerHandle response_body_stream, |
| + uint32_t node_id) OVERRIDE { |
| + 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_body_stream.get(), |
| + buf, |
| + &num_bytes, |
| + MOJO_READ_DATA_FLAG_NONE); |
| + if (result == MOJO_RESULT_SHOULD_WAIT) { |
| + Wait(response_body_stream.get(), |
| + MOJO_WAIT_FLAG_READABLE, |
| + MOJO_DEADLINE_INDEFINITE); |
| + } else if (result == MOJO_RESULT_OK) { |
| + buf += num_bytes; |
| + num_bytes = bytes_remaining -= num_bytes; |
| + } else { |
| + break; |
| + } |
| + } |
| + |
| + SkBitmap bitmap; |
| + gfx::PNGCodec::Decode(static_cast<const unsigned char*>(data), |
| + content_length, &bitmap); |
|
Aaron Boodman
2014/06/13 20:09:52
Wow, this is a cool outcome of Mojo's design.
Whe
Ben Goodger (Google)
2014/06/13 20:29:28
Yeah it's interesting. We do this in chrome today
|
| + UpdateView(node_id, bitmap); |
| + |
| + delete[] data; |
| + } |
| + |
| + void UpdateView(view_manager::Id node_id, const SkBitmap& bitmap); |
| + |
| + int GetContentLength(const Array<String>& headers) { |
| + for (size_t i = 0; i < headers.size(); ++i) { |
| + base::StringTokenizer t(headers[i], ": ;="); |
|
Aaron Boodman
2014/06/13 20:09:52
Probably right thing to do here is either reuse Bl
Ben Goodger (Google)
2014/06/13 20:29:28
Agreed, esp as this is the second time I've copy-p
|
| + 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; |
| + } |
| + |
| + ImageViewer* viewer_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(LaunchableConnection); |
| +}; |
| + |
| +class ImageViewer : public Application, |
| + public view_manager::ViewManagerDelegate { |
| + public: |
| + ImageViewer() : content_view_(NULL) {} |
| + virtual ~ImageViewer() {} |
| + |
| + void UpdateView(view_manager::Id node_id, const SkBitmap& bitmap) { |
| + bitmap_ = bitmap; |
| + DrawBitmap(); |
| + } |
| + |
| + private: |
| + // Overridden from Application: |
| + virtual void Initialize() OVERRIDE { |
| + AddService<LaunchableConnection>(this); |
| + view_manager::ViewManager::Create(this, this); |
| + } |
| + |
| + // Overridden from view_manager::ViewManagerDelegate: |
| + virtual void OnRootAdded(view_manager::ViewManager* view_manager, |
| + view_manager::ViewTreeNode* root) OVERRIDE { |
| + content_view_ = view_manager::View::Create(view_manager); |
| + root->SetActiveView(content_view_); |
| + content_view_->SetColor(SK_ColorRED); |
| + if (!bitmap_.isNull()) |
| + DrawBitmap(); |
| + } |
| + |
| + void DrawBitmap() { |
| + if (content_view_) |
| + content_view_->SetContents(bitmap_); |
| + } |
| + |
| + view_manager::View* content_view_; |
| + SkBitmap bitmap_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ImageViewer); |
| +}; |
| + |
| +void LaunchableConnection::UpdateView(view_manager::Id node_id, |
| + const SkBitmap& bitmap) { |
| + viewer_->UpdateView(node_id, bitmap); |
| +} |
| + |
| +} // namespace examples |
| + |
| +// static |
| +Application* Application::Create() { |
| + return new examples::ImageViewer; |
| +} |
| + |
| +} // namespace mojo |