OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "remoting/protocol/jingle_messages.h" | 5 #include "remoting/protocol/jingle_messages.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/string_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
9 #include "remoting/base/constants.h" | 9 #include "remoting/base/constants.h" |
10 #include "remoting/protocol/content_description.h" | 10 #include "remoting/protocol/content_description.h" |
| 11 #include "remoting/protocol/name_value_map.h" |
11 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" | 12 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" |
12 | 13 |
13 using buzz::QName; | 14 using buzz::QName; |
14 using buzz::XmlElement; | 15 using buzz::XmlElement; |
15 | 16 |
16 namespace remoting { | 17 namespace remoting { |
17 namespace protocol { | 18 namespace protocol { |
18 | 19 |
19 const char kJabberNamespace[] = "jabber:client"; | 20 const char kJabberNamespace[] = "jabber:client"; |
20 const char kJingleNamespace[] = "urn:xmpp:jingle:1"; | 21 const char kJingleNamespace[] = "urn:xmpp:jingle:1"; |
21 const char kP2PTransportNamespace[] = "http://www.google.com/transport/p2p"; | 22 const char kP2PTransportNamespace[] = "http://www.google.com/transport/p2p"; |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
25 const char kEmptyNamespace[] = ""; | 26 const char kEmptyNamespace[] = ""; |
26 const char kXmlNamespace[] = "http://www.w3.org/XML/1998/namespace"; | 27 const char kXmlNamespace[] = "http://www.w3.org/XML/1998/namespace"; |
27 | 28 |
28 const int kPortMin = 1000; | 29 const int kPortMin = 1000; |
29 const int kPortMax = 65535; | 30 const int kPortMax = 65535; |
30 | 31 |
31 template <typename T> | |
32 struct NameMapElement { | |
33 const T value; | |
34 const char* const name; | |
35 }; | |
36 | |
37 template <typename T> | |
38 const char* ValueToName(const NameMapElement<T> map[], size_t map_size, | |
39 T value) { | |
40 for (size_t i = 0; i < map_size; ++i) { | |
41 if (map[i].value == value) | |
42 return map[i].name; | |
43 } | |
44 return NULL; | |
45 } | |
46 | |
47 template <typename T> | |
48 T NameToValue(const NameMapElement<T> map[], size_t map_size, | |
49 const std::string& name, T default_value) { | |
50 for (size_t i = 0; i < map_size; ++i) { | |
51 if (map[i].name == name) | |
52 return map[i].value; | |
53 } | |
54 return default_value; | |
55 } | |
56 | |
57 const NameMapElement<JingleMessage::ActionType> kActionTypes[] = { | 32 const NameMapElement<JingleMessage::ActionType> kActionTypes[] = { |
58 { JingleMessage::SESSION_INITIATE, "session-initiate" }, | 33 { JingleMessage::SESSION_INITIATE, "session-initiate" }, |
59 { JingleMessage::SESSION_ACCEPT, "session-accept" }, | 34 { JingleMessage::SESSION_ACCEPT, "session-accept" }, |
60 { JingleMessage::SESSION_TERMINATE, "session-terminate" }, | 35 { JingleMessage::SESSION_TERMINATE, "session-terminate" }, |
61 { JingleMessage::SESSION_INFO, "session-info" }, | 36 { JingleMessage::SESSION_INFO, "session-info" }, |
62 { JingleMessage::TRANSPORT_INFO, "transport-info" }, | 37 { JingleMessage::TRANSPORT_INFO, "transport-info" }, |
63 }; | 38 }; |
64 | 39 |
65 const NameMapElement<JingleMessage::Reason> kReasons[] = { | 40 const NameMapElement<JingleMessage::Reason> kReasons[] = { |
66 { JingleMessage::SUCCESS, "success" }, | 41 { JingleMessage::SUCCESS, "success" }, |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 // static | 125 // static |
151 bool JingleMessage::IsJingleMessage(const buzz::XmlElement* stanza) { | 126 bool JingleMessage::IsJingleMessage(const buzz::XmlElement* stanza) { |
152 return | 127 return |
153 stanza->Name() == QName(kJabberNamespace, "iq") && | 128 stanza->Name() == QName(kJabberNamespace, "iq") && |
154 stanza->Attr(QName("", "type")) == "set" && | 129 stanza->Attr(QName("", "type")) == "set" && |
155 stanza->FirstNamed(QName(kJingleNamespace, "jingle")) != NULL; | 130 stanza->FirstNamed(QName(kJingleNamespace, "jingle")) != NULL; |
156 } | 131 } |
157 | 132 |
158 // static | 133 // static |
159 std::string JingleMessage::GetActionName(ActionType action) { | 134 std::string JingleMessage::GetActionName(ActionType action) { |
160 return ValueToName(kActionTypes, arraysize(kActionTypes), action); | 135 return ValueToName(kActionTypes, action); |
161 } | 136 } |
162 | 137 |
163 JingleMessage::JingleMessage() | 138 JingleMessage::JingleMessage() |
164 : action(UNKNOWN_ACTION), | 139 : action(UNKNOWN_ACTION), |
165 reason(UNKNOWN_REASON) { | 140 reason(UNKNOWN_REASON) { |
166 } | 141 } |
167 | 142 |
168 JingleMessage::JingleMessage( | 143 JingleMessage::JingleMessage( |
169 const std::string& to_value, | 144 const std::string& to_value, |
170 ActionType action_value, | 145 ActionType action_value, |
171 const std::string& sid_value) | 146 const std::string& sid_value) |
172 : to(to_value), | 147 : to(to_value), |
173 action(action_value), | 148 action(action_value), |
174 sid(sid_value), | 149 sid(sid_value), |
175 reason(UNKNOWN_REASON) { | 150 reason(UNKNOWN_REASON) { |
176 } | 151 } |
177 | 152 |
178 JingleMessage::~JingleMessage() { | 153 JingleMessage::~JingleMessage() { |
179 } | 154 } |
180 | 155 |
181 bool JingleMessage::ParseXml(const buzz::XmlElement* stanza, | 156 bool JingleMessage::ParseXml(const buzz::XmlElement* stanza, |
182 std::string* error) { | 157 std::string* error) { |
183 if (!IsJingleMessage(stanza)) { | 158 if (!IsJingleMessage(stanza)) { |
184 *error = "Not a jingle message"; | 159 *error = "Not a jingle message"; |
185 return false; | 160 return false; |
186 } | 161 } |
187 | 162 |
188 const XmlElement* jingle_tag = | 163 const XmlElement* jingle_tag = |
189 stanza->FirstNamed(QName(kJingleNamespace, "jingle")); | 164 stanza->FirstNamed(QName(kJingleNamespace, "jingle")); |
190 if (jingle_tag == NULL) { | 165 if (!jingle_tag) { |
191 *error = "Not a jingle message"; | 166 *error = "Not a jingle message"; |
192 return false; | 167 return false; |
193 } | 168 } |
194 | 169 |
195 from = stanza->Attr(QName(kEmptyNamespace, "from")); | 170 from = stanza->Attr(QName(kEmptyNamespace, "from")); |
196 to = stanza->Attr(QName(kEmptyNamespace, "to")); | 171 to = stanza->Attr(QName(kEmptyNamespace, "to")); |
197 | 172 |
198 std::string action_str = jingle_tag->Attr(QName(kEmptyNamespace, "action")); | 173 std::string action_str = jingle_tag->Attr(QName(kEmptyNamespace, "action")); |
199 if (action_str.empty()) { | 174 if (action_str.empty()) { |
200 *error = "action attribute is missing"; | 175 *error = "action attribute is missing"; |
201 return false; | 176 return false; |
202 } | 177 } |
203 action = NameToValue( | 178 if (!NameToValue(kActionTypes, action_str, &action)) { |
204 kActionTypes, arraysize(kActionTypes), action_str, UNKNOWN_ACTION); | |
205 if (action == UNKNOWN_ACTION) { | |
206 *error = "Unknown action " + action_str; | 179 *error = "Unknown action " + action_str; |
207 return false; | 180 return false; |
208 } | 181 } |
209 | 182 |
210 sid = jingle_tag->Attr(QName(kEmptyNamespace, "sid")); | 183 sid = jingle_tag->Attr(QName(kEmptyNamespace, "sid")); |
211 if (sid.empty()) { | 184 if (sid.empty()) { |
212 *error = "sid attribute is missing"; | 185 *error = "sid attribute is missing"; |
213 return false; | 186 return false; |
214 } | 187 } |
215 | 188 |
216 if (action == SESSION_INFO) { | 189 if (action == SESSION_INFO) { |
217 // session-info messages may contain arbitrary information not | 190 // session-info messages may contain arbitrary information not |
218 // defined by the Jingle protocol. We don't need to parse it. | 191 // defined by the Jingle protocol. We don't need to parse it. |
219 const XmlElement* child = jingle_tag->FirstElement(); | 192 const XmlElement* child = jingle_tag->FirstElement(); |
220 if (child) { | 193 if (child) { |
221 // session-info is allowed to be empty. | 194 // session-info is allowed to be empty. |
222 info.reset(new XmlElement(*child)); | 195 info.reset(new XmlElement(*child)); |
223 } else { | 196 } else { |
224 info.reset(NULL); | 197 info.reset(NULL); |
225 } | 198 } |
226 return true; | 199 return true; |
227 } | 200 } |
228 | 201 |
229 const XmlElement* reason_tag = | 202 const XmlElement* reason_tag = |
230 jingle_tag->FirstNamed(QName(kJingleNamespace, "reason")); | 203 jingle_tag->FirstNamed(QName(kJingleNamespace, "reason")); |
231 if (reason_tag && reason_tag->FirstElement()) { | 204 if (reason_tag && reason_tag->FirstElement()) { |
232 reason = NameToValue( | 205 if (!NameToValue(kReasons, reason_tag->FirstElement()->Name().LocalPart(), |
233 kReasons, arraysize(kReasons), | 206 &reason)) { |
234 reason_tag->FirstElement()->Name().LocalPart(), UNKNOWN_REASON); | 207 reason = UNKNOWN_REASON; |
| 208 } |
235 } | 209 } |
236 | 210 |
237 if (action == SESSION_TERMINATE) | 211 if (action == SESSION_TERMINATE) |
238 return true; | 212 return true; |
239 | 213 |
240 const XmlElement* content_tag = | 214 const XmlElement* content_tag = |
241 jingle_tag->FirstNamed(QName(kJingleNamespace, "content")); | 215 jingle_tag->FirstNamed(QName(kJingleNamespace, "content")); |
242 if (!content_tag) { | 216 if (!content_tag) { |
243 *error = "content tag is missing"; | 217 *error = "content tag is missing"; |
244 return false; | 218 return false; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 root->AddAttr(QName(kEmptyNamespace, "to"), to); | 269 root->AddAttr(QName(kEmptyNamespace, "to"), to); |
296 if (!from.empty()) | 270 if (!from.empty()) |
297 root->AddAttr(QName(kEmptyNamespace, "from"), from); | 271 root->AddAttr(QName(kEmptyNamespace, "from"), from); |
298 root->SetAttr(QName(kEmptyNamespace, "type"), "set"); | 272 root->SetAttr(QName(kEmptyNamespace, "type"), "set"); |
299 | 273 |
300 XmlElement* jingle_tag = | 274 XmlElement* jingle_tag = |
301 new XmlElement(QName(kJingleNamespace, "jingle"), true); | 275 new XmlElement(QName(kJingleNamespace, "jingle"), true); |
302 root->AddElement(jingle_tag); | 276 root->AddElement(jingle_tag); |
303 jingle_tag->AddAttr(QName(kEmptyNamespace, "sid"), sid); | 277 jingle_tag->AddAttr(QName(kEmptyNamespace, "sid"), sid); |
304 | 278 |
305 const char* action_attr = ValueToName( | 279 const char* action_attr = ValueToName(kActionTypes, action); |
306 kActionTypes, arraysize(kActionTypes), action); | |
307 if (!action_attr) | 280 if (!action_attr) |
308 LOG(FATAL) << "Invalid action value " << action; | 281 LOG(FATAL) << "Invalid action value " << action; |
309 jingle_tag->AddAttr(QName(kEmptyNamespace, "action"), action_attr); | 282 jingle_tag->AddAttr(QName(kEmptyNamespace, "action"), action_attr); |
310 | 283 |
311 if (action == SESSION_INFO) { | 284 if (action == SESSION_INFO) { |
312 if (info.get()) | 285 if (info.get()) |
313 jingle_tag->AddElement(new XmlElement(*info.get())); | 286 jingle_tag->AddElement(new XmlElement(*info.get())); |
314 return root.Pass(); | 287 return root.Pass(); |
315 } | 288 } |
316 | 289 |
317 if (action == SESSION_INITIATE) | 290 if (action == SESSION_INITIATE) |
318 jingle_tag->AddAttr(QName(kEmptyNamespace, "initiator"), from); | 291 jingle_tag->AddAttr(QName(kEmptyNamespace, "initiator"), from); |
319 | 292 |
320 if (reason != UNKNOWN_REASON) { | 293 if (reason != UNKNOWN_REASON) { |
321 XmlElement* reason_tag = new XmlElement(QName(kJingleNamespace, "reason")); | 294 XmlElement* reason_tag = new XmlElement(QName(kJingleNamespace, "reason")); |
322 jingle_tag->AddElement(reason_tag); | 295 jingle_tag->AddElement(reason_tag); |
323 const char* reason_string = | 296 const char* reason_string = |
324 ValueToName(kReasons, arraysize(kReasons), reason); | 297 ValueToName(kReasons, reason); |
325 if (reason_string == NULL) | 298 if (!reason_string) |
326 LOG(FATAL) << "Invalid reason: " << reason; | 299 LOG(FATAL) << "Invalid reason: " << reason; |
327 reason_tag->AddElement(new XmlElement( | 300 reason_tag->AddElement(new XmlElement( |
328 QName(kJingleNamespace, reason_string))); | 301 QName(kJingleNamespace, reason_string))); |
329 } | 302 } |
330 | 303 |
331 if (action != SESSION_TERMINATE) { | 304 if (action != SESSION_TERMINATE) { |
332 XmlElement* content_tag = | 305 XmlElement* content_tag = |
333 new XmlElement(QName(kJingleNamespace, "content")); | 306 new XmlElement(QName(kJingleNamespace, "content")); |
334 jingle_tag->AddElement(content_tag); | 307 jingle_tag->AddElement(content_tag); |
335 | 308 |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
448 text_elem->SetAttr(QName(kXmlNamespace, "lang"), "en"); | 421 text_elem->SetAttr(QName(kXmlNamespace, "lang"), "en"); |
449 text_elem->SetBodyText(error_text); | 422 text_elem->SetBodyText(error_text); |
450 error->AddElement(text_elem); | 423 error->AddElement(text_elem); |
451 } | 424 } |
452 | 425 |
453 return iq.Pass(); | 426 return iq.Pass(); |
454 } | 427 } |
455 | 428 |
456 } // namespace protocol | 429 } // namespace protocol |
457 } // namespace remoting | 430 } // namespace remoting |
OLD | NEW |