blob: 1d5cfe449a25b74a601ccda7bba1b46b3c7ee8fb [file] [log] [blame]
Sergei Datsenkobcd8e462018-04-20 15:44:56 +10001// 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 Degros15a44a82019-11-19 14:01:08 +110010#include <base/strings/string_piece.h>
11
Sergei Datsenkobcd8e462018-04-20 15:44:56 +100012namespace 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.
17class Uri {
18 public:
François Degros15a44a82019-11-19 14:01:08 +110019 // 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 Datsenkobcd8e462018-04-20 15:44:56 +100024
25 bool operator==(const Uri& other) const { return value() == other.value(); }
26
François Degros15a44a82019-11-19 14:01:08 +110027 // Gets the value of this Uri as "<scheme>://<path>", or an empty string if
28 // this Uri is not valid.
Sergei Datsenkobcd8e462018-04-20 15:44:56 +100029 std::string value() const;
François Degros15a44a82019-11-19 14:01:08 +110030
Sergei Datsenkobcd8e462018-04-20 15:44:56 +100031 const std::string& scheme() const { return scheme_; }
32 const std::string& path() const { return path_; }
33
François Degros15a44a82019-11-19 14:01:08 +110034 // Returns true if the scheme is not empty.
35 bool valid() const { return !scheme_.empty(); }
36
Sergei Datsenkobcd8e462018-04-20 15:44:56 +100037 // 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 Degros15a44a82019-11-19 14:01:08 +110039 static bool IsUri(base::StringPiece s) { return Parse(s).valid(); }
Sergei Datsenkobcd8e462018-04-20 15:44:56 +100040
François Degros15a44a82019-11-19 14:01:08 +110041 // 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 Datsenkobcd8e462018-04-20 15:44:56 +100044
45 private:
46 std::string scheme_;
47 std::string path_;
48};
49
50} // namespace cros_disks
51
52#endif // CROS_DISKS_URI_H_