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_CONTROL_MANAGER_H 17 #define OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_APPACCOUNT_APP_ACCOUNT_CONTROL_MANAGER_H 18 19 #include "app_account_app_state_observer.h" 20 #include "app_mgr_interface.h" 21 #include "app_mgr_proxy.h" 22 #include "app_account_authenticator_manager.h" 23 #include "app_account_data_storage.h" 24 #include "iapp_account_authenticator_callback.h" 25 #include "iremote_object.h" 26 #include "singleton.h" 27 #include "want_params.h" 28 29 namespace OHOS { 30 namespace AccountSA { 31 namespace { 32 struct AssociatedDataCacheItem { 33 std::map<std::string, std::string> data; 34 std::string name; 35 int32_t freq; 36 }; 37 } 38 39 class AppAccountControlManager : public DelayedSingleton<AppAccountControlManager> { 40 public: 41 AppAccountControlManager(); 42 virtual ~AppAccountControlManager() = default; 43 44 ErrCode AddAccount(const std::string &name, const std::string &extraInfo, const uid_t &uid, 45 const std::string &bundleName, AppAccountInfo &appAccountInfo); 46 ErrCode CreateAccount(const std::string &name, const CreateAccountOptions &options, const uid_t &uid, 47 const std::string &bundleName, AppAccountInfo &appAccountInfo); 48 ErrCode DeleteAccount( 49 const std::string &name, const uid_t &uid, const std::string &bundleName, AppAccountInfo &appAccountInfo); 50 51 ErrCode GetAccountExtraInfo(const std::string &name, std::string &extraInfo, 52 const uid_t &uid, const std::string &bundleName, const uint32_t &appIndex); 53 ErrCode SetAccountExtraInfo(const std::string &name, const std::string &extraInfo, const uid_t &uid, 54 const std::string &bundleName, AppAccountInfo &appAccountInfo); 55 56 ErrCode EnableAppAccess(const std::string &name, const std::string &authorizedApp, 57 AppAccountCallingInfo &appAccountCallingInfo, AppAccountInfo &appAccountInfo, 58 const uint32_t apiVersion = Constants::API_VERSION7); 59 ErrCode DisableAppAccess(const std::string &name, const std::string &authorizedApp, 60 AppAccountCallingInfo &appAccountCallingInfo, AppAccountInfo &appAccountInfo, 61 const uint32_t apiVersion = Constants::API_VERSION7); 62 ErrCode CheckAppAccess(const std::string &name, const std::string &authorizedApp, bool &isAccessible, 63 const AppAccountCallingInfo &appAccountCallingInfo); 64 65 ErrCode CheckAppAccountSyncEnable(const std::string &name, bool &syncEnable, 66 const uid_t &uid, const std::string &bundleName, const uint32_t &appIndex); 67 ErrCode SetAppAccountSyncEnable(const std::string &name, const bool &syncEnable, const uid_t &uid, 68 const std::string &bundleName, AppAccountInfo &appAccountInfo); 69 ErrCode GetAssociatedData(const std::string &name, const std::string &key, 70 std::string &value, const uid_t &uid); 71 ErrCode SetAssociatedData(const std::string &name, const std::string &key, const std::string &value, 72 const AppAccountCallingInfo &appAccountCallingInfo); 73 74 ErrCode GetAccountCredential(const std::string &name, const std::string &credentialType, std::string &credential, 75 const AppAccountCallingInfo &appAccountCallingInfo); 76 ErrCode SetAccountCredential(const std::string &name, const std::string &credentialType, 77 const std::string &credential, const AppAccountCallingInfo &appAccountCallingInfo, bool isDelete = false); 78 79 ErrCode GetOAuthToken(const AuthenticatorSessionRequest &request, 80 std::string &token, const uint32_t apiVersion = Constants::API_VERSION8); 81 ErrCode SetOAuthToken(const AuthenticatorSessionRequest &request); 82 ErrCode DeleteOAuthToken( 83 const AuthenticatorSessionRequest &request, const uint32_t apiVersion = Constants::API_VERSION8); 84 ErrCode SetOAuthTokenVisibility( 85 const AuthenticatorSessionRequest &request, const uint32_t apiVersion = Constants::API_VERSION8); 86 ErrCode CheckOAuthTokenVisibility(const AuthenticatorSessionRequest &request, 87 bool &isVisible, const uint32_t apiVersion = Constants::API_VERSION8); 88 ErrCode GetAllOAuthTokens(const AuthenticatorSessionRequest &request, std::vector<OAuthTokenInfo> &tokenInfos); 89 ErrCode GetOAuthList(const AuthenticatorSessionRequest &request, 90 std::set<std::string> &oauthList, const uint32_t apiVersion = Constants::API_VERSION8); 91 92 ErrCode GetAllAccounts(const std::string &owner, std::vector<AppAccountInfo> &appAccounts, const uid_t &uid, 93 const std::string &bundleName, const uint32_t &appIndex); 94 ErrCode GetAllAccessibleAccounts(std::vector<AppAccountInfo> &appAccounts, 95 const uid_t &uid, const std::string &bundleName, const uint32_t &appIndex); 96 97 ErrCode SelectAccountsByOptions( 98 const SelectAccountsOptions &options, const sptr<IAppAccountAuthenticatorCallback> &callback, 99 const uid_t &uid, const std::string &bundleName, const uint32_t &appIndex); 100 101 ErrCode OnPackageRemoved(const uid_t &uid, const std::string &bundleName, const uint32_t &appIndex); 102 ErrCode OnUserRemoved(int32_t userId); 103 104 ErrCode GetAllAccountsFromDataStorage(const std::string &owner, std::vector<AppAccountInfo> &appAccounts, 105 const std::string &bundleName, const std::shared_ptr<AppAccountDataStorage> &dataStoragePtr); 106 ErrCode GetAllAccessibleAccountsFromDataStorage(std::vector<AppAccountInfo> &appAccounts, 107 const std::string &bundleName, const std::shared_ptr<AppAccountDataStorage> &dataStoragePtr, 108 const uint32_t &appIndex); 109 std::shared_ptr<AppAccountDataStorage> GetDataStorage(const uid_t &uid, const bool &autoSync = false); 110 111 void OnAbilityStateChanged(const AppExecFwk::AbilityStateData &abilityStateData); 112 113 private: 114 bool RegisterApplicationStateObserver(); 115 void UnregisterApplicationStateObserver(); 116 void PopDataFromAssociatedDataCache(); 117 void RemoveAssociatedDataCacheByUid(const uid_t &uid); 118 void RemoveAssociatedDataCacheByAccount(const uid_t &uid, const std::string &name); 119 ErrCode GetAssociatedDataFromStorage(const std::string &name, const std::string &key, std::string &value, 120 const uid_t &uid, const uint32_t &appIndex); 121 std::shared_ptr<AppAccountDataStorage> GetDataStorageByUserId(int32_t userId, const bool &autoSync = false); 122 bool NeedSyncDataStorage(const AppAccountInfo &appAccountInfo); 123 ErrCode GetAccountInfoFromDataStorage( 124 AppAccountInfo &appAccountInfo, std::shared_ptr<AppAccountDataStorage> &dataStoragePtr); 125 ErrCode AddAccountInfoIntoDataStorage(AppAccountInfo &appAccountInfo, 126 const std::shared_ptr<AppAccountDataStorage> &dataStoragePtr, const uid_t &uid); 127 ErrCode SaveAccountInfoIntoDataStorage(AppAccountInfo &appAccountInfo, 128 const std::shared_ptr<AppAccountDataStorage> &dataStoragePtr, const uid_t &uid); 129 ErrCode DeleteAccountInfoFromDataStorage( 130 AppAccountInfo &appAccountInfo, std::shared_ptr<AppAccountDataStorage> &dataStoragePtr, const uid_t &uid); 131 132 ErrCode SaveAuthorizedAccount(const std::string &authorizedApp, AppAccountInfo &appAccountInfo, 133 const std::shared_ptr<AppAccountDataStorage> &dataStoragePtr, const uid_t &uid); 134 ErrCode RemoveAuthorizedAccount(const std::string &authorizedApp, AppAccountInfo &appAccountInfo, 135 const std::shared_ptr<AppAccountDataStorage> &dataStoragePtr, const uid_t &uid); 136 ErrCode SaveAuthorizedAccountIntoDataStorage(const std::string &authorizedApp, AppAccountInfo &appAccountInfo, 137 const std::shared_ptr<AppAccountDataStorage> &dataStoragePtr); 138 ErrCode RemoveAuthorizedAccountFromDataStorage(const std::string &authorizedApp, AppAccountInfo &appAccountInfo, 139 const std::shared_ptr<AppAccountDataStorage> &dataStoragePtr); 140 141 private: 142 std::mutex mutex_; 143 std::mutex storePtrMutex_; 144 std::mutex associatedDataMutex_; 145 std::map<uid_t, AssociatedDataCacheItem> associatedDataCache_; 146 std::map<std::string, std::shared_ptr<AppAccountDataStorage>> storePtrMap_; 147 sptr<AppExecFwk::IAppMgr> iAppMgr_; 148 sptr<AppAccountAppStateObserver> appStateObserver_; 149 std::size_t ACCOUNT_MAX_SIZE = 1000; 150 std::size_t ASSOCIATED_DATA_CACHE_MAX_SIZE = 5; 151 }; 152 } // namespace AccountSA 153 } // namespace OHOS 154 155 #endif // OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_APPACCOUNT_APP_ACCOUNT_CONTROL_MANAGER_H 156