| 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/node.h" | 10 #include "mojo/services/public/cpp/view_manager/node.h" |
| 10 #include "mojo/services/public/cpp/view_manager/types.h" | 11 #include "mojo/services/public/cpp/view_manager/types.h" |
| 11 #include "mojo/services/public/cpp/view_manager/view.h" | 12 #include "mojo/services/public/cpp/view_manager/view.h" |
| 12 #include "mojo/services/public/cpp/view_manager/view_manager.h" | 13 #include "mojo/services/public/cpp/view_manager/view_manager.h" |
| 13 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" | 14 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.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( |
| 31 ScopedDataPipeConsumerHandle response_body_stream, | 31 uint32_t node_id, |
| 32 uint32_t node_id) OVERRIDE { | 32 navigation::NavigationDetailsPtr navigation_details, |
| 33 int content_length = GetContentLength(response->headers); | 33 navigation::ResponseDetailsPtr response_details) OVERRIDE { |
| 34 int content_length = GetContentLength(response_details->response->headers); |
| 34 unsigned char* data = new unsigned char[content_length]; | 35 unsigned char* data = new unsigned char[content_length]; |
| 35 unsigned char* buf = data; | 36 unsigned char* buf = data; |
| 36 uint32_t bytes_remaining = content_length; | 37 uint32_t bytes_remaining = content_length; |
| 37 uint32_t num_bytes = bytes_remaining; | 38 uint32_t num_bytes = bytes_remaining; |
| 38 while (bytes_remaining > 0) { | 39 while (bytes_remaining > 0) { |
| 39 MojoResult result = ReadDataRaw( | 40 MojoResult result = ReadDataRaw( |
| 40 response_body_stream.get(), | 41 response_details->response_body_stream.get(), |
| 41 buf, | 42 buf, |
| 42 &num_bytes, | 43 &num_bytes, |
| 43 MOJO_READ_DATA_FLAG_NONE); | 44 MOJO_READ_DATA_FLAG_NONE); |
| 44 if (result == MOJO_RESULT_SHOULD_WAIT) { | 45 if (result == MOJO_RESULT_SHOULD_WAIT) { |
| 45 Wait(response_body_stream.get(), | 46 Wait(response_details->response_body_stream.get(), |
| 46 MOJO_HANDLE_SIGNAL_READABLE, | 47 MOJO_HANDLE_SIGNAL_READABLE, |
| 47 MOJO_DEADLINE_INDEFINITE); | 48 MOJO_DEADLINE_INDEFINITE); |
| 48 } else if (result == MOJO_RESULT_OK) { | 49 } else if (result == MOJO_RESULT_OK) { |
| 49 buf += num_bytes; | 50 buf += num_bytes; |
| 50 num_bytes = bytes_remaining -= num_bytes; | 51 num_bytes = bytes_remaining -= num_bytes; |
| 51 } else { | 52 } else { |
| 52 break; | 53 break; |
| 53 } | 54 } |
| 54 } | 55 } |
| 55 | 56 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 73 return atoi(t.token().c_str()); | 74 return atoi(t.token().c_str()); |
| 74 } | 75 } |
| 75 } | 76 } |
| 76 } | 77 } |
| 77 } | 78 } |
| 78 return 0; | 79 return 0; |
| 79 } | 80 } |
| 80 | 81 |
| 81 ImageViewer* viewer_; | 82 ImageViewer* viewer_; |
| 82 | 83 |
| 83 DISALLOW_COPY_AND_ASSIGN(LaunchableConnection); | 84 DISALLOW_COPY_AND_ASSIGN(NavigatorImpl); |
| 84 }; | 85 }; |
| 85 | 86 |
| 86 class ImageViewer : public Application, | 87 class ImageViewer : public Application, |
| 87 public view_manager::ViewManagerDelegate { | 88 public view_manager::ViewManagerDelegate { |
| 88 public: | 89 public: |
| 89 ImageViewer() : content_view_(NULL) {} | 90 ImageViewer() : content_view_(NULL) {} |
| 90 virtual ~ImageViewer() {} | 91 virtual ~ImageViewer() {} |
| 91 | 92 |
| 92 void UpdateView(view_manager::Id node_id, const SkBitmap& bitmap) { | 93 void UpdateView(view_manager::Id node_id, const SkBitmap& bitmap) { |
| 93 bitmap_ = bitmap; | 94 bitmap_ = bitmap; |
| 94 DrawBitmap(); | 95 DrawBitmap(); |
| 95 } | 96 } |
| 96 | 97 |
| 97 private: | 98 private: |
| 98 // Overridden from Application: | 99 // Overridden from Application: |
| 99 virtual void Initialize() OVERRIDE { | 100 virtual void Initialize() OVERRIDE { |
| 100 AddService<LaunchableConnection>(this); | 101 AddService<NavigatorImpl>(this); |
| 101 view_manager::ViewManager::Create(this, this); | 102 view_manager::ViewManager::Create(this, this); |
| 102 } | 103 } |
| 103 | 104 |
| 104 // Overridden from view_manager::ViewManagerDelegate: | 105 // Overridden from view_manager::ViewManagerDelegate: |
| 105 virtual void OnRootAdded(view_manager::ViewManager* view_manager, | 106 virtual void OnRootAdded(view_manager::ViewManager* view_manager, |
| 106 view_manager::Node* root) OVERRIDE { | 107 view_manager::Node* root) OVERRIDE { |
| 107 content_view_ = view_manager::View::Create(view_manager); | 108 content_view_ = view_manager::View::Create(view_manager); |
| 108 root->SetActiveView(content_view_); | 109 root->SetActiveView(content_view_); |
| 109 content_view_->SetColor(SK_ColorRED); | 110 content_view_->SetColor(SK_ColorRED); |
| 110 if (!bitmap_.isNull()) | 111 if (!bitmap_.isNull()) |
| 111 DrawBitmap(); | 112 DrawBitmap(); |
| 112 } | 113 } |
| 113 | 114 |
| 114 void DrawBitmap() { | 115 void DrawBitmap() { |
| 115 if (content_view_) | 116 if (content_view_) |
| 116 content_view_->SetContents(bitmap_); | 117 content_view_->SetContents(bitmap_); |
| 117 } | 118 } |
| 118 | 119 |
| 119 view_manager::View* content_view_; | 120 view_manager::View* content_view_; |
| 120 SkBitmap bitmap_; | 121 SkBitmap bitmap_; |
| 121 | 122 |
| 122 DISALLOW_COPY_AND_ASSIGN(ImageViewer); | 123 DISALLOW_COPY_AND_ASSIGN(ImageViewer); |
| 123 }; | 124 }; |
| 124 | 125 |
| 125 void LaunchableConnection::UpdateView(view_manager::Id node_id, | 126 void NavigatorImpl::UpdateView(view_manager::Id node_id, |
| 126 const SkBitmap& bitmap) { | 127 const SkBitmap& bitmap) { |
| 127 viewer_->UpdateView(node_id, bitmap); | 128 viewer_->UpdateView(node_id, bitmap); |
| 128 } | 129 } |
| 129 | 130 |
| 130 } // namespace examples | 131 } // namespace examples |
| 131 | 132 |
| 132 // static | 133 // static |
| 133 Application* Application::Create() { | 134 Application* Application::Create() { |
| 134 return new examples::ImageViewer; | 135 return new examples::ImageViewer; |
| 135 } | 136 } |
| 136 | 137 |
| 137 } // namespace mojo | 138 } // namespace mojo |
| OLD | NEW |