1 // Copyright 2014 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 #ifndef REMOTING_SIGNALING_IQ_SENDER_H_
6 #define REMOTING_SIGNALING_IQ_SENDER_H_
11 #include "base/callback.h"
12 #include "base/compiler_specific.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "remoting/signaling/signal_strategy.h"
31 // IqSender handles sending iq requests and routing of responses to
33 class IqSender
: public SignalStrategy::Listener
{
35 // Callback that is called when an Iq response is received. Called
36 // with the |response| set to NULL in case of a timeout.
37 typedef base::Callback
<void(IqRequest
* request
,
38 const buzz::XmlElement
* response
)> ReplyCallback
;
40 explicit IqSender(SignalStrategy
* signal_strategy
);
43 // Send an iq stanza. Returns an IqRequest object that represends
44 // the request. |callback| is called when response to |stanza| is
45 // received. Destroy the returned IqRequest to cancel the callback.
46 // Caller must take ownership of the result. Result must be
47 // destroyed before sender is destroyed.
48 scoped_ptr
<IqRequest
> SendIq(scoped_ptr
<buzz::XmlElement
> stanza
,
49 const ReplyCallback
& callback
);
51 // Same as above, but also formats the message.
52 scoped_ptr
<IqRequest
> SendIq(const std::string
& type
,
53 const std::string
& addressee
,
54 scoped_ptr
<buzz::XmlElement
> iq_body
,
55 const ReplyCallback
& callback
);
57 // SignalStrategy::Listener implementation.
58 void OnSignalStrategyStateChange(SignalStrategy::State state
) override
;
59 bool OnSignalStrategyIncomingStanza(const buzz::XmlElement
* stanza
) override
;
62 typedef std::map
<std::string
, IqRequest
*> IqRequestMap
;
63 friend class IqRequest
;
65 // Helper function used to create iq stanzas.
66 static scoped_ptr
<buzz::XmlElement
> MakeIqStanza(
67 const std::string
& type
,
68 const std::string
& addressee
,
69 scoped_ptr
<buzz::XmlElement
> iq_body
);
71 // Removes |request| from the list of pending requests. Called by IqRequest.
72 void RemoveRequest(IqRequest
* request
);
74 SignalStrategy
* signal_strategy_
;
75 IqRequestMap requests_
;
77 DISALLOW_COPY_AND_ASSIGN(IqSender
);
80 // This call must only be used on the thread it was created on.
81 class IqRequest
: public base::SupportsWeakPtr
<IqRequest
> {
83 IqRequest(IqSender
* sender
, const IqSender::ReplyCallback
& callback
,
84 const std::string
& addressee
);
87 // Sets timeout for the request. When the timeout expires the
88 // callback is called with the |response| set to NULL.
89 void SetTimeout(base::TimeDelta timeout
);
92 friend class IqSender
;
94 void CallCallback(const buzz::XmlElement
* stanza
);
97 // Called by IqSender when a response is received.
98 void OnResponse(const buzz::XmlElement
* stanza
);
100 void DeliverResponse(scoped_ptr
<buzz::XmlElement
> stanza
);
103 IqSender::ReplyCallback callback_
;
104 std::string addressee_
;
106 DISALLOW_COPY_AND_ASSIGN(IqRequest
);
109 } // namespace remoting
111 #endif // REMOTING_SIGNALING_IQ_SENDER_H_