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

Side by Side Diff: remoting/signaling/xmpp_stream_parser.cc

Issue 958703003: Remove dependency on XMPP implementation in WebRTC (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 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
(Empty)
1 // Copyright 2015 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.
4
5 #include "remoting/signaling/xmpp_stream_parser.h"
6
7 #include "base/location.h"
8 #include "base/single_thread_task_runner.h"
9 #include "base/thread_task_runner_handle.h"
10 #include "third_party/webrtc/libjingle/xmllite/xmlbuilder.h"
11 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h"
12 #include "third_party/webrtc/libjingle/xmllite/xmlparser.h"
13
14 namespace remoting {
15
16 class XmppStreamParser::Core : public buzz::XmlParseHandler {
17 public:
18 typedef base::Callback<void(scoped_ptr<buzz::XmlElement> stanza)>
19 OnStanzaCallback;
20
21 Core();
22 ~Core() override;
23
24 void SetCallbacks(const OnStanzaCallback& on_stanza_callback,
25 const base::Closure& on_error_callback);
26
27 void AppendData(const std::string& data);
28
29 private:
30 // buzz::XmlParseHandler interface.
31 void StartElement(buzz::XmlParseContext* context,
32 const char* name,
33 const char** atts) override;
34 void EndElement(buzz::XmlParseContext* context, const char* name) override;
35 void CharacterData(buzz::XmlParseContext* context,
36 const char* text,
37 int len) override;
38 void Error(buzz::XmlParseContext* context, XML_Error error_code) override;
39
40 void ProcessError();
41
42 OnStanzaCallback on_stanza_callback_;
43 base::Closure on_error_callback_;
44
45 buzz::XmlParser parser_;
46 int depth_;
47 buzz::XmlBuilder builder_;
48
49 bool error_;
50
51 DISALLOW_COPY_AND_ASSIGN(Core);
52 };
53
54 XmppStreamParser::Core::Core()
55 : parser_(this),
56 depth_(0),
57 error_(false) {
58 }
59
60 XmppStreamParser::Core::~Core() {
61 }
62
63 void XmppStreamParser::Core::SetCallbacks(
64 const OnStanzaCallback& on_stanza_callback,
65 const base::Closure& on_error_callback) {
66 on_stanza_callback_ = on_stanza_callback;
67 on_error_callback_ = on_error_callback;
68 }
69
70 void XmppStreamParser::Core::AppendData(const std::string& data) {
71 if (error_)
72 return;
73 parser_.Parse(data.data(), data.size(), false);
74 }
75
76 void XmppStreamParser::Core::StartElement(buzz::XmlParseContext* context,
77 const char* name,
78 const char** atts) {
79 DCHECK(!error_);
80
81 ++depth_;
82 if (depth_ == 1) {
83 scoped_ptr<buzz::XmlElement> header(
84 buzz::XmlBuilder::BuildElement(context, name, atts));
85 if (!header) {
86 LOG(ERROR) << "Failed to parse XMPP stream header.";
87 ProcessError();
88 }
89 return;
90 }
91
92 builder_.StartElement(context, name, atts);
93 }
94
95 void XmppStreamParser::Core::EndElement(buzz::XmlParseContext* context,
96 const char* name) {
97 DCHECK(!error_);
98
99 --depth_;
100 if (depth_ == 0) {
101 LOG(ERROR) << "XMPP stream ended unexpectedly.";
102 ProcessError();
103 return;
104 }
105
106 builder_.EndElement(context, name);
107
108 if (depth_ == 1) {
109 if (!on_stanza_callback_.is_null())
110 on_stanza_callback_.Run(make_scoped_ptr(builder_.CreateElement()));
111 }
112 }
113
114 void XmppStreamParser::Core::CharacterData(buzz::XmlParseContext* context,
115 const char* text,
116 int len) {
117 DCHECK(!error_);
118
119 // Ignore data between stanzas.
120 if (depth_ <= 1) {
121 // Only whitespace is allowed outside of the stanzas.
122 bool all_spaces = true;
123 for (char c: std::string(text, len)) {
124 if (c != ' ') {
125 all_spaces = false;
126 break;
127 }
128 }
129 if (!all_spaces) {
130 LOG(ERROR) << "Received unexpected string: " << std::string(text,
131 text + len);
132 ProcessError();
133 }
134 } else if (depth_ > 1) {
135 builder_.CharacterData(context, text, len);
136 }
137 }
138
139 void XmppStreamParser::Core::Error(buzz::XmlParseContext* context,
140 XML_Error error_code) {
141 LOG(ERROR) << "XMPP parser error: " << error_code;
142 ProcessError();
143 }
144
145 void XmppStreamParser::Core::ProcessError() {
146 error_ = true;
147 if (!on_error_callback_.is_null())
148 on_error_callback_.Run();
149 }
150
151 XmppStreamParser::XmppStreamParser() : core_(new Core()) {
152 }
153
154 XmppStreamParser::~XmppStreamParser() {
155 // Set null callbacks and delete |core_| asynchronously to make sure it's not
156 // deleted from a callback.
157 core_->SetCallbacks(OnStanzaCallback(), base::Closure());
158 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, core_.release());
159 }
160
161 void XmppStreamParser::SetCallbacks(const OnStanzaCallback& on_stanza_callback,
162 const base::Closure& on_error_callback) {
163 core_->SetCallbacks(on_stanza_callback, on_error_callback);
164 }
165
166 void XmppStreamParser::AppendData(const std::string& data) {
167 core_->AppendData(data);
168 }
169
170 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698