Chromium Code Reviews| Index: remoting/protocol/jingle_session.cc |
| diff --git a/remoting/protocol/jingle_session.cc b/remoting/protocol/jingle_session.cc |
| index f421b025e6716af41571af663bc24fefa1568f53..3d0d6b1e1d8340a59ac9ee2ba76879f2468fb513 100644 |
| --- a/remoting/protocol/jingle_session.cc |
| +++ b/remoting/protocol/jingle_session.cc |
| @@ -7,9 +7,11 @@ |
| #include <stdint.h> |
| #include <limits> |
| +#include <memory> |
| #include <utility> |
| #include "base/bind.h" |
| +#include "base/callback.h" |
| #include "base/single_thread_task_runner.h" |
| #include "base/stl_util.h" |
| #include "base/strings/string_split.h" |
| @@ -21,6 +23,7 @@ |
| #include "remoting/protocol/jingle_messages.h" |
| #include "remoting/protocol/jingle_session_manager.h" |
| #include "remoting/protocol/session_config.h" |
| +#include "remoting/protocol/session_plugin.h" |
| #include "remoting/protocol/transport.h" |
| #include "remoting/signaling/iq_sender.h" |
| #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" |
| @@ -208,14 +211,11 @@ void JingleSession::StartConnection( |
| session_id_ = base::Uint64ToString( |
| base::RandGenerator(std::numeric_limits<uint64_t>::max())); |
| - // Send session-initiate message. |
| - std::unique_ptr<JingleMessage> message(new JingleMessage( |
| - peer_address_, JingleMessage::SESSION_INITIATE, session_id_)); |
| - message->initiator = session_manager_->signal_strategy_->GetLocalJid(); |
| - message->description.reset(new ContentDescription( |
| - session_manager_->protocol_config_->Clone(), |
| - authenticator_->GetNextMessage())); |
| - SendMessage(std::move(message)); |
| + // Delay sending session-initiate message to ensure SessionPlugin can be |
| + // attached before the message. |
| + base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| + base::Bind(&JingleSession::SendSessionInitiateMessage, |
| + weak_factory_.GetWeakPtr())); |
| SetState(CONNECTING); |
| } |
| @@ -249,6 +249,7 @@ void JingleSession::AcceptIncomingConnection( |
| const JingleMessage& initiate_message) { |
| DCHECK(config_); |
| + ExecutePluginsOnIncomingMessage(initiate_message); |
| // Process the first authentication message. |
| const buzz::XmlElement* first_auth_message = |
| initiate_message.description->authenticator_message(); |
| @@ -322,6 +323,7 @@ void JingleSession::SendTransportInfo( |
| std::unique_ptr<JingleMessage> message(new JingleMessage( |
| peer_address_, JingleMessage::TRANSPORT_INFO, session_id_)); |
| message->transport_info = std::move(transport_info); |
| + ExecutePluginsOnOutgoingMessage(message.get()); |
| std::unique_ptr<buzz::XmlElement> stanza = message->ToXml(); |
| stanza->AddAttr(buzz::QN_ID, GetNextOutgoingId()); |
| @@ -386,9 +388,15 @@ void JingleSession::Close(protocol::ErrorCode error) { |
| } |
| } |
| +void JingleSession::AddPlugin(std::unique_ptr<SessionPlugin> plugin) { |
| + DCHECK(plugin); |
| + plugins_.push_back(std::move(plugin)); |
| +} |
| + |
| void JingleSession::SendMessage(std::unique_ptr<JingleMessage> message) { |
| DCHECK(thread_checker_.CalledOnValidThread()); |
| + ExecutePluginsOnOutgoingMessage(message.get()); |
| std::unique_ptr<buzz::XmlElement> stanza = message->ToXml(); |
| stanza->AddAttr(buzz::QN_ID, GetNextOutgoingId()); |
| @@ -482,6 +490,7 @@ void JingleSession::OnTransportInfoResponse(IqRequest* request, |
| void JingleSession::OnIncomingMessage(const std::string& id, |
| std::unique_ptr<JingleMessage> message, |
| const ReplyCallback& reply_callback) { |
| + ExecutePluginsOnIncomingMessage(*message); |
| std::vector<PendingMessage> ordered = message_queue_->OnIncomingMessage( |
| id, PendingMessage{std::move(message), reply_callback}); |
| base::WeakPtr<JingleSession> self = weak_factory_.GetWeakPtr(); |
| @@ -747,6 +756,34 @@ bool JingleSession::is_session_active() { |
| state_ == AUTHENTICATING || state_ == AUTHENTICATED; |
| } |
| +void JingleSession::ExecutePluginsOnIncomingMessage( |
| + const JingleMessage& message) { |
| + for (const auto& plugin : plugins_) { |
| + plugin->OnIncoming(state_, message.action, message.attachments.get()); |
|
Sergey Ulanov
2016/12/23 00:14:51
I think it would be cleaner to call this function
Hzj_jie
2016/12/24 00:09:55
Done.
|
| + } |
| +} |
| + |
| +void JingleSession::ExecutePluginsOnOutgoingMessage(JingleMessage* message) { |
| + DCHECK(message); |
| + for (const auto& plugin : plugins_) { |
| + std::unique_ptr<XmlElement> attachment = |
| + plugin->OnOutgoing(state_, message->action); |
| + if (attachment) { |
| + message->AddAttachment(std::move(attachment)); |
| + } |
| + } |
| +} |
| + |
| +void JingleSession::SendSessionInitiateMessage() { |
| + std::unique_ptr<JingleMessage> message(new JingleMessage( |
| + peer_address_, JingleMessage::SESSION_INITIATE, session_id_)); |
| + message->initiator = session_manager_->signal_strategy_->GetLocalJid(); |
| + message->description.reset(new ContentDescription( |
| + session_manager_->protocol_config_->Clone(), |
| + authenticator_->GetNextMessage())); |
| + SendMessage(std::move(message)); |
| +} |
| + |
| std::string JingleSession::GetNextOutgoingId() { |
| return outgoing_id_prefix_ + "_" + base::IntToString(++next_outgoing_id_); |
| } |