File: delegating_signal_strategy.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 (83 lines) | stat: -rw-r--r-- 3,162 bytes parent folder | download | duplicates (4)
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
// Copyright 2013 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_DELEGATING_SIGNAL_STRATEGY_H_
#define REMOTING_SIGNALING_DELEGATING_SIGNAL_STRATEGY_H_

#include "base/functional/callback.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "remoting/signaling/signal_strategy.h"
#include "remoting/signaling/signaling_address.h"

namespace base {
class SingleThreadTaskRunner;
}  // namespace base

namespace remoting {

// A signaling strategy class that delegates IQ sending and receiving.
//
// Notes on thread safety:
// 1. This object can be created on any thread.
// 2. |send_iq_callback| will always be called on the thread that it is created.
//    Note that |send_iq_callback| may be called after this object is destroyed.
// 3. The caller should invoke all methods on the SignalStrategy interface on
//    the |client_task_runner|.
// 4. All listeners will be called on |client_task_runner| as well.
// 5. The destructor should always be called on the |client_task_runner|.
// 6. As a result of (5), use MakeIncomingMessageCallback() to obtain a callback
//    when passing incoming signaling messages from the delegate.  The callback
//    can then be invoked at any thread.
class DelegatingSignalStrategy : public SignalStrategy {
 public:
  typedef base::RepeatingCallback<void(const std::string&)> IqCallback;

  DelegatingSignalStrategy(
      const SignalingAddress& local_address,
      scoped_refptr<base::SingleThreadTaskRunner> client_task_runner,
      const IqCallback& send_iq_callback);

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

  ~DelegatingSignalStrategy() override;

  IqCallback GetIncomingMessageCallback();

  // SignalStrategy interface.
  void Connect() override;
  void Disconnect() override;
  State GetState() const override;
  Error GetError() const override;
  const SignalingAddress& GetLocalAddress() const override;
  void AddListener(Listener* listener) override;
  void RemoveListener(Listener* listener) override;
  bool SendStanza(std::unique_ptr<jingle_xmpp::XmlElement> stanza) override;
  bool SendMessage(const SignalingAddress& destination_address,
                   const ftl::ChromotingMessage& message) override;
  std::string GetNextId() override;

 private:
  static void OnIncomingMessageFromDelegate(
      base::WeakPtr<DelegatingSignalStrategy> weak_ptr,
      scoped_refptr<base::SingleThreadTaskRunner> client_task_runner,
      const std::string& message);

  void OnIncomingMessage(const std::string& message);

  SignalingAddress local_address_;
  scoped_refptr<base::SingleThreadTaskRunner> delegate_task_runner_;
  scoped_refptr<base::SingleThreadTaskRunner> client_task_runner_;

  IqCallback incoming_iq_callback_;
  IqCallback send_iq_callback_;
  base::ObserverList<Listener> listeners_;

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

}  // namespace remoting

#endif  // REMOTING_SIGNALING_DELEGATING_SIGNAL_STRATEGY_H_