File: iq_sender.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (123 lines) | stat: -rw-r--r-- 3,714 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef REMOTING_SIGNALING_IQ_SENDER_H_
#define REMOTING_SIGNALING_IQ_SENDER_H_

#include <map>
#include <memory>
#include <string>

#include "base/compiler_specific.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "remoting/signaling/signal_strategy.h"

namespace base {
class TimeDelta;
}  // namespace base

namespace jingle_xmpp {
class XmlElement;
}  // namespace jingle_xmpp

namespace remoting {

class IqRequest;
class SignalStrategy;

// IqSender handles sending iq requests and routing of responses to
// those requests.
class IqSender : public SignalStrategy::Listener {
 public:
  // Callback that is called when an Iq response is received. Called
  // with the |response| set to nullptr in case of a timeout.
  using ReplyCallback =
      base::OnceCallback<void(IqRequest* request,
                              const jingle_xmpp::XmlElement* response)>;

  explicit IqSender(SignalStrategy* signal_strategy);

  IqSender(const IqSender&) = delete;
  IqSender& operator=(const IqSender&) = delete;

  ~IqSender() override;

  // Send an iq stanza. Returns an IqRequest object that represends
  // the request. |callback| is called when response to |stanza| is
  // received. Destroy the returned IqRequest to cancel the callback.
  // Caller must take ownership of the result. Result must be
  // destroyed before sender is destroyed.
  std::unique_ptr<IqRequest> SendIq(
      std::unique_ptr<jingle_xmpp::XmlElement> stanza,
      ReplyCallback callback);

  // Same as above, but also formats the message.
  std::unique_ptr<IqRequest> SendIq(
      const std::string& type,
      const std::string& addressee,
      std::unique_ptr<jingle_xmpp::XmlElement> iq_body,
      ReplyCallback callback);

  // SignalStrategy::Listener implementation.
  void OnSignalStrategyStateChange(SignalStrategy::State state) override;
  bool OnSignalStrategyIncomingStanza(
      const jingle_xmpp::XmlElement* stanza) override;

 private:
  typedef std::map<std::string, raw_ptr<IqRequest, CtnExperimental>>
      IqRequestMap;
  friend class IqRequest;

  // Helper function used to create iq stanzas.
  static std::unique_ptr<jingle_xmpp::XmlElement> MakeIqStanza(
      const std::string& type,
      const std::string& addressee,
      std::unique_ptr<jingle_xmpp::XmlElement> iq_body);

  // Removes |request| from the list of pending requests. Called by IqRequest.
  void RemoveRequest(IqRequest* request);

  raw_ptr<SignalStrategy> signal_strategy_;
  IqRequestMap requests_;
};

// This call must only be used on the thread it was created on.
class IqRequest {
 public:
  IqRequest(IqSender* sender,
            IqSender::ReplyCallback callback,
            const std::string& addressee);

  IqRequest(const IqRequest&) = delete;
  IqRequest& operator=(const IqRequest&) = delete;

  ~IqRequest();

  // Sets timeout for the request. When the timeout expires the
  // callback is called with the |response| set to nullptr.
  void SetTimeout(base::TimeDelta timeout);

 private:
  friend class IqSender;

  void CallCallback(const jingle_xmpp::XmlElement* stanza);
  void OnTimeout();

  // Called by IqSender when a response is received.
  void OnResponse(const jingle_xmpp::XmlElement* stanza);

  void DeliverResponse(std::unique_ptr<jingle_xmpp::XmlElement> stanza);

  raw_ptr<IqSender> sender_;
  IqSender::ReplyCallback callback_;
  std::string addressee_;

  base::WeakPtrFactory<IqRequest> weak_factory_{this};
};

}  // namespace remoting

#endif  // REMOTING_SIGNALING_IQ_SENDER_H_