summaryrefslogtreecommitdiffstats
path: root/remoting/signaling/jid_util.cc
blob: 08e525d750a6984670f823e9e0db4dd62c67924e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/signaling/jid_util.h"

#include "base/strings/string_util.h"

namespace remoting {

std::string NormalizeJid(const std::string& jid) {
  std::string bare_jid;
  std::string resource;
  if (SplitJidResource(jid, &bare_jid, &resource)) {
    return base::ToLowerASCII(bare_jid) + "/" + resource;
  }
  return base::ToLowerASCII(bare_jid);
}

bool SplitJidResource(const std::string& full_jid,
                      std::string* bare_jid,
                      std::string* resource) {
  size_t slash_index = full_jid.find('/');
  if (slash_index == std::string::npos) {
    if (bare_jid) {
      *bare_jid = full_jid;
    }
    if (resource) {
      resource->clear();
    }
    return false;
  }

  if (bare_jid) {
    *bare_jid = full_jid.substr(0, slash_index);
  }
  if (resource) {
    *resource = full_jid.substr(slash_index + 1);
  }
  return true;
}

}  // namespace remoting