Home | History | Annotate | Download | only in jingle_glue
      1 // Copyright (c) 2012 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.
      4 
      5 // The XmppSignalStrategy encapsulates all the logic to perform the signaling
      6 // STUN/ICE for jingle via a direct XMPP connection.
      7 //
      8 // This class is not threadsafe.
      9 
     10 #ifndef REMOTING_JINGLE_GLUE_XMPP_SIGNAL_STRATEGY_H_
     11 #define REMOTING_JINGLE_GLUE_XMPP_SIGNAL_STRATEGY_H_
     12 
     13 #include "remoting/jingle_glue/signal_strategy.h"
     14 
     15 #include <vector>
     16 
     17 #include "base/compiler_specific.h"
     18 #include "base/observer_list.h"
     19 #include "base/threading/non_thread_safe.h"
     20 #include "base/timer/timer.h"
     21 #include "third_party/libjingle/source/talk/base/sigslot.h"
     22 #include "third_party/libjingle/source/talk/xmpp/xmppclient.h"
     23 
     24 namespace net {
     25 class URLRequestContextGetter;
     26 }  // namespace net
     27 
     28 namespace talk_base {
     29 class TaskRunner;
     30 }  // namespace talk_base
     31 
     32 namespace remoting {
     33 
     34 class JingleThread;
     35 
     36 class XmppSignalStrategy : public base::NonThreadSafe,
     37                            public SignalStrategy,
     38                            public buzz::XmppStanzaHandler,
     39                            public sigslot::has_slots<> {
     40  public:
     41   // XMPP Server configuration for XmppSignalStrategy.
     42   struct XmppServerConfig {
     43     std::string host;
     44     int port;
     45     bool use_tls;
     46   };
     47 
     48   XmppSignalStrategy(
     49       scoped_refptr<net::URLRequestContextGetter> request_context_getter,
     50       const std::string& username,
     51       const std::string& auth_token,
     52       const std::string& auth_token_service,
     53       const XmppServerConfig& xmpp_server_config);
     54   virtual ~XmppSignalStrategy();
     55 
     56   // SignalStrategy interface.
     57   virtual void Connect() OVERRIDE;
     58   virtual void Disconnect() OVERRIDE;
     59   virtual State GetState() const OVERRIDE;
     60   virtual Error GetError() const OVERRIDE;
     61   virtual std::string GetLocalJid() const OVERRIDE;
     62   virtual void AddListener(Listener* listener) OVERRIDE;
     63   virtual void RemoveListener(Listener* listener) OVERRIDE;
     64   virtual bool SendStanza(scoped_ptr<buzz::XmlElement> stanza) OVERRIDE;
     65   virtual std::string GetNextId() OVERRIDE;
     66 
     67   // buzz::XmppStanzaHandler interface.
     68   virtual bool HandleStanza(const buzz::XmlElement* stanza) OVERRIDE;
     69 
     70   // This method is used to update the auth info (for example when the OAuth
     71   // access token is renewed). It is OK to call this even when we are in the
     72   // CONNECTED state. It will be used on the next Connect() call.
     73   void SetAuthInfo(const std::string& username,
     74                    const std::string& auth_token,
     75                    const std::string& auth_token_service);
     76 
     77   // Use this method to override the default resource name used (optional).
     78   // This will be used on the next Connect() call.
     79   void SetResourceName(const std::string& resource_name);
     80 
     81  private:
     82   static buzz::PreXmppAuth* CreatePreXmppAuth(
     83       const buzz::XmppClientSettings& settings);
     84 
     85   void OnConnectionStateChanged(buzz::XmppEngine::State state);
     86   void SetState(State new_state);
     87 
     88   void SendKeepAlive();
     89 
     90   scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
     91   std::string username_;
     92   std::string auth_token_;
     93   std::string auth_token_service_;
     94   std::string resource_name_;
     95   scoped_ptr<talk_base::TaskRunner> task_runner_;
     96   buzz::XmppClient* xmpp_client_;
     97   XmppServerConfig xmpp_server_config_;
     98 
     99   State state_;
    100   Error error_;
    101 
    102   ObserverList<Listener, true> listeners_;
    103 
    104   base::RepeatingTimer<XmppSignalStrategy> keep_alive_timer_;
    105 
    106   DISALLOW_COPY_AND_ASSIGN(XmppSignalStrategy);
    107 };
    108 
    109 }  // namespace remoting
    110 
    111 #endif  // REMOTING_JINGLE_GLUE_XMPP_SIGNAL_STRATEGY_H_
    112