1 /* 2 * Copyright (c) 2021 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 PLUGIN_WATCHER_H 17 #define PLUGIN_WATCHER_H 18 19 #include <logging.h> 20 21 #include <map> 22 #include <memory> 23 #include <mutex> 24 #include <string> 25 #include <thread> 26 class PluginManager; 27 28 using PluginManagerPtr = STD_PTR(shared, PluginManager); 29 30 class PluginWatcher { 31 public: 32 explicit PluginWatcher(const PluginManagerPtr& pluginManager); 33 ~PluginWatcher(); 34 bool ScanPlugins(const std::string& pluginDir); 35 bool WatchPlugins(const std::string& pluginDir); 36 37 private: 38 int inotifyFd_; 39 std::weak_ptr<PluginManager> pluginManager_; 40 std::map<int, std::string> wdToDir_; 41 std::thread monitorThread_; 42 std::mutex mtx_; 43 bool runMonitor_; 44 virtual void OnPluginAdded(const std::string& pluginPath); 45 virtual void OnPluginRemoved(const std::string& pluginPath); 46 void Monitor(); 47 bool MonitorIsSet(); 48 }; 49 50 #endif // !PLUGIN_WATCHER_H 51