OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <algorithm> |
| 6 |
| 7 #include "base/strings/string_tokenizer.h" |
| 8 #include "mojo/public/cpp/application/application.h" |
| 9 #include "mojo/services/public/cpp/view_manager/view.h" |
| 10 #include "mojo/services/public/cpp/view_manager/view_manager.h" |
| 11 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" |
| 12 #include "mojo/services/public/cpp/view_manager/view_manager_types.h" |
| 13 #include "mojo/services/public/cpp/view_manager/view_tree_node.h" |
| 14 #include "mojo/services/public/interfaces/launcher/launcher.mojom.h" |
| 15 #include "third_party/skia/include/core/SkBitmap.h" |
| 16 #include "ui/gfx/codec/png_codec.h" |
| 17 |
| 18 namespace mojo { |
| 19 namespace examples { |
| 20 |
| 21 class ImageViewer; |
| 22 |
| 23 class LaunchableConnection : public InterfaceImpl<launcher::Launchable> { |
| 24 public: |
| 25 explicit LaunchableConnection(ImageViewer* viewer) : viewer_(viewer) {} |
| 26 virtual ~LaunchableConnection() {} |
| 27 |
| 28 private: |
| 29 // Overridden from launcher::Launchable: |
| 30 virtual void OnLaunch(URLResponsePtr response, |
| 31 ScopedDataPipeConsumerHandle response_body_stream, |
| 32 uint32_t node_id) OVERRIDE { |
| 33 int content_length = GetContentLength(response->headers); |
| 34 unsigned char* data = new unsigned char[content_length]; |
| 35 unsigned char* buf = data; |
| 36 uint32_t bytes_remaining = content_length; |
| 37 uint32_t num_bytes = bytes_remaining; |
| 38 while (bytes_remaining > 0) { |
| 39 MojoResult result = ReadDataRaw( |
| 40 response_body_stream.get(), |
| 41 buf, |
| 42 &num_bytes, |
| 43 MOJO_READ_DATA_FLAG_NONE); |
| 44 if (result == MOJO_RESULT_SHOULD_WAIT) { |
| 45 Wait(response_body_stream.get(), |
| 46 MOJO_WAIT_FLAG_READABLE, |
| 47 MOJO_DEADLINE_INDEFINITE); |
| 48 } else if (result == MOJO_RESULT_OK) { |
| 49 buf += num_bytes; |
| 50 num_bytes = bytes_remaining -= num_bytes; |
| 51 } else { |
| 52 break; |
| 53 } |
| 54 } |
| 55 |
| 56 SkBitmap bitmap; |
| 57 gfx::PNGCodec::Decode(static_cast<const unsigned char*>(data), |
| 58 content_length, &bitmap); |
| 59 UpdateView(node_id, bitmap); |
| 60 |
| 61 delete[] data; |
| 62 } |
| 63 |
| 64 void UpdateView(view_manager::Id node_id, const SkBitmap& bitmap); |
| 65 |
| 66 int GetContentLength(const Array<String>& headers) { |
| 67 for (size_t i = 0; i < headers.size(); ++i) { |
| 68 base::StringTokenizer t(headers[i], ": ;="); |
| 69 while (t.GetNext()) { |
| 70 if (!t.token_is_delim() && t.token() == "Content-Length") { |
| 71 while (t.GetNext()) { |
| 72 if (!t.token_is_delim()) |
| 73 return atoi(t.token().c_str()); |
| 74 } |
| 75 } |
| 76 } |
| 77 } |
| 78 return 0; |
| 79 } |
| 80 |
| 81 ImageViewer* viewer_; |
| 82 |
| 83 DISALLOW_COPY_AND_ASSIGN(LaunchableConnection); |
| 84 }; |
| 85 |
| 86 class ImageViewer : public Application, |
| 87 public view_manager::ViewManagerDelegate { |
| 88 public: |
| 89 ImageViewer() : content_view_(NULL) {} |
| 90 virtual ~ImageViewer() {} |
| 91 |
| 92 void UpdateView(view_manager::Id node_id, const SkBitmap& bitmap) { |
| 93 bitmap_ = bitmap; |
| 94 DrawBitmap(); |
| 95 } |
| 96 |
| 97 private: |
| 98 // Overridden from Application: |
| 99 virtual void Initialize() OVERRIDE { |
| 100 AddService<LaunchableConnection>(this); |
| 101 view_manager::ViewManager::Create(this, this); |
| 102 } |
| 103 |
| 104 // Overridden from view_manager::ViewManagerDelegate: |
| 105 virtual void OnRootAdded(view_manager::ViewManager* view_manager, |
| 106 view_manager::ViewTreeNode* root) OVERRIDE { |
| 107 content_view_ = view_manager::View::Create(view_manager); |
| 108 root->SetActiveView(content_view_); |
| 109 content_view_->SetColor(SK_ColorRED); |
| 110 if (!bitmap_.isNull()) |
| 111 DrawBitmap(); |
| 112 } |
| 113 |
| 114 void DrawBitmap() { |
| 115 if (content_view_) |
| 116 content_view_->SetContents(bitmap_); |
| 117 } |
| 118 |
| 119 view_manager::View* content_view_; |
| 120 SkBitmap bitmap_; |
| 121 |
| 122 DISALLOW_COPY_AND_ASSIGN(ImageViewer); |
| 123 }; |
| 124 |
| 125 void LaunchableConnection::UpdateView(view_manager::Id node_id, |
| 126 const SkBitmap& bitmap) { |
| 127 viewer_->UpdateView(node_id, bitmap); |
| 128 } |
| 129 |
| 130 } // namespace examples |
| 131 |
| 132 // static |
| 133 Application* Application::Create() { |
| 134 return new examples::ImageViewer; |
| 135 } |
| 136 |
| 137 } // namespace mojo |
OLD | NEW |