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 #include "plugin_proxy.h"
16
17 #include "logger.h"
18
19 namespace OHOS {
20 namespace HiviewDFX {
21 DEFINE_LOG_TAG("HiView-PluginProxy");
OnEvent(std::shared_ptr<Event> & event)22 bool PluginProxy::OnEvent(std::shared_ptr<Event>& event)
23 {
24 HIVIEW_LOGD("called!");
25 std::lock_guard<std::mutex> lock(lock_);
26 if (LoadPluginIfNeed()) {
27 HIVIEW_LOGD("Plugin name: %{public}s plugin_->OnEvent", plugin_->GetName().c_str());
28 return plugin_->OnEvent(event);
29 }
30 return false;
31 }
32
CanProcessEvent(std::shared_ptr<Event> event)33 bool PluginProxy::CanProcessEvent(std::shared_ptr<Event> event)
34 {
35 HIVIEW_LOGD("called!");
36 std::lock_guard<std::mutex> lock(lock_);
37 if (LoadPluginIfNeed()) {
38 HIVIEW_LOGD("Plugin name: %{public}s plugin_->CanProcessEvent", plugin_->GetName().c_str());
39 return plugin_->CanProcessEvent(event);
40 }
41 return false;
42 }
43
CanProcessMoreEvents()44 bool PluginProxy::CanProcessMoreEvents()
45 {
46 HIVIEW_LOGD("called!");
47 std::lock_guard<std::mutex> lock(lock_);
48 if (LoadPluginIfNeed()) {
49 HIVIEW_LOGD("Plugin name: %{public}s plugin_->CanProcessMoreEvents", plugin_->GetName().c_str());
50 return plugin_->CanProcessMoreEvents();
51 }
52 return false;
53 }
54
GetHandlerInfo()55 std::string PluginProxy::GetHandlerInfo()
56 {
57 HIVIEW_LOGD("called!");
58 std::lock_guard<std::mutex> lock(lock_);
59 if (plugin_ != nullptr) {
60 HIVIEW_LOGD("Plugin name: %{public}s plugin_->GetHandlerInfo", plugin_->GetName().c_str());
61 plugin_->UpdateActiveTime();
62 return plugin_->GetHandlerInfo();
63 }
64 return Plugin::GetHandlerInfo();
65 }
66
Dump(int fd,const std::vector<std::string> & cmds)67 void PluginProxy::Dump(int fd, const std::vector<std::string>& cmds)
68 {
69 std::lock_guard<std::mutex> lock(lock_);
70 if (plugin_ != nullptr) {
71 return plugin_->Dump(fd, cmds);
72 }
73 }
74
OnEventListeningCallback(const Event & msg)75 void PluginProxy::OnEventListeningCallback(const Event &msg)
76 {
77 HIVIEW_LOGD("called!");
78 std::lock_guard<std::mutex> lock(lock_);
79 if (LoadPluginIfNeed()) {
80 HIVIEW_LOGD("Plugin name: %{public}s plugin_->OnEventListeningCallback", plugin_->GetName().c_str());
81 plugin_->OnEventListeningCallback(msg);
82 }
83 }
84
LoadPluginIfNeed()85 bool PluginProxy::LoadPluginIfNeed()
86 {
87 if (plugin_ != nullptr) {
88 plugin_->UpdateActiveTime();
89 return true;
90 }
91
92 if (plugin_ == nullptr && GetHiviewContext() != nullptr) {
93 plugin_ = GetHiviewContext()->InstancePluginByProxy(shared_from_this());
94 }
95
96 if (plugin_ == nullptr) {
97 // log failure
98 HIVIEW_LOGE("Failed to instaniate plugin with name :%{public}s", name_.c_str());
99 return false;
100 }
101
102 plugin_->UpdateActiveTime();
103 return true;
104 }
105
DestroyInstanceIfNeed(time_t maxIdleTime)106 void PluginProxy::DestroyInstanceIfNeed(time_t maxIdleTime)
107 {
108 std::lock_guard<std::mutex> lock(lock_);
109 time_t now = time(nullptr);
110 if (plugin_ == nullptr) {
111 return;
112 }
113
114 if (plugin_->GetUseCount() > 0) {
115 plugin_->UpdateActiveTime();
116 return;
117 }
118
119 if (now - plugin_->GetLastActiveTime() >= maxIdleTime) {
120 auto count = plugin_.use_count();
121 if (count > 1) {
122 HIVIEW_LOGW("Plugin %{public}s has more refs(%l{public}d), may caused by unfinished task. unload failed.",
123 plugin_->GetName().c_str(), count);
124 return;
125 }
126 HIVIEW_LOGI("plugin(%{public}s) reach max idle time, unload.", name_.c_str());
127 plugin_->OnUnload();
128 plugin_ = nullptr;
129 }
130 }
131 } // namespace HiviewDFX
132 } // namespace OHOS
133