Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / remoting / host / chromoting_param_traits.cc
blob6a46d9cedf0dc5cb54c3fb3719dd766e852bb478
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "remoting/host/chromoting_param_traits.h"
7 #include "base/strings/stringprintf.h"
8 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h"
10 namespace IPC {
12 // static
13 void ParamTraits<webrtc::DesktopVector>::Write(Message* m,
14 const webrtc::DesktopVector& p) {
15 m->WriteInt(p.x());
16 m->WriteInt(p.y());
19 // static
20 bool ParamTraits<webrtc::DesktopVector>::Read(const Message* m,
21 base::PickleIterator* iter,
22 webrtc::DesktopVector* r) {
23 int x, y;
24 if (!iter->ReadInt(&x) || !iter->ReadInt(&y))
25 return false;
26 *r = webrtc::DesktopVector(x, y);
27 return true;
30 // static
31 void ParamTraits<webrtc::DesktopVector>::Log(const webrtc::DesktopVector& p,
32 std::string* l) {
33 l->append(base::StringPrintf("webrtc::DesktopVector(%d, %d)",
34 p.x(), p.y()));
37 // static
38 void ParamTraits<webrtc::DesktopSize>::Write(Message* m,
39 const webrtc::DesktopSize& p) {
40 m->WriteInt(p.width());
41 m->WriteInt(p.height());
44 // static
45 bool ParamTraits<webrtc::DesktopSize>::Read(const Message* m,
46 base::PickleIterator* iter,
47 webrtc::DesktopSize* r) {
48 int width, height;
49 if (!iter->ReadInt(&width) || !iter->ReadInt(&height))
50 return false;
51 *r = webrtc::DesktopSize(width, height);
52 return true;
55 // static
56 void ParamTraits<webrtc::DesktopSize>::Log(const webrtc::DesktopSize& p,
57 std::string* l) {
58 l->append(base::StringPrintf("webrtc::DesktopSize(%d, %d)",
59 p.width(), p.height()));
62 // static
63 void ParamTraits<webrtc::DesktopRect>::Write(Message* m,
64 const webrtc::DesktopRect& p) {
65 m->WriteInt(p.left());
66 m->WriteInt(p.top());
67 m->WriteInt(p.right());
68 m->WriteInt(p.bottom());
71 // static
72 bool ParamTraits<webrtc::DesktopRect>::Read(const Message* m,
73 base::PickleIterator* iter,
74 webrtc::DesktopRect* r) {
75 int left, right, top, bottom;
76 if (!iter->ReadInt(&left) || !iter->ReadInt(&top) ||
77 !iter->ReadInt(&right) || !iter->ReadInt(&bottom)) {
78 return false;
80 *r = webrtc::DesktopRect::MakeLTRB(left, top, right, bottom);
81 return true;
84 // static
85 void ParamTraits<webrtc::DesktopRect>::Log(const webrtc::DesktopRect& p,
86 std::string* l) {
87 l->append(base::StringPrintf("webrtc::DesktopRect(%d, %d, %d, %d)",
88 p.left(), p.top(), p.right(), p.bottom()));
91 // static
92 void ParamTraits<webrtc::MouseCursor>::Write(
93 Message* m,
94 const webrtc::MouseCursor& p) {
95 ParamTraits<webrtc::DesktopSize>::Write(m, p.image()->size());
97 // Data is serialized in such a way that size is exactly width * height *
98 // |kBytesPerPixel|.
99 std::string data;
100 uint8_t* current_row = p.image()->data();
101 for (int y = 0; y < p.image()->size().height(); ++y) {
102 data.append(current_row,
103 current_row + p.image()->size().width() *
104 webrtc::DesktopFrame::kBytesPerPixel);
105 current_row += p.image()->stride();
107 m->WriteData(reinterpret_cast<const char*>(p.image()->data()), data.size());
109 ParamTraits<webrtc::DesktopVector>::Write(m, p.hotspot());
112 // static
113 bool ParamTraits<webrtc::MouseCursor>::Read(const Message* m,
114 base::PickleIterator* iter,
115 webrtc::MouseCursor* r) {
116 webrtc::DesktopSize size;
117 if (!ParamTraits<webrtc::DesktopSize>::Read(m, iter, &size) ||
118 size.width() <= 0 || size.width() > (SHRT_MAX / 2) ||
119 size.height() <= 0 || size.height() > (SHRT_MAX / 2)) {
120 return false;
123 const int expected_length =
124 size.width() * size.height() * webrtc::DesktopFrame::kBytesPerPixel;
126 const char* data;
127 int data_length;
128 if (!iter->ReadData(&data, &data_length) || data_length != expected_length)
129 return false;
131 webrtc::DesktopVector hotspot;
132 if (!ParamTraits<webrtc::DesktopVector>::Read(m, iter, &hotspot))
133 return false;
135 webrtc::BasicDesktopFrame* image = new webrtc::BasicDesktopFrame(size);
136 memcpy(image->data(), data, data_length);
138 r->set_image(image);
139 r->set_hotspot(hotspot);
140 return true;
143 // static
144 void ParamTraits<webrtc::MouseCursor>::Log(
145 const webrtc::MouseCursor& p,
146 std::string* l) {
147 l->append(base::StringPrintf(
148 "webrtc::DesktopRect{image(%d, %d), hotspot(%d, %d)}",
149 p.image()->size().width(), p.image()->size().height(),
150 p.hotspot().x(), p.hotspot().y()));
154 // static
155 void ParamTraits<remoting::ScreenResolution>::Write(
156 Message* m,
157 const remoting::ScreenResolution& p) {
158 ParamTraits<webrtc::DesktopSize>::Write(m, p.dimensions());
159 ParamTraits<webrtc::DesktopVector>::Write(m, p.dpi());
162 // static
163 bool ParamTraits<remoting::ScreenResolution>::Read(
164 const Message* m,
165 base::PickleIterator* iter,
166 remoting::ScreenResolution* r) {
167 webrtc::DesktopSize size;
168 webrtc::DesktopVector dpi;
169 if (!ParamTraits<webrtc::DesktopSize>::Read(m, iter, &size) ||
170 !ParamTraits<webrtc::DesktopVector>::Read(m, iter, &dpi)) {
171 return false;
173 if (size.width() < 0 || size.height() < 0 ||
174 dpi.x() < 0 || dpi.y() < 0) {
175 return false;
177 *r = remoting::ScreenResolution(size, dpi);
178 return true;
181 // static
182 void ParamTraits<remoting::ScreenResolution>::Log(
183 const remoting::ScreenResolution& p,
184 std::string* l) {
185 l->append(base::StringPrintf("webrtc::ScreenResolution(%d, %d, %d, %d)",
186 p.dimensions().width(), p.dimensions().height(),
187 p.dpi().x(), p.dpi().y()));
190 } // namespace IPC