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