• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 RUNTIME_CONTEXT_IMPL_H
17 #define RUNTIME_CONTEXT_IMPL_H
18 
19 #include <map>
20 #include <mutex>
21 #include <shared_mutex>
22 
23 #include "auto_launch.h"
24 #include "evloop/include/ievent.h"
25 #include "evloop/include/ievent_loop.h"
26 #include "icommunicator_aggregator.h"
27 #include "lock_status_observer.h"
28 #include "task_pool.h"
29 #include "time_tick_monitor.h"
30 #include "user_change_monitor.h"
31 
32 namespace DistributedDB {
33 class RuntimeContextImpl final : public RuntimeContext {
34 public:
35     RuntimeContextImpl();
36     ~RuntimeContextImpl() override;
37 
38     // Get/Set the label of this process.
39     void SetProcessLabel(const std::string &label) override;
40     std::string GetProcessLabel() const override;
41     int SetCommunicatorAdapter(IAdapter *adapter) override;
42     int GetCommunicatorAggregator(ICommunicatorAggregator *&outAggregator) override;
43     void SetCommunicatorAggregator(ICommunicatorAggregator *inAggregator) override;
44     int GetLocalIdentity(std::string &outTarget) override;
45     // Add and start a timer.
46     int SetTimer(int milliSeconds, const TimerAction &action,
47         const TimerFinalizer &finalizer, TimerId &timerId) override;
48 
49     // Modify the interval of the timer.
50     int ModifyTimer(TimerId timerId, int milliSeconds) override;
51 
52     // Remove the timer.
53     void RemoveTimer(TimerId timerId, bool wait) override;
54 
55     // Task interfaces.
56     int ScheduleTask(const TaskAction &task) override;
57     int ScheduleQueuedTask(const std::string &queueTag, const TaskAction &task) override;
58 
59     // Shrink as much memory as possible.
60     void ShrinkMemory(const std::string &description) override;
61 
62     // Register a time changed lister, it will be callback when local time changed.
63     NotificationChain::Listener *RegisterTimeChangedLister(const TimeChangedAction &action, int &errCode) override;
64 
65     int SetPermissionCheckCallback(const PermissionCheckCallback &callback) override;
66 
67     int SetPermissionCheckCallback(const PermissionCheckCallbackV2 &callback) override;
68 
69     int SetPermissionCheckCallback(const PermissionCheckCallbackV3 &callback) override;
70 
71     int RunPermissionCheck(const PermissionCheckParam &param, uint8_t flag) const override;
72 
73     int EnableKvStoreAutoLaunch(const KvDBProperties &properties, AutoLaunchNotifier notifier,
74         const AutoLaunchOption &option) override;
75 
76     int DisableKvStoreAutoLaunch(const std::string &normalIdentifier, const std::string &dualTupleIdentifier,
77         const std::string &userId) override;
78 
79     void GetAutoLaunchSyncDevices(const std::string &identifier, std::vector<std::string> &devices) const override;
80 
81     void SetAutoLaunchRequestCallback(const AutoLaunchRequestCallback &callback, DBType type) override;
82 
83     NotificationChain::Listener *RegisterLockStatusLister(const LockStatusNotifier &action, int &errCode) override;
84 
85     bool IsAccessControlled() const override;
86 
87     int SetSecurityOption(const std::string &filePath, const SecurityOption &option) const override;
88 
89     int GetSecurityOption(const std::string &filePath, SecurityOption &option) const override;
90 
91     bool CheckDeviceSecurityAbility(const std::string &devId, const SecurityOption &option) const override;
92 
93     int SetProcessSystemApiAdapter(const std::shared_ptr<IProcessSystemApiAdapter> &adapter) override;
94 
95     bool IsProcessSystemApiAdapterValid() const override;
96 
97     bool IsCommunicatorAggregatorValid() const override;
98 
99     // Notify TIME_CHANGE_EVENT.
100     void NotifyTimestampChanged(TimeOffset offset) const override;
101 
102     void SetStoreStatusNotifier(const StoreStatusNotifier &notifier) override;
103 
104     void NotifyDatabaseStatusChange(const std::string &userId, const std::string &appId, const std::string &storeId,
105         const std::string &deviceId, bool onlineStatus) override;
106 
107     int SetSyncActivationCheckCallback(const SyncActivationCheckCallback &callback) override;
108 
109     int SetSyncActivationCheckCallback(const SyncActivationCheckCallbackV2 &callback) override;
110 
111     bool IsSyncerNeedActive(const DBProperties &properties) const override;
112 
113     // Register a user changed lister, it will be callback when user change.
114     NotificationChain::Listener *RegisterUserChangedListener(const UserChangedAction &action,
115         EventType event) override;
116     // Notify TIME_CHANGE_EVENT.
117     int NotifyUserChanged() const override;
118 
119     uint32_t GenerateSessionId() override;
120 
121     void DumpCommonInfo(int fd) override;
122 
123     void CloseAutoLaunchConnection(DBType type, const DBProperties &properties) override;
124 
125     int SetPermissionConditionCallback(const PermissionConditionCallback &callback) override;
126 
127     std::map<std::string, std::string> GetPermissionCheckParam(const DBProperties &properties) override;
128 
129     void StopTaskPool() override;
130 
131     void StopTimeTickMonitorIfNeed() override;
132 
133 private:
134     static constexpr int MAX_TP_THREADS = 10;  // max threads of the task pool.
135     static constexpr int MIN_TP_THREADS = 1;   // min threads of the task pool.
136     static constexpr int TASK_POOL_REPORTS_INTERVAL = 10000;   // task pool reports its state every 10 seconds.
137 
138     int PrepareLoop(IEventLoop *&loop);
139     int PrepareTaskPool();
140     int AllocTimerId(IEvent *evTimer, TimerId &timerId);
141 
142     // Context fields
143     mutable std::mutex labelMutex_;
144     std::string processLabel_;
145 
146     // Communicator
147     mutable std::mutex communicatorLock_;
148     IAdapter *adapter_;
149     ICommunicatorAggregator *communicatorAggregator_;
150 
151     // Loop and timer
152     mutable std::mutex loopLock_;
153     IEventLoop *mainLoop_;
154     std::mutex timersLock_;
155     TimerId currentTimerId_;
156     std::map<TimerId, IEvent *> timers_;
157 
158     // Task pool
159     std::mutex taskLock_;
160     TaskPool *taskPool_;
161     TimerId taskPoolReportsTimerId_;
162 
163     // TimeTick
164     mutable std::mutex timeTickMonitorLock_;
165     std::unique_ptr<TimeTickMonitor> timeTickMonitor_;
166 
167     mutable std::shared_mutex permissionCheckCallbackMutex_ {};
168     PermissionCheckCallback permissionCheckCallback_;
169     PermissionCheckCallbackV2 permissionCheckCallbackV2_;
170     PermissionCheckCallbackV3 permissionCheckCallbackV3_;
171 
172     AutoLaunch autoLaunch_;
173 
174     // System api
175     mutable std::recursive_mutex systemApiAdapterLock_;
176     std::shared_ptr<IProcessSystemApiAdapter> systemApiAdapter_;
177     mutable std::mutex lockStatusLock_; // Mutex for lockStatusObserver_.
178     LockStatusObserver *lockStatusObserver_;
179 
180     mutable std::shared_mutex databaseStatusCallbackMutex_ {};
181     StoreStatusNotifier databaseStatusNotifyCallback_;
182 
183     mutable std::shared_mutex syncActivationCheckCallbackMutex_ {};
184     SyncActivationCheckCallback syncActivationCheckCallback_;
185     SyncActivationCheckCallbackV2 syncActivationCheckCallbackV2_;
186 
187     mutable std::mutex userChangeMonitorLock_;
188     std::unique_ptr<UserChangeMonitor> userChangeMonitor_;
189 
190     std::atomic<uint32_t> currentSessionId_;
191 
192     // Get map from this callback, use for run permission check in remote device
193     mutable std::shared_mutex permissionConditionLock_;
194     PermissionConditionCallback permissionConditionCallback_;
195 };
196 } // namespace DistributedDB
197 
198 #endif // RUNTIME_CONTEXT_IMPL_H
199