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 "DfxCollector"
16 
17 #include "dfx_collector.h"
18 
19 #include "media_file_utils.h"
20 #include "media_log.h"
21 #include "preferences.h"
22 #include "preferences_helper.h"
23 
24 namespace OHOS {
25 namespace Media {
26 
27 using namespace std;
28 
DfxCollector()29 DfxCollector::DfxCollector()
30 {
31 }
32 
~DfxCollector()33 DfxCollector::~DfxCollector()
34 {
35 }
36 
CollectThumbnailError(const std::string & path,int32_t method,int32_t errorCode)37 void DfxCollector::CollectThumbnailError(const std::string &path, int32_t method, int32_t errorCode)
38 {
39     lock_guard<mutex> lock(thumbnailErrorLock_);
40     ThumbnailErrorInfo thunmbailErrorInfo = { method, errorCode, MediaFileUtils::UTCTimeSeconds() };
41     thumbnailErrorMap_[path] = thunmbailErrorInfo;
42 }
43 
GetThumbnailError()44 std::unordered_map<std::string, ThumbnailErrorInfo> DfxCollector::GetThumbnailError()
45 {
46     lock_guard<mutex> lock(thumbnailErrorLock_);
47     std::unordered_map<std::string, ThumbnailErrorInfo> result = thumbnailErrorMap_;
48     thumbnailErrorMap_.clear();
49     return result;
50 }
51 
AddCommonBahavior(string bundleName,int32_t type)52 void DfxCollector::AddCommonBahavior(string bundleName, int32_t type)
53 {
54     lock_guard<mutex> lock(commonBehaviorLock_);
55     if (commonBehaviorMap_.count(bundleName) == 0) {
56         CommonBehavior commonBehavior = { 0 };
57         commonBehaviorMap_[bundleName] = commonBehavior;
58     }
59     commonBehaviorMap_[bundleName].times++;
60 }
61 
GetCommonBehavior()62 std::unordered_map<string, CommonBehavior> DfxCollector::GetCommonBehavior()
63 {
64     lock_guard<mutex> lock(commonBehaviorLock_);
65     std::unordered_map<string, CommonBehavior> result = commonBehaviorMap_;
66     commonBehaviorMap_.clear();
67     return result;
68 }
69 
CollectDeleteBehavior(std::string bundleName,int32_t type,int32_t size)70 void DfxCollector::CollectDeleteBehavior(std::string bundleName, int32_t type, int32_t size)
71 {
72     if (type == DfxType::TRASH_PHOTO) {
73         lock_guard<mutex> lock(deleteToTrashLock_);
74         if (deleteToTrashMap_.count(bundleName) == 0) {
75             deleteToTrashMap_[bundleName] = 0;
76         }
77         deleteToTrashMap_[bundleName]++;
78     } else if (type == DfxType::ALBUM_DELETE_ASSETS) {
79         lock_guard<mutex> lock(deleteFromDiskLock_);
80         if (deleteFromDiskMap_.count(bundleName) == 0) {
81             deleteFromDiskMap_[bundleName] = 0;
82         }
83         deleteFromDiskMap_[bundleName]++;
84     } else if (type == DfxType::ALBUM_REMOVE_PHOTOS) {
85         lock_guard<mutex> lock(removeLock_);
86         if (removeMap_.count(bundleName) == 0) {
87             removeMap_[bundleName] = 0;
88         }
89         removeMap_[bundleName]++;
90     }
91 }
92 
GetDeleteBehavior(int32_t type)93 std::unordered_map<std::string, int32_t> DfxCollector::GetDeleteBehavior(int32_t type)
94 {
95     std::unordered_map<std::string, int32_t> result;
96     if (type == DfxType::TRASH_PHOTO) {
97         lock_guard<mutex> lock(deleteToTrashLock_);
98         result = deleteToTrashMap_;
99         deleteToTrashMap_.clear();
100     } else if (type == DfxType::ALBUM_DELETE_ASSETS) {
101         lock_guard<mutex> lock(deleteFromDiskLock_);
102         result = deleteFromDiskMap_;
103         deleteFromDiskMap_.clear();
104     } else if (type == DfxType::ALBUM_REMOVE_PHOTOS) {
105         lock_guard<mutex> lock(removeLock_);
106         result = removeMap_;
107         removeMap_.clear();
108     }
109     return result;
110 }
111 
CollectAdaptationToMovingPhotoInfo(const string & appName,bool adapted)112 void DfxCollector::CollectAdaptationToMovingPhotoInfo(const string &appName, bool adapted)
113 {
114     lock_guard<mutex> lock(adaptationToMovingPhotoLock_);
115     if (adapted) {
116         adaptationToMovingPhotoInfo_.adaptedAppPackages.emplace(appName);
117     } else {
118         adaptationToMovingPhotoInfo_.unadaptedAppPackages.emplace(appName);
119     }
120 }
121 
GetAdaptationToMovingPhotoInfo()122 AdaptationToMovingPhotoInfo DfxCollector::GetAdaptationToMovingPhotoInfo()
123 {
124     lock_guard<mutex> lock(adaptationToMovingPhotoLock_);
125     AdaptationToMovingPhotoInfo infoCopy = adaptationToMovingPhotoInfo_;
126     adaptationToMovingPhotoInfo_.unadaptedAppPackages.clear();
127     adaptationToMovingPhotoInfo_.adaptedAppPackages.clear();
128     return infoCopy;
129 }
130 } // namespace Media
131 } // namespace OHOS