hashimoto@chromium.org | 5fdbcf7 | 2012-06-05 13:15:50 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
| 5 | #include "dbus/string_util.h" |
| 6 | |
avi@chromium.org | ffcdb95 | 2013-06-11 16:27:01 +0900 | [diff] [blame^] | 7 | #include "base/strings/string_util.h" |
hashimoto@chromium.org | 5fdbcf7 | 2012-06-05 13:15:50 +0900 | [diff] [blame] | 8 | |
| 9 | namespace dbus { |
| 10 | |
| 11 | bool IsValidObjectPath(const std::string& value) { |
| 12 | // This implementation is based upon D-Bus Specification Version 0.19. |
| 13 | |
| 14 | const bool kCaseSensitive = true; |
| 15 | |
| 16 | // A valid object path begins with '/'. |
| 17 | if (!StartsWithASCII(value, "/", kCaseSensitive)) |
| 18 | return false; |
| 19 | |
| 20 | // Elements are pieces delimited by '/'. For instance, "org", "chromium", |
| 21 | // "Foo" are elements of "/org/chromium/Foo". |
| 22 | int element_length = 0; |
| 23 | for (size_t i = 1; i < value.size(); ++i) { |
| 24 | const char c = value[i]; |
| 25 | if (c == '/') { |
| 26 | // No element may be the empty string. |
| 27 | if (element_length == 0) |
| 28 | return false; |
| 29 | element_length = 0; |
| 30 | } else { |
| 31 | // Each element must only contain "[A-Z][a-z][0-9]_". |
| 32 | const bool is_valid_character = |
| 33 | ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || |
| 34 | ('0' <= c && c <= '9') || c == '_'; |
| 35 | if (!is_valid_character) |
| 36 | return false; |
| 37 | element_length++; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | // A trailing '/' character is not allowed unless the path is the root path. |
| 42 | if (value.size() > 1 && EndsWith(value, "/", kCaseSensitive)) |
| 43 | return false; |
| 44 | |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | } // namespace dbus |