1 /*
2  * Copyright (c) 2023 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 HIVIEW_SERVICE_AGENT_H
17 #define HIVIEW_SERVICE_AGENT_H
18 
19 #include <mutex>
20 #include <string>
21 #include <vector>
22 
23 #include "hiview_file_info.h"
24 #include "iremote_broker.h"
25 
26 namespace OHOS {
27 namespace HiviewDFX {
28 class HiviewServiceAgent {
29 public:
30     int32_t List(const std::string& logType, std::vector<HiviewFileInfo>& fileInfos);
31     int32_t Copy(const std::string& logType, const std::string& logName, const std::string& dest);
32     int32_t Move(const std::string& logType, const std::string& logName, const std::string& dest);
33     int32_t Remove(const std::string& logType, const std::string& logName);
34     void ProcessDeathObserver(const wptr<IRemoteObject>& remote);
35     sptr<IRemoteObject> GetRemoteService();
36     static HiviewServiceAgent& GetInstance();
37 
38 private:
39     HiviewServiceAgent() = default;
40     ~HiviewServiceAgent() = default;
41 
42     int32_t CopyOrMoveFile(
43         const std::string& logType, const std::string& logName, const std::string& dest, bool isMove);
44     bool CheckAndCreateHiviewDir(const std::string& destDir);
45     bool CreateDestDirs(const std::string& rootDir, const std::string& destDir);
46     bool CreateAndGrantAclPermission(const std::string& dirPath);
47 
48 private:
49     sptr<IRemoteObject> hiviewServiceAbilityProxy_;
50     sptr<IRemoteObject::DeathRecipient> deathRecipient_;
51     std::mutex proxyMutex_;
52 };
53 
54 class HiviewServiceDeathRecipient : public IRemoteObject::DeathRecipient {
55 public:
HiviewServiceDeathRecipient(HiviewServiceAgent & agent)56     HiviewServiceDeathRecipient(HiviewServiceAgent& agent) : hiviewServiceAgent_(agent) {};
57     ~HiviewServiceDeathRecipient() = default;
58     DISALLOW_COPY_AND_MOVE(HiviewServiceDeathRecipient);
59 
OnRemoteDied(const wptr<IRemoteObject> & remote)60     void OnRemoteDied(const wptr<IRemoteObject>& remote)
61     {
62         hiviewServiceAgent_.ProcessDeathObserver(remote);
63     }
64 
65 private:
66     HiviewServiceAgent& hiviewServiceAgent_;
67 };
68 } // namespace HiviewDFX
69 } // namespace OHOS
70 #endif