• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef URI_HELPER_H
17 #define URI_HELPER_H
18 
19 #include <cstdint>
20 #include <string>
21 #include <string_view>
22 #include <map>
23 #include <unistd.h>
24 #include <fcntl.h>
25 
26 namespace OHOS {
27 namespace Media {
28 /**
29  * The simple utility is designed to facilitate the uri processing.
30  */
31 class __attribute__((visibility("default"))) UriHelper {
32 public:
33     enum UriType : uint8_t {
34         URI_TYPE_FILE,
35         URI_TYPE_FD,
36         URI_TYPE_HTTP,
37         URI_TYPE_UNKNOWN,
38     };
39 
40     enum UriAccessMode : uint8_t {
41         URI_READ = 1 << 0,
42         URI_WRITE = 1 << 1,
43     };
44 
45     explicit UriHelper(const std::string_view &uri);
46     UriHelper(int32_t fd, int64_t offset, int64_t size);
47     ~UriHelper();
48 
49     UriHelper(UriHelper &&rhs) noexcept;
50     UriHelper &operator=(UriHelper &&rhs) noexcept;
51     UriHelper(const UriHelper &rhs);
52     UriHelper &operator=(const UriHelper &rhs);
53 
54     uint8_t UriType() const;
55     std::string FormattedUri() const;
56     bool AccessCheck(uint8_t flag) const;
57 
58 private:
59     void FormatMeForUri(const std::string_view &uri) noexcept;
60     void FormatMeForFd() noexcept;
61     bool ParseFdUri(std::string_view uri);
62     bool CorrectFdParam();
63     void Swap(UriHelper &&rhs) noexcept;
64     void Copy(const UriHelper &rhs) noexcept;
65 
66     std::string formattedUri_ = "";
67     std::string_view rawFileUri_ = "";
68     uint8_t type_ = 0;
69     int32_t fd_ = -1;
70     int64_t offset_ = 0;
71     int64_t size_ = 0;
72 };
73 } // namespace Media
74 } // namespace OHOS
75 
76 #endif