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/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/strings/string_tokenizer.h" | 8 #include "base/strings/string_tokenizer.h" |
9 #include "mojo/examples/media_viewer/media_viewer.mojom.h" | 9 #include "mojo/examples/media_viewer/media_viewer.mojom.h" |
10 #include "mojo/public/cpp/application/application_connection.h" | 10 #include "mojo/public/cpp/application/application_connection.h" |
11 #include "mojo/public/cpp/application/application_delegate.h" | 11 #include "mojo/public/cpp/application/application_delegate.h" |
| 12 #include "mojo/public/cpp/application/context_interface_factory.h" |
12 #include "mojo/services/public/cpp/view_manager/node.h" | 13 #include "mojo/services/public/cpp/view_manager/node.h" |
13 #include "mojo/services/public/cpp/view_manager/types.h" | 14 #include "mojo/services/public/cpp/view_manager/types.h" |
14 #include "mojo/services/public/cpp/view_manager/view.h" | 15 #include "mojo/services/public/cpp/view_manager/view.h" |
15 #include "mojo/services/public/cpp/view_manager/view_manager.h" | 16 #include "mojo/services/public/cpp/view_manager/view_manager.h" |
| 17 #include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h" |
16 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" | 18 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" |
17 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h" | 19 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h" |
18 #include "skia/ext/platform_canvas.h" | 20 #include "skia/ext/platform_canvas.h" |
19 #include "skia/ext/refptr.h" | 21 #include "skia/ext/refptr.h" |
20 #include "third_party/skia/include/core/SkBitmap.h" | 22 #include "third_party/skia/include/core/SkBitmap.h" |
21 #include "third_party/skia/include/core/SkCanvas.h" | 23 #include "third_party/skia/include/core/SkCanvas.h" |
22 #include "third_party/skia/include/core/SkPaint.h" | 24 #include "third_party/skia/include/core/SkPaint.h" |
23 #include "third_party/skia/include/core/SkScalar.h" | 25 #include "third_party/skia/include/core/SkScalar.h" |
24 #include "ui/gfx/codec/png_codec.h" | 26 #include "ui/gfx/codec/png_codec.h" |
25 | 27 |
26 namespace mojo { | 28 namespace mojo { |
27 namespace examples { | 29 namespace examples { |
28 | 30 |
29 class PNGViewer; | 31 class PNGViewer; |
30 | 32 |
31 class ZoomableMediaImpl : public InterfaceImpl<ZoomableMedia> { | 33 class ZoomableMediaImpl : public InterfaceImpl<ZoomableMedia> { |
32 public: | 34 public: |
33 ZoomableMediaImpl(ApplicationConnection* connection, | 35 explicit ZoomableMediaImpl(PNGViewer* viewer) : viewer_(viewer) {} |
34 PNGViewer* viewer) : viewer_(viewer) {} | |
35 virtual ~ZoomableMediaImpl() {} | 36 virtual ~ZoomableMediaImpl() {} |
36 | 37 |
37 private: | 38 private: |
38 // Overridden from ZoomableMedia: | 39 // Overridden from ZoomableMedia: |
39 virtual void ZoomIn() OVERRIDE; | 40 virtual void ZoomIn() OVERRIDE; |
40 virtual void ZoomOut() OVERRIDE; | 41 virtual void ZoomOut() OVERRIDE; |
41 virtual void ZoomToActualSize() OVERRIDE; | 42 virtual void ZoomToActualSize() OVERRIDE; |
42 | 43 |
43 PNGViewer* viewer_; | 44 PNGViewer* viewer_; |
44 | 45 |
45 DISALLOW_COPY_AND_ASSIGN(ZoomableMediaImpl); | 46 DISALLOW_COPY_AND_ASSIGN(ZoomableMediaImpl); |
46 }; | 47 }; |
47 | 48 |
48 class NavigatorImpl : public InterfaceImpl<navigation::Navigator> { | 49 class NavigatorImpl : public InterfaceImpl<navigation::Navigator> { |
49 public: | 50 public: |
50 NavigatorImpl(ApplicationConnection* connection, | 51 explicit NavigatorImpl(PNGViewer* viewer) : viewer_(viewer) {} |
51 PNGViewer* viewer) : viewer_(viewer) {} | |
52 virtual ~NavigatorImpl() {} | 52 virtual ~NavigatorImpl() {} |
53 | 53 |
54 private: | 54 private: |
55 // Overridden from navigation::Navigate: | 55 // Overridden from navigation::Navigate: |
56 virtual void Navigate( | 56 virtual void Navigate( |
57 uint32_t node_id, | 57 uint32_t node_id, |
58 navigation::NavigationDetailsPtr navigation_details, | 58 navigation::NavigationDetailsPtr navigation_details, |
59 navigation::ResponseDetailsPtr response_details) OVERRIDE { | 59 navigation::ResponseDetailsPtr response_details) OVERRIDE { |
60 int content_length = GetContentLength(response_details->response->headers); | 60 int content_length = GetContentLength(response_details->response->headers); |
61 unsigned char* data = new unsigned char[content_length]; | 61 unsigned char* data = new unsigned char[content_length]; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 } | 104 } |
105 return 0; | 105 return 0; |
106 } | 106 } |
107 | 107 |
108 PNGViewer* viewer_; | 108 PNGViewer* viewer_; |
109 | 109 |
110 DISALLOW_COPY_AND_ASSIGN(NavigatorImpl); | 110 DISALLOW_COPY_AND_ASSIGN(NavigatorImpl); |
111 }; | 111 }; |
112 | 112 |
113 class PNGViewer : public ApplicationDelegate, | 113 class PNGViewer : public ApplicationDelegate, |
114 public view_manager::ViewManagerDelegate { | 114 public view_manager::ViewManagerDelegate, |
| 115 public ContextInterfaceFactory<NavigatorImpl, PNGViewer>, |
| 116 public ContextInterfaceFactory<ZoomableMediaImpl, PNGViewer> { |
115 public: | 117 public: |
116 PNGViewer() : content_view_(NULL), zoom_percentage_(kDefaultZoomPercentage) {} | 118 PNGViewer() |
| 119 : ContextInterfaceFactory<NavigatorImpl, PNGViewer>(this), |
| 120 ContextInterfaceFactory<ZoomableMediaImpl, PNGViewer>(this), |
| 121 content_view_(NULL), |
| 122 zoom_percentage_(kDefaultZoomPercentage), |
| 123 view_manager_client_factory_(this) {} |
117 virtual ~PNGViewer() {} | 124 virtual ~PNGViewer() {} |
118 | 125 |
119 void UpdateView(view_manager::Id node_id, const SkBitmap& bitmap) { | 126 void UpdateView(view_manager::Id node_id, const SkBitmap& bitmap) { |
120 bitmap_ = bitmap; | 127 bitmap_ = bitmap; |
121 zoom_percentage_ = kDefaultZoomPercentage; | 128 zoom_percentage_ = kDefaultZoomPercentage; |
122 DrawBitmap(); | 129 DrawBitmap(); |
123 } | 130 } |
124 | 131 |
125 void ZoomIn() { | 132 void ZoomIn() { |
126 if (zoom_percentage_ >= kMaxZoomPercentage) | 133 if (zoom_percentage_ >= kMaxZoomPercentage) |
(...skipping 18 matching lines...) Expand all Loading... |
145 | 152 |
146 private: | 153 private: |
147 static const uint16_t kMaxZoomPercentage = 400; | 154 static const uint16_t kMaxZoomPercentage = 400; |
148 static const uint16_t kMinZoomPercentage = 20; | 155 static const uint16_t kMinZoomPercentage = 20; |
149 static const uint16_t kDefaultZoomPercentage = 100; | 156 static const uint16_t kDefaultZoomPercentage = 100; |
150 static const uint16_t kZoomStep = 20; | 157 static const uint16_t kZoomStep = 20; |
151 | 158 |
152 // Overridden from ApplicationDelegate: | 159 // Overridden from ApplicationDelegate: |
153 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) | 160 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) |
154 MOJO_OVERRIDE { | 161 MOJO_OVERRIDE { |
155 connection->AddService<NavigatorImpl>(this); | 162 connection->AddServiceFactory<navigation::Navigator>(this); |
156 connection->AddService<ZoomableMediaImpl>(this); | 163 connection->AddServiceFactory<ZoomableMedia>(this); |
157 view_manager::ViewManager::ConfigureIncomingConnection(connection, this); | 164 connection->AddServiceFactory(&view_manager_client_factory_); |
158 return true; | 165 return true; |
159 } | 166 } |
160 | 167 |
161 // Overridden from view_manager::ViewManagerDelegate: | 168 // Overridden from view_manager::ViewManagerDelegate: |
162 virtual void OnRootAdded(view_manager::ViewManager* view_manager, | 169 virtual void OnRootAdded(view_manager::ViewManager* view_manager, |
163 view_manager::Node* root) OVERRIDE { | 170 view_manager::Node* root) OVERRIDE { |
164 content_view_ = view_manager::View::Create(view_manager); | 171 content_view_ = view_manager::View::Create(view_manager); |
165 root->SetActiveView(content_view_); | 172 root->SetActiveView(content_view_); |
166 content_view_->SetColor(SK_ColorGRAY); | 173 content_view_->SetColor(SK_ColorGRAY); |
167 if (!bitmap_.isNull()) | 174 if (!bitmap_.isNull()) |
(...skipping 17 matching lines...) Expand all Loading... |
185 SkScalar scale = | 192 SkScalar scale = |
186 SkFloatToScalar(zoom_percentage_ * 1.0f / kDefaultZoomPercentage); | 193 SkFloatToScalar(zoom_percentage_ * 1.0f / kDefaultZoomPercentage); |
187 canvas->scale(scale, scale); | 194 canvas->scale(scale, scale); |
188 canvas->drawBitmap(bitmap_, 0, 0, &paint); | 195 canvas->drawBitmap(bitmap_, 0, 0, &paint); |
189 content_view_->SetContents(skia::GetTopDevice(*canvas)->accessBitmap(true)); | 196 content_view_->SetContents(skia::GetTopDevice(*canvas)->accessBitmap(true)); |
190 } | 197 } |
191 | 198 |
192 view_manager::View* content_view_; | 199 view_manager::View* content_view_; |
193 SkBitmap bitmap_; | 200 SkBitmap bitmap_; |
194 uint16_t zoom_percentage_; | 201 uint16_t zoom_percentage_; |
| 202 view_manager::ViewManagerClientFactory view_manager_client_factory_; |
195 | 203 |
196 DISALLOW_COPY_AND_ASSIGN(PNGViewer); | 204 DISALLOW_COPY_AND_ASSIGN(PNGViewer); |
197 }; | 205 }; |
198 | 206 |
199 void ZoomableMediaImpl::ZoomIn() { | 207 void ZoomableMediaImpl::ZoomIn() { |
200 viewer_->ZoomIn(); | 208 viewer_->ZoomIn(); |
201 } | 209 } |
202 | 210 |
203 void ZoomableMediaImpl::ZoomOut() { | 211 void ZoomableMediaImpl::ZoomOut() { |
204 viewer_->ZoomOut(); | 212 viewer_->ZoomOut(); |
205 } | 213 } |
206 | 214 |
207 void ZoomableMediaImpl::ZoomToActualSize() { | 215 void ZoomableMediaImpl::ZoomToActualSize() { |
208 viewer_->ZoomToActualSize(); | 216 viewer_->ZoomToActualSize(); |
209 } | 217 } |
210 | 218 |
211 void NavigatorImpl::UpdateView(view_manager::Id node_id, | 219 void NavigatorImpl::UpdateView(view_manager::Id node_id, |
212 const SkBitmap& bitmap) { | 220 const SkBitmap& bitmap) { |
213 viewer_->UpdateView(node_id, bitmap); | 221 viewer_->UpdateView(node_id, bitmap); |
214 } | 222 } |
215 | 223 |
216 } // namespace examples | 224 } // namespace examples |
217 | 225 |
218 // static | 226 // static |
219 ApplicationDelegate* ApplicationDelegate::Create() { | 227 ApplicationDelegate* ApplicationDelegate::Create() { |
220 return new examples::PNGViewer; | 228 return new examples::PNGViewer; |
221 } | 229 } |
222 | 230 |
223 } // namespace mojo | 231 } // namespace mojo |
OLD | NEW |