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 OHOS_ABILITY_RUNTIME_DATAOBS_MGR_SERVICE_H 17 #define OHOS_ABILITY_RUNTIME_DATAOBS_MGR_SERVICE_H 18 19 #include <memory> 20 #include <singleton.h> 21 #include <thread_ex.h> 22 #include <unordered_map> 23 #include "cpp/mutex.h" 24 25 #include "dataobs_mgr_inner.h" 26 #include "dataobs_mgr_inner_common.h" 27 #include "dataobs_mgr_inner_ext.h" 28 #include "dataobs_mgr_inner_pref.h" 29 #include "dataobs_mgr_stub.h" 30 #include "iremote_object.h" 31 #include "system_ability.h" 32 #include "task_handler_wrap.h" 33 #include "uri.h" 34 35 namespace OHOS { 36 namespace AAFwk { 37 enum class DataObsServiceRunningState { STATE_NOT_START, STATE_RUNNING }; 38 constexpr char SHARE_PREFERENCES[] = "sharepreferences"; 39 /** 40 * @class DataObsMgrService 41 * DataObsMgrService provides a facility for dataobserver. 42 */ 43 class DataObsMgrService : public SystemAbility, 44 public DataObsManagerStub, 45 public std::enable_shared_from_this<DataObsMgrService> { 46 DECLARE_DELAYED_SINGLETON(DataObsMgrService) 47 DECLEAR_SYSTEM_ABILITY(DataObsMgrService) 48 public: 49 void OnStart() override; 50 void OnStop() override; 51 DataObsServiceRunningState QueryServiceState() const; 52 53 std::pair<bool, struct ObserverNode> ConstructObserverNode(sptr<IDataAbilityObserver> dataObserver, 54 int32_t userId, uint32_t tokenId); 55 virtual int RegisterObserver(const Uri &uri, 56 sptr<IDataAbilityObserver> dataObserver, int32_t userId = DATAOBS_DEFAULT_CURRENT_USER, 57 DataObsOption opt = DataObsOption()) override; 58 virtual int RegisterObserverFromExtension(const Uri &uri, 59 sptr<IDataAbilityObserver> dataObserver, int32_t userId = DATAOBS_DEFAULT_CURRENT_USER, 60 DataObsOption opt = DataObsOption()) override; 61 virtual int UnregisterObserver(const Uri &uri, 62 sptr<IDataAbilityObserver> dataObserver, int32_t userId = DATAOBS_DEFAULT_CURRENT_USER, 63 DataObsOption opt = DataObsOption()) override; 64 virtual int NotifyChange(const Uri &uri, int32_t userId = DATAOBS_DEFAULT_CURRENT_USER, 65 DataObsOption opt = DataObsOption()) override; 66 virtual int NotifyChangeFromExtension(const Uri &uri, int32_t userId = DATAOBS_DEFAULT_CURRENT_USER, 67 DataObsOption opt = DataObsOption()) override; 68 virtual int CheckTrusts(uint32_t consumerToken, uint32_t providerToken) override; 69 virtual Status RegisterObserverExt(const Uri &uri, sptr<IDataAbilityObserver> dataObserver, 70 bool isDescendants, DataObsOption opt = DataObsOption()) override; 71 virtual Status UnregisterObserverExt(const Uri &uri, sptr<IDataAbilityObserver> dataObserver, 72 DataObsOption opt = DataObsOption()) override; 73 virtual Status UnregisterObserverExt(sptr<IDataAbilityObserver> dataObserver, 74 DataObsOption opt = DataObsOption()) override; 75 virtual Status NotifyChangeExt(const ChangeInfo &changeInfo, DataObsOption opt = DataObsOption()) override; 76 virtual Status NotifyProcessObserver(const std::string &key, const sptr<IRemoteObject> &observer, 77 DataObsOption opt = DataObsOption()) override; 78 79 /** 80 * @brief DataObs hidumper. 81 * @param fd Indicates the fd. 82 * @param args Indicates the params. 83 * @return Returns the dump result. 84 */ 85 int Dump(int fd, const std::vector<std::u16string>& args) override; 86 87 private: 88 bool Init(); 89 void Dump(const std::vector<std::u16string>& args, std::string& result) const; 90 void ShowHelp(std::string& result) const; 91 Status DeepCopyChangeInfo(const ChangeInfo &src, ChangeInfo &dst) const; 92 void GetFocusedAppInfo(int32_t &windowId, sptr<IRemoteObject> &abilityToken) const; 93 sptr<IRemoteObject> GetAbilityManagerService() const; 94 static int32_t GetCallingUserId(uint32_t tokenId); 95 static bool IsSystemApp(uint32_t tokenId, uint64_t fullTokenId); 96 static bool IsCallingPermissionValid(DataObsOption &opt, int32_t userId, int32_t callingUserId); 97 static bool IsCallingPermissionValid(DataObsOption &opt); 98 static int32_t GetDataMgrServiceUid(); 99 static bool IsDataMgrService(uint32_t tokenId, int32_t uid); 100 int32_t RegisterObserverInner(const Uri &uri, sptr<IDataAbilityObserver> dataObserver, int32_t userId, 101 DataObsOption opt, bool isExtension); 102 int32_t VerifyDataSharePermission(Uri &uri, bool isRead, ObserverInfo &info); 103 int32_t VerifyDataSharePermissionInner(Uri &uri, bool isRead, ObserverInfo &info); 104 int32_t NotifyChangeInner(Uri &uri, int32_t userId, 105 DataObsOption opt, bool isExtension); 106 private: 107 static constexpr std::uint32_t TASK_COUNT_MAX = 50; 108 ffrt::mutex taskCountMutex_; 109 std::uint32_t taskCount_ = 0; 110 std::shared_ptr<TaskHandlerWrap> handler_; 111 112 DataObsServiceRunningState state_; 113 114 std::shared_ptr<DataObsMgrInner> dataObsMgrInner_; 115 std::shared_ptr<DataObsMgrInnerExt> dataObsMgrInnerExt_; 116 std::shared_ptr<DataObsMgrInnerPref> dataObsMgrInnerPref_; 117 }; 118 } // namespace AAFwk 119 } // namespace OHOS 120 #endif // OHOS_ABILITY_RUNTIME_DATAOBS_MGR_SERVICE_H 121