aboutsummaryrefslogtreecommitdiff
path: root/src/notification/xmpp_iq_stanza_handler.h
blob: 7259cffa8ac34a8fd9b1fae7c2c78424fd121637 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2015 The Weave Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef LIBWEAVE_SRC_NOTIFICATION_XMPP_IQ_STANZA_HANDLER_H_
#define LIBWEAVE_SRC_NOTIFICATION_XMPP_IQ_STANZA_HANDLER_H_

#include <map>
#include <memory>
#include <string>

#include <base/callback_forward.h>
#include <base/macros.h>
#include <base/memory/weak_ptr.h>
#include <base/time/time.h>

#include "src/notification/xmpp_stream_parser.h"

namespace weave {

class XmppChannelInterface;

namespace provider {
class TaskRunner;
}

class IqStanzaHandler {
 public:
  using ResponseCallback = base::Callback<void(std::unique_ptr<XmlNode>)>;
  using TimeoutCallback = base::Closure;

  IqStanzaHandler(XmppChannelInterface* xmpp_channel,
                  provider::TaskRunner* task_runner);

  // Sends <iq> request to the server.
  // |type| is the IQ stanza type, one of "get", "set", "query".
  // |to| is the target of the message. If empty string, 'to' is omitted.
  // |body| the XML snipped to include between <iq>...</iq>
  // |response_callback| is called with result or error XML stanza received
  // from the server in response to the request sent.
  // |timeout_callback| is called when the response to the request hasn't been
  // received within the time allotted.
  void SendRequest(const std::string& type,
                   const std::string& from,
                   const std::string& to,
                   const std::string& body,
                   const ResponseCallback& response_callback,
                   const TimeoutCallback& timeout_callback);

  // |timeout| is the custom time interval after which requests should be
  // considered failed.
  void SendRequestWithCustomTimeout(const std::string& type,
                                    const std::string& from,
                                    const std::string& to,
                                    const std::string& body,
                                    base::TimeDelta timeout,
                                    const ResponseCallback& response_callback,
                                    const TimeoutCallback& timeout_callback);

  // Processes an <iq> stanza is received from the server. This will match the
  // stanza's 'id' attribute with pending request ID and if found, will
  // call the |response_callback|, or if the request is not found, an error
  // stanza fill be sent back to the server.
  // Returns false if some unexpected condition occurred and the stream should
  // be restarted.
  bool HandleIqStanza(std::unique_ptr<XmlNode> stanza);

 private:
  using RequestId = int;
  void OnTimeOut(RequestId id, const TimeoutCallback& timeout_callback);

  XmppChannelInterface* xmpp_channel_;
  provider::TaskRunner* task_runner_{nullptr};
  std::map<RequestId, ResponseCallback> requests_;
  RequestId last_request_id_{0};

  base::WeakPtrFactory<IqStanzaHandler> weak_ptr_factory_{this};
  DISALLOW_COPY_AND_ASSIGN(IqStanzaHandler);
};

}  // namespace weave

#endif  // LIBWEAVE_SRC_NOTIFICATION_XMPP_IQ_STANZA_HANDLER_H_