summaryrefslogtreecommitdiffstats
path: root/mojo/shell/public/cpp/lib/names.cc
blob: 971e8aae835184c5250b0a7a9c2e8e49d77f278c (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
44
45
46
// Copyright 2016 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 "mojo/shell/public/cpp/names.h"

#include "base/strings/string_split.h"
#include "base/strings/string_util.h"

namespace mojo {

const char kNameType_Mojo[] = "mojo";
const char kNameType_Exe[] = "exe";

bool IsValidName(const std::string& name) {
  std::vector<std::string> parts =
      base::SplitString(name, ":", base::KEEP_WHITESPACE,
                        base::SPLIT_WANT_ALL);
  if (parts.size() != 2)
    return false;

  if (parts.front().empty())
    return false;

  const std::string& path = parts.back();
  return !path.empty() &&
      !base::StartsWith(path, "//", base::CompareCase::INSENSITIVE_ASCII);
}

std::string GetNameType(const std::string& name) {
  std::vector<std::string> parts =
      base::SplitString(name, ":", base::KEEP_WHITESPACE,
                        base::SPLIT_WANT_ALL);
  DCHECK(2 == parts.size());
  return parts.front();
}

std::string GetNamePath(const std::string& name) {
  std::vector<std::string> parts =
      base::SplitString(name, ":", base::KEEP_WHITESPACE,
                        base::SPLIT_WANT_ALL);
  DCHECK(2 == parts.size());
  return parts.back();
}

}  // namespace mojo