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