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