• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 ACCOUNT_H
17 #define ACCOUNT_H
18 #include <memory>
19 #include <vector>
20 
21 #include "cxx.h"
22 #include "errors.h"
23 #include "ohos_account_kits.h"
24 #include "os_account_info.h"
25 #include "os_account_manager.h"
26 #include "os_account_subscribe_info.h"
27 #include "os_account_subscriber.h"
28 #include "refbase.h"
29 
30 namespace OHOS::Request {
31 using namespace OHOS::AccountSA;
32 
33 struct TaskManagerTx;
34 class SubscriberWrapper : public OsAccountSubscriber {
35 public:
36     explicit SubscriberWrapper(OS_ACCOUNT_SUBSCRIBE_TYPE type, rust::box<TaskManagerTx> task_manager,
37         rust::fn<void(const int &id, const TaskManagerTx &task_manager)> on_accounts_changed,
38         rust::fn<void(const int &newId, const int &oldId, const TaskManagerTx &task_manager)> on_accounts_switch);
39 
40     ~SubscriberWrapper();
41 
42     virtual void OnAccountsChanged(const int &id) override;
43     virtual void OnAccountsSwitch(const int &newId, const int &oldId) override;
44 
45 private:
46     TaskManagerTx *task_manager_;
47     rust::fn<void(const int &id, const TaskManagerTx &task_manager)> on_accounts_changed_;
48     rust::fn<void(const int &newId, const int &oldId, const TaskManagerTx &task_manager)> on_accounts_switch_;
49 };
50 
51 int RegistryAccountSubscriber(OS_ACCOUNT_SUBSCRIBE_TYPE type, rust::box<TaskManagerTx> task_manager,
52     rust::fn<void(const int &id, const TaskManagerTx &task_manager)> on_accounts_changed,
53     rust::fn<void(const int &newId, const int &oldId, const TaskManagerTx &task_manager)> on_accounts_switch);
54 
GetForegroundOsAccount(int & account)55 inline ErrCode GetForegroundOsAccount(int &account)
56 {
57     return OsAccountManager::GetForegroundOsAccountLocalId(account);
58 }
59 
GetBackgroundOsAccounts(rust::vec<int> & accounts)60 inline ErrCode GetBackgroundOsAccounts(rust::vec<int> &accounts)
61 {
62     auto v = std::vector<int32_t>();
63     auto ret = OsAccountManager::GetBackgroundOsAccountLocalIds(v);
64     if (ret == 0) {
65         for (auto &account : v) {
66             accounts.push_back(account);
67         };
68     }
69     return ret;
70 }
71 
GetOsAccountLocalIdFromUid(const int uid,int & id)72 inline ErrCode GetOsAccountLocalIdFromUid(const int uid, int &id)
73 {
74     return OsAccountManager::GetOsAccountLocalIdFromUid(uid, id);
75 }
76 
77 rust::String GetOhosAccountUid();
78 } // namespace OHOS::Request
79 #endif // ACCOUNT_H