Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 1 | // Copyright 2018 The Chromium OS 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 | #ifndef CROS_DISKS_URI_H_ |
| 6 | #define CROS_DISKS_URI_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | |
François Degros | 15a44a8 | 2019-11-19 14:01:08 +1100 | [diff] [blame] | 10 | #include <base/strings/string_piece.h> |
| 11 | |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 12 | namespace cros_disks { |
| 13 | |
| 14 | // Wrapper for string representing URI. By no mean it's a complete |
| 15 | // implementation of what should be in such class, just to group some |
| 16 | // related utilities. |
| 17 | class Uri { |
| 18 | public: |
François Degros | 15a44a8 | 2019-11-19 14:01:08 +1100 | [diff] [blame] | 19 | // Creates an invalid Uri. |
| 20 | Uri() = default; |
| 21 | |
| 22 | // Creates a Uri with the given scheme and path. |
| 23 | Uri(base::StringPiece scheme, base::StringPiece path); |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 24 | |
| 25 | bool operator==(const Uri& other) const { return value() == other.value(); } |
| 26 | |
François Degros | 15a44a8 | 2019-11-19 14:01:08 +1100 | [diff] [blame] | 27 | // Gets the value of this Uri as "<scheme>://<path>", or an empty string if |
| 28 | // this Uri is not valid. |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 29 | std::string value() const; |
François Degros | 15a44a8 | 2019-11-19 14:01:08 +1100 | [diff] [blame] | 30 | |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 31 | const std::string& scheme() const { return scheme_; } |
| 32 | const std::string& path() const { return path_; } |
| 33 | |
François Degros | 15a44a8 | 2019-11-19 14:01:08 +1100 | [diff] [blame] | 34 | // Returns true if the scheme is not empty. |
| 35 | bool valid() const { return !scheme_.empty(); } |
| 36 | |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 37 | // Returns true if the given string is URI, i.e. <scheme>://[something]. |
| 38 | // It checks only the scheme part and doesn't verify validity of the path. |
François Degros | 15a44a8 | 2019-11-19 14:01:08 +1100 | [diff] [blame] | 39 | static bool IsUri(base::StringPiece s) { return Parse(s).valid(); } |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 40 | |
François Degros | 15a44a8 | 2019-11-19 14:01:08 +1100 | [diff] [blame] | 41 | // Parses the given string s as a URI. If s doesn't have a valid scheme, then |
| 42 | // a Uri with an empty scheme, ie an invalid Uri, is returned. |
| 43 | static Uri Parse(base::StringPiece s); |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 44 | |
| 45 | private: |
| 46 | std::string scheme_; |
| 47 | std::string path_; |
| 48 | }; |
| 49 | |
| 50 | } // namespace cros_disks |
| 51 | |
| 52 | #endif // CROS_DISKS_URI_H_ |