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 <mutex> 20 #include <map> 21 #include <shared_mutex> 22 23 #include "runtime_context.h" 24 #include "task_pool.h" 25 #include "evloop/include/ievent.h" 26 #include "evloop/include/ievent_loop.h" 27 #include "lock_status_observer.h" 28 #include "time_tick_monitor.h" 29 #include "icommunicator_aggregator.h" 30 #include "auto_launch.h" 31 #include "user_change_monitor.h" 32 33 namespace DistributedDB { 34 class RuntimeContextImpl final : public RuntimeContext { 35 public: 36 RuntimeContextImpl(); 37 ~RuntimeContextImpl() override; 38 39 // Get/Set the label of this process. 40 void SetProcessLabel(const std::string &label) override; 41 std::string GetProcessLabel() const override; 42 int SetCommunicatorAdapter(IAdapter *adapter) override; 43 int GetCommunicatorAggregator(ICommunicatorAggregator *&outAggregator) override; 44 void SetCommunicatorAggregator(ICommunicatorAggregator *inAggregator) override; 45 int GetLocalIdentity(std::string &outTarget) override; 46 // Add and start a timer. 47 int SetTimer(int milliSeconds, const TimerAction &action, 48 const TimerFinalizer &finalizer, TimerId &timerId) override; 49 50 // Modify the interval of the timer. 51 int ModifyTimer(TimerId timerId, int milliSeconds) override; 52 53 // Remove the timer. 54 void RemoveTimer(TimerId timerId, bool wait) override; 55 56 // Task interfaces. 57 int ScheduleTask(const TaskAction &task) override; 58 int ScheduleQueuedTask(const std::string &queueTag, const TaskAction &task) override; 59 60 // Shrink as much memory as possible. 61 void ShrinkMemory(const std::string &description) override; 62 63 // Register a time changed lister, it will be callback when local time changed. 64 NotificationChain::Listener *RegisterTimeChangedLister(const TimeChangedAction &action, int &errCode) override; 65 66 int SetPermissionCheckCallback(const PermissionCheckCallback &callback) override; 67 68 int SetPermissionCheckCallback(const PermissionCheckCallbackV2 &callback) override; 69 70 int RunPermissionCheck(const std::string &userId, const std::string &appId, const std::string &storeId, 71 const std::string &deviceId, 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 ¬ifier) 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 bool IsSyncerNeedActive(std::string &userId, std::string &appId, std::string &storeId) const override; 110 111 // Register a user changed lister, it will be callback when user change. 112 NotificationChain::Listener *RegisterUserChangedListerner(const UserChangedAction &action, 113 EventType event) override; 114 // Notify TIME_CHANGE_EVENT. 115 int NotifyUserChanged() const override; 116 private: 117 static constexpr int MAX_TP_THREADS = 10; // max threads of the task pool. 118 static constexpr int MIN_TP_THREADS = 1; // min threads of the task pool. 119 static constexpr int TASK_POOL_REPORTS_INTERVAL = 10000; // task pool reports its state every 10 seconds. 120 121 int PrepareLoop(IEventLoop *&loop); 122 int PrepareTaskPool(); 123 int AllocTimerId(IEvent *evTimer, TimerId &timerId); 124 125 // Context fields 126 mutable std::mutex labelMutex_; 127 std::string processLabel_; 128 129 // Communicator 130 mutable std::mutex communicatorLock_; 131 IAdapter *adapter_; 132 ICommunicatorAggregator *communicatorAggregator_; 133 134 // Loop and timer 135 mutable std::mutex loopLock_; 136 IEventLoop *mainLoop_; 137 std::mutex timersLock_; 138 TimerId currentTimerId_; 139 std::map<TimerId, IEvent *> timers_; 140 141 // Task pool 142 std::mutex taskLock_; 143 TaskPool *taskPool_; 144 TimerId taskPoolReportsTimerId_; 145 146 // TimeTick 147 mutable std::mutex timeTickMonitorLock_; 148 std::unique_ptr<TimeTickMonitor> timeTickMonitor_; 149 150 mutable std::shared_mutex permissionCheckCallbackMutex_{}; 151 PermissionCheckCallback permissionCheckCallback_; 152 PermissionCheckCallbackV2 permissionCheckCallbackV2_; 153 154 AutoLaunch autoLaunch_; 155 156 // System api 157 mutable std::recursive_mutex systemApiAdapterLock_; 158 std::shared_ptr<IProcessSystemApiAdapter> systemApiAdapter_; 159 mutable std::mutex lockStatusLock_; // Mutex for lockStatusObserver_. 160 LockStatusObserver *lockStatusObserver_; 161 162 mutable std::shared_mutex databaseStatusCallbackMutex_{}; 163 StoreStatusNotifier databaseStatusNotifyCallback_; 164 165 mutable std::shared_mutex syncActivationCheckCallbackMutex_{}; 166 SyncActivationCheckCallback syncActivationCheckCallback_; 167 168 mutable std::mutex userChangeMonitorLock_; 169 std::unique_ptr<UserChangeMonitor> userChangeMonitor_; 170 }; 171 } // namespace DistributedDB 172 173 #endif // RUNTIME_CONTEXT_IMPL_H 174