1 /*
2  * Copyright (c) 2022-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 #include "clip/clip_plugin.h"
17 
18 #include "default_clip.h"
19 #include "pasteboard_event_dfx.h"
20 namespace OHOS::MiscServices {
21 std::map<std::string, ClipPlugin::Factory *> ClipPlugin::factories_;
22 DefaultClip g_defaultClip;
RegCreator(const std::string & name,Factory * factory)23 bool ClipPlugin::RegCreator(const std::string &name, Factory *factory)
24 {
25     auto it = factories_.find(name);
26     if (it != factories_.end()) {
27         return false;
28     }
29     factories_[name] = factory;
30     return true;
31 }
32 
CreatePlugin(const std::string & name)33 ClipPlugin *ClipPlugin::CreatePlugin(const std::string &name)
34 {
35     auto it = factories_.find(name);
36     if (it == factories_.end() || it->second == nullptr) {
37         return &g_defaultClip;
38     }
39     RADAR_REPORT(RadarReporter::DFX_PLUGIN_CREATE_DESTROY, RadarReporter::DFX_PLUGIN_CREATE,
40         RadarReporter::DFX_SUCCESS);
41     return it->second->Create();
42 }
43 
DestroyPlugin(const std::string & name,ClipPlugin * plugin)44 bool ClipPlugin::DestroyPlugin(const std::string &name, ClipPlugin *plugin)
45 {
46     if (plugin == &g_defaultClip) {
47         return true;
48     }
49 
50     auto it = factories_.find(name);
51     if (it == factories_.end() || it->second == nullptr) {
52         return false;
53     }
54     RADAR_REPORT(RadarReporter::DFX_PLUGIN_CREATE_DESTROY, RadarReporter::DFX_PLUGIN_DESTROY,
55         RadarReporter::DFX_SUCCESS);
56     return it->second->Destroy(plugin);
57 }
58 
~ClipPlugin()59 ClipPlugin::~ClipPlugin()
60 {
61 }
62 
GetTopEvents(uint32_t topN)63 std::vector<ClipPlugin::GlobalEvent> ClipPlugin::GetTopEvents(uint32_t topN)
64 {
65     (void)topN;
66     return std::vector<GlobalEvent>();
67 }
68 
GetTopEvents(uint32_t topN,int32_t user)69 std::vector<ClipPlugin::GlobalEvent> ClipPlugin::GetTopEvents(uint32_t topN, int32_t user)
70 {
71     (void)user;
72     (void)topN;
73     return std::vector<GlobalEvent>();
74 }
75 
Clear()76 void ClipPlugin::Clear()
77 {
78 }
79 
PublishServiceState(const std::string & networkId,ServiceStatus status)80 int32_t ClipPlugin::PublishServiceState(const std::string &networkId, ServiceStatus status)
81 {
82     return 0;
83 }
84 
Clear(int32_t user)85 void ClipPlugin::Clear(int32_t user)
86 {
87     (void)user;
88 }
89 
Marshal(Serializable::json & node) const90 bool ClipPlugin::GlobalEvent::Marshal(Serializable::json &node) const
91 {
92     bool ret = true;
93     ret = ret && SetValue(node, version, GET_NAME(version));
94     ret = ret && SetValue(node, frameNum, GET_NAME(frameNum));
95     ret = ret && SetValue(node, user, GET_NAME(user));
96     ret = ret && SetValue(node, seqId, GET_NAME(seqId));
97     ret = ret && SetValue(node, expiration, GET_NAME(expiration));
98     ret = ret && SetValue(node, status, GET_NAME(status));
99     ret = ret && SetValue(node, deviceId, GET_NAME(deviceId));
100     ret = ret && SetValue(node, account, GET_NAME(account));
101     ret = ret && SetValue(node, dataType, GET_NAME(dataType));
102     ret = ret && SetValue(node, syncTime, GET_NAME(syncTime));
103     return ret;
104 }
105 
Unmarshal(const Serializable::json & node)106 bool ClipPlugin::GlobalEvent::Unmarshal(const Serializable::json &node)
107 {
108     bool ret = true;
109     ret = ret && GetValue(node, GET_NAME(version), version);
110     ret = ret && GetValue(node, GET_NAME(frameNum), frameNum);
111     ret = ret && GetValue(node, GET_NAME(user), user);
112     ret = ret && GetValue(node, GET_NAME(seqId), seqId);
113     ret = ret && GetValue(node, GET_NAME(expiration), expiration);
114     ret = ret && GetValue(node, GET_NAME(status), status);
115     ret = ret && GetValue(node, GET_NAME(deviceId), deviceId);
116     ret = ret && GetValue(node, GET_NAME(account), account);
117     ret = ret && GetValue(node, GET_NAME(dataType), dataType);
118     ret = ret && GetValue(node, GET_NAME(syncTime), syncTime);
119     return ret;
120 }
121 } // namespace OHOS::MiscServices
122