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