signaling_id_util.cc 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2019 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. #include "remoting/signaling/signaling_id_util.h"
  5. #include <stddef.h>
  6. #include "base/logging.h"
  7. #include "base/strings/string_util.h"
  8. namespace remoting {
  9. namespace {
  10. constexpr char kFtlResourcePrefix[] = "chromoting_ftl_";
  11. constexpr char kGmailDomain[] = "gmail.com";
  12. constexpr char kGooglemailDomain[] = "googlemail.com";
  13. } // namespace
  14. std::string NormalizeSignalingId(const std::string& id) {
  15. std::string email;
  16. std::string resource;
  17. if (SplitSignalingIdResource(id, &email, &resource)) {
  18. std::string normalized_email = resource.find(kFtlResourcePrefix) == 0
  19. ? GetCanonicalEmail(email)
  20. : base::ToLowerASCII(email);
  21. return normalized_email + "/" + resource;
  22. }
  23. return base::ToLowerASCII(email);
  24. }
  25. std::string GetCanonicalEmail(const std::string& email) {
  26. DCHECK(email.find('/') == std::string::npos)
  27. << "This function expects an email address, not a signaling ID.";
  28. std::string canonical_email = base::ToLowerASCII(email);
  29. base::TrimString(canonical_email, base::kWhitespaceASCII, &canonical_email);
  30. size_t at_index = canonical_email.find('@');
  31. if (at_index == std::string::npos) {
  32. LOG(ERROR) << "Unexpected email address. Character '@' is missing.";
  33. return canonical_email;
  34. }
  35. std::string username = canonical_email.substr(0, at_index);
  36. std::string domain = canonical_email.substr(at_index + 1);
  37. if (domain == kGmailDomain || domain == kGooglemailDomain) {
  38. // GMail/GoogleMail domains ignore dots, whereas other domains may not.
  39. base::RemoveChars(username, ".", &username);
  40. return username + '@' + kGmailDomain;
  41. }
  42. return canonical_email;
  43. }
  44. bool SplitSignalingIdResource(const std::string& full_id,
  45. std::string* email,
  46. std::string* resource) {
  47. size_t slash_index = full_id.find('/');
  48. if (slash_index == std::string::npos) {
  49. if (email) {
  50. *email = full_id;
  51. }
  52. if (resource) {
  53. resource->clear();
  54. }
  55. return false;
  56. }
  57. if (email) {
  58. *email = full_id.substr(0, slash_index);
  59. }
  60. if (resource) {
  61. *resource = full_id.substr(slash_index + 1);
  62. }
  63. return true;
  64. }
  65. } // namespace remoting