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

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

Issue 514063003: Update view_manager and window_manager to make use of content handling. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@viewman2
Patch Set: Created 6 years, 3 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
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/memory/scoped_ptr.h"
7 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
8 #include "base/strings/string_tokenizer.h" 9 #include "base/strings/string_tokenizer.h"
9 #include "mojo/examples/media_viewer/media_viewer.mojom.h" 10 #include "mojo/examples/media_viewer/media_viewer.mojom.h"
10 #include "mojo/public/c/system/main.h" 11 #include "mojo/public/c/system/main.h"
11 #include "mojo/public/cpp/application/application_connection.h" 12 #include "mojo/public/cpp/application/application_connection.h"
12 #include "mojo/public/cpp/application/application_delegate.h" 13 #include "mojo/public/cpp/application/application_delegate.h"
13 #include "mojo/public/cpp/application/application_runner_chromium.h" 14 #include "mojo/public/cpp/application/application_runner_chromium.h"
14 #include "mojo/public/cpp/application/interface_factory_impl.h" 15 #include "mojo/public/cpp/application/interface_factory_impl.h"
16 #include "mojo/public/cpp/application/service_provider_impl.h"
15 #include "mojo/services/public/cpp/view_manager/types.h" 17 #include "mojo/services/public/cpp/view_manager/types.h"
16 #include "mojo/services/public/cpp/view_manager/view.h" 18 #include "mojo/services/public/cpp/view_manager/view.h"
17 #include "mojo/services/public/cpp/view_manager/view_manager.h" 19 #include "mojo/services/public/cpp/view_manager/view_manager.h"
18 #include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h" 20 #include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h"
19 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" 21 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h"
20 #include "mojo/services/public/cpp/view_manager/view_observer.h" 22 #include "mojo/services/public/cpp/view_manager/view_observer.h"
21 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h" 23 #include "mojo/services/public/interfaces/content_handler/content_handler.mojom. h"
22 #include "skia/ext/platform_canvas.h" 24 #include "skia/ext/platform_canvas.h"
23 #include "skia/ext/refptr.h" 25 #include "skia/ext/refptr.h"
24 #include "third_party/skia/include/core/SkBitmap.h" 26 #include "third_party/skia/include/core/SkBitmap.h"
25 #include "third_party/skia/include/core/SkCanvas.h" 27 #include "third_party/skia/include/core/SkCanvas.h"
26 #include "third_party/skia/include/core/SkPaint.h" 28 #include "third_party/skia/include/core/SkPaint.h"
27 #include "third_party/skia/include/core/SkScalar.h" 29 #include "third_party/skia/include/core/SkScalar.h"
28 #include "ui/gfx/codec/png_codec.h" 30 #include "ui/gfx/codec/png_codec.h"
29 31
30 namespace mojo { 32 namespace mojo {
31 namespace examples { 33 namespace examples {
32 34
33 class PNGViewer; 35 class PNGViewer;
34 36
35 class ZoomableMediaImpl : public InterfaceImpl<ZoomableMedia> { 37 // TODO(aa): Hook up ZoomableMedia interface again.
38 class PNGView : public ViewManagerDelegate,
39 public ViewObserver {
36 public: 40 public:
37 explicit ZoomableMediaImpl(PNGViewer* viewer) : viewer_(viewer) {} 41 PNGView(URLResponsePtr response,
38 virtual ~ZoomableMediaImpl() {} 42 ServiceProviderImpl* exported_services,
43 scoped_ptr<ServiceProvider> imported_services)
44 : imported_services_(imported_services.Pass()),
45 root_(NULL),
46 view_manager_client_factory_(this),
47 zoom_percentage_(kDefaultZoomPercentage) {
48 exported_services->AddService(&view_manager_client_factory_);
49 DecodePNG(response.Pass());
50 }
39 51
40 private: 52 private:
41 // Overridden from ZoomableMedia: 53 static const uint16_t kMaxZoomPercentage = 400;
42 virtual void ZoomIn() OVERRIDE; 54 static const uint16_t kMinZoomPercentage = 20;
43 virtual void ZoomOut() OVERRIDE; 55 static const uint16_t kDefaultZoomPercentage = 100;
44 virtual void ZoomToActualSize() OVERRIDE; 56 static const uint16_t kZoomStep = 20;
45 57
46 PNGViewer* viewer_; 58 virtual ~PNGView() {
59 if (root_)
60 root_->RemoveObserver(this);
61 }
47 62
48 DISALLOW_COPY_AND_ASSIGN(ZoomableMediaImpl); 63 // Overridden from ViewManagerDelegate:
49 }; 64 virtual void OnEmbed(ViewManager* view_manager,
65 View* root,
66 ServiceProviderImpl* exported_services,
67 scoped_ptr<ServiceProvider> imported_services) OVERRIDE {
68 root_ = root;
69 root_->AddObserver(this);
70 root_->SetColor(SK_ColorGRAY);
71 if (!bitmap_.isNull())
72 DrawBitmap();
73 }
50 74
51 class NavigatorImpl : public InterfaceImpl<Navigator> { 75 virtual void OnViewManagerDisconnected(
52 public: 76 ViewManager* view_manager) OVERRIDE {
53 explicit NavigatorImpl(PNGViewer* viewer) : viewer_(viewer) {} 77 // TODO(aa): Need to figure out how shutdown works.
54 virtual ~NavigatorImpl() {} 78 }
55 79
56 private: 80 // Overridden from ViewObserver:
57 // Overridden from Navigator: 81 virtual void OnViewBoundsChanged(View* view,
58 virtual void Navigate( 82 const gfx::Rect& old_bounds,
59 uint32_t view_id, 83 const gfx::Rect& new_bounds) OVERRIDE {
60 NavigationDetailsPtr navigation_details, 84 DCHECK_EQ(view, root_);
61 ResponseDetailsPtr response_details) OVERRIDE { 85 DrawBitmap();
62 int content_length = GetContentLength(response_details->response->headers); 86 }
63 unsigned char* data = new unsigned char[content_length]; 87
64 unsigned char* buf = data; 88 virtual void OnViewDestroyed(View* view) OVERRIDE {
89 DCHECK_EQ(view, root_);
90 delete this;
91 }
92
93 void DecodePNG(URLResponsePtr response) {
94 int content_length = GetContentLength(response->headers);
95 scoped_ptr<unsigned char[]> data(new unsigned char[content_length]);
96 unsigned char* buf = data.get();
65 uint32_t bytes_remaining = content_length; 97 uint32_t bytes_remaining = content_length;
66 uint32_t num_bytes = bytes_remaining; 98 uint32_t num_bytes = bytes_remaining;
67 while (bytes_remaining > 0) { 99 while (bytes_remaining > 0) {
68 MojoResult result = ReadDataRaw( 100 MojoResult result = ReadDataRaw(response->body.get(),
69 response_details->response->body.get(), 101 buf,
70 buf, 102 &num_bytes,
71 &num_bytes, 103 MOJO_READ_DATA_FLAG_NONE);
72 MOJO_READ_DATA_FLAG_NONE);
73 if (result == MOJO_RESULT_SHOULD_WAIT) { 104 if (result == MOJO_RESULT_SHOULD_WAIT) {
74 Wait(response_details->response->body.get(), 105 Wait(response->body.get(),
75 MOJO_HANDLE_SIGNAL_READABLE, 106 MOJO_HANDLE_SIGNAL_READABLE,
76 MOJO_DEADLINE_INDEFINITE); 107 MOJO_DEADLINE_INDEFINITE);
77 } else if (result == MOJO_RESULT_OK) { 108 } else if (result == MOJO_RESULT_OK) {
78 buf += num_bytes; 109 buf += num_bytes;
79 num_bytes = bytes_remaining -= num_bytes; 110 num_bytes = bytes_remaining -= num_bytes;
80 } else { 111 } else {
81 break; 112 break;
82 } 113 }
83 } 114 }
84 115
85 SkBitmap bitmap; 116 gfx::PNGCodec::Decode(static_cast<const unsigned char*>(data.get()),
86 gfx::PNGCodec::Decode(static_cast<const unsigned char*>(data), 117 content_length, &bitmap_);
87 content_length, &bitmap);
88 UpdateView(view_id, bitmap);
89
90 delete[] data;
91 } 118 }
92 119
93 void UpdateView(Id view_id, const SkBitmap& bitmap); 120 void DrawBitmap() {
121 if (!root_)
122 return;
123
124 skia::RefPtr<SkCanvas> canvas(skia::AdoptRef(skia::CreatePlatformCanvas(
125 root_->bounds().width(),
126 root_->bounds().height(),
127 true)));
128 canvas->drawColor(SK_ColorGRAY);
129 SkPaint paint;
130 SkScalar scale =
131 SkFloatToScalar(zoom_percentage_ * 1.0f / kDefaultZoomPercentage);
132 canvas->scale(scale, scale);
133 canvas->drawBitmap(bitmap_, 0, 0, &paint);
134 root_->SetContents(skia::GetTopDevice(*canvas)->accessBitmap(true));
135 }
136
137 void ZoomIn() {
138 if (zoom_percentage_ >= kMaxZoomPercentage)
139 return;
140 zoom_percentage_ += kZoomStep;
141 DrawBitmap();
142 }
143
144 void ZoomOut() {
145 if (zoom_percentage_ <= kMinZoomPercentage)
146 return;
147 zoom_percentage_ -= kZoomStep;
148 DrawBitmap();
149 }
150
151 void ZoomToActualSize() {
152 if (zoom_percentage_ == kDefaultZoomPercentage)
153 return;
154 zoom_percentage_ = kDefaultZoomPercentage;
155 DrawBitmap();
156 }
94 157
95 int GetContentLength(const Array<String>& headers) { 158 int GetContentLength(const Array<String>& headers) {
96 for (size_t i = 0; i < headers.size(); ++i) { 159 for (size_t i = 0; i < headers.size(); ++i) {
97 base::StringTokenizer t(headers[i], ": ;="); 160 base::StringTokenizer t(headers[i], ": ;=");
98 while (t.GetNext()) { 161 while (t.GetNext()) {
99 if (!t.token_is_delim() && t.token() == "Content-Length") { 162 if (!t.token_is_delim() && t.token() == "Content-Length") {
100 while (t.GetNext()) { 163 while (t.GetNext()) {
101 if (!t.token_is_delim()) 164 if (!t.token_is_delim())
102 return atoi(t.token().c_str()); 165 return atoi(t.token().c_str());
103 } 166 }
104 } 167 }
105 } 168 }
106 } 169 }
107 return 0; 170 return 0;
108 } 171 }
109 172
110 PNGViewer* viewer_; 173 SkBitmap bitmap_;
174 scoped_ptr<ServiceProvider> imported_services_;
175 View* root_;
176 ViewManagerClientFactory view_manager_client_factory_;
177 uint16_t zoom_percentage_;
111 178
112 DISALLOW_COPY_AND_ASSIGN(NavigatorImpl); 179 DISALLOW_COPY_AND_ASSIGN(PNGView);
113 }; 180 };
114 181
115 class PNGViewer 182 class ContentHandlerImpl : public InterfaceImpl<ContentHandler> {
116 : public ApplicationDelegate,
117 public ViewManagerDelegate,
118 public ViewObserver {
119 public: 183 public:
120 PNGViewer() 184 explicit ContentHandlerImpl(ApplicationConnection* application_connection)
121 : navigator_factory_(this), 185 : application_connection_(application_connection) {
122 zoomable_media_factory_(this), 186 }
123 view_manager_client_factory_(this), 187 virtual ~ContentHandlerImpl() {}
124 root_(NULL), 188
125 zoom_percentage_(kDefaultZoomPercentage) {} 189 private:
126 virtual ~PNGViewer() { 190 // Overridden from ContentHandler:
127 if (root_) 191 virtual void OnConnect(
128 root_->RemoveObserver(this); 192 const mojo::String& url,
193 URLResponsePtr response,
194 InterfaceRequest<ServiceProvider> service_provider) OVERRIDE {
195 ServiceProviderImpl* exported_services =
196 new ServiceProviderImpl(application_connection_);
197 BindToRequest(exported_services, &service_provider);
198 scoped_ptr<ServiceProvider> remote(
199 exported_services->CreateRemoteServiceProvider());
200 new PNGView(response.Pass(), exported_services, remote.Pass());
129 } 201 }
130 202
131 void UpdateView(Id view_id, const SkBitmap& bitmap) { 203 ApplicationConnection* application_connection_;
132 bitmap_ = bitmap; 204
133 zoom_percentage_ = kDefaultZoomPercentage; 205 DISALLOW_COPY_AND_ASSIGN(ContentHandlerImpl);
134 DrawBitmap(); 206 };
207
208 class PNGViewer : public ApplicationDelegate {
209 public:
210 PNGViewer() : content_handler_factory_(NULL) {
135 } 211 }
136
137 void ZoomIn() {
138 if (zoom_percentage_ >= kMaxZoomPercentage)
139 return;
140 zoom_percentage_ += kZoomStep;
141 DrawBitmap();
142 }
143
144 void ZoomOut() {
145 if (zoom_percentage_ <= kMinZoomPercentage)
146 return;
147 zoom_percentage_ -= kZoomStep;
148 DrawBitmap();
149 }
150
151 void ZoomToActualSize() {
152 if (zoom_percentage_ == kDefaultZoomPercentage)
153 return;
154 zoom_percentage_ = kDefaultZoomPercentage;
155 DrawBitmap();
156 }
157
158 private: 212 private:
159 static const uint16_t kMaxZoomPercentage = 400;
160 static const uint16_t kMinZoomPercentage = 20;
161 static const uint16_t kDefaultZoomPercentage = 100;
162 static const uint16_t kZoomStep = 20;
163
164 // Overridden from ApplicationDelegate: 213 // Overridden from ApplicationDelegate:
165 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) 214 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection)
166 MOJO_OVERRIDE { 215 MOJO_OVERRIDE {
167 connection->AddService(&navigator_factory_); 216 content_handler_factory_.set_context(connection);
168 connection->AddService(&zoomable_media_factory_); 217 connection->AddService(&content_handler_factory_);
169 connection->AddService(&view_manager_client_factory_);
170 return true; 218 return true;
171 } 219 }
172 220
173 // Overridden from ViewManagerDelegate: 221 InterfaceFactoryImplWithContext<
174 virtual void OnEmbed(ViewManager* view_manager, 222 ContentHandlerImpl, ApplicationConnection> content_handler_factory_;
175 View* root,
176 ServiceProviderImpl* exported_services,
177 scoped_ptr<ServiceProvider> imported_services) OVERRIDE {
178 root_ = root;
179 root_->AddObserver(this);
180 root_->SetColor(SK_ColorGRAY);
181 if (!bitmap_.isNull())
182 DrawBitmap();
183 }
184 virtual void OnViewManagerDisconnected(
185 ViewManager* view_manager) OVERRIDE {
186 base::MessageLoop::current()->Quit();
187 }
188
189 void DrawBitmap() {
190 if (!root_)
191 return;
192
193 skia::RefPtr<SkCanvas> canvas(skia::AdoptRef(skia::CreatePlatformCanvas(
194 root_->bounds().width(),
195 root_->bounds().height(),
196 true)));
197 canvas->drawColor(SK_ColorGRAY);
198 SkPaint paint;
199 SkScalar scale =
200 SkFloatToScalar(zoom_percentage_ * 1.0f / kDefaultZoomPercentage);
201 canvas->scale(scale, scale);
202 canvas->drawBitmap(bitmap_, 0, 0, &paint);
203 root_->SetContents(skia::GetTopDevice(*canvas)->accessBitmap(true));
204 }
205
206 // ViewObserver:
207 virtual void OnViewBoundsChanged(View* view,
208 const gfx::Rect& old_bounds,
209 const gfx::Rect& new_bounds) OVERRIDE {
210 DCHECK_EQ(view, root_);
211 DrawBitmap();
212 }
213 virtual void OnViewDestroyed(View* view) OVERRIDE {
214 DCHECK_EQ(view, root_);
215 view->RemoveObserver(this);
216 root_ = NULL;
217 }
218
219 InterfaceFactoryImplWithContext<NavigatorImpl, PNGViewer> navigator_factory_;
220 InterfaceFactoryImplWithContext<ZoomableMediaImpl, PNGViewer>
221 zoomable_media_factory_;
222 ViewManagerClientFactory view_manager_client_factory_;
223
224 View* root_;
225 SkBitmap bitmap_;
226 uint16_t zoom_percentage_;
227 223
228 DISALLOW_COPY_AND_ASSIGN(PNGViewer); 224 DISALLOW_COPY_AND_ASSIGN(PNGViewer);
229 }; 225 };
230 226
231 void ZoomableMediaImpl::ZoomIn() {
232 viewer_->ZoomIn();
233 }
234
235 void ZoomableMediaImpl::ZoomOut() {
236 viewer_->ZoomOut();
237 }
238
239 void ZoomableMediaImpl::ZoomToActualSize() {
240 viewer_->ZoomToActualSize();
241 }
242
243 void NavigatorImpl::UpdateView(Id view_id,
244 const SkBitmap& bitmap) {
245 viewer_->UpdateView(view_id, bitmap);
246 }
247
248 } // namespace examples 227 } // namespace examples
249 } // namespace mojo 228 } // namespace mojo
250 229
251 MojoResult MojoMain(MojoHandle shell_handle) { 230 MojoResult MojoMain(MojoHandle shell_handle) {
252 mojo::ApplicationRunnerChromium runner(new mojo::examples::PNGViewer); 231 mojo::ApplicationRunnerChromium runner(new mojo::examples::PNGViewer);
253 return runner.Run(shell_handle); 232 return runner.Run(shell_handle);
254 } 233 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698