| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef REMOTING_CLIENT_PLUGIN_DELEGATING_SIGNAL_STRATEGY_H_ | 5 #ifndef REMOTING_CLIENT_PLUGIN_DELEGATING_SIGNAL_STRATEGY_H_ |
| 6 #define REMOTING_CLIENT_PLUGIN_DELEGATING_SIGNAL_STRATEGY_H_ | 6 #define REMOTING_CLIENT_PLUGIN_DELEGATING_SIGNAL_STRATEGY_H_ |
| 7 | 7 |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| 11 #include "base/observer_list_threadsafe.h" | 11 #include "base/observer_list_threadsafe.h" |
| 12 #include "remoting/signaling/signal_strategy.h" | 12 #include "remoting/signaling/signal_strategy.h" |
| 13 #include "remoting/signaling/signaling_address.h" |
| 13 | 14 |
| 14 namespace base { | 15 namespace base { |
| 15 class SingleThreadTaskRunner; | 16 class SingleThreadTaskRunner; |
| 16 } // namespace base | 17 } // namespace base |
| 17 | 18 |
| 18 namespace remoting { | 19 namespace remoting { |
| 19 | 20 |
| 20 // A signaling strategy class that delegates IQ sending and receiving. | 21 // A signaling strategy class that delegates IQ sending and receiving. |
| 21 // | 22 // |
| 22 // Notes on thread safety: | 23 // Notes on thread safety: |
| 23 // 1. This object can be created on any thread. | 24 // 1. This object can be created on any thread. |
| 24 // 2. |send_iq_callback| will always be called on the thread that it is created. | 25 // 2. |send_iq_callback| will always be called on the thread that it is created. |
| 25 // Note that |send_iq_callback| may be called after this object is destroyed. | 26 // Note that |send_iq_callback| may be called after this object is destroyed. |
| 26 // 3. The caller should invoke all methods on the SignalStrategy interface on | 27 // 3. The caller should invoke all methods on the SignalStrategy interface on |
| 27 // the |client_task_runner|. | 28 // the |client_task_runner|. |
| 28 // 4. All listeners will be called on |client_task_runner| as well. | 29 // 4. All listeners will be called on |client_task_runner| as well. |
| 29 // 5. The destructor should always be called on the |client_task_runner|. | 30 // 5. The destructor should always be called on the |client_task_runner|. |
| 30 // 6. As a result of (5), use MakeIncomingMessageCallback() to obtain a callback | 31 // 6. As a result of (5), use MakeIncomingMessageCallback() to obtain a callback |
| 31 // when passing incoming signaling messages from the delegate. The callback | 32 // when passing incoming signaling messages from the delegate. The callback |
| 32 // can then be invoked at any thread. | 33 // can then be invoked at any thread. |
| 33 class DelegatingSignalStrategy : public SignalStrategy { | 34 class DelegatingSignalStrategy : public SignalStrategy { |
| 34 public: | 35 public: |
| 35 typedef base::RepeatingCallback<void(const std::string&)> IqCallback; | 36 typedef base::RepeatingCallback<void(const std::string&)> IqCallback; |
| 36 | 37 |
| 37 DelegatingSignalStrategy( | 38 DelegatingSignalStrategy( |
| 38 std::string local_jid, | 39 const SignalingAddress& local_address, |
| 39 scoped_refptr<base::SingleThreadTaskRunner> client_task_runner, | 40 scoped_refptr<base::SingleThreadTaskRunner> client_task_runner, |
| 40 const IqCallback& send_iq_callback); | 41 const IqCallback& send_iq_callback); |
| 41 ~DelegatingSignalStrategy() override; | 42 ~DelegatingSignalStrategy() override; |
| 42 | 43 |
| 43 IqCallback GetIncomingMessageCallback(); | 44 IqCallback GetIncomingMessageCallback(); |
| 44 | 45 |
| 45 // SignalStrategy interface. | 46 // SignalStrategy interface. |
| 46 void Connect() override; | 47 void Connect() override; |
| 47 void Disconnect() override; | 48 void Disconnect() override; |
| 48 State GetState() const override; | 49 State GetState() const override; |
| 49 Error GetError() const override; | 50 Error GetError() const override; |
| 50 std::string GetLocalJid() const override; | 51 const SignalingAddress& GetLocalAddress() const override; |
| 51 void AddListener(Listener* listener) override; | 52 void AddListener(Listener* listener) override; |
| 52 void RemoveListener(Listener* listener) override; | 53 void RemoveListener(Listener* listener) override; |
| 53 bool SendStanza(std::unique_ptr<buzz::XmlElement> stanza) override; | 54 bool SendStanza(std::unique_ptr<buzz::XmlElement> stanza) override; |
| 54 std::string GetNextId() override; | 55 std::string GetNextId() override; |
| 55 | 56 |
| 56 private: | 57 private: |
| 57 static void OnIncomingMessageFromDelegate( | 58 static void OnIncomingMessageFromDelegate( |
| 58 base::WeakPtr<DelegatingSignalStrategy> weak_ptr, | 59 base::WeakPtr<DelegatingSignalStrategy> weak_ptr, |
| 59 scoped_refptr<base::SingleThreadTaskRunner> client_task_runner, | 60 scoped_refptr<base::SingleThreadTaskRunner> client_task_runner, |
| 60 const std::string& message); | 61 const std::string& message); |
| 61 | 62 |
| 62 void OnIncomingMessage(const std::string& message); | 63 void OnIncomingMessage(const std::string& message); |
| 63 | 64 |
| 64 std::string local_jid_; | 65 SignalingAddress local_address_; |
| 65 scoped_refptr<base::SingleThreadTaskRunner> delegate_task_runner_; | 66 scoped_refptr<base::SingleThreadTaskRunner> delegate_task_runner_; |
| 66 scoped_refptr<base::SingleThreadTaskRunner> client_task_runner_; | 67 scoped_refptr<base::SingleThreadTaskRunner> client_task_runner_; |
| 67 | 68 |
| 68 IqCallback incoming_iq_callback_; | 69 IqCallback incoming_iq_callback_; |
| 69 IqCallback send_iq_callback_; | 70 IqCallback send_iq_callback_; |
| 70 base::ObserverList<Listener> listeners_; | 71 base::ObserverList<Listener> listeners_; |
| 71 | 72 |
| 72 base::WeakPtrFactory<DelegatingSignalStrategy> weak_factory_; | 73 base::WeakPtrFactory<DelegatingSignalStrategy> weak_factory_; |
| 73 | 74 |
| 74 DISALLOW_COPY_AND_ASSIGN(DelegatingSignalStrategy); | 75 DISALLOW_COPY_AND_ASSIGN(DelegatingSignalStrategy); |
| 75 }; | 76 }; |
| 76 | 77 |
| 77 } // namespace remoting | 78 } // namespace remoting |
| 78 | 79 |
| 79 #endif // REMOTING_CLIENT_PLUGIN_DELEGATING_SIGNAL_STRATEGY_H_ | 80 #endif // REMOTING_CLIENT_PLUGIN_DELEGATING_SIGNAL_STRATEGY_H_ |
| OLD | NEW |