Roll src/third_party/skia cd87c51:f421ec6
[chromium-blink-merge.git] / remoting / signaling / iq_sender.h
blobdfdfbfe3c59ab02cd233d6a00f2192442dbbd021
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_
8 #include <map>
9 #include <string>
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"
18 namespace base {
19 class TimeDelta;
20 } // namespace base
22 namespace buzz {
23 class XmlElement;
24 } // namespace buzz
26 namespace remoting {
28 class IqRequest;
29 class SignalStrategy;
31 // IqSender handles sending iq requests and routing of responses to
32 // those requests.
33 class IqSender : public SignalStrategy::Listener {
34 public:
35 // Callback that is called when an Iq response is received. Called
36 // with the |response| set to nullptr in case of a timeout.
37 typedef base::Callback<void(IqRequest* request,
38 const buzz::XmlElement* response)> ReplyCallback;
40 explicit IqSender(SignalStrategy* signal_strategy);
41 ~IqSender() override;
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;
61 private:
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> {
82 public:
83 IqRequest(IqSender* sender, const IqSender::ReplyCallback& callback,
84 const std::string& addressee);
85 ~IqRequest();
87 // Sets timeout for the request. When the timeout expires the
88 // callback is called with the |response| set to nullptr.
89 void SetTimeout(base::TimeDelta timeout);
91 private:
92 friend class IqSender;
94 void CallCallback(const buzz::XmlElement* stanza);
95 void OnTimeout();
97 // Called by IqSender when a response is received.
98 void OnResponse(const buzz::XmlElement* stanza);
100 void DeliverResponse(scoped_ptr<buzz::XmlElement> stanza);
102 IqSender* sender_;
103 IqSender::ReplyCallback callback_;
104 std::string addressee_;
106 DISALLOW_COPY_AND_ASSIGN(IqRequest);
109 } // namespace remoting
111 #endif // REMOTING_SIGNALING_IQ_SENDER_H_