• 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_H
17 #define RUNTIME_CONTEXT_H
18 
19 #include <cstdint>
20 #include <string>
21 #include <functional>
22 
23 #include "auto_launch.h"
24 #include "auto_launch_export.h"
25 #include "icommunicator_aggregator.h"
26 #include "iprocess_system_api_adapter.h"
27 #include "kv_store_observer.h"
28 #include "kvdb_properties.h"
29 #include "macro_utils.h"
30 #include "notification_chain.h"
31 #include "types_export.h"
32 
33 namespace DistributedDB {
34 using TimerId = uint64_t;
35 using TimerAction = std::function<int(TimerId timerId)>;
36 using TimerFinalizer = std::function<void(void)>;
37 using TaskAction = std::function<void(void)>;
38 using TimeChangedAction = std::function<void(void *)>;
39 using LockStatusNotifier = std::function<void(void *isLocked)>;
40 using UserChangedAction = std::function<void(void *)>;
41 
42 class RuntimeContext {
43 public:
44     DISABLE_COPY_ASSIGN_MOVE(RuntimeContext);
45 
46     // Global setting interfaces.
47     virtual void SetProcessLabel(const std::string &label) = 0;
48     virtual std::string GetProcessLabel() const = 0;
49 
50     // If the pre adapter is not nullptr, set new adapter will release the pre adapter,
51     // must be called after SetCommunicatorAggregator
52     virtual int SetCommunicatorAdapter(IAdapter *adapter) = 0;
53     virtual int GetCommunicatorAggregator(ICommunicatorAggregator *&outAggregator) = 0;
54     virtual void SetCommunicatorAggregator(ICommunicatorAggregator *inAggregator) = 0;
55     virtual int GetLocalIdentity(std::string &outTarget) = 0;
56 
57     // Timer interfaces.
58     virtual int SetTimer(int milliSeconds, const TimerAction &action,
59         const TimerFinalizer &finalizer, TimerId &timerId) = 0;
60     virtual int ModifyTimer(TimerId timerId, int milliSeconds) = 0;
61     virtual void RemoveTimer(TimerId timerId, bool wait = false) = 0;
62 
63     // Task interfaces.
64     virtual int ScheduleTask(const TaskAction &task) = 0;
65     virtual int ScheduleQueuedTask(const std::string &queueTag,
66         const TaskAction &task) = 0;
67 
68     // Shrink as much memory as possible.
69     virtual void ShrinkMemory(const std::string &description) = 0;
70 
71     // Register a time changed lister, it will be callback when local time changed.
72     virtual NotificationChain::Listener *RegisterTimeChangedLister(const TimeChangedAction &action, int &errCode) = 0;
73 
74     // Get the global context object(singleton), never return nullptr.
75     static RuntimeContext *GetInstance();
76 
77     virtual int SetPermissionCheckCallback(const PermissionCheckCallback &callback) = 0;
78 
79     virtual int SetPermissionCheckCallback(const PermissionCheckCallbackV2 &callback) = 0;
80 
81     virtual int RunPermissionCheck(const std::string &userId, const std::string &appId, const std::string &storeId,
82         const std::string &deviceId, uint8_t flag) const = 0;
83 
84     virtual int EnableKvStoreAutoLaunch(const KvDBProperties &properties, AutoLaunchNotifier notifier,
85         const AutoLaunchOption &option) = 0;
86 
87     virtual int DisableKvStoreAutoLaunch(const std::string &normalIdentifier, const std::string &dualTupleIdentifier,
88         const std::string &userId) = 0;
89 
90     virtual void GetAutoLaunchSyncDevices(const std::string &identifier, std::vector<std::string> &devices) const = 0;
91 
92     virtual void SetAutoLaunchRequestCallback(const AutoLaunchRequestCallback &callback, DBType type) = 0;
93 
94     virtual NotificationChain::Listener *RegisterLockStatusLister(const LockStatusNotifier &action, int &errCode) = 0;
95 
96     virtual bool IsAccessControlled() const = 0;
97 
98     virtual int SetSecurityOption(const std::string &filePath, const SecurityOption &option) const = 0;
99 
100     virtual int GetSecurityOption(const std::string &filePath, SecurityOption &option) const = 0;
101 
102     virtual bool CheckDeviceSecurityAbility(const std::string &devId, const SecurityOption &option) const = 0;
103 
104     virtual int SetProcessSystemApiAdapter(const std::shared_ptr<IProcessSystemApiAdapter> &adapter) = 0;
105 
106     virtual bool IsProcessSystemApiAdapterValid() const = 0;
107 
108     virtual bool IsCommunicatorAggregatorValid() const = 0;
109 
110     // Notify TIME_CHANGE_EVENT.
111     virtual void NotifyTimeStampChanged(TimeOffset offset) const = 0;
112 
113     virtual void SetStoreStatusNotifier(const StoreStatusNotifier &notifier) = 0;
114 
115     virtual void NotifyDatabaseStatusChange(const std::string &userId, const std::string &appId,
116         const std::string &storeId, const std::string &deviceId, bool onlineStatus) = 0;
117 
118     virtual int SetSyncActivationCheckCallback(const SyncActivationCheckCallback &callback) = 0;
119 
120     virtual bool IsSyncerNeedActive(std::string &userId, std::string &appId, std::string &storeId) const = 0;
121 
122     virtual NotificationChain::Listener *RegisterUserChangedListerner(const UserChangedAction &action,
123         EventType event) = 0;
124 
125     virtual int NotifyUserChanged() const = 0;
126 protected:
127     RuntimeContext() = default;
~RuntimeContext()128     virtual ~RuntimeContext() {}
129 };
130 } // namespace DistributedDB
131 
132 #endif // RUNTIME_CONTEXT_H
133