Vitaly Buka | 4615e0d | 2015-10-14 15:35:12 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
Alex Vakulenko | dea76b2 | 2015-06-01 13:18:06 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 5 | #ifndef LIBWEAVE_SRC_NOTIFICATION_XMPP_IQ_STANZA_HANDLER_H_ |
| 6 | #define LIBWEAVE_SRC_NOTIFICATION_XMPP_IQ_STANZA_HANDLER_H_ |
Alex Vakulenko | dea76b2 | 2015-06-01 13:18:06 -0700 | [diff] [blame] | 7 | |
| 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 Buka | 34668e7 | 2015-12-15 14:46:47 -0800 | [diff] [blame] | 15 | #include <base/time/time.h> |
Alex Vakulenko | dea76b2 | 2015-06-01 13:18:06 -0700 | [diff] [blame] | 16 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 17 | #include "src/notification/xmpp_stream_parser.h" |
Alex Vakulenko | dea76b2 | 2015-06-01 13:18:06 -0700 | [diff] [blame] | 18 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 19 | namespace weave { |
Alex Vakulenko | dea76b2 | 2015-06-01 13:18:06 -0700 | [diff] [blame] | 20 | |
| 21 | class XmppChannelInterface; |
| 22 | |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 23 | namespace provider { |
| 24 | class TaskRunner; |
| 25 | } |
| 26 | |
Alex Vakulenko | dea76b2 | 2015-06-01 13:18:06 -0700 | [diff] [blame] | 27 | class IqStanzaHandler { |
| 28 | public: |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 29 | using ResponseCallback = base::Callback<void(std::unique_ptr<XmlNode>)>; |
Alex Vakulenko | dea76b2 | 2015-06-01 13:18:06 -0700 | [diff] [blame] | 30 | using TimeoutCallback = base::Closure; |
| 31 | |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 32 | IqStanzaHandler(XmppChannelInterface* xmpp_channel, |
| 33 | provider::TaskRunner* task_runner); |
Alex Vakulenko | dea76b2 | 2015-06-01 13:18:06 -0700 | [diff] [blame] | 34 | |
| 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 Buka | 63cc3d2 | 2015-06-23 20:11:36 -0700 | [diff] [blame] | 50 | // |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 Vakulenko | dea76b2 | 2015-06-01 13:18:06 -0700 | [diff] [blame] | 60 | // 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 Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 73 | provider::TaskRunner* task_runner_{nullptr}; |
Alex Vakulenko | dea76b2 | 2015-06-01 13:18:06 -0700 | [diff] [blame] | 74 | 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 Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 81 | } // namespace weave |
Alex Vakulenko | dea76b2 | 2015-06-01 13:18:06 -0700 | [diff] [blame] | 82 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 83 | #endif // LIBWEAVE_SRC_NOTIFICATION_XMPP_IQ_STANZA_HANDLER_H_ |