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.
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 "talk/base/sigslot.h"
18 #include "talk/xmpp/xmppengine.h"
23 class XmppClientSettings
;
24 class XmppTaskParentInterface
;
27 namespace jingle_glue
{
29 } // namespace jingle_glue
36 : public sigslot::has_slots
<>,
37 public base::NonThreadSafe
{
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
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
60 virtual void OnError(buzz::XmppEngine::Error error
, int subcode
,
61 const buzz::XmlElement
* stream_error
) = 0;
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
,
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 virtual ~XmppConnection();
83 void OnStateChange(buzz::XmppEngine::State state
);
84 void OnInputLog(const char* data
, int len
);
85 void OnOutputLog(const char* data
, int len
);
89 scoped_ptr
<jingle_glue::TaskPump
> task_pump_
;
90 base::WeakPtr
<WeakXmppClient
> weak_xmpp_client_
;
91 bool on_connect_called_
;
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_