• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_APPACCOUNT_APP_ACCOUNT_SUBSCRIBE_MANAGER_H
17 #define OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_APPACCOUNT_APP_ACCOUNT_SUBSCRIBE_MANAGER_H
18 
19 #include <map>
20 #include <set>
21 
22 #include "app_account_data_storage.h"
23 #include "app_account_event_record.h"
24 #include "event_handler.h"
25 #include "singleton.h"
26 
27 namespace OHOS {
28 namespace AccountSA {
29 class AppAccountSubscribeManager : public DelayedSingleton<AppAccountSubscribeManager> {
30 public:
31     using EventHandler = OHOS::AppExecFwk::EventHandler;
32     using EventRunner = OHOS::AppExecFwk::EventRunner;
33     using Callback = OHOS::AppExecFwk::InnerEvent::Callback;
34 
35     AppAccountSubscribeManager();
36     virtual ~AppAccountSubscribeManager() = default;
37 
38     ErrCode SubscribeAppAccount(const std::shared_ptr<AppAccountSubscribeInfo> &subscribeInfoPtr,
39         const sptr<IRemoteObject> &eventListener, const uid_t &uid,
40         const std::string &bundleName, const uint32_t &appIndex);
41     ErrCode UnsubscribeAppAccount(const sptr<IRemoteObject> &eventListener);
42 
43     bool PublishAccount(AppAccountInfo &appAccountInfo, const uid_t &uid, const std::string &bundleName);
44 
45 private:
46     std::shared_ptr<AppAccountDataStorage> GetDataStorage(const uid_t &uid, const bool &autoSync = false);
47     ErrCode GetStoreId(const uid_t &uid, std::string &storeId);
48     ErrCode GetEventHandler(void);
49 
50     std::vector<AppAccountSubscribeRecordPtr> GetSubscribeRecords(const std::string &owner);
51     ErrCode OnAccountsChanged(const std::shared_ptr<AppAccountEventRecord> &record);
52     ErrCode GetAccessibleAccountsBySubscribeInfo(const std::shared_ptr<AppAccountSubscribeInfo> &subscribeInfoPtr,
53         const std::vector<AppAccountInfo> &accessibleAccounts, std::vector<AppAccountInfo> &appAccounts);
54 
55     ErrCode CheckAppAccess(const std::shared_ptr<AppAccountSubscribeInfo> &subscribeInfoPtr, const uid_t &uid,
56         const std::string &bundleName, const uint32_t &appIndex);
57 
58     ErrCode InsertSubscribeRecord(
59         const std::vector<std::string> &owners, const AppAccountSubscribeRecordPtr &subscribeRecordPtr);
60     ErrCode RemoveSubscribeRecord(const sptr<IRemoteObject> &eventListener);
61 
62 private:
63     std::shared_ptr<EventHandler> handler_ = nullptr;
64     std::mutex mutex_;
65     sptr<IRemoteObject::DeathRecipient> subscribeDeathRecipient_;
66     std::mutex subscribeRecordMutex_;
67     std::map<std::string, std::multiset<AppAccountSubscribeRecordPtr>> ownerSubscribeRecords_;
68     std::vector<AppAccountSubscribeRecordPtr> subscribeRecords_;
69 };
70 }  // namespace AccountSA
71 }  // namespace OHOS
72 
73 #endif  // OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_APPACCOUNT_APP_ACCOUNT_SUBSCRIBE_MANAGER_H
74