dom/media/webrtc/PeerIdentity.h
author Sylvestre Ledru <sledru@mozilla.com>
Mon, 19 Nov 2018 13:25:37 +0000
changeset 446960 0ceae9db9ec0be18daa1a279511ad305723185d4
parent 345311 a8f45b2afbf3bc116fc9c52d4631e426fb3d5733
child 465986 d56e561be43f7494a3f5362a427589947b4345f2
permissions -rw-r--r--
Bug 1204606 - Reformat of dom/media r=jya # skip-blame Differential Revision: https://phabricator.services.mozilla.com/D12251

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: sw=2 ts=2 sts=2 expandtab
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef PeerIdentity_h
#define PeerIdentity_h

#include "nsString.h"

template <class T>
class nsCOMPtr;
class nsIIDNService;

namespace mozilla {

/**
 * This class implements the identifier used in WebRTC identity.  Peers are
 * identified using a string in the form [<user>@]<domain>, for instance,
 * "user@example.com'. The (optional) user portion is a site-controlled string
 * containing any character other than '@'.  The domain portion is a valid IDN
 * domain name and is compared accordingly.
 *
 * See:
 * http://tools.ietf.org/html/draft-ietf-rtcweb-security-arch-09#section-5.6.5.3.3.1
 */
class PeerIdentity final : public RefCounted<PeerIdentity> {
 public:
  MOZ_DECLARE_REFCOUNTED_TYPENAME(PeerIdentity)

  explicit PeerIdentity(const nsAString& aPeerIdentity)
      : mPeerIdentity(aPeerIdentity) {}
  ~PeerIdentity() {}

  bool Equals(const PeerIdentity& aOther) const;
  bool Equals(const nsAString& aOtherString) const;
  const nsString& ToString() const { return mPeerIdentity; }

 private:
  static void GetUser(const nsAString& aPeerIdentity, nsAString& aUser);
  static void GetHost(const nsAString& aPeerIdentity, nsAString& aHost);

  static void GetNormalizedHost(const nsCOMPtr<nsIIDNService>& aIdnService,
                                const nsAString& aHost,
                                nsACString& aNormalizedHost);

  nsString mPeerIdentity;
};

inline bool operator==(const PeerIdentity& aOne, const PeerIdentity& aTwo) {
  return aOne.Equals(aTwo);
}

inline bool operator==(const PeerIdentity& aOne, const nsAString& aString) {
  return aOne.Equals(aString);
}

inline bool operator!=(const PeerIdentity& aOne, const PeerIdentity& aTwo) {
  return !aOne.Equals(aTwo);
}

inline bool operator!=(const PeerIdentity& aOne, const nsAString& aString) {
  return !aOne.Equals(aString);
}

} /* namespace mozilla */

#endif /* PeerIdentity_h */