Roll WebRTC 7546:7549.
[chromium-blink-merge.git] / jingle / notifier / base / xmpp_connection.h
blobbc1ec2a1e48cd5191e30109236a4ab77e8d8dc29
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 // A class that manages a connection to an XMPP server.
7 #ifndef JINGLE_NOTIFIER_BASE_XMPP_CONNECTION_H_
8 #define JINGLE_NOTIFIER_BASE_XMPP_CONNECTION_H_
10 #include "base/basictypes.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/threading/non_thread_safe.h"
16 #include "net/url_request/url_request_context_getter.h"
17 #include "webrtc/base/sigslot.h"
18 #include "webrtc/libjingle/xmpp/xmppengine.h"
20 namespace buzz {
21 class PreXmppAuth;
22 class XmlElement;
23 class XmppClientSettings;
24 class XmppTaskParentInterface;
25 } // namespace
27 namespace jingle_glue {
28 class TaskPump;
29 } // namespace jingle_glue
31 namespace notifier {
33 class WeakXmppClient;
35 class XmppConnection
36 : public sigslot::has_slots<>,
37 public base::NonThreadSafe {
38 public:
39 class Delegate {
40 public:
41 // Called (at most once) when a connection has been established.
42 // |base_task| can be used by the client as the parent of any Task
43 // it creates as long as it is valid (i.e., non-NULL).
44 virtual void OnConnect(
45 base::WeakPtr<buzz::XmppTaskParentInterface> base_task) = 0;
47 // Called if an error has occurred (either before or after a call
48 // to OnConnect()). No calls to the delegate will be made after
49 // this call. Invalidates any weak pointers passed to the client
50 // by OnConnect().
52 // |error| is the code for the raised error. |subcode| is an
53 // error-dependent subcode (0 if not applicable). |stream_error|
54 // is non-NULL iff |error| == ERROR_STREAM. |stream_error| is
55 // valid only for the lifetime of this function.
57 // Ideally, |error| would always be set to something that is not
58 // ERROR_NONE, but due to inconsistent error-handling this doesn't
59 // always happen.
60 virtual void OnError(buzz::XmppEngine::Error error, int subcode,
61 const buzz::XmlElement* stream_error) = 0;
63 protected:
64 virtual ~Delegate();
67 // Does not take ownership of |delegate|, which must not be
68 // NULL. Takes ownership of |pre_xmpp_auth|, which may be NULL.
70 // TODO(akalin): Avoid the need for |pre_xmpp_auth|.
71 XmppConnection(const buzz::XmppClientSettings& xmpp_client_settings,
72 const scoped_refptr<net::URLRequestContextGetter>&
73 request_context_getter,
74 Delegate* delegate,
75 buzz::PreXmppAuth* pre_xmpp_auth);
77 // Invalidates any weak pointers passed to the delegate by
78 // OnConnect(), but does not trigger a call to the delegate's
79 // OnError() function.
80 ~XmppConnection() override;
82 private:
83 void OnStateChange(buzz::XmppEngine::State state);
84 void OnInputLog(const char* data, int len);
85 void OnOutputLog(const char* data, int len);
87 void ClearClient();
89 scoped_ptr<jingle_glue::TaskPump> task_pump_;
90 base::WeakPtr<WeakXmppClient> weak_xmpp_client_;
91 bool on_connect_called_;
92 Delegate* delegate_;
94 FRIEND_TEST(XmppConnectionTest, RaisedError);
95 FRIEND_TEST(XmppConnectionTest, Connect);
96 FRIEND_TEST(XmppConnectionTest, MultipleConnect);
97 FRIEND_TEST(XmppConnectionTest, ConnectThenError);
98 FRIEND_TEST(XmppConnectionTest, TasksDontRunAfterXmppConnectionDestructor);
100 DISALLOW_COPY_AND_ASSIGN(XmppConnection);
103 } // namespace notifier
105 #endif // JINGLE_NOTIFIER_BASE_XMPP_CONNECTION_H_