henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #ifndef WEBRTC_LIBJINGLE_XMPP_XMPPTHREAD_H_ |
| 12 | #define WEBRTC_LIBJINGLE_XMPP_XMPPTHREAD_H_ |
| 13 | |
kwiberg | 322c4a0 | 2016-04-30 02:40:22 -0700 | [diff] [blame] | 14 | #include <memory> |
| 15 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 16 | #include "webrtc/libjingle/xmpp/moduleimpl.h" |
| 17 | #include "webrtc/libjingle/xmpp/rostermodule.h" |
| 18 | |
| 19 | namespace buzz { |
| 20 | |
| 21 | //! Presence Information |
| 22 | //! This class stores both presence information for outgoing presence and is |
| 23 | //! returned by methods in XmppRosterModule to represent received incoming |
| 24 | //! presence information. When this class is writeable (non-const) then each |
| 25 | //! update to any property will set the inner xml. Setting the raw_xml will |
| 26 | //! rederive all of the other properties. |
| 27 | class XmppPresenceImpl : public XmppPresence { |
| 28 | public: |
| 29 | virtual ~XmppPresenceImpl() {} |
| 30 | |
| 31 | //! The from Jid of for the presence information. |
| 32 | //! Typically this will be a full Jid with resource specified. For outgoing |
| 33 | //! presence this should remain JID_NULL and will be scrubbed from the |
| 34 | //! stanza when being sent. |
| 35 | virtual const Jid jid() const; |
| 36 | |
| 37 | //! Is the contact available? |
| 38 | virtual XmppPresenceAvailable available() const; |
| 39 | |
| 40 | //! Sets if the user is available or not |
| 41 | virtual XmppReturnStatus set_available(XmppPresenceAvailable available); |
| 42 | |
| 43 | //! The show value of the presence info |
| 44 | virtual XmppPresenceShow presence_show() const; |
| 45 | |
| 46 | //! Set the presence show value |
| 47 | virtual XmppReturnStatus set_presence_show(XmppPresenceShow show); |
| 48 | |
| 49 | //! The Priority of the presence info |
| 50 | virtual int priority() const; |
| 51 | |
| 52 | //! Set the priority of the presence |
| 53 | virtual XmppReturnStatus set_priority(int priority); |
| 54 | |
| 55 | //! The plain text status of the presence info. |
| 56 | //! If there are multiple status because of language, this will either be a |
| 57 | //! status that is not tagged for language or the first available |
| 58 | virtual const std::string status() const; |
| 59 | |
| 60 | //! Sets the status for the presence info. |
| 61 | //! If there is more than one status present already then this will remove |
| 62 | //! them all and replace it with one status element we no specified language |
| 63 | virtual XmppReturnStatus set_status(const std::string& status); |
| 64 | |
| 65 | //! The connection status |
| 66 | virtual XmppPresenceConnectionStatus connection_status() const; |
| 67 | |
| 68 | //! The focus obfuscated GAIA id |
| 69 | virtual const std::string google_user_id() const; |
| 70 | |
| 71 | //! The nickname in the presence |
| 72 | virtual const std::string nickname() const; |
| 73 | |
| 74 | //! The raw xml of the presence update |
| 75 | virtual const XmlElement* raw_xml() const; |
| 76 | |
| 77 | //! Sets the raw presence stanza for the presence update |
| 78 | //! This will cause all other data items in this structure to be rederived |
| 79 | virtual XmppReturnStatus set_raw_xml(const XmlElement * xml); |
| 80 | |
| 81 | private: |
| 82 | XmppPresenceImpl(); |
| 83 | |
| 84 | friend class XmppPresence; |
| 85 | friend class XmppRosterModuleImpl; |
| 86 | |
| 87 | void CreateRawXmlSkeleton(); |
| 88 | |
| 89 | // Store everything in the XML element. If this becomes a perf issue we can |
| 90 | // cache the data. |
kwiberg | 322c4a0 | 2016-04-30 02:40:22 -0700 | [diff] [blame] | 91 | std::unique_ptr<XmlElement> raw_xml_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | //! A contact as given by the server |
| 95 | class XmppRosterContactImpl : public XmppRosterContact { |
| 96 | public: |
| 97 | virtual ~XmppRosterContactImpl() {} |
| 98 | |
| 99 | //! The jid for the contact. |
| 100 | //! Typically this will be a bare Jid. |
| 101 | virtual const Jid jid() const; |
| 102 | |
| 103 | //! Sets the jid for the roster contact update |
| 104 | virtual XmppReturnStatus set_jid(const Jid& jid); |
| 105 | |
| 106 | //! The name (nickname) stored for this contact |
| 107 | virtual const std::string name() const; |
| 108 | |
| 109 | //! Sets the name |
| 110 | virtual XmppReturnStatus set_name(const std::string& name); |
| 111 | |
| 112 | //! The Presence subscription state stored on the server for this contact |
| 113 | //! This is never settable and will be ignored when generating a roster |
| 114 | //! add/update request |
| 115 | virtual XmppSubscriptionState subscription_state() const; |
| 116 | |
| 117 | //! The number of Groups applied to this contact |
| 118 | virtual size_t GetGroupCount() const; |
| 119 | |
| 120 | //! Gets a Group applied to the contact based on index. |
| 121 | virtual const std::string GetGroup(size_t index) const; |
| 122 | |
| 123 | //! Adds a group to this contact. |
| 124 | //! This will return a no error if the group is already present. |
| 125 | virtual XmppReturnStatus AddGroup(const std::string& group); |
| 126 | |
| 127 | //! Removes a group from the contact. |
| 128 | //! This will return no error if the group isn't there |
| 129 | virtual XmppReturnStatus RemoveGroup(const std::string& group); |
| 130 | |
| 131 | //! The raw xml for this roster contact |
| 132 | virtual const XmlElement* raw_xml() const; |
| 133 | |
| 134 | //! Sets the raw presence stanza for the presence update |
| 135 | //! This will cause all other data items in this structure to be rederived |
| 136 | virtual XmppReturnStatus set_raw_xml(const XmlElement * xml); |
| 137 | |
| 138 | private: |
| 139 | XmppRosterContactImpl(); |
| 140 | |
| 141 | void CreateRawXmlSkeleton(); |
| 142 | void SetXmlFromWire(const XmlElement * xml); |
| 143 | void ResetGroupCache(); |
| 144 | |
| 145 | bool FindGroup(const std::string& group, |
| 146 | XmlElement** element, |
| 147 | XmlChild** child_before); |
| 148 | |
| 149 | |
| 150 | friend class XmppRosterContact; |
| 151 | friend class XmppRosterModuleImpl; |
| 152 | |
| 153 | int group_count_; |
| 154 | int group_index_returned_; |
| 155 | XmlElement * group_returned_; |
kwiberg | 322c4a0 | 2016-04-30 02:40:22 -0700 | [diff] [blame] | 156 | std::unique_ptr<XmlElement> raw_xml_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | //! An XmppModule for handle roster and presence functionality |
| 160 | class XmppRosterModuleImpl : public XmppModuleImpl, |
| 161 | public XmppRosterModule, public XmppIqHandler { |
| 162 | public: |
| 163 | virtual ~XmppRosterModuleImpl(); |
| 164 | |
| 165 | IMPLEMENT_XMPPMODULE |
| 166 | |
| 167 | //! Sets the roster handler (callbacks) for the module |
| 168 | virtual XmppReturnStatus set_roster_handler(XmppRosterHandler * handler); |
| 169 | |
| 170 | //! Gets the roster handler for the module |
| 171 | virtual XmppRosterHandler* roster_handler(); |
| 172 | |
| 173 | // USER PRESENCE STATE ------------------------------------------------------- |
| 174 | |
| 175 | //! Gets the aggregate outgoing presence |
| 176 | //! This object is non-const and be edited directly. No update is sent |
| 177 | //! to the server until a Broadcast is sent |
| 178 | virtual XmppPresence* outgoing_presence(); |
| 179 | |
| 180 | //! Broadcasts that the user is available. |
| 181 | //! Nothing with respect to presence is sent until this is called. |
| 182 | virtual XmppReturnStatus BroadcastPresence(); |
| 183 | |
| 184 | //! Sends a directed presence to a Jid |
| 185 | //! Note that the client doesn't store where directed presence notifications |
| 186 | //! have been sent. The server can keep the appropriate state |
| 187 | virtual XmppReturnStatus SendDirectedPresence(const XmppPresence* presence, |
| 188 | const Jid& to_jid); |
| 189 | |
| 190 | // INCOMING PRESENCE STATUS -------------------------------------------------- |
| 191 | |
| 192 | //! Returns the number of incoming presence data recorded |
| 193 | virtual size_t GetIncomingPresenceCount(); |
| 194 | |
| 195 | //! Returns an incoming presence datum based on index |
| 196 | virtual const XmppPresence* GetIncomingPresence(size_t index); |
| 197 | |
| 198 | //! Gets the number of presence data for a bare Jid |
| 199 | //! There may be a datum per resource |
| 200 | virtual size_t GetIncomingPresenceForJidCount(const Jid& jid); |
| 201 | |
| 202 | //! Returns a single presence data for a Jid based on index |
| 203 | virtual const XmppPresence* GetIncomingPresenceForJid(const Jid& jid, |
| 204 | size_t index); |
| 205 | |
| 206 | // ROSTER MANAGEMENT --------------------------------------------------------- |
| 207 | |
| 208 | //! Requests an update of the roster from the server |
| 209 | //! This must be called to initialize the client side cache of the roster |
| 210 | //! After this is sent the server should keep this module apprised of any |
| 211 | //! changes. |
| 212 | virtual XmppReturnStatus RequestRosterUpdate(); |
| 213 | |
| 214 | //! Returns the number of contacts in the roster |
| 215 | virtual size_t GetRosterContactCount(); |
| 216 | |
| 217 | //! Returns a contact by index |
| 218 | virtual const XmppRosterContact* GetRosterContact(size_t index); |
| 219 | |
| 220 | //! Finds a contact by Jid |
| 221 | virtual const XmppRosterContact* FindRosterContact(const Jid& jid); |
| 222 | |
| 223 | //! Send a request to the server to add a contact |
| 224 | //! Note that the contact won't show up in the roster until the server can |
| 225 | //! respond. This happens async when the socket is being serviced |
| 226 | virtual XmppReturnStatus RequestRosterChange( |
| 227 | const XmppRosterContact* contact); |
| 228 | |
| 229 | //! Request that the server remove a contact |
| 230 | //! The jabber protocol specifies that the server should also cancel any |
| 231 | //! subscriptions when this is done. Like adding, this contact won't be |
| 232 | //! removed until the server responds. |
| 233 | virtual XmppReturnStatus RequestRosterRemove(const Jid& jid); |
| 234 | |
| 235 | // SUBSCRIPTION MANAGEMENT --------------------------------------------------- |
| 236 | |
| 237 | //! Request a subscription to presence notifications form a Jid |
| 238 | virtual XmppReturnStatus RequestSubscription(const Jid& jid); |
| 239 | |
| 240 | //! Cancel a subscription to presence notifications from a Jid |
| 241 | virtual XmppReturnStatus CancelSubscription(const Jid& jid); |
| 242 | |
| 243 | //! Approve a request to deliver presence notifications to a jid |
| 244 | virtual XmppReturnStatus ApproveSubscriber(const Jid& jid); |
| 245 | |
| 246 | //! Deny or cancel presence notification deliver to a jid |
| 247 | virtual XmppReturnStatus CancelSubscriber(const Jid& jid); |
| 248 | |
| 249 | // XmppIqHandler IMPLEMENTATION ---------------------------------------------- |
| 250 | virtual void IqResponse(XmppIqCookie cookie, const XmlElement * stanza); |
| 251 | |
| 252 | protected: |
| 253 | // XmppModuleImpl OVERRIDES -------------------------------------------------- |
| 254 | virtual bool HandleStanza(const XmlElement *); |
| 255 | |
| 256 | // PRIVATE DATA -------------------------------------------------------------- |
| 257 | private: |
| 258 | friend class XmppRosterModule; |
| 259 | XmppRosterModuleImpl(); |
| 260 | |
| 261 | // Helper functions |
| 262 | void DeleteIncomingPresence(); |
| 263 | void DeleteContacts(); |
| 264 | XmppReturnStatus SendSubscriptionRequest(const Jid& jid, |
| 265 | const std::string& type); |
| 266 | void InternalSubscriptionRequest(const Jid& jid, const XmlElement* stanza, |
| 267 | XmppSubscriptionRequestType request_type); |
| 268 | void InternalIncomingPresence(const Jid& jid, const XmlElement* stanza); |
| 269 | void InternalIncomingPresenceError(const Jid& jid, const XmlElement* stanza); |
| 270 | void InternalRosterItems(const XmlElement* stanza); |
| 271 | |
| 272 | // Member data |
| 273 | XmppPresenceImpl outgoing_presence_; |
| 274 | XmppRosterHandler* roster_handler_; |
| 275 | |
| 276 | typedef std::vector<XmppPresenceImpl*> PresenceVector; |
| 277 | typedef std::map<Jid, PresenceVector*> JidPresenceVectorMap; |
kwiberg | 322c4a0 | 2016-04-30 02:40:22 -0700 | [diff] [blame] | 278 | std::unique_ptr<JidPresenceVectorMap> incoming_presence_map_; |
| 279 | std::unique_ptr<PresenceVector> incoming_presence_vector_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 280 | |
| 281 | typedef std::vector<XmppRosterContactImpl*> ContactVector; |
kwiberg | 322c4a0 | 2016-04-30 02:40:22 -0700 | [diff] [blame] | 282 | std::unique_ptr<ContactVector> contacts_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 283 | }; |
| 284 | |
| 285 | } |
| 286 | |
| 287 | #endif // WEBRTC_LIBJINGLE_XMPP_XMPPTHREAD_H_ |