Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(38)

Side by Side Diff: mojo/examples/png_viewer/png_viewer.cc

Issue 380413003: Mojo: Use InterfaceFactory<Interface> for service registration (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review feedback Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/interface_factory_with_context.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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 } 103 }
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
114 public view_manager::ViewManagerDelegate { 114 : public ApplicationDelegate,
115 public view_manager::ViewManagerDelegate,
116 public InterfaceFactoryWithContext<NavigatorImpl, PNGViewer>,
117 public InterfaceFactoryWithContext<ZoomableMediaImpl, PNGViewer> {
115 public: 118 public:
116 PNGViewer() : content_view_(NULL), zoom_percentage_(kDefaultZoomPercentage) {} 119 PNGViewer()
120 : InterfaceFactoryWithContext<NavigatorImpl, PNGViewer>(this),
121 InterfaceFactoryWithContext<ZoomableMediaImpl, PNGViewer>(this),
122 content_view_(NULL),
123 zoom_percentage_(kDefaultZoomPercentage),
124 view_manager_client_factory_(this) {}
117 virtual ~PNGViewer() {} 125 virtual ~PNGViewer() {}
118 126
119 void UpdateView(view_manager::Id node_id, const SkBitmap& bitmap) { 127 void UpdateView(view_manager::Id node_id, const SkBitmap& bitmap) {
120 bitmap_ = bitmap; 128 bitmap_ = bitmap;
121 zoom_percentage_ = kDefaultZoomPercentage; 129 zoom_percentage_ = kDefaultZoomPercentage;
122 DrawBitmap(); 130 DrawBitmap();
123 } 131 }
124 132
125 void ZoomIn() { 133 void ZoomIn() {
126 if (zoom_percentage_ >= kMaxZoomPercentage) 134 if (zoom_percentage_ >= kMaxZoomPercentage)
(...skipping 18 matching lines...) Expand all
145 153
146 private: 154 private:
147 static const uint16_t kMaxZoomPercentage = 400; 155 static const uint16_t kMaxZoomPercentage = 400;
148 static const uint16_t kMinZoomPercentage = 20; 156 static const uint16_t kMinZoomPercentage = 20;
149 static const uint16_t kDefaultZoomPercentage = 100; 157 static const uint16_t kDefaultZoomPercentage = 100;
150 static const uint16_t kZoomStep = 20; 158 static const uint16_t kZoomStep = 20;
151 159
152 // Overridden from ApplicationDelegate: 160 // Overridden from ApplicationDelegate:
153 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) 161 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection)
154 MOJO_OVERRIDE { 162 MOJO_OVERRIDE {
155 connection->AddService<NavigatorImpl>(this); 163 connection->AddServiceFactory<navigation::Navigator>(this);
156 connection->AddService<ZoomableMediaImpl>(this); 164 connection->AddServiceFactory<ZoomableMedia>(this);
157 view_manager::ViewManager::ConfigureIncomingConnection(connection, this); 165 connection->AddServiceFactory(&view_manager_client_factory_);
158 return true; 166 return true;
159 } 167 }
160 168
161 // Overridden from view_manager::ViewManagerDelegate: 169 // Overridden from view_manager::ViewManagerDelegate:
162 virtual void OnRootAdded(view_manager::ViewManager* view_manager, 170 virtual void OnRootAdded(view_manager::ViewManager* view_manager,
163 view_manager::Node* root) OVERRIDE { 171 view_manager::Node* root) OVERRIDE {
164 content_view_ = view_manager::View::Create(view_manager); 172 content_view_ = view_manager::View::Create(view_manager);
165 root->SetActiveView(content_view_); 173 root->SetActiveView(content_view_);
166 content_view_->SetColor(SK_ColorGRAY); 174 content_view_->SetColor(SK_ColorGRAY);
167 if (!bitmap_.isNull()) 175 if (!bitmap_.isNull())
(...skipping 17 matching lines...) Expand all
185 SkScalar scale = 193 SkScalar scale =
186 SkFloatToScalar(zoom_percentage_ * 1.0f / kDefaultZoomPercentage); 194 SkFloatToScalar(zoom_percentage_ * 1.0f / kDefaultZoomPercentage);
187 canvas->scale(scale, scale); 195 canvas->scale(scale, scale);
188 canvas->drawBitmap(bitmap_, 0, 0, &paint); 196 canvas->drawBitmap(bitmap_, 0, 0, &paint);
189 content_view_->SetContents(skia::GetTopDevice(*canvas)->accessBitmap(true)); 197 content_view_->SetContents(skia::GetTopDevice(*canvas)->accessBitmap(true));
190 } 198 }
191 199
192 view_manager::View* content_view_; 200 view_manager::View* content_view_;
193 SkBitmap bitmap_; 201 SkBitmap bitmap_;
194 uint16_t zoom_percentage_; 202 uint16_t zoom_percentage_;
203 view_manager::ViewManagerClientFactory view_manager_client_factory_;
195 204
196 DISALLOW_COPY_AND_ASSIGN(PNGViewer); 205 DISALLOW_COPY_AND_ASSIGN(PNGViewer);
197 }; 206 };
198 207
199 void ZoomableMediaImpl::ZoomIn() { 208 void ZoomableMediaImpl::ZoomIn() {
200 viewer_->ZoomIn(); 209 viewer_->ZoomIn();
201 } 210 }
202 211
203 void ZoomableMediaImpl::ZoomOut() { 212 void ZoomableMediaImpl::ZoomOut() {
204 viewer_->ZoomOut(); 213 viewer_->ZoomOut();
205 } 214 }
206 215
207 void ZoomableMediaImpl::ZoomToActualSize() { 216 void ZoomableMediaImpl::ZoomToActualSize() {
208 viewer_->ZoomToActualSize(); 217 viewer_->ZoomToActualSize();
209 } 218 }
210 219
211 void NavigatorImpl::UpdateView(view_manager::Id node_id, 220 void NavigatorImpl::UpdateView(view_manager::Id node_id,
212 const SkBitmap& bitmap) { 221 const SkBitmap& bitmap) {
213 viewer_->UpdateView(node_id, bitmap); 222 viewer_->UpdateView(node_id, bitmap);
214 } 223 }
215 224
216 } // namespace examples 225 } // namespace examples
217 226
218 // static 227 // static
219 ApplicationDelegate* ApplicationDelegate::Create() { 228 ApplicationDelegate* ApplicationDelegate::Create() {
220 return new examples::PNGViewer; 229 return new examples::PNGViewer;
221 } 230 }
222 231
223 } // namespace mojo 232 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698