Remove dependency on content from remoting_host.
[chromium-blink-merge.git] / remoting / host / chromoting_param_traits.cc
blob9434cceecf85db4f69807f9080996b215dec90c3
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 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 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 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(
114 const Message* m,
115 PickleIterator* iter,
116 webrtc::MouseCursor* r) {
117 webrtc::DesktopSize size;
118 if (!ParamTraits<webrtc::DesktopSize>::Read(m, iter, &size) ||
119 size.width() <= 0 || size.width() > (SHRT_MAX / 2) ||
120 size.height() <= 0 || size.height() > (SHRT_MAX / 2)) {
121 return false;
124 const int expected_length =
125 size.width() * size.height() * webrtc::DesktopFrame::kBytesPerPixel;
127 const char* data;
128 int data_length;
129 if (!iter->ReadData(&data, &data_length) || data_length != expected_length)
130 return false;
132 webrtc::DesktopVector hotspot;
133 if (!ParamTraits<webrtc::DesktopVector>::Read(m, iter, &hotspot))
134 return false;
136 webrtc::BasicDesktopFrame* image = new webrtc::BasicDesktopFrame(size);
137 memcpy(image->data(), data, data_length);
139 r->set_image(image);
140 r->set_hotspot(hotspot);
141 return true;
144 // static
145 void ParamTraits<webrtc::MouseCursor>::Log(
146 const webrtc::MouseCursor& p,
147 std::string* l) {
148 l->append(base::StringPrintf(
149 "webrtc::DesktopRect{image(%d, %d), hotspot(%d, %d)}",
150 p.image()->size().width(), p.image()->size().height(),
151 p.hotspot().x(), p.hotspot().y()));
155 // static
156 void ParamTraits<remoting::ScreenResolution>::Write(
157 Message* m,
158 const remoting::ScreenResolution& p) {
159 ParamTraits<webrtc::DesktopSize>::Write(m, p.dimensions());
160 ParamTraits<webrtc::DesktopVector>::Write(m, p.dpi());
163 // static
164 bool ParamTraits<remoting::ScreenResolution>::Read(
165 const Message* m,
166 PickleIterator* iter,
167 remoting::ScreenResolution* r) {
168 webrtc::DesktopSize size;
169 webrtc::DesktopVector dpi;
170 if (!ParamTraits<webrtc::DesktopSize>::Read(m, iter, &size) ||
171 !ParamTraits<webrtc::DesktopVector>::Read(m, iter, &dpi)) {
172 return false;
174 if (size.width() < 0 || size.height() < 0 ||
175 dpi.x() < 0 || dpi.y() < 0) {
176 return false;
178 *r = remoting::ScreenResolution(size, dpi);
179 return true;
182 // static
183 void ParamTraits<remoting::ScreenResolution>::Log(
184 const remoting::ScreenResolution& p,
185 std::string* l) {
186 l->append(base::StringPrintf("webrtc::ScreenResolution(%d, %d, %d, %d)",
187 p.dimensions().width(), p.dimensions().height(),
188 p.dpi().x(), p.dpi().y()));
191 } // namespace IPC