1 /*
2  * Copyright (c) 2024 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 #define MLOG_TAG "RingtonePrivacyManager"
16 
17 #include "ringtone_privacy_manager.h"
18 
19 #include "ringtone_errno.h"
20 #include "ringtone_file_utils.h"
21 #include "ringtone_log.h"
22 #include "ringtone_type.h"
23 
24 namespace OHOS {
25 namespace Media {
26 using namespace std;
27 using PrivacyRanges = vector<pair<uint32_t, uint32_t>>;
RingtonePrivacyManager(const string & path,const string & mode)28 RingtonePrivacyManager::RingtonePrivacyManager(const string &path, const string &mode) : path_(path), mode_(mode)
29 {}
30 
~RingtonePrivacyManager()31 RingtonePrivacyManager::~RingtonePrivacyManager()
32 {}
33 
34 const vector<string> EXIF_SUPPORTED_EXTENSION = {
35     RINGTONE_CONTAINER_TYPE_MP3,
36     RINGTONE_CONTAINER_TYPE_OGG
37 };
38 
IsTargetExtension(const string & path)39 static bool IsTargetExtension(const string &path)
40 {
41     const string ext = RingtoneFileUtils::GetExtensionFromPath(path);
42     bool ret = find(EXIF_SUPPORTED_EXTENSION.begin(), EXIF_SUPPORTED_EXTENSION.end(), ext) !=
43         EXIF_SUPPORTED_EXTENSION.end();
44     if (!ret) {
45         RINGTONE_ERR_LOG("invalid target extension:%{public}s", ext.c_str());
46     }
47     return ret;
48 }
49 
50 /* Caller is responsible to close the returned fd */
OpenOriginFd(const string & path,const string & mode)51 static int32_t OpenOriginFd(const string &path, const string &mode)
52 {
53     return RingtoneFileUtils::OpenFile(path, mode);
54 }
55 
Open()56 int32_t RingtonePrivacyManager::Open()
57 {
58     if (!IsTargetExtension(path_)) {
59         return E_INVALID_PATH;
60     }
61     return OpenOriginFd(path_, mode_);
62 }
63 } // namespace Media
64 } // namespace OHOS
65