blob: 7259cffa8ac34a8fd9b1fae7c2c78424fd121637 [file] [log] [blame]
Vitaly Buka4615e0d2015-10-14 15:35:12 -07001// Copyright 2015 The Weave Authors. All rights reserved.
Alex Vakulenkodea76b22015-06-01 13:18:06 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Vitaly Buka912b6982015-07-06 11:13:03 -07005#ifndef LIBWEAVE_SRC_NOTIFICATION_XMPP_IQ_STANZA_HANDLER_H_
6#define LIBWEAVE_SRC_NOTIFICATION_XMPP_IQ_STANZA_HANDLER_H_
Alex Vakulenkodea76b22015-06-01 13:18:06 -07007
8#include <map>
9#include <memory>
10#include <string>
11
12#include <base/callback_forward.h>
13#include <base/macros.h>
14#include <base/memory/weak_ptr.h>
Vitaly Buka34668e72015-12-15 14:46:47 -080015#include <base/time/time.h>
Alex Vakulenkodea76b22015-06-01 13:18:06 -070016
Stefan Sauer2d16dfa2015-09-25 17:08:35 +020017#include "src/notification/xmpp_stream_parser.h"
Alex Vakulenkodea76b22015-06-01 13:18:06 -070018
Vitaly Bukab6f015a2015-07-09 14:59:23 -070019namespace weave {
Alex Vakulenkodea76b22015-06-01 13:18:06 -070020
21class XmppChannelInterface;
22
Vitaly Buka1e363672015-09-25 14:01:16 -070023namespace provider {
24class TaskRunner;
25}
26
Alex Vakulenkodea76b22015-06-01 13:18:06 -070027class IqStanzaHandler {
28 public:
Vitaly Bukaa647c852015-07-06 14:51:01 -070029 using ResponseCallback = base::Callback<void(std::unique_ptr<XmlNode>)>;
Alex Vakulenkodea76b22015-06-01 13:18:06 -070030 using TimeoutCallback = base::Closure;
31
Vitaly Buka1e363672015-09-25 14:01:16 -070032 IqStanzaHandler(XmppChannelInterface* xmpp_channel,
33 provider::TaskRunner* task_runner);
Alex Vakulenkodea76b22015-06-01 13:18:06 -070034
35 // Sends <iq> request to the server.
36 // |type| is the IQ stanza type, one of "get", "set", "query".
37 // |to| is the target of the message. If empty string, 'to' is omitted.
38 // |body| the XML snipped to include between <iq>...</iq>
39 // |response_callback| is called with result or error XML stanza received
40 // from the server in response to the request sent.
41 // |timeout_callback| is called when the response to the request hasn't been
42 // received within the time allotted.
43 void SendRequest(const std::string& type,
44 const std::string& from,
45 const std::string& to,
46 const std::string& body,
47 const ResponseCallback& response_callback,
48 const TimeoutCallback& timeout_callback);
49
Vitaly Buka63cc3d22015-06-23 20:11:36 -070050 // |timeout| is the custom time interval after which requests should be
51 // considered failed.
52 void SendRequestWithCustomTimeout(const std::string& type,
53 const std::string& from,
54 const std::string& to,
55 const std::string& body,
56 base::TimeDelta timeout,
57 const ResponseCallback& response_callback,
58 const TimeoutCallback& timeout_callback);
59
Alex Vakulenkodea76b22015-06-01 13:18:06 -070060 // Processes an <iq> stanza is received from the server. This will match the
61 // stanza's 'id' attribute with pending request ID and if found, will
62 // call the |response_callback|, or if the request is not found, an error
63 // stanza fill be sent back to the server.
64 // Returns false if some unexpected condition occurred and the stream should
65 // be restarted.
66 bool HandleIqStanza(std::unique_ptr<XmlNode> stanza);
67
68 private:
69 using RequestId = int;
70 void OnTimeOut(RequestId id, const TimeoutCallback& timeout_callback);
71
72 XmppChannelInterface* xmpp_channel_;
Vitaly Buka1e363672015-09-25 14:01:16 -070073 provider::TaskRunner* task_runner_{nullptr};
Alex Vakulenkodea76b22015-06-01 13:18:06 -070074 std::map<RequestId, ResponseCallback> requests_;
75 RequestId last_request_id_{0};
76
77 base::WeakPtrFactory<IqStanzaHandler> weak_ptr_factory_{this};
78 DISALLOW_COPY_AND_ASSIGN(IqStanzaHandler);
79};
80
Vitaly Bukab6f015a2015-07-09 14:59:23 -070081} // namespace weave
Alex Vakulenkodea76b22015-06-01 13:18:06 -070082
Vitaly Buka912b6982015-07-06 11:13:03 -070083#endif // LIBWEAVE_SRC_NOTIFICATION_XMPP_IQ_STANZA_HANDLER_H_