1 /* 2 * Copyright (c) 2023-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 #ifndef HIAPPEVENT_INTERFACES_NATIVE_INNER_API_INCLUDE_OS_EVENT_LISTENER_H 16 #define HIAPPEVENT_INTERFACES_NATIVE_INNER_API_INCLUDE_OS_EVENT_LISTENER_H 17 18 #include <string> 19 #include <thread> 20 #include <vector> 21 22 #include "json/json.h" 23 24 namespace OHOS { 25 namespace HiviewDFX { 26 class AppEventPack; 27 28 class OsEventListener : public std::enable_shared_from_this<OsEventListener> { 29 public: 30 OsEventListener(); 31 ~OsEventListener(); 32 bool StartListening(); 33 bool RemoveOsEventDir(); 34 void GetEvents(std::vector<std::shared_ptr<AppEventPack>>& events); 35 bool AddListenedEvents(uint64_t eventsMask); 36 bool SetListenedEvents(uint64_t eventsMask); 37 38 private: 39 void Init(); 40 bool InitDir(const std::string& dirPath); 41 bool RegisterDirListener(const std::string& dirPath); 42 void HandleDirEvent(); 43 void HandleInotify(const std::string& files); 44 void GetEventsFromFiles(const std::vector<std::string>& files, std::vector<std::shared_ptr<AppEventPack>>& events); 45 std::shared_ptr<AppEventPack> GetAppEventPackFromJson(const std::string& jsonStr); 46 47 private: 48 int inotifyFd_ = -1; 49 int inotifyWd_ = -1; 50 bool inotifyStopFlag_ = true; 51 std::string osEventPath_; 52 std::unique_ptr<std::thread> inotifyThread_ = nullptr; 53 std::vector<std::shared_ptr<AppEventPack>> historyEvents_; 54 uint64_t osEventsMask_ = 0; 55 }; 56 } // namespace HiviewDFX 57 } // namespace OHOS 58 #endif // HIAPPEVENT_INTERFACES_NATIVE_INNER_API_INCLUDE_OS_EVENT_LISTENER_H