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