/* * Copyright (c) 2021-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "ability_scheduler_proxy.h" #include "ability_manager_errors.h" #include "data_ability_observer_interface.h" #include "data_ability_operation.h" #include "data_ability_predicates.h" #include "data_ability_result.h" #include "hilog_tag_wrapper.h" #include "hitrace_meter.h" #include "ishared_result_set.h" #include "session_info.h" #include "values_bucket.h" namespace OHOS { namespace AAFwk { namespace { const int64_t SCHEDULE_IPC_LOG_TIME = 10000; } bool AbilitySchedulerProxy::WriteInterfaceToken(MessageParcel &data) { if (!data.WriteInterfaceToken(AbilitySchedulerProxy::GetDescriptor())) { TAG_LOGE(AAFwkTag::ABILITYMGR, "write interface token failed"); return false; } return true; } bool AbilitySchedulerProxy::ScheduleAbilityTransaction(const Want &want, const LifeCycleStateInfo &stateInfo, sptr sessionInfo) { HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); TAG_LOGD(AAFwkTag::ABILITYMGR, "begin"); auto start = std::chrono::system_clock::now(); MessageParcel data; MessageParcel reply; MessageOption option(MessageOption::TF_ASYNC); if (!WriteInterfaceToken(data)) { return false; } if (!data.WriteParcelable(&want)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "write want failed"); return false; } data.WriteParcelable(&stateInfo); if (sessionInfo) { SessionInfo tmpInfo = *sessionInfo; tmpInfo.want = Want(); if (!data.WriteBool(true) || !data.WriteParcelable(&tmpInfo)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "write sessionInfo failed"); return false; } } else { if (!data.WriteBool(false)) { return false; } } int32_t err = SendTransactCmd(IAbilityScheduler::SCHEDULE_ABILITY_TRANSACTION, data, reply, option); if (err != NO_ERROR) { TAG_LOGE(AAFwkTag::ABILITYMGR, "ScheduleAbilityTransaction fail to SendRequest. err: %{public}d", err); return false; } int64_t cost = std::chrono::duration_cast( std::chrono::system_clock::now() - start).count(); if (cost > SCHEDULE_IPC_LOG_TIME) { TAG_LOGI(AAFwkTag::ABILITYMGR, "ScheduleAbilityTransaction proxy cost %{public}" PRId64 "mirco seconds," " data size: %{public}zu", cost, data.GetWritePosition()); } else { TAG_LOGD(AAFwkTag::ABILITYMGR, "ScheduleAbilityTransaction proxy cost %{public}" PRId64 "mirco seconds," " data size: %{public}zu", cost, data.GetWritePosition()); } return true; } void AbilitySchedulerProxy::ScheduleShareData(const int32_t &uniqueId) { MessageParcel data; MessageParcel reply; MessageOption option(MessageOption::TF_ASYNC); if (!WriteInterfaceToken(data)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "write interface token failed."); return; } if (!data.WriteInt32(uniqueId)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "uniqueId write failed."); return; } int32_t err = SendTransactCmd(IAbilityScheduler::SCHEDULE_SHARE_DATA, data, reply, option); if (err != NO_ERROR) { TAG_LOGE(AAFwkTag::ABILITYMGR, "ScheduleShareData fail to SendRequest, err: %{public}d.", err); } return; } void AbilitySchedulerProxy::SendResult(int requestCode, int resultCode, const Want &resultWant) { MessageParcel data; MessageParcel reply; MessageOption option(MessageOption::TF_ASYNC); if (!WriteInterfaceToken(data)) { return; } data.WriteInt32(requestCode); data.WriteInt32(resultCode); if (!data.WriteParcelable(&resultWant)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteParcelable"); return; } int32_t err = SendTransactCmd(IAbilityScheduler::SEND_RESULT, data, reply, option); if (err != NO_ERROR) { TAG_LOGE(AAFwkTag::ABILITYMGR, "SendResult fail to SendRequest. err: %{public}d", err); } } void AbilitySchedulerProxy::ScheduleConnectAbility(const Want &want) { MessageParcel data; MessageParcel reply; MessageOption option(MessageOption::TF_ASYNC); if (!WriteInterfaceToken(data)) { return; } if (!data.WriteParcelable(&want)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteParcelable"); return; } int32_t err = SendTransactCmd(IAbilityScheduler::SCHEDULE_ABILITY_CONNECT, data, reply, option); if (err != NO_ERROR) { TAG_LOGE(AAFwkTag::ABILITYMGR, "ScheduleConnectAbility fail to SendRequest. err: %{public}d", err); } } void AbilitySchedulerProxy::ScheduleDisconnectAbility(const Want &want) { MessageParcel data; MessageParcel reply; MessageOption option(MessageOption::TF_ASYNC); if (!WriteInterfaceToken(data)) { return; } if (!data.WriteParcelable(&want)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteParcelable."); return; } int32_t err = SendTransactCmd(IAbilityScheduler::SCHEDULE_ABILITY_DISCONNECT, data, reply, option); if (err != NO_ERROR) { TAG_LOGE(AAFwkTag::ABILITYMGR, "ScheduleDisconnectAbility fail to SendRequest. err: %{public}d.", err); } } void AbilitySchedulerProxy::ScheduleCommandAbility(const Want &want, bool restart, int startId) { MessageParcel data; MessageParcel reply; MessageOption option(MessageOption::TF_ASYNC); if (!WriteInterfaceToken(data)) { return; } if (!data.WriteParcelable(&want)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "WriteParcelable failed"); return; } if (!data.WriteBool(restart)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "WriteBool failed"); return; } TAG_LOGD(AAFwkTag::ABILITYMGR, "WriteInt32,startId:%{public}d", startId); if (!data.WriteInt32(startId)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteInt32"); return; } int32_t err = SendTransactCmd(IAbilityScheduler::SCHEDULE_ABILITY_COMMAND, data, reply, option); if (err != NO_ERROR) { TAG_LOGE(AAFwkTag::ABILITYMGR, "ScheduleCommandAbility fail to SendRequest. err: %{public}d", err); } } bool AbilitySchedulerProxy::SchedulePrepareTerminateAbility() { MessageParcel data; MessageParcel reply; MessageOption option(MessageOption::TF_SYNC); if (!WriteInterfaceToken(data)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to write interface."); return false; } int32_t err = SendTransactCmd(IAbilityScheduler::SCHEDULE_ABILITY_PREPARE_TERMINATE, data, reply, option); if (err != NO_ERROR) { TAG_LOGE(AAFwkTag::ABILITYMGR, "end failed. err: %{public}d", err); return false; } return reply.ReadBool(); } void AbilitySchedulerProxy::ScheduleCommandAbilityWindow(const Want &want, const sptr &sessionInfo, WindowCommand winCmd) { MessageParcel data; MessageParcel reply; MessageOption option(MessageOption::TF_ASYNC); if (!WriteInterfaceToken(data)) { return; } if (!data.WriteParcelable(&want)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "WriteParcelable failed."); return; } if (!data.WriteParcelable(sessionInfo)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "WriteParcelable failed."); return; } if (!data.WriteInt32(winCmd)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteInt32"); return; } int32_t err = SendTransactCmd(IAbilityScheduler::SCHEDULE_ABILITY_COMMAND_WINDOW, data, reply, option); if (err != NO_ERROR) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to SendRequest. err: %{public}d", err); } } void AbilitySchedulerProxy::ScheduleSaveAbilityState() { MessageParcel data; MessageParcel reply; MessageOption option(MessageOption::TF_ASYNC); if (!WriteInterfaceToken(data)) { return; } int32_t err = SendTransactCmd(IAbilityScheduler::SCHEDULE_SAVE_ABILITY_STATE, data, reply, option); if (err != NO_ERROR) { TAG_LOGE(AAFwkTag::ABILITYMGR, "ScheduleSaveAbilityState fail to SendRequest. err: %{public}d", err); } } void AbilitySchedulerProxy::ScheduleRestoreAbilityState(const PacMap &inState) { MessageParcel data; MessageParcel reply; MessageOption option(MessageOption::TF_ASYNC); if (!WriteInterfaceToken(data)) { return; } if (!data.WriteParcelable(&inState)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "WriteParcelable error"); return; } int32_t err = SendTransactCmd(IAbilityScheduler::SCHEDULE_RESTORE_ABILITY_STATE, data, reply, option); if (err != NO_ERROR) { TAG_LOGE(AAFwkTag::ABILITYMGR, "ScheduleRestoreAbilityState fail to SendRequest. err: %{public}d", err); } } /** * @brief Obtains the MIME types of files supported. * * @param uri Indicates the path of the files to obtain. * @param mimeTypeFilter Indicates the MIME types of the files to obtain. This parameter cannot be null. * * @return Returns the matched MIME types. If there is no match, null is returned. */ std::vector AbilitySchedulerProxy::GetFileTypes(const Uri &uri, const std::string &mimeTypeFilter) { std::vector types; MessageParcel data; MessageParcel reply; MessageOption option; if (!WriteInterfaceToken(data)) { return types; } if (!data.WriteParcelable(&uri)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteParcelable uri"); return types; } if (!data.WriteString(mimeTypeFilter)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteString mimeTypeFilter"); return types; } int32_t err = SendTransactCmd(IAbilityScheduler::SCHEDULE_GETFILETYPES, data, reply, option); if (err != NO_ERROR) { TAG_LOGE(AAFwkTag::ABILITYMGR, "GetFileTypes fail to SendRequest. err: %{public}d", err); } if (!reply.ReadStringVector(&types)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to ReadStringVector types"); } return types; } /** * @brief Opens a file in a specified remote path. * * @param uri Indicates the path of the file to open. * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file, * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing data, * or "rwt" for read and write access that truncates any existing file. * * @return Returns the file descriptor. */ int AbilitySchedulerProxy::OpenFile(const Uri &uri, const std::string &mode) { int fd = -1; MessageParcel data; MessageParcel reply; MessageOption option; if (!WriteInterfaceToken(data)) { return fd; } if (!data.WriteParcelable(&uri)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteParcelable uri"); return fd; } if (!data.WriteString(mode)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteString mode"); return fd; } int32_t err = SendTransactCmd(IAbilityScheduler::SCHEDULE_OPENFILE, data, reply, option); if (err != NO_ERROR) { TAG_LOGE(AAFwkTag::ABILITYMGR, "OpenFile fail to SendRequest. err: %{public}d", err); return fd; } fd = reply.ReadFileDescriptor(); if (fd == -1) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to ReadInt32 fd"); return fd; } return fd; } /** * @brief This is like openFile, open a file that need to be able to return sub-sections of files,often assets * inside of their .hap. * * @param uri Indicates the path of the file to open. * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file, * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing * data, or "rwt" for read and write access that truncates any existing file. * * @return Returns the RawFileDescriptor object containing file descriptor. */ int AbilitySchedulerProxy::OpenRawFile(const Uri &uri, const std::string &mode) { int fd = -1; MessageParcel data; MessageParcel reply; MessageOption option; if (!WriteInterfaceToken(data)) { return fd; } if (!data.WriteParcelable(&uri)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteParcelable uri"); return fd; } if (!data.WriteString(mode)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteString mode"); return fd; } int32_t err = SendTransactCmd(IAbilityScheduler::SCHEDULE_OPENRAWFILE, data, reply, option); if (err != NO_ERROR) { TAG_LOGE(AAFwkTag::ABILITYMGR, "OpenFile fail to SendRequest. err: %{public}d", err); return fd; } if (!reply.ReadInt32(fd)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to ReadInt32 fd"); return fd; } return fd; } /** * @brief Inserts a single data record into the database. * * @param uri Indicates the path of the data to operate. * @param value Indicates the data record to insert. If this parameter is null, a blank row will be inserted. * * @return Returns the index of the inserted data record. */ int AbilitySchedulerProxy::Insert(const Uri &uri, const NativeRdb::ValuesBucket &value) { int index = -1; MessageParcel data; MessageParcel reply; MessageOption option; if (!WriteInterfaceToken(data)) { return index; } if (!data.WriteParcelable(&uri)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteParcelable uri"); return index; } if (!value.Marshalling(data)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteParcelable value"); return index; } int32_t err = SendTransactCmd(IAbilityScheduler::SCHEDULE_INSERT, data, reply, option); if (err != NO_ERROR) { TAG_LOGE(AAFwkTag::ABILITYMGR, "Insert fail to SendRequest. err: %{public}d", err); return index; } if (!reply.ReadInt32(index)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to ReadInt32 index"); return index; } return index; } /** * @brief Inserts a single data record into the database. * * @param uri Indicates the path of the data to operate. * @param value Indicates the data record to insert. If this parameter is null, a blank row will be inserted. * * @return Returns the index of the inserted data record. */ std::shared_ptr AbilitySchedulerProxy::Call( const Uri &uri, const std::string &method, const std::string &arg, const AppExecFwk::PacMap &pacMap) { MessageParcel data; MessageParcel reply; MessageOption option; if (!WriteInterfaceToken(data)) { return nullptr; } if (!data.WriteParcelable(&uri)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteParcelable uri"); return nullptr; } if (!data.WriteString(method)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteString method"); return nullptr; } if (!data.WriteString(arg)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteString arg"); return nullptr; } if (!data.WriteParcelable(&pacMap)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteParcelable pacMap"); return nullptr; } int32_t err = SendTransactCmd(IAbilityScheduler::SCHEDULE_CALL, data, reply, option); if (err != NO_ERROR) { TAG_LOGE(AAFwkTag::ABILITYMGR, "Call fail to SendRequest. err: %{public}d", err); return nullptr; } std::shared_ptr result(reply.ReadParcelable()); if (!result) { TAG_LOGE(AAFwkTag::ABILITYMGR, "ReadParcelable value is nullptr."); return nullptr; } return result; } /** * @brief Updates data records in the database. * * @param uri Indicates the path of data to update. * @param value Indicates the data to update. This parameter can be null. * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null. * * @return Returns the number of data records updated. */ int AbilitySchedulerProxy::Update(const Uri &uri, const NativeRdb::ValuesBucket &value, const NativeRdb::DataAbilityPredicates &predicates) { int index = -1; MessageParcel data; MessageParcel reply; MessageOption option; if (!WriteInterfaceToken(data)) { return index; } if (!data.WriteParcelable(&uri)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteParcelable uri"); return index; } if (!value.Marshalling(data)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteParcelable value"); return index; } if (!data.WriteParcelable(&predicates)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteParcelable predicates"); return index; } int32_t err = SendTransactCmd(IAbilityScheduler::SCHEDULE_UPDATE, data, reply, option); if (err != NO_ERROR) { TAG_LOGE(AAFwkTag::ABILITYMGR, "Update fail to SendRequest. err: %{public}d", err); return index; } if (!reply.ReadInt32(index)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to ReadInt32 index"); return index; } return index; } /** * @brief Deletes one or more data records from the database. * * @param uri Indicates the path of the data to operate. * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null. * * @return Returns the number of data records deleted. */ int AbilitySchedulerProxy::Delete(const Uri &uri, const NativeRdb::DataAbilityPredicates &predicates) { int index = -1; MessageParcel data; MessageParcel reply; MessageOption option; if (!WriteInterfaceToken(data)) { return index; } if (!data.WriteParcelable(&uri)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteParcelable uri"); return index; } if (!data.WriteParcelable(&predicates)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteParcelable predicates"); return index; } int32_t err = SendTransactCmd(IAbilityScheduler::SCHEDULE_DELETE, data, reply, option); if (err != NO_ERROR) { TAG_LOGE(AAFwkTag::ABILITYMGR, "Delete fail to SendRequest. err: %{public}d", err); return index; } if (!reply.ReadInt32(index)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to ReadInt32 index"); return index; } return index; } /** * @brief Deletes one or more data records from the database. * * @param uri Indicates the path of data to query. * @param columns Indicates the columns to query. If this parameter is null, all columns are queried. * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null. * * @return Returns the query result. */ std::shared_ptr AbilitySchedulerProxy::Query( const Uri &uri, std::vector &columns, const NativeRdb::DataAbilityPredicates &predicates) { MessageParcel data; MessageParcel reply; MessageOption option; if (!WriteInterfaceToken(data)) { return nullptr; } if (!data.WriteParcelable(&uri)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteParcelable uri"); return nullptr; } if (!data.WriteStringVector(columns)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteStringVector columns"); return nullptr; } if (!data.WriteParcelable(&predicates)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteParcelable predicates"); return nullptr; } int32_t err = SendTransactCmd(IAbilityScheduler::SCHEDULE_QUERY, data, reply, option); if (err != NO_ERROR) { TAG_LOGE(AAFwkTag::ABILITYMGR, "Query fail to SendRequest. err: %{public}d", err); return nullptr; } return OHOS::NativeRdb::ISharedResultSet::ReadFromParcel(reply); } /** * @brief Obtains the MIME type matching the data specified by the URI of the Data ability. This method should be * implemented by a Data ability. Data abilities supports general data types, including text, HTML, and JPEG. * * @param uri Indicates the URI of the data. * * @return Returns the MIME type that matches the data specified by uri. */ std::string AbilitySchedulerProxy::GetType(const Uri &uri) { std::string type; MessageParcel data; MessageParcel reply; MessageOption option; if (!WriteInterfaceToken(data)) { return type; } if (!data.WriteParcelable(&uri)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteParcelable uri"); return type; } int32_t err = SendTransactCmd(IAbilityScheduler::SCHEDULE_GETTYPE, data, reply, option); if (err != NO_ERROR) { TAG_LOGE(AAFwkTag::ABILITYMGR, "GetFileTypes fail to SendRequest. err: %{public}d", err); return type; } type = reply.ReadString(); if (type.empty()) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to ReadString type"); return type; } return type; } /** * @brief Reloads data in the database. * * @param uri Indicates the position where the data is to reload. This parameter is mandatory. * @param extras Indicates the PacMap object containing the additional parameters to be passed in this call. This * parameter can be null. If a custom Sequenceable object is put in the PacMap object and will be transferred across * processes, you must call BasePacMap.setClassLoader(ClassLoader) to set a class loader for the custom object. * * @return Returns true if the data is successfully reloaded; returns false otherwise. */ bool AbilitySchedulerProxy::Reload(const Uri &uri, const PacMap &extras) { bool ret = false; MessageParcel data; MessageParcel reply; MessageOption option; if (!WriteInterfaceToken(data)) { return ret; } if (!data.WriteParcelable(&uri)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteParcelable uri"); return ret; } if (!data.WriteParcelable(&extras)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteParcelable extras"); return ret; } int32_t err = SendTransactCmd(IAbilityScheduler::SCHEDULE_RELOAD, data, reply, option); if (err != NO_ERROR) { TAG_LOGE(AAFwkTag::ABILITYMGR, "GetFileTypes fail to SendRequest. err: %{public}d", err); return ret; } ret = reply.ReadBool(); if (!ret) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to ReadBool ret"); return ret; } return ret; } /** * @brief Inserts multiple data records into the database. * * @param uri Indicates the path of the data to operate. * @param values Indicates the data records to insert. * * @return Returns the number of data records inserted. */ int AbilitySchedulerProxy::BatchInsert(const Uri &uri, const std::vector &values) { int ret = -1; MessageParcel data; MessageParcel reply; MessageOption option; if (!WriteInterfaceToken(data)) { return ret; } if (!data.WriteParcelable(&uri)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteParcelable uri"); return ret; } int count = (int)values.size(); if (!data.WriteInt32(count)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteInt32 ret"); return ret; } for (int i = 0; i < count; i++) { if (!values[i].Marshalling(data)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteParcelable ret, index = %{public}d", i); return ret; } } int32_t err = SendTransactCmd(IAbilityScheduler::SCHEDULE_BATCHINSERT, data, reply, option); if (err != NO_ERROR) { TAG_LOGE(AAFwkTag::ABILITYMGR, "GetFileTypes fail to SendRequest. err: %{public}d", err); return ret; } if (!reply.ReadInt32(ret)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to ReadInt32 index"); return ret; } return ret; } /** * @brief Registers an observer to DataObsMgr specified by the given Uri. * * @param uri, Indicates the path of the data to operate. * @param dataObserver, Indicates the IDataAbilityObserver object. */ bool AbilitySchedulerProxy::ScheduleRegisterObserver(const Uri &uri, const sptr &dataObserver) { MessageParcel data; MessageParcel reply; MessageOption option; if (!WriteInterfaceToken(data)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "%{public}s WriteInterfaceToken(data) return false", __func__); return false; } if (!data.WriteParcelable(&uri)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "%{public}s failed to WriteParcelable uri ", __func__); return false; } if (!data.WriteRemoteObject(dataObserver->AsObject())) { TAG_LOGE(AAFwkTag::ABILITYMGR, "%{public}s failed to WriteParcelable dataObserver ", __func__); return false; } int32_t result = SendTransactCmd(IAbilityScheduler::SCHEDULE_REGISTEROBSERVER, data, reply, option); if (result == ERR_NONE) { TAG_LOGI(AAFwkTag::ABILITYMGR, "%{public}s SendRequest ok, retval is %{public}d", __func__, reply.ReadInt32()); return true; } else { TAG_LOGE(AAFwkTag::ABILITYMGR, "%{public}s SendRequest error, result=%{public}d", __func__, result); return false; } } /** * @brief Deregisters an observer used for DataObsMgr specified by the given Uri. * * @param uri, Indicates the path of the data to operate. * @param dataObserver, Indicates the IDataAbilityObserver object. */ bool AbilitySchedulerProxy::ScheduleUnregisterObserver(const Uri &uri, const sptr &dataObserver) { MessageParcel data; MessageParcel reply; MessageOption option; if (!WriteInterfaceToken(data)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "%{public}s WriteInterfaceToken(data) return false", __func__); return false; } if (!data.WriteParcelable(&uri)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "%{public}s failed to WriteParcelable uri ", __func__); return false; } if (!data.WriteRemoteObject(dataObserver->AsObject())) { TAG_LOGE(AAFwkTag::ABILITYMGR, "%{public}s failed to WriteParcelable dataObserver ", __func__); return false; } int32_t result = SendTransactCmd(IAbilityScheduler::SCHEDULE_UNREGISTEROBSERVER, data, reply, option); if (result == ERR_NONE) { TAG_LOGI(AAFwkTag::ABILITYMGR, "%{public}s SendRequest ok, retval is %{public}d", __func__, reply.ReadInt32()); return true; } else { TAG_LOGE(AAFwkTag::ABILITYMGR, "%{public}s SendRequest error, result=%{public}d", __func__, result); return false; } } /** * @brief Notifies the registered observers of a change to the data resource specified by Uri. * * @param uri, Indicates the path of the data to operate. */ bool AbilitySchedulerProxy::ScheduleNotifyChange(const Uri &uri) { MessageParcel data; MessageParcel reply; MessageOption option; if (!WriteInterfaceToken(data)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "%{public}s WriteInterfaceToken(data) return false", __func__); return false; } if (!data.WriteParcelable(&uri)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "%{public}s failed to WriteParcelable uri ", __func__); return false; } int32_t result = SendTransactCmd(IAbilityScheduler::SCHEDULE_NOTIFYCHANGE, data, reply, option); if (result == ERR_NONE) { TAG_LOGI(AAFwkTag::ABILITYMGR, "%{public}s SendRequest ok, retval is %{public}d", __func__, reply.ReadInt32()); return true; } else { TAG_LOGE(AAFwkTag::ABILITYMGR, "%{public}s SendRequest error, result=%{public}d", __func__, result); return false; } } /** * @brief Converts the given uri that refer to the Data ability into a normalized URI. A normalized URI can be used * across devices, persisted, backed up, and restored. It can refer to the same item in the Data ability even if the * context has changed. If you implement URI normalization for a Data ability, you must also implement * denormalizeUri(ohos.utils.net.Uri) to enable URI denormalization. After this feature is enabled, URIs passed to * any method that is called on the Data ability must require normalization verification and denormalization. The * default implementation of this method returns null, indicating that this Data ability does not support URI * normalization. * * @param uri Indicates the Uri object to normalize. * * @return Returns the normalized Uri object if the Data ability supports URI normalization; returns null otherwise. */ Uri AbilitySchedulerProxy::NormalizeUri(const Uri &uri) { Uri urivalue(""); MessageParcel data; MessageParcel reply; MessageOption option; if (!WriteInterfaceToken(data)) { return urivalue; } if (!data.WriteParcelable(&uri)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteParcelable uri"); return urivalue; } int32_t err = SendTransactCmd(IAbilityScheduler::SCHEDULE_NORMALIZEURI, data, reply, option); if (err != NO_ERROR) { TAG_LOGE(AAFwkTag::ABILITYMGR, "NormalizeUri fail to SendRequest. err: %{public}d", err); return Uri(""); } std::unique_ptr info(reply.ReadParcelable()); if (!info) { TAG_LOGE(AAFwkTag::ABILITYMGR, "ReadParcelable value is nullptr."); return Uri(""); } return *info; } /** * @brief Converts the given normalized uri generated by normalizeUri(ohos.utils.net.Uri) into a denormalized one. * The default implementation of this method returns the original URI passed to it. * * @param uri uri Indicates the Uri object to denormalize. * * @return Returns the denormalized Uri object if the denormalization is successful; returns the original Uri passed * to this method if there is nothing to do; returns null if the data identified by the original Uri cannot be found * in the current environment. */ Uri AbilitySchedulerProxy::DenormalizeUri(const Uri &uri) { Uri urivalue(""); MessageParcel data; MessageParcel reply; MessageOption option; if (!WriteInterfaceToken(data)) { return urivalue; } if (!data.WriteParcelable(&uri)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteParcelable uri"); return urivalue; } int32_t err = SendTransactCmd(IAbilityScheduler::SCHEDULE_DENORMALIZEURI, data, reply, option); if (err != NO_ERROR) { TAG_LOGE(AAFwkTag::ABILITYMGR, "DenormalizeUri fail to SendRequest. err: %{public}d", err); return Uri(""); } std::unique_ptr info(reply.ReadParcelable()); if (!info) { TAG_LOGE(AAFwkTag::ABILITYMGR, "ReadParcelable value is nullptr."); return Uri(""); } return *info; } std::vector> AbilitySchedulerProxy::ExecuteBatch( const std::vector> &operations) { TAG_LOGI(AAFwkTag::ABILITYMGR, "AbilitySchedulerProxy::ExecuteBatch start"); MessageParcel data; MessageParcel reply; MessageOption option; std::vector> results; results.clear(); if (!WriteInterfaceToken(data)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerProxy::ExecuteBatch fail to Writer token"); return results; } int count = (int)operations.size(); if (!data.WriteInt32(count)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerProxy::ExecuteBatch fail to WriteInt32 ret"); return results; } for (int i = 0; i < count; i++) { if (!data.WriteParcelable(operations[i].get())) { TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerProxy::ExecuteBatch fail to WriteParcelable ret, index = %{public}d", i); return results; } } int32_t err = SendTransactCmd(IAbilityScheduler::SCHEDULE_EXECUTEBATCH, data, reply, option); if (err != NO_ERROR) { TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerProxy::ExecuteBatch fail to SendRequest. err: %{public}d", err); return results; } int total = 0; if (!reply.ReadInt32(total)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerProxy::ExecuteBatch fail to ReadInt32 count %{public}d", total); return results; } for (int i = 0; i < total; i++) { std::shared_ptr dataAbilityResult( reply.ReadParcelable()); if (dataAbilityResult == nullptr) { TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerProxy::ExecuteBatch dataAbilityResult is nullptr, index = %{public}d", i); return results; } results.push_back(dataAbilityResult); } TAG_LOGI(AAFwkTag::ABILITYMGR, "AbilitySchedulerProxy::ExecuteBatch end %{public}d", total); return results; } void AbilitySchedulerProxy::ContinueAbility(const std::string& deviceId, uint32_t versionCode) { MessageParcel data; MessageParcel reply; MessageOption option(MessageOption::TF_ASYNC); if (!WriteInterfaceToken(data)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "ContinueAbility fail to write token"); return; } if (!data.WriteString(deviceId)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "ContinueAbility fail to write deviceId"); return; } if (!data.WriteUint32(versionCode)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "ContinueAbility fail to write versionCode"); return; } int32_t err = SendTransactCmd(IAbilityScheduler::CONTINUE_ABILITY, data, reply, option); if (err != NO_ERROR) { TAG_LOGE(AAFwkTag::ABILITYMGR, "ContinueAbility fail to SendRequest. err: %{public}d", err); } } void AbilitySchedulerProxy::NotifyContinuationResult(int32_t result) { MessageParcel data; MessageParcel reply; MessageOption option(MessageOption::TF_ASYNC); if (!WriteInterfaceToken(data)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "NotifyContinuationResult fail to write token"); return; } if (!data.WriteInt32(result)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "NotifyContinuationResult fail to write result"); return; } int32_t err = SendTransactCmd(IAbilityScheduler::NOTIFY_CONTINUATION_RESULT, data, reply, option); if (err != NO_ERROR) { TAG_LOGE(AAFwkTag::ABILITYMGR, "NotifyContinuationResult fail to SendRequest. err: %{public}d", err); } } void AbilitySchedulerProxy::DumpAbilityInfo(const std::vector ¶ms, std::vector &info) { MessageParcel data; MessageParcel reply; MessageOption option(MessageOption::TF_ASYNC); if (!WriteInterfaceToken(data)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "DumpAbilityRunner fail to write token"); return; } if (!data.WriteStringVector(params)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "DumpAbilityRunner fail to write params"); return; } int32_t err = SendTransactCmd(IAbilityScheduler::DUMP_ABILITY_RUNNER_INNER, data, reply, option); if (err != NO_ERROR) { TAG_LOGE(AAFwkTag::ABILITYMGR, "DumpAbilityRunner fail to SendRequest. err: %{public}d", err); } } void AbilitySchedulerProxy::CallRequest() { TAG_LOGI(AAFwkTag::ABILITYMGR, "AbilitySchedulerProxy::CallRequest start"); MessageParcel data; MessageParcel reply; MessageOption option(MessageOption::TF_ASYNC); if (!WriteInterfaceToken(data)) { return; } int32_t err = SendTransactCmd(IAbilityScheduler::REQUEST_CALL_REMOTE, data, reply, option); if (err != NO_ERROR) { TAG_LOGE(AAFwkTag::ABILITYMGR, "CallRequest fail to SendRequest. err: %{public}d", err); return; } TAG_LOGI(AAFwkTag::ABILITYMGR, "AbilitySchedulerProxy::CallRequest end"); } void AbilitySchedulerProxy::OnExecuteIntent(const Want &want) { TAG_LOGI(AAFwkTag::ABILITYMGR, "AbilitySchedulerProxy::OnExecuteIntent start"); MessageParcel data; MessageParcel reply; MessageOption option(MessageOption::TF_ASYNC); if (!WriteInterfaceToken(data)) { return; } data.WriteParcelable(&want); int32_t err = SendTransactCmd(IAbilityScheduler::SCHEDULE_ONEXECUTE_INTENT, data, reply, option); if (err != NO_ERROR) { TAG_LOGE(AAFwkTag::ABILITYMGR, "ScheduleAbilityTransaction fail to SendRequest. err: %{public}d", err); } TAG_LOGI(AAFwkTag::ABILITYMGR, "AbilitySchedulerProxy::OnExecuteIntent end"); } int32_t AbilitySchedulerProxy::CreateModalUIExtension(const Want &want) { HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); TAG_LOGD(AAFwkTag::ABILITYMGR, "AbilitySchedulerProxy::CreateModalUIExtension start"); MessageParcel data; MessageParcel reply; MessageOption option(MessageOption::TF_ASYNC); if (!WriteInterfaceToken(data)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "Write interface fail"); return INNER_ERR; } if (!data.WriteParcelable(&want)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "Write wants fail"); return INNER_ERR; } int32_t err = SendTransactCmd(IAbilityScheduler::CREATE_MODAL_UI_EXTENSION, data, reply, option); if (err != NO_ERROR) { TAG_LOGE(AAFwkTag::ABILITYMGR, "CreateModalUIExtension fail to SendRequest. err: %{public}d", err); return err; } return reply.ReadInt32(); } void AbilitySchedulerProxy::UpdateSessionToken(sptr sessionToken) { MessageParcel data; if (!WriteInterfaceToken(data)) { return; } if (!data.WriteRemoteObject(sessionToken)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "Write sessionToken failed."); return; } MessageParcel reply; MessageOption option(MessageOption::TF_ASYNC); int32_t err = SendTransactCmd(IAbilityScheduler::UPDATE_SESSION_TOKEN, data, reply, option); if (err != NO_ERROR) { TAG_LOGE(AAFwkTag::ABILITYMGR, "Fail to SendRequest. err: %{public}d", err); } } int32_t AbilitySchedulerProxy::SendTransactCmd(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) { sptr remote = Remote(); if (remote == nullptr) { TAG_LOGE(AAFwkTag::ABILITYMGR, "remote object is nullptr."); return ERR_NULL_OBJECT; } int32_t ret = remote->SendRequest(code, data, reply, option); if (ret != NO_ERROR) { TAG_LOGE(AAFwkTag::ABILITYMGR, "SendRequest failed. code is %{public}d, ret is %{public}d.", code, ret); return ret; } return NO_ERROR; } } // namespace AAFwk } // namespace OHOS