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