diff --git a/frameworks/kits/ability/native/include/ability.h b/frameworks/kits/ability/native/include/ability.h index 32292eb688e3032aa4ce5dbbe76a4beb4230ab75..fc2a5a80a2b934fdd5033fa408e149bf0697a205 100755 --- a/frameworks/kits/ability/native/include/ability.h +++ b/frameworks/kits/ability/native/include/ability.h @@ -1188,19 +1188,6 @@ public: */ sptr GetBundleMgr(); - /** - * @brief check permission of bundle, if it not existed. - * @return returns the permission is vaild, or false for failed. - */ - bool CheckPermission(); - - /** - * @brief Permission check. - * @param bundleName bundleName. - * @return Returns true on success, false on failure. - */ - bool CheckFormPermission(const std::string &bundleName) const; - /** * @brief Add the bundle manager instance for debug. * @param bundleManager the bundle manager ipc object. diff --git a/frameworks/kits/ability/native/src/ability.cpp b/frameworks/kits/ability/native/src/ability.cpp index 55faab618af0c71bfc9bca2da91050ad91e1fbff..1847d598d30959bc18b6e9224bcd31c0e251631f 100755 --- a/frameworks/kits/ability/native/src/ability.cpp +++ b/frameworks/kits/ability/native/src/ability.cpp @@ -1,2961 +1,2922 @@ -/* - * Copyright (c) 2021 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 -#include - -#include "ability.h" -#include "ability_loader.h" -#include "app_log_wrapper.h" -#include "display_type.h" -#include "form_provider_client.h" -#include "if_system_ability_manager.h" -#include "iservice_registry.h" -#include "system_ability_definition.h" -#include "task_handler_client.h" -#include "ohos_application.h" -#include "continuation_manager.h" -#include "continuation_register_manager.h" -#include "continuation_register_manager_proxy.h" -#include "reverse_continuation_scheduler_primary.h" -#include "reverse_continuation_scheduler_replica.h" -#include "reverse_continuation_scheduler_replica_handler_interface.h" -#include "string_wrapper.h" -#include "permission/permission.h" -#include "permission/permission_kit.h" -#include "abs_shared_result_set.h" -#include "data_ability_predicates.h" -#include "values_bucket.h" -#include "ability_post_event_timeout.h" -#include "data_ability_result.h" -#include "data_ability_operation.h" -#include "data_uri_utils.h" - - -#include "key_event.h" -#include "touch_event.h" -#include "form_host_client.h" -#include "form_mgr.h" -#include "ipc_skeleton.h" - -namespace OHOS { -namespace AppExecFwk { -using PermissionKit = OHOS::Security::Permission::PermissionKit; -using PermissionState = OHOS::Security::Permission::PermissionState; - -REGISTER_AA(Ability) -const std::string Ability::SYSTEM_UI("com.ohos.systemui"); -const std::string Ability::STATUS_BAR("com.ohos.systemui.statusbar.MainAbility"); -const std::string Ability::NAVIGATION_BAR("com.ohos.systemui.navigationbar.MainAbility"); -const std::string DEVICE_MANAGER_BUNDLE_NAME = "com.ohos.devicemanagerui"; -const std::string DEVICE_MANAGER_NAME = "com.ohos.devicemanagerui.MainAbility"; -const std::string Ability::DMS_SESSION_ID("sessionId"); -const std::string Ability::DMS_ORIGIN_DEVICE_ID("deviceId"); -const int Ability::DEFAULT_DMS_SESSION_ID(0); -const std::string PERMISSION_REQUIRE_FORM = "ohos.permission.REQUIRE_FORM"; - -static std::mutex formLock; - -constexpr int64_t SEC_TO_MILLISEC = 1000; -constexpr int64_t MILLISEC_TO_NANOSEC = 1000000; - -void Ability::Init(const std::shared_ptr &abilityInfo, const std::shared_ptr &application, - std::shared_ptr &handler, const sptr &token) -{ - APP_LOGI("%{public}s begin.", __func__); - abilityInfo_ = abilityInfo; - handler_ = handler; - AbilityContext::token_ = token; - - // page ability only. - if (abilityInfo_->type == AbilityType::PAGE) { - abilityWindow_ = std::make_shared(); - - if (abilityWindow_ != nullptr) { - APP_LOGI("%{public}s begin abilityWindow_->Init", __func__); - abilityWindow_->Init(handler_, shared_from_this()); - APP_LOGI("%{public}s end abilityWindow_->Init", __func__); - } - continuationManager_ = std::make_shared(); - std::weak_ptr ability = shared_from_this(); - std::weak_ptr continuationManager = continuationManager_; - continuationHandler_ = std::make_shared(continuationManager, ability); - if (!continuationManager_->Init(shared_from_this(), GetToken(), GetAbilityInfo(), continuationHandler_)) { - continuationManager_.reset(); - } else { - std::weak_ptr continuationHandler = continuationHandler_; - sptr Primary = sptr( - new (std::nothrow) ReverseContinuationSchedulerPrimary(continuationHandler, handler_)); - if (Primary == nullptr) { - APP_LOGE("Ability::Init failed,Primary create failed"); - } else { - continuationHandler_->SetPrimaryStub(Primary); - continuationHandler_->SetAbilityInfo(abilityInfo_); - } - } - } - lifecycle_ = std::make_shared(); - abilityLifecycleExecutor_ = std::make_shared(); - if (abilityLifecycleExecutor_ != nullptr) { - abilityLifecycleExecutor_->DispatchLifecycleState(AbilityLifecycleExecutor::LifecycleState::INITIAL); - } else { - APP_LOGE("%{public}s abilityLifecycleExecutor_ make failed.", __func__); - } - - application_ = application; - APP_LOGI("%{public}s end.", __func__); -} - -/** - * @brief Obtains a resource manager. - * - * @return Returns a ResourceManager object. - */ -std::shared_ptr Ability::GetResourceManager() const -{ - return AbilityContext::GetResourceManager(); -} - -/** - * Will be called when ability start. You should override this function - * - * @param want ability start information - */ -void Ability::OnStart(const Want &want) -{ - APP_LOGI("%{public}s begin.", __func__); - if (abilityInfo_ == nullptr) { - APP_LOGE("Ability::OnStart falied abilityInfo_ is nullptr."); - return; - } - - if (abilityInfo_->type == AppExecFwk::AbilityType::PAGE) { - sptr config = WindowOption::Get(); - - OHOS::WindowType winType = OHOS::WindowType::WINDOW_TYPE_NORMAL; - if (abilityInfo_->bundleName == SYSTEM_UI) { - if (abilityInfo_->name == STATUS_BAR) { - winType = OHOS::WindowType::WINDOW_TYPE_STATUS_BAR; - } - if (abilityInfo_->name == NAVIGATION_BAR) { - winType = OHOS::WindowType::WINDOW_TYPE_NAVI_BAR; - } - } - if (abilityInfo_->bundleName == OHOS_REQUEST_PERMISSION_BUNDLENAME && - abilityInfo_->name == OHOS_REQUEST_PERMISSION_ABILITY_NAME) { - winType = OHOS::WindowType::WINDOW_TYPE_ALARM_SCREEN; - } - - if (abilityInfo_->bundleName == DEVICE_MANAGER_BUNDLE_NAME && abilityInfo_->name == DEVICE_MANAGER_NAME) { - winType = OHOS::WindowType::WINDOW_TYPE_ALARM_SCREEN; - } - - config->SetWindowType(winType); - APP_LOGI("Ability::OnStart bundleName:%{public}s abilityName:%{public}s: set config.type = %{public}d", - abilityInfo_->bundleName.c_str(), - abilityInfo_->name.c_str(), - winType); - if (setting_ != nullptr) { - auto windowMode = static_cast( - std::atoi(setting_->GetProperty(AbilityStartSetting::WINDOW_MODE_KEY).c_str())); - APP_LOGI("%{public}s windowMode : %{public}d", __func__, windowMode); - if (windowMode == AbilityWindowConfiguration::MULTI_WINDOW_DISPLAY_FLOATING) { - APP_LOGI("%{public}s begin SetWindowMode : WINDOW_MODE_FREE.", __func__); - config->SetWindowType(WINDOW_TYPE_FLOAT); - APP_LOGI("%{public}s end SetWindowMode : WINDOW_MODE_FREE.", __func__); - } - } else { - APP_LOGI("Ability::OnStart setting_ == nullptr."); - } - SetUIContent(config); - - if (abilityWindow_ != nullptr) { - APP_LOGI("%{public}s begin abilityWindow_->OnPostAbilityStart.", __func__); - abilityWindow_->OnPostAbilityStart(); - APP_LOGI("%{public}s end abilityWindow_->OnPostAbilityStart.", __func__); - } - } - - SetWant(want); - if (abilityLifecycleExecutor_ == nullptr) { - APP_LOGE("Ability::OnStart error. abilityLifecycleExecutor_ == nullptr."); - return; - } - abilityLifecycleExecutor_->DispatchLifecycleState(AbilityLifecycleExecutor::LifecycleState::INACTIVE); - - if (lifecycle_ == nullptr) { - APP_LOGE("Ability::OnStart error. lifecycle_ == nullptr."); - return; - } - lifecycle_->DispatchLifecycle(LifeCycle::Event::ON_START, want); - APP_LOGI("%{public}s end.", __func__); -} - -/** - * @brief Called when this ability enters the STATE_STOP state. - * - * The ability in the STATE_STOP is being destroyed. - * You can override this function to implement your own processing logic. - */ -void Ability::OnStop() -{ - APP_LOGI("%{public}s begin.", __func__); - if (continuationManager_ != nullptr) { - continuationManager_->UnregisterAbilityTokenIfNeed(); - } else { - APP_LOGE("%{public}s continuationManager_ is nullptr.", __func__); - } - - if (abilityWindow_ != nullptr && abilityInfo_->type == AppExecFwk::AbilityType::PAGE) { - APP_LOGI("%{public}s begin abilityWindow_->OnPostAbilityStop.", __func__); - abilityWindow_->OnPostAbilityStop(); - APP_LOGI("%{public}s end abilityWindow_->OnPostAbilityStop.", __func__); - } - if (abilityLifecycleExecutor_ == nullptr) { - APP_LOGE("Ability::OnStop error. abilityLifecycleExecutor_ == nullptr."); - return; - } - abilityLifecycleExecutor_->DispatchLifecycleState(AbilityLifecycleExecutor::LifecycleState::INITIAL); - - if (lifecycle_ == nullptr) { - APP_LOGE("Ability::OnStop error. lifecycle_ == nullptr."); - return; - } - lifecycle_->DispatchLifecycle(LifeCycle::Event::ON_STOP); - APP_LOGI("%{public}s end.", __func__); -} - -/** - * @brief Called when this ability enters the STATE_ACTIVE state. - * - * The ability in the STATE_ACTIVE state is visible and has focus. - * You can override this function to implement your own processing logic. - * - * @param Want Indicates the {@link Want} structure containing activation information about the ability. - */ -void Ability::OnActive() -{ - APP_LOGI("%{public}s begin.", __func__); - if (abilityWindow_ != nullptr) { - APP_LOGI("%{public}s begin abilityWindow_->OnPostAbilityActive.", __func__); - abilityWindow_->OnPostAbilityActive(); - APP_LOGI("%{public}s end abilityWindow_->OnPostAbilityActive.", __func__); - } - bWindowFocus_ = true; - if (abilityLifecycleExecutor_ == nullptr) { - APP_LOGE("Ability::OnActive error. abilityLifecycleExecutor_ == nullptr."); - return; - } - abilityLifecycleExecutor_->DispatchLifecycleState(AbilityLifecycleExecutor::LifecycleState::ACTIVE); - - if (lifecycle_ == nullptr) { - APP_LOGE("Ability::OnActive error. lifecycle_ == nullptr."); - return; - } - lifecycle_->DispatchLifecycle(LifeCycle::Event::ON_ACTIVE); - APP_LOGI("%{public}s end.", __func__); -} - -/** - * @brief Called when this ability enters the STATE_INACTIVE state. - * - * STATE_INACTIVE is an instantaneous state. The ability in this state may be visible but does not have - * focus.You can override this function to implement your own processing logic. - */ -void Ability::OnInactive() -{ - APP_LOGI("%{public}s begin.", __func__); - if (abilityWindow_ != nullptr && abilityInfo_->type == AppExecFwk::AbilityType::PAGE) { - APP_LOGI("%{public}s begin abilityWindow_->OnPostAbilityInactive.", __func__); - abilityWindow_->OnPostAbilityInactive(); - APP_LOGI("%{public}s end abilityWindow_->OnPostAbilityInactive.", __func__); - } - bWindowFocus_ = false; - if (abilityLifecycleExecutor_ == nullptr) { - APP_LOGE("Ability::OnInactive error. abilityLifecycleExecutor_ == nullptr."); - return; - } - abilityLifecycleExecutor_->DispatchLifecycleState(AbilityLifecycleExecutor::LifecycleState::INACTIVE); - - if (lifecycle_ == nullptr) { - APP_LOGE("Ability::OnInactive error. lifecycle_ == nullptr."); - return; - } - lifecycle_->DispatchLifecycle(LifeCycle::Event::ON_INACTIVE); - APP_LOGI("%{public}s end.", __func__); -} - -/** - * @brief Called when this ability enters the STATE_FOREGROUND state. - * - * - * The ability in the STATE_FOREGROUND state is visible. - * You can override this function to implement your own processing logic. - */ -void Ability::OnForeground(const Want &want) -{ - APP_LOGI("%{public}s begin.", __func__); - if (abilityWindow_ != nullptr) { - APP_LOGI("%{public}s begin abilityWindow_->OnPostAbilityForeground.", __func__); - abilityWindow_->OnPostAbilityForeground(); - APP_LOGI("%{public}s end abilityWindow_->OnPostAbilityForeground.", __func__); - } - - if (abilityLifecycleExecutor_ == nullptr) { - APP_LOGE("Ability::OnForeground error. abilityLifecycleExecutor_ == nullptr."); - return; - } - abilityLifecycleExecutor_->DispatchLifecycleState(AbilityLifecycleExecutor::LifecycleState::INACTIVE); - - if (lifecycle_ == nullptr) { - APP_LOGE("Ability::OnForeground error. lifecycle_ == nullptr."); - return; - } - lifecycle_->DispatchLifecycle(LifeCycle::Event::ON_FOREGROUND, want); - APP_LOGI("%{public}s end.", __func__); -} - -/** - * @brief Called when this ability enters the STATE_BACKGROUND state. - * - * - * The ability in the STATE_BACKGROUND state is invisible. - * You can override this function to implement your own processing logic. - */ -void Ability::OnBackground() -{ - APP_LOGI("%{public}s begin.", __func__); - if (abilityWindow_ != nullptr && abilityInfo_->type == AppExecFwk::AbilityType::PAGE) { - APP_LOGI("%{public}s begin abilityWindow_->OnPostAbilityBackground.", __func__); - abilityWindow_->OnPostAbilityBackground(); - APP_LOGI("%{public}s end abilityWindow_->OnPostAbilityBackground.", __func__); - } - - if (abilityLifecycleExecutor_ == nullptr) { - APP_LOGE("Ability::OnBackground error. abilityLifecycleExecutor_ == nullptr."); - return; - } - abilityLifecycleExecutor_->DispatchLifecycleState(AbilityLifecycleExecutor::LifecycleState::BACKGROUND); - - if (lifecycle_ == nullptr) { - APP_LOGE("Ability::OnBackground error. lifecycle_ == nullptr."); - return; - } - lifecycle_->DispatchLifecycle(LifeCycle::Event::ON_BACKGROUND); - APP_LOGI("%{public}s end.", __func__); -} - -/** - * @brief Called when this Service ability is connected for the first time. - * - * You can override this function to implement your own processing logic. - * - * @param want Indicates the {@link Want} structure containing connection information about the Service ability. - * @return Returns a pointer to the sid of the connected Service ability. - */ -sptr Ability::OnConnect(const Want &want) -{ - APP_LOGI("%{public}s begin.", __func__); - if (abilityLifecycleExecutor_ == nullptr) { - APP_LOGE("Ability::OnConnect error. abilityLifecycleExecutor_ == nullptr."); - return nullptr; - } - abilityLifecycleExecutor_->DispatchLifecycleState(AbilityLifecycleExecutor::LifecycleState::ACTIVE); - - if (lifecycle_ == nullptr) { - APP_LOGE("Ability::OnConnect error. lifecycle_ == nullptr."); - return nullptr; - } - lifecycle_->DispatchLifecycle(LifeCycle::Event::ON_ACTIVE); - APP_LOGI("%{public}s end.", __func__); - return nullptr; -} - -/** - * @brief Called when all abilities connected to this Service ability are disconnected. - * - * You can override this function to implement your own processing logic. - * - */ -void Ability::OnDisconnect(const Want &want) -{} - -/** - * Start other ability for result. - * - * @param want information of other ability - * @param requestCode request code for abilityMS to return result - */ -void Ability::StartAbilityForResult(const Want &want, int requestCode) -{ - APP_LOGI("%{public}s begin.", __func__); - if (abilityInfo_ == nullptr) { - APP_LOGE("Ability::StartAbilityForResult abilityInfo_ == nullptr"); - return; - } - APP_LOGI("Ability::StartAbilityForResult called type = %{public}d", abilityInfo_->type); - if (abilityInfo_->type == AppExecFwk::AbilityType::PAGE) { - AbilityContext::StartAbility(want, requestCode); - } - APP_LOGI("%{public}s end.", __func__); -} - -/** - * Starts an ability with specific start settings and returns the execution result when the ability is destroyed. - * When the ability is destroyed, onAbilityResult(int,int,ohos.aafwk.content.Want) is called and the returned - * requestCode is transferred to the current method. The given requestCode is customized and cannot be a negative - * number. - * - * @param want Indicates the ability to start. - * @param requestCode Indicates the request code returned after the ability is started. You can define the request code - * to identify the results returned by abilities. The value ranges from 0 to 65535. - * @param abilityStartSetting Indicates the setting ability used to start. - */ -void Ability::StartAbilityForResult(const Want &want, int requestCode, AbilityStartSetting abilityStartSetting) -{ - APP_LOGI("%{public}s begin.", __func__); - if (abilityInfo_ == nullptr) { - APP_LOGE("Ability::StartAbilityForResult abilityInfo_ == nullptr"); - return; - } - APP_LOGI("Ability::StartAbilityForResult called type = %{public}d", abilityInfo_->type); - if (abilityInfo_->type == AppExecFwk::AbilityType::PAGE) { - AbilityContext::StartAbility(want, requestCode, abilityStartSetting); - } - APP_LOGI("%{public}s end.", __func__); -} - -/** - * Starts a new ability with specific start settings. - * A Page or Service ability uses this method to start a specific ability. - * The system locates the target ability from installed abilities based on - * the value of the intent parameter and then starts it. You can specify the - * ability to start using the intent parameter. - * - * @param want Indicates the ability to start. - * @param abilityStartSetting Indicates the setting ability used to start. - */ -void Ability::StartAbility(const Want &want, AbilityStartSetting abilityStartSetting) -{ - APP_LOGI("%{public}s beign.", __func__); - if (abilityInfo_ == nullptr) { - APP_LOGE("Ability::StartAbility abilityInfo_ == nullptr"); - return; - } - APP_LOGI("Ability::StartAbility called type = %{public}d", abilityInfo_->type); - if (abilityInfo_->type == AppExecFwk::AbilityType::PAGE || abilityInfo_->type == AppExecFwk::AbilityType::SERVICE) { - AbilityContext::StartAbility(want, -1, abilityStartSetting); - } - APP_LOGI("%{public}s end.", __func__); -} - -/** - * @brief Called when a key is pressed. When any component in the Ability gains focus, the key-down event for - * the component will be handled first. This callback will not be invoked if the callback triggered for the - * key-down event of the component returns true. The default implementation of this callback does nothing - * and returns false. - * - * @param keyCode Indicates the code of the key pressed. - * @param keyEvent Indicates the key-down event. - * - * @return Returns true if this event is handled and will not be passed further; returns false if this event - * is not handled and should be passed to other handlers. - */ -bool Ability::OnKeyDown(int keyCode, const KeyEvent &keyEvent) -{ - APP_LOGI("Ability::OnKeyDown called"); - APP_LOGI("Ability::OnKeyDown keyCode: %{public}d.", keyCode); - return false; -} - -/** - * @brief Called when a key is released. When any component in the Ability gains focus, the key-up event for - * the component will be handled first. This callback will not be invoked if the callback triggered for the - * key-up event of the component returns true. The default implementation of this callback does nothing and - * returns false. - * - * @param keyCode Indicates the code of the key released. - * @param keyEvent Indicates the key-up event. - * - * @return Returns true if this event is handled and will not be passed further; returns false if this event - * is not handled and should be passed to other handlers. - */ -bool Ability::OnKeyUp(int keyCode, const KeyEvent &keyEvent) -{ - APP_LOGI("Ability::OnKeyUp called"); - APP_LOGI("Ability::OnKeyUp keyCode: %{public}d.", keyCode); - switch (keyCode) { - case OHOS::KeyEventEnum::KEY_BACK: - APP_LOGI("Ability::OnKey Back key pressed."); - OnBackPressed(); - return true; - default: - break; - } - return false; -} - -/** - * @brief Called when a touch event is dispatched to this ability. The default implementation of this callback - * does nothing and returns false. - * - * @param event Indicates information about the touch event. - * - * @return Returns true if the event is handled; returns false otherwise. - */ -bool Ability::OnTouchEvent(const TouchEvent &touchEvent) -{ - APP_LOGI("Ability::OnTouchEvent called"); - APP_LOGI("Ability::OnTouchEvent action: %{public}d phase: %{public}d", - const_cast(touchEvent).GetAction(), - const_cast(touchEvent).GetPhase()); - return false; -} - -/** - * @brief Inflates UI controls by using ComponentContainer. - * You can create a ComponentContainer instance that contains multiple components. - * - * @param componentContainer Indicates a set of customized components. - */ -void Ability::SetUIContent(const ComponentContainer &componentContainer) -{} - -/** - * @brief Inflates layout resources by using the layout resource ID. - * - * @param layoutRes Indicates the layout resource ID, which cannot be a negative number. - */ -void Ability::SetUIContent(int layoutRes) -{} - -/** - * @brief Inflates UI controls by using ComponentContainer. - * You can create a ComponentContainer instance that contains multiple components. - * - * @param componentContainer Indicates the component layout defined by the user. - * @param context Indicates the context to use. - * @param typeFlag Indicates the window type. - */ -void Ability::SetUIContent( - const ComponentContainer &componentContainer, std::shared_ptr &context, int typeFlag) -{} - -/** - * @brief Inflates layout resources by using the layout resource ID. - * - * @param layoutRes Indicates the layout resource ID, which cannot be a negative number. - * @param context Indicates the context to use. - * @param typeFlag Indicates the window type. - */ -void Ability::SetUIContent(int layoutRes, std::shared_ptr &context, int typeFlag) -{} - -/** - * @brief Inflates UI controls by using WindowConfig. - * - * @param config Indicates the window config defined by the user. - */ -void Ability::SetUIContent(const sptr &config) -{ - if (abilityWindow_ == nullptr) { - APP_LOGE("Ability::SetUIContent abilityWindow_ is nullptr"); - return; - } - - APP_LOGI("%{public}s beign abilityWindow_->SetWindowConfig.", __func__); - abilityWindow_->SetWindowConfig(config); - APP_LOGI("%{public}s end abilityWindow_->SetWindowConfig.", __func__); -} - -/** - * @brief Get the window belong to the ability. - * - * @return Returns a IWindowsManager object pointer. - */ -const sptr Ability::GetWindow() -{ - if (abilityWindow_ != nullptr) { - return abilityWindow_->GetWindow(); - } else { - APP_LOGI("%{public}s abilityWindow_ is nullptr.", __func__); - return nullptr; - } -} - -/** - * @brief Obtains the type of audio whose volume is adjusted by the volume button. - * - * @return Returns the AudioManager.AudioVolumeType. - */ -int Ability::GetVolumeTypeAdjustedByKey() -{ - return 0; -} - -/** - * @brief Checks whether the main window of this ability has window focus. - * - * @return Returns true if this ability currently has window focus; returns false otherwise. - */ -bool Ability::HasWindowFocus() -{ - if (abilityInfo_ == nullptr) { - APP_LOGI("Ability::HasWindowFocus abilityInfo_ == nullptr"); - return false; - } - - if (abilityInfo_->type == AppExecFwk::AbilityType::PAGE) { - return bWindowFocus_; - } - - return false; -} - -/** - * @brief Called when a key is lone pressed. - * - * @param keyCode Indicates the code of the key long pressed. - * @param keyEvent Indicates the key-long-press event. - * - * @return Returns true if this event is handled and will not be passed further; returns false if this event - * is not handled and should be passed to other handlers. - */ -bool Ability::OnKeyPressAndHold(int keyCode, const std::shared_ptr &keyEvent) -{ - return false; -} - -/** - * @brief Called back after permissions are requested by using - * AbilityContext.requestPermissionsFromUser(java.lang.String[],int). - * - * @param requestCode Indicates the request code passed to this method from - * AbilityContext.requestPermissionsFromUser(java.lang.String[],int). - * @param permissions Indicates the list of permissions requested by using - * AbilityContext.requestPermissionsFromUser(java.lang.String[],int). This parameter cannot be null. - * @param grantResults Indicates the granting results of the corresponding permissions requested using - * AbilityContext.requestPermissionsFromUser(java.lang.String[],int). The value 0 indicates that a - * permission is granted, and the value -1 indicates not. - * - */ -void Ability::OnRequestPermissionsFromUserResult( - int requestCode, const std::vector &permissions, const std::vector &grantResults) -{} - -/** - * @brief Called when this ability is about to leave the foreground and enter the background due to a user operation, - * for example, when the user touches the Home key. - * - */ -void Ability::OnLeaveForeground() -{} - -/** - * @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 Ability::GetType(const Uri &uri) -{ - return ""; -} - -/** - * @brief Inserts a data record into the database. This method should be implemented by a Data ability. - * - * @param uri Indicates the position where the data is to insert. - * @param value Indicates the data to insert. - * - * @return Returns the index of the newly inserted data record. - */ -int Ability::Insert(const Uri &uri, const NativeRdb::ValuesBucket &value) -{ - return 0; -} - -/** - * @brief Called when the system configuration is updated. - * - * @param configuration Indicates the updated configuration information. - */ -void Ability::OnConfigurationUpdated(const Configuration &configuration) -{} - -/** - * @brief Called when the system configuration is updated. - * - * @param level Indicates the memory trim level, which shows the current memory usage status. - * - */ -void Ability::OnMemoryLevel(int level) -{} - -/** - * @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 Ability::OpenRawFile(const Uri &uri, const std::string &mode) -{ - return -1; -} - -/** - * @brief Updates one or more data records in the database. This method should be implemented by a Data ability. - * - * @param uri Indicates the database table storing the data to update. - * @param value Indicates the data to update. This parameter can be null. - * @param predicates Indicates filter criteria. If this parameter is null, all data records will be updated by default. - * - * @return Returns the number of data records updated. - */ -int Ability::Update( - const Uri &uri, const NativeRdb::ValuesBucket &value, const NativeRdb::DataAbilityPredicates &predicates) -{ - return 0; -} - -/** - * @brief get application witch the ability belong - * - * @return Returns the application ptr - */ -std::shared_ptr Ability::GetApplication() -{ - APP_LOGI("%{public}s begin.", __func__); - if (application_ == nullptr) { - APP_LOGE("Ability::GetApplication error. application_ == nullptr."); - return nullptr; - } - APP_LOGI("%{public}s end.", __func__); - return application_; -} - -/** - * @brief Obtains the class name in this ability name, without the prefixed bundle name. - * - * @return Returns the class name of this ability. - */ -std::string Ability::GetAbilityName() -{ - if (abilityInfo_ == nullptr) { - APP_LOGE("Ability::GetAbilityName abilityInfo_ is nullptr"); - return ""; - } - - return abilityInfo_->name; -} - -/** - * @brief OChecks whether the current ability is being destroyed. - * An ability is being destroyed if you called terminateAbility() on it or someone else requested to destroy it. - * - * @return Returns true if the current ability is being destroyed; returns false otherwise. - */ -bool Ability::IsTerminating() -{ - return false; -} - -/** - * @brief Called when startAbilityForResult(ohos.aafwk.content.Want,int) is called to start an ability and the result is - * returned. This method is called only on Page abilities. You can start a new ability to perform some calculations and - * use setResult (int,ohos.aafwk.content.Want) to return the calculation result. Then the system calls back the current - * method to use the returned data to execute its own logic. - * - * @param requestCode Indicates the request code returned after the ability is started. You can define the request code - * to identify the results returned by abilities. The value ranges from 0 to 65535. - * @param resultCode Indicates the result code returned after the ability is started. You can define the result code to - * identify an error. - * @param resultData Indicates the data returned after the ability is started. You can define the data returned. The - * value can be null. - * - */ -void Ability::OnAbilityResult(int requestCode, int resultCode, const Want &want) -{} - -/** - * @brief Called back when the Back key is pressed. - * The default implementation destroys the ability. You can override this method. - * - */ -void Ability::OnBackPressed() -{ - APP_LOGI("%{public}s begin.", __func__); - if (abilityInfo_ == nullptr) { - APP_LOGE("Ability::OnBackPressed abilityInfo_ is nullptr"); - return; - } - - if (abilityInfo_->isLauncherAbility == false) { - APP_LOGI("Ability::OnBackPressed the ability is not Launcher"); - TerminateAbility(); - } - APP_LOGI("%{public}s end.", __func__); -} - -/** - * @brief Called when the launch mode of an ability is set to singleInstance. This happens when you re-launch an - * ability that has been at the top of the ability stack. - * - * @param want Indicates the new Want containing information about the ability. - */ -void Ability::OnNewWant(const Want &want) -{ - APP_LOGI("Ability::OnNewWant called"); -} - -/** - * @brief Restores data and states of an ability when it is restored by the system. This method should be implemented by - * a Page ability. This method is called if an ability was destroyed at a certain time due to resource reclaim or was - * unexpectedly destroyed and the onSaveAbilityState(ohos.utils.PacMap) method was called to save its user data and - * states. Generally, this method is called after the onStart(ohos.aafwk.content.Want) method. - * - * @param inState Indicates the PacMap object used for storing data and states. This parameter can not be null. - * - */ -void Ability::OnRestoreAbilityState(const PacMap &inState) -{ - APP_LOGI("Ability::OnRestoreAbilityState called"); -} - -/** - * @brief Saves temporary data and states of this ability. This method should be implemented by a Page ability. - * This method is called when the system determines that the ability may be destroyed in an unexpected situation, for - * example, when the screen orientation changes or the user touches the Home key. Generally, this method is used only to - * save temporary states. - * - * @param outState Indicates the PacMap object used for storing user data and states. This parameter cannot be null. - * - */ -void Ability::OnSaveAbilityState(PacMap &outState) -{ - APP_LOGI("Ability::OnSaveAbilityState called"); -} - -/** - * @brief Called every time a key, touch, or trackball event is dispatched to this ability. - * You can override this callback method if you want to know that the user has interacted with - * the device in a certain way while this ability is running. This method, together with onLeaveForeground(), - * is designed to help abilities intelligently manage status bar notifications. Specifically, they help - * abilities determine when to cancel a notification. - * - */ -void Ability::OnEventDispatch() -{} - -/** - * @brief Sets the want object that can be obtained by calling getWant(). - * @param Want information of other ability - */ -void Ability::SetWant(const AAFwk::Want &want) -{ - setWant_ = std::make_shared(want); -} - -/** - * @brief Obtains the Want object that starts this ability. - * - * @return Returns the Want object that starts this ability. - */ -std::shared_ptr Ability::GetWant() -{ - return setWant_; -} - -/** - * @brief Sets the result code and data to be returned by this Page ability to the caller. - * When a Page ability is destroyed, the caller overrides the AbilitySlice#onAbilityResult(int, int, Want) method to - * receive the result set in the current method. This method can be called only after the ability has been initialized. - * - * @param resultCode Indicates the result code returned after the ability is destroyed. You can define the result code - * to identify an error. - * @param resultData Indicates the data returned after the ability is destroyed. You can define the data returned. This - * parameter can be null. - */ -void Ability::SetResult(int resultCode, const Want &resultData) -{ - APP_LOGI("%{public}s begin.", __func__); - if (abilityInfo_ == nullptr) { - APP_LOGE("Ability::SetResult abilityInfo_ == nullptr"); - return; - } - APP_LOGI("Ability::SetResult called type = %{public}d", abilityInfo_->type); - if (abilityInfo_->type == AppExecFwk::AbilityType::PAGE) { - AbilityContext::resultWant_ = resultData; - AbilityContext::resultCode_ = resultCode; - } - APP_LOGI("%{public}s end.", __func__); -} - -/** - * @brief Sets the type of audio whose volume will be adjusted by the volume button. - * - * @param volumeType Indicates the AudioManager.AudioVolumeType to set. - */ -void Ability::SetVolumeTypeAdjustedByKey(int volumeType) -{} - -/** - * @brief Called back when Service is started. - * This method can be called only by Service. You can use the StartAbility(ohos.aafwk.content.Want) method to start - * Service. Then the system calls back the current method to use the transferred want parameter to execute its own - * logic. - * - * @param want Indicates the want of Service to start. - * @param restart Indicates the startup mode. The value true indicates that Service is restarted after being destroyed, - * and the value false indicates a normal startup. - * @param startId Indicates the number of times the Service ability has been started. The startId is incremented by 1 - * every time the ability is started. For example, if the ability has been started for six times, the value of startId - * is 6. - */ -void Ability::OnCommand(const AAFwk::Want &want, bool restart, int startId) -{ - APP_LOGI("%{public}s begin restart=%{public}s,startId=%{public}d.", __func__, restart ? "true" : "false", startId); - if (abilityLifecycleExecutor_ == nullptr) { - APP_LOGE("Ability::OnCommand error. abilityLifecycleExecutor_ == nullptr."); - return; - } - abilityLifecycleExecutor_->DispatchLifecycleState(AbilityLifecycleExecutor::LifecycleState::ACTIVE); - - if (lifecycle_ == nullptr) { - APP_LOGE("Ability::OnCommand error. lifecycle_ == nullptr."); - return; - } - lifecycle_->DispatchLifecycle(LifeCycle::Event::ON_ACTIVE); - APP_LOGI("%{public}s end.", __func__); -} - -/** - * @brief dump ability info - * - * @param extra dump ability info - */ -void Ability::Dump(const std::string &extra) -{ - APP_LOGI("Ability::Dump called"); - // abilityInfo - APP_LOGI("==============AbilityInfo=============="); - if (abilityInfo_ != nullptr) { - APP_LOGI("abilityInfo: package: %{public}s", abilityInfo_->package.c_str()); - APP_LOGI("abilityInfo: name: %{public}s", abilityInfo_->name.c_str()); - APP_LOGI("abilityInfo: label: %{public}s", abilityInfo_->label.c_str()); - APP_LOGI("abilityInfo: description: %{public}s", abilityInfo_->description.c_str()); - APP_LOGI("abilityInfo: iconPath: %{public}s", abilityInfo_->iconPath.c_str()); - APP_LOGI("abilityInfo: visible: %{public}d", abilityInfo_->visible); - APP_LOGI("abilityInfo: kind: %{public}s", abilityInfo_->kind.c_str()); - APP_LOGI("abilityInfo: type: %{public}d", abilityInfo_->type); - APP_LOGI("abilityInfo: orientation: %{public}d", abilityInfo_->orientation); - APP_LOGI("abilityInfo: launchMode: %{public}d", abilityInfo_->launchMode); - for (auto permission : abilityInfo_->permissions) { - APP_LOGI("abilityInfo: permission: %{public}s", permission.c_str()); - } - APP_LOGI("abilityInfo: bundleName: %{public}s", abilityInfo_->bundleName.c_str()); - APP_LOGI("abilityInfo: applicationName: %{public}s", abilityInfo_->applicationName.c_str()); - } else { - APP_LOGI("abilityInfo is nullptr"); - } - - // lifecycle_Event - APP_LOGI("==============lifecycle_Event=============="); - if (lifecycle_ != nullptr) { - APP_LOGI("lifecycle_Event: launchMode: %{public}d", lifecycle_->GetLifecycleState()); - } else { - APP_LOGI("lifecycle is nullptr"); - } - - // lifecycle_State - APP_LOGI("==============lifecycle_State=============="); - if (abilityLifecycleExecutor_ != nullptr) { - APP_LOGI("lifecycle_State: launchMode: %{public}d", abilityLifecycleExecutor_->GetState()); - } else { - APP_LOGI("abilityLifecycleExecutor is nullptr"); - } - - // applicationInfo - APP_LOGI("==============applicationInfo=============="); - std::shared_ptr ApplicationInfoPtr = GetApplicationInfo(); - if (ApplicationInfoPtr != nullptr) { - APP_LOGI("applicationInfo: name: %{public}s", ApplicationInfoPtr->name.c_str()); - APP_LOGI("applicationInfo: bundleName: %{public}s", ApplicationInfoPtr->bundleName.c_str()); - } else { - APP_LOGI("ApplicationInfoPtr is nullptr"); - } -} - -/** - * @brief Keeps this Service ability in the background and displays a notification bar. - * To use this method, you need to request the ohos.permission.KEEP_BACKGROUND_RUNNING permission from the system. - * The ohos.permission.KEEP_BACKGROUND_RUNNING permission is of the normal level. - * This method can be called only by Service abilities after the onStart(ohos.aafwk.content.Want) method is called. - * - * @param id Identifies the notification bar information. - * @param notificationRequest Indicates the NotificationRequest instance containing information for displaying a - * notification bar. - */ -void Ability::KeepBackgroundRunning(int id, const NotificationRequest ¬ificationRequest) -{} - -/** - * @brief Cancels background running of this ability to free up system memory. - * This method can be called only by Service abilities when the onStop() method is called. - * - */ -void Ability::CancelBackgroundRunning() -{} - -/** - * @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 Ability::NormalizeUri(const Uri &uri) -{ - return uri; -} - -/** - * @brief Deletes one or more data records. This method should be implemented by a Data ability. - * - * @param uri Indicates the database table storing the data to delete. - * @param predicates Indicates filter criteria. If this parameter is null, all data records will be deleted by default. - * - * @return Returns the number of data records deleted. - */ -int Ability::Delete(const Uri &uri, const NativeRdb::DataAbilityPredicates &predicates) -{ - return 0; -} - -/** - * @brief Obtains the MIME type of files. This method should be implemented by a Data ability. - * - * @param uri Indicates the path of the files to obtain. - * @param mimeTypeFilter Indicates the MIME type of the files to obtain. This parameter cannot be set to null. - * 1. * / *: Obtains all types supported by a Data ability. - * 2. image/ *: Obtains files whose main type is image of any subtype. - * 3. * /jpg: Obtains files whose subtype is JPG of any main type. - * - * @return Returns the MIME type of the matched files; returns null if there is no type that matches the Data ability. - */ -std::vector Ability::GetFileTypes(const Uri &uri, const std::string &mimeTypeFilter) -{ - return types_; -} - -/** - * @brief Opens a file. This method should be implemented by a Data ability. - * - * @param uri Indicates the path of the file to open. - * @param mode Indicates the 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 FileDescriptor object of the file descriptor. - */ -int Ability::OpenFile(const Uri &uri, const std::string &mode) -{ - return -1; -} - -/** - * @brief Queries one or more data records in the database. This method should be implemented by a Data ability. - * - * @param uri Indicates the database table storing the data to query. - * @param columns Indicates the columns to be queried, in array, for example, {"name","age"}. You should define the - * processing logic when this parameter is null. - * @param predicates Indicates filter criteria. If this parameter is null, all data records will be queried by default. - * - * @return Returns the queried data. - */ -std::shared_ptr Ability::Query( - const Uri &uri, const std::vector &columns, const NativeRdb::DataAbilityPredicates &predicates) -{ - return nullptr; -} - -/** - * @brief Reloads data in the database. - * The default implementation of this method returns false. You must implement this method in the child class - * of your Data Ability if you want to use it. - * - * @param uri Indicates the position where the data is to reload. - * @param extras Indicates the additional parameters contained in the PacMap object to be passed in this call. - * - * @return Returns true if the data is successfully reloaded; returns false otherwise. - */ -bool Ability::Reload(const Uri &uri, const PacMap &extras) -{ - return false; -} - -/** - * @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 Ability::BatchInsert(const Uri &uri, const std::vector &values) -{ - APP_LOGI("%{public}s begin.", __func__); - int amount = 0; - for (auto it = values.begin(); it != values.end(); it++) { - if (Insert(uri, *it) >= 0) { - amount++; - } - } - APP_LOGI("%{public}s end, amount=%{public}d", __func__, amount); - return amount; -} - -/** - * @brief Migrates this ability to the given device on the same distributed network in a reversible way that allows this - * ability to be migrated back to the local device through reverseContinueAbility(). The ability to migrate and its - * ability slices must implement the IAbilityContinuation interface. Otherwise, an exception is thrown, indicating that - * the ability does not support migration. - * - * @param deviceId Indicates the ID of the target device where this ability will be migrated to. If this parameter is - * null, this method has the same effect as ContinueAbilityReversibly(). - * - */ -void Ability::ContinueAbilityReversibly(const std::string &deviceId) -{ - if (!VerifySupportForContinuation()) { - APP_LOGE("Ability::ContinueAbilityReversibly(deviceId) failed. VerifySupportForContinuation faled"); - return; - } - continuationManager_->ContinueAbility(true, deviceId); -} - -/** - * @brief public final String getOriginalDeviceId​() throws UnsupportedOperationException - * Obtains the ID of the source device from which this ability is migrated. - * - * @return Returns the source device ID. - */ -std::string Ability::GetOriginalDeviceId() -{ - return ""; -} - -/** - * @brief Obtains the migration state of this ability. - * @return Returns the migration state. - * - * @return Returns the source device ID. - */ -ContinuationState Ability::GetContinuationState() -{ - if (!VerifySupportForContinuation()) { - APP_LOGE("Ability::GetContinuationState failed. VerifySupportForContinuation faled"); - return ContinuationState::LOCAL_RUNNING; - } - return continuationManager_->GetContinuationState(); -} - -/** - * @brief Obtains the singleton AbilityPackage object to which this ability belongs. - * - * @return Returns the singleton AbilityPackage object to which this ability belongs. - */ -std::shared_ptr Ability::GetAbilityPackage() -{ - return nullptr; -} - -/** - * @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 Ability::DenormalizeUri(const Uri &uri) -{ - return uri; -} - -/** - * @brief Obtains the Lifecycle object of the current ability. - * - * @return Returns the Lifecycle object. - */ -std::shared_ptr Ability::GetLifecycle() -{ - APP_LOGI("Ability::GetLifecycle called"); - return lifecycle_; -} - -/** - * @brief Obtains the lifecycle state of this ability. - * - * @return Returns the lifecycle state of this ability. - */ -AbilityLifecycleExecutor::LifecycleState Ability::GetState() -{ - APP_LOGI("Ability::GetState called"); - - if (abilityLifecycleExecutor_ == nullptr) { - APP_LOGE("Ability::GetState error. abilityLifecycleExecutor_ == nullptr."); - return AbilityLifecycleExecutor::LifecycleState::UNINITIALIZED; - } - - return (AbilityLifecycleExecutor::LifecycleState)abilityLifecycleExecutor_->GetState(); -} - -/** - * @brief A Page or Service ability uses this method to start a specific ability. The system locates the target ability - * from installed abilities based on the value of the intent parameter and then starts it. You can specify the ability - * to start using the intent parameter. - * - * @param intent Indicates the ability to start. - */ -void Ability::StartAbility(const Want &want) -{ - APP_LOGI("%{public}s begin.", __func__); - AbilityContext::StartAbility(want, -1); - APP_LOGI("%{public}s end.", __func__); -} - -/** - * @brief Destroys this Page or Service ability. - * After a Page or Service ability performs all operations, it can use this method to destroy itself - * to free up memory. This method can be called only after the ability is initialized. - */ -void Ability::TerminateAbility() -{ - APP_LOGI("%{public}s begin.", __func__); - AbilityContext::TerminateAbility(); - APP_LOGI("%{public}s end.", __func__); -} - -/** - * @brief Destroys ability. - * - * @param want Indicates the want containing information about TerminateAbility - * - * @return Returns the result of TerminateAbility - */ -int Ability::TerminateAbility(Want &want) -{ - return -1; -} - -/** - * @brief Sets the main route for this ability. - * - * The main route, also called main entry, refers to the default AbilitySlice to present for this ability. - * This function should be called only on Feature Abilities. If this function is not called in the - * {@link OnStart(const Want &want)} function for a Feature Ability, the Feature Ability will fail to start. - * - * @param entry Indicates the main entry, which is the class name of the AbilitySlice instance to start. - * - * @return Returns the result of SetMainRouter - */ -void Ability::SetMainRoute(const std::string &entry) -{} - -/** - * @brief By binding an action, you can set different action parameters in Intent to present different initial pages. - * You must register actions in the profile file. - * - * @param action Indicates the action to bind. - * - * @param entry Indicates the entry, which is the fully qualified name of your AbilitySlice class. - * - * @return Returns the result of AddActionRoute - */ -void Ability::AddActionRoute(const std::string &action, const std::string &entry) -{} - -/** - * @brief Sets the background color of the window in RGB color mode. - * - * @param red The value ranges from 0 to 255. - * - * @param green The value ranges from 0 to 255. - * - * @param blue The value ranges from 0 to 255. - * - * @return Returns the result of SetWindowBackgroundColor - */ -int Ability::SetWindowBackgroundColor(int red, int green, int blue) -{ - return -1; -} - -/** - * @brief Connects the current ability to an ability using the AbilityInfo.AbilityType.SERVICE template. - * - * @param want Indicates the want containing information about the ability to connect - * - * @param conn Indicates the callback object when the target ability is connected. - * - * @return True means success and false means failure - */ -bool Ability::ConnectAbility(const Want &want, const sptr &conn) -{ - return AbilityContext::ConnectAbility(want, conn); -} - -/** - * @brief Disconnects the current ability from an ability - * - * @param conn Indicates the IAbilityConnection callback object passed by connectAbility after the connection - * is set up. The IAbilityConnection object uniquely identifies a connection between two abilities. - */ -void Ability::DisconnectAbility(const sptr &conn) -{ - return AbilityContext::DisconnectAbility(conn); -} - -/** - * @brief Destroys another ability that uses the AbilityInfo.AbilityType.SERVICE template. - * The current ability using either the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE - * template can call this method to destroy another ability that uses the AbilityInfo.AbilityType.SERVICE - * template. The current ability itself can be destroyed by calling the terminateAbility() method. - * - * @param want Indicates the Want containing information about the ability to destroy. - * - * @return Returns true if the ability is destroyed successfully; returns false otherwise. - */ -bool Ability::StopAbility(const AAFwk::Want &want) -{ - return AbilityContext::StopAbility(want); -} - -/** - * @brief Posts a scheduled Runnable task to a new non-UI thread. - * The task posted via this method will be executed in a new thread, which allows you to perform certain time-consuming - * operations. To use this method, you must also override the supportHighPerformanceUI() method. - * - * @param task Indicates the Runnable task to post. - * - * @param delayTime Indicates the number of milliseconds after which the task will be executed. - * - * @return - - */ -void Ability::PostTask(std::function task, long delayTime) -{ - APP_LOGI("%{public}s begin.", __func__); - TaskHandlerClient::GetInstance()->PostTask(task, delayTime); - APP_LOGI("%{public}s end.", __func__); -} - -/** - * @brief You can use the IContinuationRegisterManager object to interact with the Device+ control center, - * including registering and unregistering the ability to migrate, updating the device connection state, and - * showing the list of devices that can be selected for ability migration. - * - * @return Returns true if the migration request is successful; returns false otherwise. - */ -std::weak_ptr Ability::GetContinuationRegisterManager() -{ - if (abilityInfo_ != nullptr) { - if ((abilityInfo_->type == AbilityType::PAGE) && (continuationRegisterManager_ == nullptr)) { - std::weak_ptr context = shared_from_this(); - std::shared_ptr continuationRegisterManagerProxy = - std::make_shared(context); - continuationRegisterManager_ = std::make_shared(); - continuationRegisterManager_->Init(continuationRegisterManagerProxy); - } - } - std::weak_ptr continuationRegisterManager = - std::weak_ptr(continuationRegisterManager_); - return continuationRegisterManager; -} - -/** - * @brief Migrates this ability to the given device on the same distributed network. The ability to migrate and its - * ability slices must implement the IAbilityContinuation interface. - * - * @param deviceId Indicates the ID of the target device where this ability will be migrated to. If this parameter - * is null, this method has the same effect as continueAbility(). - * - */ -void Ability::ContinueAbility(const std::string &deviceId) -{ - if (deviceId.empty()) { - APP_LOGE("Ability::ContinueAbility(deviceId) failed. deviceId is empty"); - return; - } - - if (!VerifySupportForContinuation()) { - APP_LOGE("Ability::ContinueAbility(deviceId) failed. VerifySupportForContinuation faled"); - return; - } - continuationManager_->ContinueAbility(false, deviceId); -} - -/** - * @brief Callback function to ask the user whether to start the migration . - * - * @return If the user allows migration, it returns true; otherwise, it returns false. - */ -bool Ability::OnStartContinuation() -{ - return false; -} - -/** - * @brief Save user data of local Ability generated at runtime. - * - * @param saveData Indicates the user data to be saved. - * @return If the data is saved successfully, it returns true; otherwise, it returns false. - */ -bool Ability::OnSaveData(WantParams &saveData) -{ - return false; -} - -/** - * @brief After creating the Ability on the remote device, - * immediately restore the user data saved during the migration of the Ability on the remote device. - * @param restoreData Indicates the user data to be restored. - * @return If the data is restored successfully, it returns true; otherwise, it returns false . - */ -bool Ability::OnRestoreData(WantParams &restoreData) -{ - return false; -} - -/** - * @brief This function can be used to implement the processing logic after the migration is completed. - * - * @param result Migration result code. 0 means the migration was successful, -1 means the migration failed. - * @return None. - */ -void Ability::OnCompleteContinuation(int result) -{} - -/** - * @brief Used to notify the local Ability that the remote Ability has been destroyed. - * - * @return None. - */ -void Ability::OnRemoteTerminated() -{} -bool Ability::VerifySupportForContinuation() -{ - if (continuationManager_ == nullptr) { - APP_LOGE("Ability::VerifySupportForContinuation failed. continuationManager_ is nullptr"); - return false; - } - return true; -} - -void Ability::HandleCreateAsContinuation(const Want &want) -{ - if (!IsFlagExists(Want::FLAG_ABILITY_CONTINUATION, want.GetFlags())) { - APP_LOGI("Ability::HandleCreateAsContinuation return. This not continuated ability"); - return; - } - - // check whether it needs reversible - bool reversible = false; - reversible = IsFlagExists(Want::FLAG_ABILITY_CONTINUATION_REVERSIBLE, want.GetFlags()); - if (!VerifySupportForContinuation()) { - APP_LOGE("Ability::HandleCreateAsContinuation failed. VerifySupportForContinuation failed"); - return; - } - bool success = continuationManager_->RestoreData( - want.GetParams(), reversible, want.GetStringParam(ContinuationHandler::ORIGINAL_DEVICE_ID)); - - if (success && reversible) { - // Register this ability to receive reverse continuation callback. - std::weak_ptr ReplicaHandler = continuationHandler_; - reverseContinuationSchedulerReplica_ = sptr( - new (std::nothrow) ReverseContinuationSchedulerReplica(handler_, ReplicaHandler)); - - if (reverseContinuationSchedulerReplica_ == nullptr) { - APP_LOGE("Ability::HandleCreateAsContinuation failed, create reverseContinuationSchedulerReplica failed"); - return; - } - } - - int sessionId = want.GetIntParam(DMS_SESSION_ID, DEFAULT_DMS_SESSION_ID); - std::string originDeviceId = want.GetStringParam(DMS_ORIGIN_DEVICE_ID); - APP_LOGE("Ability::HandleCreateAsContinuationoriginDeviceId: %{public}s", originDeviceId.c_str()); - continuationManager_->NotifyCompleteContinuation( - originDeviceId, sessionId, success, reverseContinuationSchedulerReplica_); -} -bool Ability::IsFlagExists(unsigned int flag, unsigned int flagSet) -{ - return (flag & flagSet) == flag; -} - -/** - * @brief Called when this ability gains or loses window focus. - * - * @param hasFocus Specifies whether this ability has focus. - */ -void Ability::OnWindowFocusChanged(bool hasFocus) -{} - -/** - * @brief Called when this ability is moved to or removed from the top of the stack. - * - * @param topActive Specifies whether this ability is moved to or removed from the top of the stack. The value true - * indicates that it is moved to the top, and false indicates that it is removed from the top of the stack. - */ -void Ability::OnTopActiveAbilityChanged(bool topActive) -{} - -/** - * @brief Called to set caller information for the application. The default implementation returns null. - * - * @return Returns the caller information. - */ -Uri Ability::OnSetCaller() -{ - return Uri(""); -} - -/** - * @brief Call this when your ability should be closed and the mission should be completely removed as a part of - * finishing the root ability of the mission. - */ -void Ability::TerminateAndRemoveMission() -{ - APP_LOGI("%{public}s begin.", __func__); - auto state = GetState(); - if (state > AbilityLifecycleExecutor::LifecycleState::INITIAL) { - APP_LOGI("Ability::TerminateAndRemoveMission the GetState retval is %d", state); - return; - } - AbilityContext::TerminateAndRemoveMission(); - APP_LOGI("%{public}s end.", __func__); -} - -/** - * @brief Create a PostEvent timeout task. The default delay is 5000ms - * - * @return Return a smart pointer to a timeout object - */ -std::shared_ptr Ability::CreatePostEventTimeouter(std::string taskstr) -{ - return std::make_shared(taskstr, handler_); -} - -/** - * Releases an obtained form by its ID. - * - *

After this method is called, the form won't be available for use by the application, but the Form Manager - * Service still keeps the cache information about the form, so that the application can quickly obtain it based on - * the {@code formId}.

- *

Permission: {@link ohos.security.SystemPermission#REQUIRE_FORM}

- * - * @param formId Indicates the form ID. - * @return Returns {@code true} if the form is successfully released; returns {@code false} otherwise. - * - *
    - *
  • The passed {@code formId} is invalid. Its value must be larger than 0.
  • - *
  • The specified form has not been added by the application.
  • - *
  • An error occurred when connecting to the Form Manager Service.
  • - *
  • The application is not granted with the {@link ohos.security.SystemPermission#REQUIRE_FORM} permission.
  • - *
  • The form has been obtained by another application and cannot be released by the current application.
  • - *
  • The form is being restored.
  • - *
- */ -bool Ability::ReleaseForm(const int64_t formId) -{ - APP_LOGI("%{public}s called.", __func__); - // release form by formId and do not release cache - return ReleaseForm(formId, false); -} - -/** - * Releases an obtained form by its ID. - * - *

After this method is called, the form won't be available for use by the application, if isReleaseCache is - * false, this method is same as {@link #releaseForm(int)}, otherwise the Form Manager Service still store this - * form in the cache.

- *

Permission: {@link ohos.security.SystemPermission#REQUIRE_FORM}

- * - * @param formId Indicates the form ID. - * @param isReleaseCache Indicates whether to clear cache in service. - * @return Returns {@code true} if the form is successfully released; returns {@code false} otherwise. - * - *
    - *
  • The passed {@code formId} is invalid. Its value must be larger than 0.
  • - *
  • The specified form has not been added by the application.
  • - *
  • An error occurred when connecting to the Form Manager Service.
  • - *
  • The application is not granted with the {@link ohos.security.SystemPermission#REQUIRE_FORM} permission.
  • - *
  • The form has been obtained by another application and cannot be released by the current application.
  • - *
  • The form is being restored.
  • - *
- */ -bool Ability::ReleaseForm(const int64_t formId, const bool isReleaseCache) -{ - APP_LOGI("%{public}s called.", __func__); - // release form with formId and specifies whether to release the cache - return DeleteForm(formId, isReleaseCache ? RELEASE_CACHED_FORM : RELEASE_FORM); -} - -/** - * Deletes an obtained form by its ID. - * - *

After this method is called, the form won't be available for use by the application and the Form Manager - * Service no longer keeps the cache information about the form.

- *

Permission: {@link ohos.security.SystemPermission#REQUIRE_FORM}

- * - * @param formId Indicates the form ID. - * @return Returns {@code true} if the form is successfully deleted; returns {@code false} otherwise. - * - *
    - *
  • The passed {@code formId} is invalid. Its value must be larger than 0.
  • - *
  • The specified form has not been added by the application.
  • - *
  • An error occurred when connecting to the Form Manager Service.
  • - *
  • The application is not granted with the {@link ohos.security.SystemPermission#REQUIRE_FORM} permission.
  • - *
  • The form has been obtained by another application and cannot be deleted by the current application.
  • - *
  • The form is being restored.
  • - *
- */ -bool Ability::DeleteForm(const int64_t formId) -{ - APP_LOGI("%{public}s called.", __func__); - // delete form with formId - return DeleteForm(formId, DELETE_FORM); -} - -/** - * @brief Cast temp form with formId. - * - * @param formId Indicates the form's ID. - * - * @return Returns {@code true} if the form is successfully casted; returns {@code false} otherwise. - */ -bool Ability::CastTempForm(const int64_t formId) -{ - APP_LOGI("%{public}s start", __func__); - if (formId <= 0) { - APP_LOGE("%{public}s error, passing in form id can't be negative.", __func__); - return false; - } - - APP_LOGI("%{public}s, castTempForm begin of temp form %{public}" PRId64, __func__, formId); - bool result = FormMgr::GetInstance().CastTempForm(formId, FormHostClient::GetInstance()); - - if (!result) { - APP_LOGE("%{public}s error, some internal server occurs, error code is %{public}d.", __func__, result); - return false; - } - - userReqParams_[formId].SetParam(Constants::PARAM_FORM_TEMPORARY_KEY, false); - - APP_LOGI("%{public}s end", __func__); - return true; -} - -/** - * @brief Obtains a specified form that matches the application bundle name, module name, form name, and - * other related information specified in the passed {@code Want}. - * - *

This method is asynchronous. After the {@link FormJsInfo} instance is obtained. - * - * @param formId Indicates the form ID. - * @param want Indicates the detailed information about the form to be obtained, including the bundle name, - * module name, ability name, form name, form id, tempForm flag, form dimension, and form customize data. - * @param callback Indicates the callback to be invoked whenever the {@link FormJsInfo} instance is obtained. - * @return Returns {@code true} if the request is successfully initiated; returns {@code false} otherwise. - */ -bool Ability::AcquireForm(const int64_t formId, const Want &want, const std::shared_ptr callback) -{ - APP_LOGI("%{public}s called.", __func__); - struct timespec ts; - clock_gettime(CLOCK_REALTIME, &ts); - long currentTime = ts.tv_sec * SEC_TO_MILLISEC + ts.tv_nsec / MILLISEC_TO_NANOSEC; - APP_LOGI("%{public}s begin, current time: %{public}ld", __func__, currentTime); - - // check fms recover status - if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) { - APP_LOGE("%{public}s error, form is in recover status, can't do action on form.", __func__); - return false; - } - - // check form id - APP_LOGD("%{public}s, param of formId %{public}" PRId64 ".", __func__, formId); - if (formId < 0) { - APP_LOGE("%{public}s error, form id should not be negative.", __func__); - return false; - } - - // check param of want - if (!CheckWantValid(formId, want)) { - APP_LOGE("%{public}s error, failed to check param of want.", __func__); - return false; - }; - - ElementName elementName = want.GetElement(); - std::string bundleName = elementName.GetBundleName(); - std::string abilityName = elementName.GetAbilityName(); - APP_LOGI("%{public}s, begin to acquire form, bundleName is %{public}s, abilityName is %{public}s, formId is " - "%{public}" PRId64 ".", - __func__, - bundleName.c_str(), - abilityName.c_str(), - formId); - - // hostClient init - sptr formHostClient = FormHostClient::GetInstance(); - if (formHostClient == nullptr) { - APP_LOGE("%{public}s error, formHostClient == nullptr.", __func__); - return false; - } - - // acquire form request to fms - FormJsInfo formJsInfo; - if (FormMgr::GetInstance().AddForm(formId, want, formHostClient, formJsInfo) != ERR_OK) { - APP_LOGE("%{public}s error, acquire form for fms failed.", __func__); - return false; - } - APP_LOGI("%{public}s, end to acquire form, the formId returned from the fms is %{public}" PRId64 ".", - __func__, - formJsInfo.formId); - - // check for form presence in hostForms - if (formHostClient->ContainsForm(formJsInfo.formId)) { - APP_LOGE("%{public}s error, form has already acquired, do not support acquire twice.", __func__); - return false; - } - - // add ability of form to hostForms - std::shared_ptr thisAbility = this->shared_from_this(); - formHostClient->AddForm(thisAbility, formJsInfo.formId); - // post the async task of handleAcquireResult - PostTask([this, want, formJsInfo, callback]() { HandleAcquireResult(want, formJsInfo, callback); }, 0L); - // the acquire form is successfully - return true; -} - -/** - * @brief Updates the content of a specified JS form. - * - *

This method is called by a form provider to update JS form data as needed. - * - * @param formId Indicates the form ID. - * @param formProviderData The data used to update the JS form displayed on the client. - * @return Returns {@code true} if the request is successfully initiated; returns {@code false} otherwise. - */ -bool Ability::UpdateForm(const int64_t formId, const FormProviderData &formProviderData) -{ - APP_LOGI("%{public}s called.", __func__); - // check fms recover status - if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) { - APP_LOGE("%{public}s error, form is in recover status, can't do action on form.", __func__); - return false; - } - - // check formId - if (formId <= 0) { - APP_LOGE("%{public}s error, the passed in formId can't be negative or zero.", __func__); - return false; - } - - // check formProviderData - if (formProviderData.GetDataString().empty()) { - APP_LOGE("%{public}s error, the formProviderData is null.", __func__); - return false; - } - - // update form request to fms - if (FormMgr::GetInstance().UpdateForm(formId, abilityInfo_->bundleName, formProviderData) != ERR_OK) { - APP_LOGE("%{public}s error, update form for fms failed.", __func__); - return false; - } - - // the update form is successfully - return true; -} - -/** - * @brief Sends a notification to the form framework to make the specified forms visible. - * - *

After this method is successfully called, {@link Ability#OnVisibilityChanged(std::map)} - * will be called to notify the form provider of the form visibility change event.

- * - * @param formIds Indicates the IDs of the forms to be made visible. - * @return Returns {@code true} if the request is successfully initiated; returns {@code false} otherwise. - */ -bool Ability::NotifyVisibleForms(const std::vector &formIds) -{ - APP_LOGI("%{public}s called.", __func__); - return NotifyWhetherVisibleForms(formIds, Constants::FORM_VISIBLE); -} - -/** - * @brief Sends a notification to the form framework to make the specified forms invisible. - * - *

After this method is successfully called, {@link Ability#OnVisibilityChanged(std::map)} - * will be called to notify the form provider of the form visibility change event.

- * - * @param formIds Indicates the IDs of the forms to be made invisible. - * @return Returns {@code true} if the request is successfully initiated; returns {@code false} otherwise. - */ -bool Ability::NotifyInvisibleForms(const std::vector &formIds) -{ - APP_LOGI("%{public}s called.", __func__); - return NotifyWhetherVisibleForms(formIds, Constants::FORM_INVISIBLE); -} - -/** - * @brief Set form next refresh time. - * - *

This method is called by a form provider to set refresh time. - * - * @param formId Indicates the ID of the form to set refresh time. - * @param nextTime Indicates the next time gap now in seconds, can not be litter than 300 seconds. - * @return Returns {@code true} if seting succeed; returns {@code false} otherwise. - */ -bool Ability::SetFormNextRefreshTime(const int64_t formId, const int64_t nextTime) -{ - APP_LOGI("%{public}s called.", __func__); - if (nextTime < MIN_NEXT_TIME) { - APP_LOGE("next time litte than 300 seconds."); - return false; - } - - if (FormMgr::GetInstance().GetRecoverStatus() == Constants::IN_RECOVERING) { - APP_LOGE("%{public}s, formManager is in recovering", __func__); - return false; - } - - int result = FormMgr::GetInstance().SetNextRefreshTime(formId, nextTime); - if (result != ERR_OK) { - APP_LOGE("%{public}s, internal error:[%{public}d]", __func__, result); - return false; - } - - return true; -} -/** - * @brief Requests for form data update. - * - * This method must be called when the application has detected that a system setting item (such as the language, - * resolution, or screen orientation) being listened for has changed. Upon receiving the update request, the form - * provider automatically updates the form data (if there is any update) through the form framework, with the update - * process being unperceivable by the application. - * - * @param formId Indicates the ID of the form to update. - * @return Returns true if the update request is successfully initiated, returns false otherwise. - */ -bool Ability::RequestForm(const int64_t formId) -{ - APP_LOGI("%{public}s called.", __func__); - Want want; - return RequestForm(formId, want); -} - -/** - * @brief Update form. - * - * @param formJsInfo Indicates the obtained {@code FormJsInfo} instance. - */ -void Ability::ProcessFormUpdate(const FormJsInfo &formJsInfo) -{ - APP_LOGI("%{public}s called.", __func__); - // post the async task of handleFormMessage - int32_t msgCode = OHOS_FORM_UPDATE_FORM; - PostTask([this, msgCode, formJsInfo]() { HandleFormMessage(msgCode, formJsInfo); }, 0L); -} -/** - * @brief Uninstall form. - * - * @param formId Indicates the ID of the form to uninstall. - */ -void Ability::ProcessFormUninstall(const int64_t formId) -{ - APP_LOGI("%{public}s start.", __func__); - // check formId - if (formId <= 0) { - APP_LOGE("%{public}s error, the passed in formId can't be negative or zero.", __func__); - return; - } - - std::shared_ptr formCallback = nullptr; - { - std::lock_guard lock(formLock); - // get callback iterator by formId - std::map>::iterator appCallbackIterator = appCallbacks_.find(formId); - - // call the callback function when you need to be notified - if (appCallbackIterator == appCallbacks_.end()) { - APP_LOGE("%{public}s failed, callback not find, formId: %{public}" PRId64 ".", __func__, formId); - return; - } - formCallback = appCallbackIterator->second; - CleanFormResource(formId); - } - if (formCallback == nullptr) { - APP_LOGE("%{public}s failed, callback is nullptr.", __func__); - return; - } - - formCallback->OnFormUninstall(formId); - - APP_LOGI("%{public}s end.", __func__); -} - -/** - * @brief Called to return a FormProviderInfo object. - * - *

You must override this method if your ability will serve as a form provider to provide a form for clients. - * The default implementation returns nullptr.

- * - * @param want Indicates the detailed information for creating a FormProviderInfo. - * The Want object must include the form ID, form name of the form, - * which can be obtained from Ability#PARAM_FORM_IDENTITY_KEY, - * Ability#PARAM_FORM_NAME_KEY, and Ability#PARAM_FORM_DIMENSION_KEY, - * respectively. Such form information must be managed as persistent data for further form - * acquisition, update, and deletion. - * - * @return Returns the created FormProviderInfo object. - */ -FormProviderInfo Ability::OnCreate(const Want &want) -{ - APP_LOGI("%{public}s called.", __func__); - FormProviderInfo formProviderInfo; - return formProviderInfo; -} - -/** - * @brief Called to notify the form provider that a specified form has been deleted. Override this method if - * you want your application, as the form provider, to be notified of form deletion. - * - * @param formId Indicates the ID of the deleted form. - * @return None. - */ -void Ability::OnDelete(const int64_t formId) -{} - -/** - * @brief Called to notify the form provider to update a specified form. - * - * @param formId Indicates the ID of the form to update. - * @return none. - */ -void Ability::OnUpdate(const int64_t formId) -{} - -/** - * @brief Called when the form provider is notified that a temporary form is successfully converted to a normal form. - * - * @param formId Indicates the ID of the form. - * @return None. - */ -void Ability::OnCastTemptoNormal(const int64_t formId) -{} - -/** - * @brief Called when the form provider receives form events from the fms. - * - * @param formEventsMap Indicates the form events occurred. The key in the Map object indicates the form ID, - * and the value indicates the event type, which can be either FORM_VISIBLE - * or FORM_INVISIBLE. FORM_VISIBLE means that the form becomes visible, - * and FORM_INVISIBLE means that the form becomes invisible. - * @return none. - */ -void Ability::OnVisibilityChanged(const std::map &formEventsMap) -{} -/** - * @brief Called to notify the form provider to update a specified form. - * - * @param formId Indicates the ID of the form to update. - * @param message Form event message. - */ -void Ability::OnTriggerEvent(const int64_t formId, const std::string &message) -{} -/** - * @brief Delete or release form with formId. - * - * @param formId Indicates the form's ID. - * @param deleteType Indicates the type of delete or release. - * @return Returns {@code true} if the form is successfully deleted; returns {@code false} otherwise. - */ -bool Ability::DeleteForm(const int64_t formId, const int32_t deleteType) -{ - APP_LOGI("%{public}s called.", __func__); - // check fms recover status - if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) { - APP_LOGE("%{public}s error, form is in recover status, can't do action on form.", __func__); - return false; - } - // check formId - if (formId <= 0) { - APP_LOGE("%{public}s error, the passed in formId can't be negative or zero.", __func__); - return false; - } - - APP_LOGI("%{public}s, delete form begin, formId is %{public}" PRId64 " and deleteType is %{public}d.", - __func__, - formId, - deleteType); - { - // form lock - std::lock_guard lock(formLock); - // clean form resource when form is temp form - if (std::find(lostedByReconnectTempForms_.begin(), lostedByReconnectTempForms_.end(), formId) != - lostedByReconnectTempForms_.end()) { - CleanFormResource(formId); - // the delete temp form is successfully - return true; - } - } - - // hostClient init - sptr formHostClient = FormHostClient::GetInstance(); - // delete or release request to fms - int result; - if (deleteType == DELETE_FORM) { - result = FormMgr::GetInstance().DeleteForm(formId, formHostClient); - } else { - result = FormMgr::GetInstance().ReleaseForm( - formId, formHostClient, (deleteType == RELEASE_CACHED_FORM) ? true : false); - } - if (result != ERR_OK) { - APP_LOGE("%{public}s error, some internal server occurs, error code is %{public}d.", __func__, result); - return false; - } - { - // form lock - std::lock_guard lock(formLock); - // clean form resource - CleanFormResource(formId); - } - // the delete form is successfully - return true; -} - -/** - * @brief Clean form resource with formId. - * - * @param formId Indicates the form's ID. - */ -void Ability::CleanFormResource(const int64_t formId) -{ - APP_LOGI("%{public}s called.", __func__); - // compatible with int form id - int64_t cleanId{-1L}; - for (auto param : userReqParams_) { - uint64_t unsignedFormId = static_cast(formId); - uint64_t unsignedParamFirst = static_cast(param.first); - if ((unsignedParamFirst & 0x00000000ffffffffL) == (unsignedFormId & 0x00000000ffffffffL)) { - cleanId = param.first; - break; - } - } - if (cleanId == -1L) { - return; - } - - APP_LOGD("%{public}s. clean id is %{public}" PRId64 ".", __func__, cleanId); - // remove wantParam, callback and lostedByReconnectTempForms - appCallbacks_.erase(cleanId); - userReqParams_.erase(cleanId); - auto tempForm = std::find(lostedByReconnectTempForms_.begin(), lostedByReconnectTempForms_.end(), cleanId); - if (tempForm != lostedByReconnectTempForms_.end()) { - lostedByReconnectTempForms_.erase(tempForm); - } - - // remove ability - std::shared_ptr thisAbility = this->shared_from_this(); - FormHostClient::GetInstance()->RemoveForm(thisAbility, cleanId); - - // unregister death callback when appCallbacks is empty - if (appCallbacks_.empty()) { - std::shared_ptr thisAbility = this->shared_from_this(); - FormMgr::GetInstance().UnRegisterDeathCallback(thisAbility); - } - APP_LOGI("%{public}s end.", __func__); -} - -/** - * @brief Handle acquire result of the obtained form instance. - * - * @param want Indicates the detailed information about the form to be obtained, including the bundle name, - * module name, ability name, form name, form id, tempForm flag, form dimension, and form customize data. - * @param formJsInfo Indicates the obtained {@code FormJsInfo} instance. - * @param callback Indicates the callback to be invoked whenever the {@link FormJsInfo} instance is obtained. - */ -void Ability::HandleAcquireResult( - const Want &want, const FormJsInfo &formJsInfo, const std::shared_ptr callback) -{ - APP_LOGI("%{public}s called.", __func__); - { - // form lock - std::lock_guard lock(formLock); - - // register death when userReqParams is empty - if (userReqParams_.empty()) { - std::shared_ptr thisAbility = this->shared_from_this(); - FormMgr::GetInstance().RegisterDeathCallback(thisAbility); - } - - // save wantParam and callback - userReqParams_.insert(std::make_pair(formJsInfo.formId, want)); - appCallbacks_.insert(std::make_pair(formJsInfo.formId, callback)); - } - - struct timespec ts; - clock_gettime(CLOCK_REALTIME, &ts); - long currentTime = ts.tv_sec * SEC_TO_MILLISEC + ts.tv_nsec / MILLISEC_TO_NANOSEC; - APP_LOGI("%{public}s, AcquireForm end, current time: %{public}ld", __func__, currentTime); - - // handle acquire message of the obtained form instance - callback->OnAcquired(FormCallback::OHOS_FORM_ACQUIRE_SUCCESS, formJsInfo); -} - -/** - * @brief Handle acquire message of the obtained form instance. - * - * @param msgCode Indicates the code of message type. - * @param formJsInfo Indicates the obtained {@code FormJsInfo} instance. - */ -void Ability::HandleFormMessage(const int32_t msgCode, const FormJsInfo &formJsInfo) -{ - APP_LOGI("%{public}s called.", __func__); - std::shared_ptr formCallback = nullptr; - { - std::lock_guard lock(formLock); - // get callback iterator by formId - std::map>::iterator appCallbackIterator = - appCallbacks_.find(formJsInfo.formId); - - // call the callback function when you need to be notified - if (appCallbackIterator == appCallbacks_.end()) { - APP_LOGE("%{public}s failed, callback not find, formId: %{public}" PRId64 ".", __func__, formJsInfo.formId); - return; - } - formCallback = appCallbackIterator->second; - } - if (formCallback == nullptr) { - APP_LOGE("%{public}s failed, callback is nullptr.", __func__); - return; - } - - APP_LOGI("%{public}s, call user implement of form %{public}" PRId64 ".", __func__, formJsInfo.formId); - - if (msgCode == OHOS_FORM_ACQUIRE_FORM) { - formCallback->OnAcquired(FormCallback::OHOS_FORM_ACQUIRE_SUCCESS, formJsInfo); - } else { - formCallback->OnUpdate(FormCallback::OHOS_FORM_UPDATE_SUCCESS, formJsInfo); - } -} - -/** - * @brief Notify the forms visibility change event. - * - * @param formIds Indicates the IDs of the forms to be made visible or invisible. - * @param eventType Indicates the form events occurred. FORM_VISIBLE means that the form becomes visible, - * and FORM_INVISIBLE means that the form becomes invisible. - * @return Returns {@code true} if the request is successfully initiated; returns {@code false} otherwise. - */ -bool Ability::NotifyWhetherVisibleForms(const std::vector &formIds, int32_t eventType) -{ - APP_LOGI("%{public}s called.", __func__); - if (formIds.empty() || formIds.size() > Constants::MAX_VISIBLE_NOTIFY_LIST) { - APP_LOGE("%{public}s, formIds is empty or exceed 32.", __func__); - return false; - } - - if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) { - APP_LOGE("%{public}s error, form is in recover status, can't do action on form.", __func__); - return false; - } - - int resultCode = - FormMgr::GetInstance().NotifyWhetherVisibleForms(formIds, FormHostClient::GetInstance(), eventType); - if (resultCode != ERR_OK) { - APP_LOGE("%{public}s error, internal error occurs, error code:%{public}d.", __func__, resultCode); - return false; - } - return true; -} - -/** - * @brief Check the param of want. - * - * @param formId Indicates the form's ID. - * @param want Indicates the detailed information about the form to be obtained, including the bundle name, - * module name, ability name, form name, form id, tempForm flag, form dimension, and form customize data. - * @return Returns {@code true} if the check result is ok; returns {@code false} ng. - */ -bool Ability::CheckWantValid(const int64_t formId, const Want &want) -{ - APP_LOGI("%{public}s called.", __func__); - // get want parameters - int32_t formDimension = want.GetIntParam(Constants::PARAM_FORM_DIMENSION_KEY, 1); - std::string moduleName = want.GetStringParam(Constants::PARAM_MODULE_NAME_KEY); - - ElementName elementName = want.GetElement(); - std::string bundleName = elementName.GetBundleName(); - std::string abilityName = elementName.GetAbilityName(); - - bool tempFormFlg = want.GetBoolParam(Constants::PARAM_FORM_TEMPORARY_KEY, false); - - APP_LOGD("%{public}s, param of formDimension %{public}d in want.", __func__, formDimension); - APP_LOGD("%{public}s, param of moduleName %{public}s in want.", __func__, moduleName.c_str()); - APP_LOGD("%{public}s, param of bundleName %{public}s in want.", __func__, bundleName.c_str()); - APP_LOGD("%{public}s, param of abilityName %{public}s in want.", __func__, abilityName.c_str()); - APP_LOGD("%{public}s, param of tempFormFlg %{public}d in want.", __func__, tempFormFlg); - - // check want parameters - if (bundleName.empty() || abilityName.empty() || moduleName.empty()) { - APP_LOGE("%{public}s error, bundleName or abilityName or moduleName is not set in want.", __func__); - return false; - } - if (FormHostClient::GetInstance()->ContainsForm(formId)) { - APP_LOGE("%{public}s error, form has already acquired, do not support acquire twice.", __func__); - return false; - } - if (formDimension <= 0) { - APP_LOGE("%{public}s error, dimension should not be zero or negative in want.", __func__); - return false; - } - if (tempFormFlg && formId != 0) { - APP_LOGE("%{public}s error, can not select form id when acquire temporary form.", __func__); - return false; - } - - // the check is successfully - return true; -} - -/** - * @brief Enable form update. - * - * @param formIds FormIds of hostclient. - */ -bool Ability::EnableUpdateForm(const std::vector &formIds) -{ - APP_LOGI("%{public}s called.", __func__); - return LifecycleUpdate(formIds, ENABLE_FORM_UPDATE); -} - -/** - * @brief Disable form update. - * - * @param formIds FormIds of hostclient. - */ -bool Ability::DisableUpdateForm(const std::vector &formIds) -{ - APP_LOGI("%{public}s called.", __func__); - return LifecycleUpdate(formIds, DISABLE_FORM_UPDATE); -} - -bool Ability::LifecycleUpdate(std::vector formIds, int32_t updateType) -{ - if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) { - APP_LOGE("%{public}s error, form is in recover status, can't do action on form.", __func__); - return false; - } - - // hostClient init - sptr formHostClient = FormHostClient::GetInstance(); - if (formHostClient == nullptr) { - APP_LOGE("%{public}s error, formHostClient == nullptr.", __func__); - return false; - } - - int result = FormMgr::GetInstance().LifecycleUpdate(formIds, formHostClient, updateType); - if (result != ERR_OK) { - APP_LOGE("%{public}s error, internal error.", __func__); - return false; - } - - return true; -} - -/** - * @brief Requests for form data update, by passing a set of parameters (using Want) to the form provider. - * - * This method must be called when the application has detected that a system setting item (such as the language, - * resolution, or screen orientation) being listened for has changed. Upon receiving the update request, the form - * provider automatically updates the form data (if there is any update) through the form framework, with the update - * process being unperceivable by the application. - * - * @param formId Indicates the ID of the form to update. - * @param want Indicates a set of parameters to be transparently passed to the form provider. - * @return Returns true if the update request is successfully initiated, returns false otherwise. - */ -bool Ability::RequestForm(const int64_t formId, const Want &want) -{ - APP_LOGI("%{public}s called.", __func__); - if (formId <= 0) { - APP_LOGE("%{public}s error, The passed formid is invalid. Its value must be larger than 0.", __func__); - return false; - } - - if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) { - APP_LOGE("%{public}s error, form is in recover status, can't do action on form.", __func__); - return false; - } - - // requestForm request to fms - int resultCode = FormMgr::GetInstance().RequestForm(formId, FormHostClient::GetInstance(), want); - if (resultCode != ERR_OK) { - APP_LOGE("%{public}s error, failed to notify the form service that the form user's lifecycle is updated, error " - "code is %{public}d.", - __func__, - resultCode); - return false; - } - - return true; -} - -/** - * @brief Called to reacquire form and update the form host after the death callback is received. - * - */ -void Ability::OnDeathReceived() -{ - APP_LOGI("%{public}s called.", __func__); - int64_t formId; - std::map &userReqParams = userReqParams_; - std::vector &lostedTempForms = lostedByReconnectTempForms_; - for (const auto &userReqRaram : userReqParams) { - formId = userReqRaram.first; - Want want; - { - std::lock_guard lock(formLock); - want = userReqRaram.second; - if (want.GetBoolParam(Constants::PARAM_FORM_TEMPORARY_KEY, false) && - std::find(lostedTempForms.begin(), lostedTempForms.end(), formId) == lostedTempForms.end()) { - lostedTempForms.emplace_back(formId); - continue; - } - } - - bool result = ReAcquireForm(formId, want); - if (!result) { - APP_LOGI("%{public}s error, reacquire form failed, formId:%{public}" PRId64 ".", __func__, formId); - std::shared_ptr formCallback = nullptr; - { - std::lock_guard lock(formLock); - // get callback iterator by formId - std::map>::iterator appCallbackIterator = - appCallbacks_.find(formId); - - if (appCallbackIterator == appCallbacks_.end()) { - APP_LOGW("%{public}s error, lack of form callback for form, formId:%{public}" PRId64 ".", - __func__, - formId); - continue; - } - formCallback = appCallbackIterator->second; - } - if (formCallback == nullptr) { - APP_LOGW("%{public}s failed, callback is nullptr.", __func__); - continue; - } - - FormJsInfo formJsInfo; - formJsInfo.formId = formId; - formCallback->OnAcquired(FormCallback::OHOS_FORM_RESTORE_FAILURE, formJsInfo); - } - } -} - -/** - * @brief Reacquire a specified form when the death callback is received. - * - * @param formId Indicates the form ID. - * @param want Indicates the detailed information about the form to be obtained. - * @return Returns true if the request is successfully initiated; returns false otherwise. - */ -bool Ability::ReAcquireForm(const int64_t formId, const Want &want) -{ - APP_LOGI("%{public}s called.", __func__); - - // get the form host client - sptr formHostClient = FormHostClient::GetInstance(); - if (formHostClient == nullptr) { - APP_LOGE("%{public}s error, formHostClient is nullptr, formId:%{public}" PRId64 ".", __func__, formId); - return false; - } - - // reacquire form - FormJsInfo formJsInfo; - if (FormMgr::GetInstance().AddForm(formId, want, formHostClient, formJsInfo) != ERR_OK || formJsInfo.formId <= 0 || - formJsInfo.formId != formId) { - APP_LOGE("%{public}s error, fms reacquire form failed, formId:%{public}" PRId64 ".", __func__, formId); - return false; - } - - // handle update message of the obtained form instance. - ProcessFormUpdate(formJsInfo); - - return true; -} - -/** - * @brief Check form manager service ready. - * - * @return Returns true if form manager service ready; returns false otherwise. - */ -bool Ability::CheckFMSReady() -{ - APP_LOGI("%{public}s called.", __func__); - - sptr systemAbilityManager = - SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - auto remoteObject = systemAbilityManager->GetSystemAbility(FORM_MGR_SERVICE_ID); - if (remoteObject == nullptr) { - APP_LOGI("%{public}s, form manager service is not ready.", __func__); - return false; - } - - return true; -} - -/** - * @brief Get All FormsInfo. - * - * @param formInfos Returns the forms' information of all forms provided. - * @return Returns true if the request is successfully initiated; returns false otherwise. - */ -bool Ability::GetAllFormsInfo(std::vector &formInfos) -{ - APP_LOGI("%{public}s called.", __func__); - sptr iBundleMgr = GetBundleMgr(); - if (iBundleMgr == nullptr) { - APP_LOGE("%{public}s error, failed to get IBundleMgr.", __func__); - return false; - } - - if (!CheckPermission()) { - return false; - } - - return iBundleMgr->GetAllFormsInfo(formInfos); -} - -/** - * @brief Get forms info by application name. - * - * @param bundleName Application name. - * @param formInfos Returns the forms' information of the specify application name. - * @return Returns true if the request is successfully initiated; returns false otherwise. - */ -bool Ability::GetFormsInfoByApp(std::string &bundleName, std::vector &formInfos) -{ - APP_LOGI("%{public}s called.", __func__); - bool IsGetFormsInfoByApp = false; - if (bundleName.empty()) { - APP_LOGW("save info fail, empty bundle name"); - IsGetFormsInfoByApp = false; - } - - sptr iBundleMgr = GetBundleMgr(); - if (iBundleMgr == nullptr) { - APP_LOGE("%{public}s error, failed to get IBundleMgr.", __func__); - return IsGetFormsInfoByApp; - } - - if (!CheckPermission()) { - return IsGetFormsInfoByApp; - } - - IsGetFormsInfoByApp = iBundleMgr->GetFormsInfoByApp(bundleName, formInfos); - if (formInfos.size() == 0) { - return IsGetFormsInfoByApp; - } - - return IsGetFormsInfoByApp; -} - -/** - * @brief Get forms info by application name and module name. - * - * @param bundleName Application name. - * @param moduleName Module name of hap. - * @param formInfos Returns the forms' information of the specify application name and module name. - * @return Returns true if the request is successfully initiated; returns false otherwise. - */ -bool Ability::GetFormsInfoByModule(std::string &bundleName, std::string &moduleName, std::vector &formInfos) -{ - APP_LOGI("%{public}s called.", __func__); - bool IsGetFormsInfoByModule = false; - if (bundleName.empty() || moduleName.empty()) { - APP_LOGW("save info fail, empty bundle name"); - return IsGetFormsInfoByModule; - } - - sptr iBundleMgr = GetBundleMgr(); - if (iBundleMgr == nullptr) { - APP_LOGE("%{public}s error, failed to get IBundleMgr.", __func__); - return IsGetFormsInfoByModule; - } - - if (!CheckPermission()) { - return IsGetFormsInfoByModule; - } - - IsGetFormsInfoByModule = iBundleMgr->GetFormsInfoByModule(bundleName, moduleName, formInfos); - if (formInfos.size() == 0) { - IsGetFormsInfoByModule = false; - } - return IsGetFormsInfoByModule; -} - -/** - * @brief Acquire a bundle manager, if it not existed. - * @return returns the bundle manager ipc object, or nullptr for failed. - */ -sptr Ability::GetBundleMgr() -{ - APP_LOGI("%{public}s called.", __func__); - if (iBundleMgr_ == nullptr) { - sptr systemAbilityManager = - SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - auto remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); - if (remoteObject == nullptr) { - APP_LOGE("%{public}s error, failed to get bundle manager service.", __func__); - return nullptr; - } - - iBundleMgr_ = iface_cast(remoteObject); - if (iBundleMgr_ == nullptr) { - APP_LOGE("%{public}s error, failed to get bundle manager service", __func__); - return nullptr; - } - } - - return iBundleMgr_; -} - -/** - * @brief check permission of bundle, if it not existed. - * @return returns the permission is vaild, or false for failed. - */ -bool Ability::CheckPermission() -{ - APP_LOGI("%{public}s called.", __func__); - return true; -} - -bool Ability::CheckFormPermission(const std::string &bundleName) const -{ - APP_LOGI("%{public}s called.", __func__); - - int result = PermissionKit::VerifyPermission(bundleName, Constants::PERMISSION_REQUIRE_FORM, 0); - if (result != PermissionState::PERMISSION_GRANTED) { - APP_LOGW("permission = %{public}s, bundleName = %{public}s, result = %{public}d", - Constants::PERMISSION_REQUIRE_FORM.c_str(), - bundleName.c_str(), - result); - } - return result == PermissionState::PERMISSION_GRANTED; -} -/** - * @brief Add the bundle manager instance for debug. - * @param bundleManager the bundle manager ipc object. - */ -void Ability::SetBundleManager(const sptr &bundleManager) -{ - APP_LOGI("%{public}s called.", __func__); - - iBundleMgr_ = bundleManager; -} - -/** - * @brief Acquire a form provider remote object. - * @return Returns form provider remote object. - */ -sptr Ability::GetFormRemoteObject() -{ - APP_LOGI("%{public}s start", __func__); - if (providerRemoteObject_ == nullptr) { - sptr providerClient = new (std::nothrow) FormProviderClient(); - std::shared_ptr thisAbility = this->shared_from_this(); - if (thisAbility == nullptr) { - APP_LOGE("%{public}s failed, thisAbility is nullptr", __func__); - } - providerClient->SetOwner(thisAbility); - providerRemoteObject_ = providerClient->AsObject(); - } - APP_LOGI("%{public}s end", __func__); - return providerRemoteObject_; -} - -/** - * @brief Set the start ability setting. - * @param setting the start ability setting. - */ -void Ability::SetStartAbilitySetting(std::shared_ptr setting) -{ - APP_LOGI("%{public}s called.", __func__); - setting_ = setting; -} - -std::vector> Ability::ExecuteBatch( - const std::vector> &operations) -{ - APP_LOGI("Ability::ExecuteBatch start"); - std::vector> results; - if (abilityInfo_ == nullptr) { - APP_LOGE("Ability::ExecuteBatch abilityInfo is nullptr"); - return results; - } - if (abilityInfo_->type != AppExecFwk::AbilityType::DATA) { - APP_LOGE("Ability::ExecuteBatch data ability type failed, current type: %{public}d", abilityInfo_->type); - return results; - } - size_t len = operations.size(); - APP_LOGI("Ability::ExecuteBatch operation is nullptr, len %{public}zu", len); - for (size_t i = 0; i < len; i++) { - std::shared_ptr operation = operations[i]; - if (operation == nullptr) { - APP_LOGI("Ability::ExecuteBatch operation is nullptr, create DataAbilityResult"); - results.push_back(std::make_shared(0)); - continue; - } - ExecuteOperation(operation, results, i); - } - APP_LOGI("Ability::ExecuteBatch end, %{public}zu", results.size()); - return results; -} -void Ability::ExecuteOperation(std::shared_ptr &operation, - std::vector> &results, int index) -{ - APP_LOGI("Ability::ExecuteOperation start, index=%{public}d", index); - if (abilityInfo_->type != AppExecFwk::AbilityType::DATA) { - APP_LOGE("Ability::ExecuteOperation data ability type failed, current type: %{public}d", abilityInfo_->type); - return; - } - if (index < 0) { - APP_LOGE( - "Ability::ExecuteOperation operation result index should not below zero, current index: %{public}d", index); - return; - } - if (operation == nullptr) { - APP_LOGI("Ability::ExecuteOperation operation is nullptr, create DataAbilityResult"); - results.push_back(std::make_shared(0)); - return; - } - - int numRows = 0; - std::shared_ptr valuesBucket = ParseValuesBucketReference(results, operation, index); - std::shared_ptr predicates = - ParsePredictionArgsReference(results, operation, index); - if (operation->IsInsertOperation()) { - APP_LOGI("Ability::ExecuteOperation IsInsertOperation"); - numRows = Insert(*(operation->GetUri().get()), *valuesBucket); - } else if (operation->IsDeleteOperation()) { - APP_LOGI("Ability::ExecuteOperation IsDeleteOperation"); - numRows = Delete(*(operation->GetUri().get()), *predicates); - } else if (operation->IsUpdateOperation()) { - APP_LOGI("Ability::ExecuteOperation IsUpdateOperation"); - numRows = Update(*(operation->GetUri().get()), *valuesBucket, *predicates); - } else if (operation->IsAssertOperation()) { - APP_LOGI("Ability::ExecuteOperation IsAssertOperation"); - std::vector columns; - std::shared_ptr queryResult = - Query(*(operation->GetUri().get()), columns, *predicates); - if (queryResult == nullptr) { - APP_LOGE("Ability::ExecuteOperation Query retval is nullptr"); - results.push_back(std::make_shared(0)); - return; - } - if (queryResult->GetRowCount(numRows) != 0) { - APP_LOGE("Ability::ExecuteOperation queryResult->GetRowCount(numRows) != E_OK"); - } - if (!CheckAssertQueryResult(queryResult, operation->GetValuesBucket())) { - if (queryResult != nullptr) { - queryResult->Close(); - } - APP_LOGE("Query Result is not equal to expected value."); - } - - if (queryResult != nullptr) { - queryResult->Close(); - } - } else { - APP_LOGE("Ability::ExecuteOperation Expected bad type %{public}d", operation->GetType()); - } - if (operation->GetExpectedCount() != numRows) { - APP_LOGE("Ability::ExecuteOperation Expected %{public}d rows but actual %{public}d", - operation->GetExpectedCount(), - numRows); - } else { - if (operation->GetUri() != nullptr) { - results.push_back(std::make_shared(*operation->GetUri(), numRows)); - } else { - results.push_back(std::make_shared(Uri(std::string("")), numRows)); - } - } -} - -std::shared_ptr Ability::ParsePredictionArgsReference( - std::vector> &results, std::shared_ptr &operation, - int numRefs) -{ - if (operation == nullptr) { - APP_LOGE("Ability::ParsePredictionArgsReference intpur is nullptr"); - return nullptr; - } - - std::map predicatesBackReferencesMap = operation->GetDataAbilityPredicatesBackReferences(); - if (predicatesBackReferencesMap.empty()) { - return operation->GetDataAbilityPredicates(); - } - - std::vector strPredicatesList; - strPredicatesList.clear(); - std::shared_ptr predicates = operation->GetDataAbilityPredicates(); - if (predicates == nullptr) { - APP_LOGI("Ability::ParsePredictionArgsReference operation->GetDataAbilityPredicates is nullptr"); - } else { - APP_LOGI("Ability::ParsePredictionArgsReference operation->GetDataAbilityPredicates isn`t nullptr"); - std::vector strPredicatesList = predicates->GetWhereArgs(); - } - - if (strPredicatesList.empty()) { - APP_LOGE("Ability::ParsePredictionArgsReference operation->GetDataAbilityPredicates()->GetWhereArgs()" - "error strList is empty()"); - } - - for (auto iterMap : predicatesBackReferencesMap) { - APP_LOGI("Ability::ParsePredictionArgsReference predicatesBackReferencesMap first:%{public}d second:%{public}d", - iterMap.first, - iterMap.second); - int tempCount = ChangeRef2Value(results, numRefs, iterMap.second); - if (tempCount < 0) { - APP_LOGE("Ability::ParsePredictionArgsReference tempCount:%{public}d", tempCount); - continue; - } - std::string strPredicates = std::to_string(tempCount); - APP_LOGI("Ability::ParsePredictionArgsReference strPredicates:%{public}s", strPredicates.c_str()); - strPredicatesList.push_back(strPredicates); - APP_LOGI("Ability::ParsePredictionArgsReference push_back done"); - } - - predicates->SetWhereArgs(strPredicatesList); - - return predicates; -} - -std::shared_ptr Ability::ParseValuesBucketReference( - std::vector> &results, std::shared_ptr &operation, - int numRefs) -{ - NativeRdb::ValuesBucket retValueBucket; - if (operation == nullptr) { - APP_LOGE("Ability::ParseValuesBucketReference intpur is nullptr"); - return nullptr; - } - - if (operation->GetValuesBucketReferences() == nullptr) { - return operation->GetValuesBucket(); - } - - retValueBucket.Clear(); - if (operation->GetValuesBucket() == nullptr) { - APP_LOGI("Ability::ParseValuesBucketReference operation->GetValuesBucket is nullptr"); - } else { - APP_LOGI("Ability::ParseValuesBucketReference operation->GetValuesBucket is nullptr"); - retValueBucket = *operation->GetValuesBucket(); - } - - std::map valuesMapReferences; - operation->GetValuesBucketReferences()->GetAll(valuesMapReferences); - - for (auto itermap : valuesMapReferences) { - std::string key = itermap.first; - NativeRdb::ValueObject obj; - if (!operation->GetValuesBucketReferences()->GetObject(key, obj)) { - APP_LOGE("Ability::ParseValuesBucketReference operation->GetValuesBucketReferences()->GetObject error"); - continue; - } - switch (obj.GetType()) { - case NativeRdb::ValueObjectType::TYPE_INT: { - int val = 0; - if (obj.GetInt(val) != 0) { - APP_LOGE("Ability::ParseValuesBucketReference ValueObject->GetInt() error"); - break; - } - APP_LOGI("Ability::ParseValuesBucketReference retValueBucket->PutInt(%{public}s, %{public}d)", - key.c_str(), - val); - retValueBucket.PutInt(key, val); - } break; - case NativeRdb::ValueObjectType::TYPE_DOUBLE: { - double val = 0.0; - if (obj.GetDouble(val) != 0) { - APP_LOGE("Ability::ParseValuesBucketReference ValueObject->GetDouble() error"); - break; - } - APP_LOGI("Ability::ParseValuesBucketReference retValueBucket->PutDouble(%{public}s, %{public}f)", - key.c_str(), - val); - retValueBucket.PutDouble(key, val); - } break; - case NativeRdb::ValueObjectType::TYPE_STRING: { - std::string val = ""; - if (obj.GetString(val) != 0) { - APP_LOGE("Ability::ParseValuesBucketReference ValueObject->GetString() error"); - break; - } - APP_LOGI("Ability::ParseValuesBucketReference retValueBucket->PutString(%{public}s, %{public}s)", - key.c_str(), - val.c_str()); - retValueBucket.PutString(key, val); - } break; - case NativeRdb::ValueObjectType::TYPE_BLOB: { - std::vector val; - if (obj.GetBlob(val) != 0) { - APP_LOGE("Ability::ParseValuesBucketReference ValueObject->GetBlob() error"); - break; - } - APP_LOGI("Ability::ParseValuesBucketReference retValueBucket->PutBlob(%{public}s, %{public}zu)", - key.c_str(), - val.size()); - retValueBucket.PutBlob(key, val); - } break; - case NativeRdb::ValueObjectType::TYPE_BOOL: { - bool val = false; - if (obj.GetBool(val) != 0) { - APP_LOGE("Ability::ParseValuesBucketReference ValueObject->GetBool() error"); - break; - } - APP_LOGI("Ability::ParseValuesBucketReference retValueBucket->PutBool(%{public}s, %{public}s)", - key.c_str(), - val ? "true" : "false"); - retValueBucket.PutBool(key, val); - } break; - default: { - APP_LOGI("Ability::ParseValuesBucketReference retValueBucket->PutNull(%{public}s)", key.c_str()); - retValueBucket.PutNull(key); - } break; - } - } - - std::map valuesMap; - retValueBucket.GetAll(valuesMap); - - return std::make_shared(valuesMap); -} - -int Ability::ChangeRef2Value(std::vector> &results, int numRefs, int index) -{ - int retval = -1; - if (index >= numRefs) { - APP_LOGE("Ability::ChangeRef2Value index >= numRefs"); - return retval; - } - - if (index >= static_cast(results.size())) { - APP_LOGE("Ability::ChangeRef2Value index:%{public}d >= results.size():%{public}zu", index, results.size()); - return retval; - } - - std::shared_ptr refResult = results[index]; - if (refResult == nullptr) { - APP_LOGE("Ability::ChangeRef2Value No.%{public}d refResult is null", index); - return retval; - } - - if (refResult->GetUri().ToString().empty()) { - retval = refResult->GetCount(); - } else { - retval = DataUriUtils::GetId(refResult->GetUri()); - } - - return retval; -} - -bool Ability::CheckAssertQueryResult(std::shared_ptr &queryResult, - std::shared_ptr &&valuesBucket) -{ - if (queryResult == nullptr) { - APP_LOGE("Ability::CheckAssertQueryResult intput queryResult is null"); - return true; - } - - if (valuesBucket == nullptr) { - APP_LOGE("Ability::CheckAssertQueryResult intput valuesBucket is null"); - return true; - } - - std::map valuesMap; - valuesBucket->GetAll(valuesMap); - if (valuesMap.empty()) { - APP_LOGE("Ability::CheckAssertQueryResult valuesMap is empty"); - return true; - } - int count = 0; - if (queryResult->GetRowCount(count) != 0) { - APP_LOGE("Ability::CheckAssertQueryResult GetRowCount is 0"); - return true; - } - - for (auto iterMap : valuesMap) { - std::string strObject; - if (iterMap.second.GetString(strObject) != 0) { - APP_LOGE("Ability::CheckAssertQueryResult GetString strObject is error"); - continue; - } - if (strObject.empty()) { - APP_LOGE("Ability::CheckAssertQueryResult strObject is empty"); - continue; - } - for (int i = 0; i < count; ++i) { - std::string strName; - if (queryResult->GetString(i, strName) != 0) { - APP_LOGE("Ability::CheckAssertQueryResult GetString strName is error"); - continue; - } - if (strName.empty()) { - APP_LOGE("Ability::CheckAssertQueryResult strName is empty"); - continue; - } - if (strName.c_str() == strObject.c_str()) { - APP_LOGE("Ability::CheckAssertQueryResult strName same to strObject"); - continue; - } - - return false; - } - } - - return true; -} -} // namespace AppExecFwk -} // namespace OHOS +/* + * Copyright (c) 2021 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.h" +#include +#include + +#include "ability_loader.h" +#include "app_log_wrapper.h" +#include "display_type.h" +#include "form_provider_client.h" +#include "if_system_ability_manager.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" +#include "task_handler_client.h" +#include "ohos_application.h" +#include "continuation_manager.h" +#include "continuation_register_manager.h" +#include "continuation_register_manager_proxy.h" +#include "reverse_continuation_scheduler_primary.h" +#include "reverse_continuation_scheduler_replica.h" +#include "reverse_continuation_scheduler_replica_handler_interface.h" +#include "string_wrapper.h" +#include "permission/permission.h" +#include "permission/permission_kit.h" +#include "abs_shared_result_set.h" +#include "data_ability_predicates.h" +#include "values_bucket.h" +#include "ability_post_event_timeout.h" +#include "data_ability_result.h" +#include "data_ability_operation.h" +#include "data_uri_utils.h" + + +#include "key_event.h" +#include "touch_event.h" +#include "form_host_client.h" +#include "form_mgr.h" +#include "ipc_skeleton.h" + +namespace OHOS { +namespace AppExecFwk { +using PermissionKit = OHOS::Security::Permission::PermissionKit; +using PermissionState = OHOS::Security::Permission::PermissionState; + +REGISTER_AA(Ability) +const std::string Ability::SYSTEM_UI("com.ohos.systemui"); +const std::string Ability::STATUS_BAR("com.ohos.systemui.statusbar.MainAbility"); +const std::string Ability::NAVIGATION_BAR("com.ohos.systemui.navigationbar.MainAbility"); +const std::string DEVICE_MANAGER_BUNDLE_NAME = "com.ohos.devicemanagerui"; +const std::string DEVICE_MANAGER_NAME = "com.ohos.devicemanagerui.MainAbility"; +const std::string Ability::DMS_SESSION_ID("sessionId"); +const std::string Ability::DMS_ORIGIN_DEVICE_ID("deviceId"); +const int Ability::DEFAULT_DMS_SESSION_ID(0); +const std::string PERMISSION_REQUIRE_FORM = "ohos.permission.REQUIRE_FORM"; + +static std::mutex formLock; + +constexpr int64_t SEC_TO_MILLISEC = 1000; +constexpr int64_t MILLISEC_TO_NANOSEC = 1000000; + +void Ability::Init(const std::shared_ptr &abilityInfo, const std::shared_ptr &application, + std::shared_ptr &handler, const sptr &token) +{ + APP_LOGI("%{public}s begin.", __func__); + abilityInfo_ = abilityInfo; + handler_ = handler; + AbilityContext::token_ = token; + + // page ability only. + if (abilityInfo_->type == AbilityType::PAGE) { + abilityWindow_ = std::make_shared(); + + if (abilityWindow_ != nullptr) { + APP_LOGI("%{public}s begin abilityWindow_->Init", __func__); + abilityWindow_->Init(handler_, shared_from_this()); + APP_LOGI("%{public}s end abilityWindow_->Init", __func__); + } + continuationManager_ = std::make_shared(); + std::weak_ptr ability = shared_from_this(); + std::weak_ptr continuationManager = continuationManager_; + continuationHandler_ = std::make_shared(continuationManager, ability); + if (!continuationManager_->Init(shared_from_this(), GetToken(), GetAbilityInfo(), continuationHandler_)) { + continuationManager_.reset(); + } else { + std::weak_ptr continuationHandler = continuationHandler_; + sptr Primary = sptr( + new (std::nothrow) ReverseContinuationSchedulerPrimary(continuationHandler, handler_)); + if (Primary == nullptr) { + APP_LOGE("Ability::Init failed,Primary create failed"); + } else { + continuationHandler_->SetPrimaryStub(Primary); + continuationHandler_->SetAbilityInfo(abilityInfo_); + } + } + } + lifecycle_ = std::make_shared(); + abilityLifecycleExecutor_ = std::make_shared(); + if (abilityLifecycleExecutor_ != nullptr) { + abilityLifecycleExecutor_->DispatchLifecycleState(AbilityLifecycleExecutor::LifecycleState::INITIAL); + } else { + APP_LOGE("%{public}s abilityLifecycleExecutor_ make failed.", __func__); + } + + application_ = application; + APP_LOGI("%{public}s end.", __func__); +} + +/** + * @brief Obtains a resource manager. + * + * @return Returns a ResourceManager object. + */ +std::shared_ptr Ability::GetResourceManager() const +{ + return AbilityContext::GetResourceManager(); +} + +/** + * Will be called when ability start. You should override this function + * + * @param want ability start information + */ +void Ability::OnStart(const Want &want) +{ + APP_LOGI("%{public}s begin.", __func__); + if (abilityInfo_ == nullptr) { + APP_LOGE("Ability::OnStart falied abilityInfo_ is nullptr."); + return; + } + + if (abilityInfo_->type == AppExecFwk::AbilityType::PAGE) { + sptr config = WindowOption::Get(); + + OHOS::WindowType winType = OHOS::WindowType::WINDOW_TYPE_NORMAL; + if (abilityInfo_->bundleName == SYSTEM_UI) { + if (abilityInfo_->name == STATUS_BAR) { + winType = OHOS::WindowType::WINDOW_TYPE_STATUS_BAR; + } + if (abilityInfo_->name == NAVIGATION_BAR) { + winType = OHOS::WindowType::WINDOW_TYPE_NAVI_BAR; + } + } + if (abilityInfo_->bundleName == OHOS_REQUEST_PERMISSION_BUNDLENAME && + abilityInfo_->name == OHOS_REQUEST_PERMISSION_ABILITY_NAME) { + winType = OHOS::WindowType::WINDOW_TYPE_ALARM_SCREEN; + } + + if (abilityInfo_->bundleName == DEVICE_MANAGER_BUNDLE_NAME && abilityInfo_->name == DEVICE_MANAGER_NAME) { + winType = OHOS::WindowType::WINDOW_TYPE_ALARM_SCREEN; + } + + config->SetWindowType(winType); + APP_LOGI("Ability::OnStart bundleName:%{public}s abilityName:%{public}s: set config.type = %{public}d", + abilityInfo_->bundleName.c_str(), + abilityInfo_->name.c_str(), + winType); + if (setting_ != nullptr) { + auto windowMode = static_cast( + std::atoi(setting_->GetProperty(AbilityStartSetting::WINDOW_MODE_KEY).c_str())); + APP_LOGI("%{public}s windowMode : %{public}d", __func__, windowMode); + if (windowMode == AbilityWindowConfiguration::MULTI_WINDOW_DISPLAY_FLOATING) { + APP_LOGI("%{public}s begin SetWindowMode : WINDOW_MODE_FREE.", __func__); + config->SetWindowType(WINDOW_TYPE_FLOAT); + APP_LOGI("%{public}s end SetWindowMode : WINDOW_MODE_FREE.", __func__); + } + } else { + APP_LOGI("Ability::OnStart setting_ == nullptr."); + } + SetUIContent(config); + + if (abilityWindow_ != nullptr) { + APP_LOGI("%{public}s begin abilityWindow_->OnPostAbilityStart.", __func__); + abilityWindow_->OnPostAbilityStart(); + APP_LOGI("%{public}s end abilityWindow_->OnPostAbilityStart.", __func__); + } + } + + SetWant(want); + if (abilityLifecycleExecutor_ == nullptr) { + APP_LOGE("Ability::OnStart error. abilityLifecycleExecutor_ == nullptr."); + return; + } + abilityLifecycleExecutor_->DispatchLifecycleState(AbilityLifecycleExecutor::LifecycleState::INACTIVE); + + if (lifecycle_ == nullptr) { + APP_LOGE("Ability::OnStart error. lifecycle_ == nullptr."); + return; + } + lifecycle_->DispatchLifecycle(LifeCycle::Event::ON_START, want); + APP_LOGI("%{public}s end.", __func__); +} + +/** + * @brief Called when this ability enters the STATE_STOP state. + * + * The ability in the STATE_STOP is being destroyed. + * You can override this function to implement your own processing logic. + */ +void Ability::OnStop() +{ + APP_LOGI("%{public}s begin.", __func__); + if (continuationManager_ != nullptr) { + continuationManager_->UnregisterAbilityTokenIfNeed(); + } else { + APP_LOGE("%{public}s continuationManager_ is nullptr.", __func__); + } + + if (abilityWindow_ != nullptr && abilityInfo_->type == AppExecFwk::AbilityType::PAGE) { + APP_LOGI("%{public}s begin abilityWindow_->OnPostAbilityStop.", __func__); + abilityWindow_->OnPostAbilityStop(); + APP_LOGI("%{public}s end abilityWindow_->OnPostAbilityStop.", __func__); + } + if (abilityLifecycleExecutor_ == nullptr) { + APP_LOGE("Ability::OnStop error. abilityLifecycleExecutor_ == nullptr."); + return; + } + abilityLifecycleExecutor_->DispatchLifecycleState(AbilityLifecycleExecutor::LifecycleState::INITIAL); + + if (lifecycle_ == nullptr) { + APP_LOGE("Ability::OnStop error. lifecycle_ == nullptr."); + return; + } + lifecycle_->DispatchLifecycle(LifeCycle::Event::ON_STOP); + APP_LOGI("%{public}s end.", __func__); +} + +/** + * @brief Called when this ability enters the STATE_ACTIVE state. + * + * The ability in the STATE_ACTIVE state is visible and has focus. + * You can override this function to implement your own processing logic. + * + * @param Want Indicates the {@link Want} structure containing activation information about the ability. + */ +void Ability::OnActive() +{ + APP_LOGI("%{public}s begin.", __func__); + if (abilityWindow_ != nullptr) { + APP_LOGI("%{public}s begin abilityWindow_->OnPostAbilityActive.", __func__); + abilityWindow_->OnPostAbilityActive(); + APP_LOGI("%{public}s end abilityWindow_->OnPostAbilityActive.", __func__); + } + bWindowFocus_ = true; + if (abilityLifecycleExecutor_ == nullptr) { + APP_LOGE("Ability::OnActive error. abilityLifecycleExecutor_ == nullptr."); + return; + } + abilityLifecycleExecutor_->DispatchLifecycleState(AbilityLifecycleExecutor::LifecycleState::ACTIVE); + + if (lifecycle_ == nullptr) { + APP_LOGE("Ability::OnActive error. lifecycle_ == nullptr."); + return; + } + lifecycle_->DispatchLifecycle(LifeCycle::Event::ON_ACTIVE); + APP_LOGI("%{public}s end.", __func__); +} + +/** + * @brief Called when this ability enters the STATE_INACTIVE state. + * + * STATE_INACTIVE is an instantaneous state. The ability in this state may be visible but does not have + * focus.You can override this function to implement your own processing logic. + */ +void Ability::OnInactive() +{ + APP_LOGI("%{public}s begin.", __func__); + if (abilityWindow_ != nullptr && abilityInfo_->type == AppExecFwk::AbilityType::PAGE) { + APP_LOGI("%{public}s begin abilityWindow_->OnPostAbilityInactive.", __func__); + abilityWindow_->OnPostAbilityInactive(); + APP_LOGI("%{public}s end abilityWindow_->OnPostAbilityInactive.", __func__); + } + bWindowFocus_ = false; + if (abilityLifecycleExecutor_ == nullptr) { + APP_LOGE("Ability::OnInactive error. abilityLifecycleExecutor_ == nullptr."); + return; + } + abilityLifecycleExecutor_->DispatchLifecycleState(AbilityLifecycleExecutor::LifecycleState::INACTIVE); + + if (lifecycle_ == nullptr) { + APP_LOGE("Ability::OnInactive error. lifecycle_ == nullptr."); + return; + } + lifecycle_->DispatchLifecycle(LifeCycle::Event::ON_INACTIVE); + APP_LOGI("%{public}s end.", __func__); +} + +/** + * @brief Called when this ability enters the STATE_FOREGROUND state. + * + * + * The ability in the STATE_FOREGROUND state is visible. + * You can override this function to implement your own processing logic. + */ +void Ability::OnForeground(const Want &want) +{ + APP_LOGI("%{public}s begin.", __func__); + if (abilityWindow_ != nullptr) { + APP_LOGI("%{public}s begin abilityWindow_->OnPostAbilityForeground.", __func__); + abilityWindow_->OnPostAbilityForeground(); + APP_LOGI("%{public}s end abilityWindow_->OnPostAbilityForeground.", __func__); + } + + if (abilityLifecycleExecutor_ == nullptr) { + APP_LOGE("Ability::OnForeground error. abilityLifecycleExecutor_ == nullptr."); + return; + } + abilityLifecycleExecutor_->DispatchLifecycleState(AbilityLifecycleExecutor::LifecycleState::INACTIVE); + + if (lifecycle_ == nullptr) { + APP_LOGE("Ability::OnForeground error. lifecycle_ == nullptr."); + return; + } + lifecycle_->DispatchLifecycle(LifeCycle::Event::ON_FOREGROUND, want); + APP_LOGI("%{public}s end.", __func__); +} + +/** + * @brief Called when this ability enters the STATE_BACKGROUND state. + * + * + * The ability in the STATE_BACKGROUND state is invisible. + * You can override this function to implement your own processing logic. + */ +void Ability::OnBackground() +{ + APP_LOGI("%{public}s begin.", __func__); + if (abilityWindow_ != nullptr && abilityInfo_->type == AppExecFwk::AbilityType::PAGE) { + APP_LOGI("%{public}s begin abilityWindow_->OnPostAbilityBackground.", __func__); + abilityWindow_->OnPostAbilityBackground(); + APP_LOGI("%{public}s end abilityWindow_->OnPostAbilityBackground.", __func__); + } + + if (abilityLifecycleExecutor_ == nullptr) { + APP_LOGE("Ability::OnBackground error. abilityLifecycleExecutor_ == nullptr."); + return; + } + abilityLifecycleExecutor_->DispatchLifecycleState(AbilityLifecycleExecutor::LifecycleState::BACKGROUND); + + if (lifecycle_ == nullptr) { + APP_LOGE("Ability::OnBackground error. lifecycle_ == nullptr."); + return; + } + lifecycle_->DispatchLifecycle(LifeCycle::Event::ON_BACKGROUND); + APP_LOGI("%{public}s end.", __func__); +} + +/** + * @brief Called when this Service ability is connected for the first time. + * + * You can override this function to implement your own processing logic. + * + * @param want Indicates the {@link Want} structure containing connection information about the Service ability. + * @return Returns a pointer to the sid of the connected Service ability. + */ +sptr Ability::OnConnect(const Want &want) +{ + APP_LOGI("%{public}s begin.", __func__); + if (abilityLifecycleExecutor_ == nullptr) { + APP_LOGE("Ability::OnConnect error. abilityLifecycleExecutor_ == nullptr."); + return nullptr; + } + abilityLifecycleExecutor_->DispatchLifecycleState(AbilityLifecycleExecutor::LifecycleState::ACTIVE); + + if (lifecycle_ == nullptr) { + APP_LOGE("Ability::OnConnect error. lifecycle_ == nullptr."); + return nullptr; + } + lifecycle_->DispatchLifecycle(LifeCycle::Event::ON_ACTIVE); + APP_LOGI("%{public}s end.", __func__); + return nullptr; +} + +/** + * @brief Called when all abilities connected to this Service ability are disconnected. + * + * You can override this function to implement your own processing logic. + * + */ +void Ability::OnDisconnect(const Want &want) +{} + +/** + * Start other ability for result. + * + * @param want information of other ability + * @param requestCode request code for abilityMS to return result + */ +void Ability::StartAbilityForResult(const Want &want, int requestCode) +{ + APP_LOGI("%{public}s begin.", __func__); + if (abilityInfo_ == nullptr) { + APP_LOGE("Ability::StartAbilityForResult abilityInfo_ == nullptr"); + return; + } + APP_LOGI("Ability::StartAbilityForResult called type = %{public}d", abilityInfo_->type); + if (abilityInfo_->type == AppExecFwk::AbilityType::PAGE) { + AbilityContext::StartAbility(want, requestCode); + } + APP_LOGI("%{public}s end.", __func__); +} + +/** + * Starts an ability with specific start settings and returns the execution result when the ability is destroyed. + * When the ability is destroyed, onAbilityResult(int,int,ohos.aafwk.content.Want) is called and the returned + * requestCode is transferred to the current method. The given requestCode is customized and cannot be a negative + * number. + * + * @param want Indicates the ability to start. + * @param requestCode Indicates the request code returned after the ability is started. You can define the request code + * to identify the results returned by abilities. The value ranges from 0 to 65535. + * @param abilityStartSetting Indicates the setting ability used to start. + */ +void Ability::StartAbilityForResult(const Want &want, int requestCode, AbilityStartSetting abilityStartSetting) +{ + APP_LOGI("%{public}s begin.", __func__); + if (abilityInfo_ == nullptr) { + APP_LOGE("Ability::StartAbilityForResult abilityInfo_ == nullptr"); + return; + } + APP_LOGI("Ability::StartAbilityForResult called type = %{public}d", abilityInfo_->type); + if (abilityInfo_->type == AppExecFwk::AbilityType::PAGE) { + AbilityContext::StartAbility(want, requestCode, abilityStartSetting); + } + APP_LOGI("%{public}s end.", __func__); +} + +/** + * Starts a new ability with specific start settings. + * A Page or Service ability uses this method to start a specific ability. + * The system locates the target ability from installed abilities based on + * the value of the intent parameter and then starts it. You can specify the + * ability to start using the intent parameter. + * + * @param want Indicates the ability to start. + * @param abilityStartSetting Indicates the setting ability used to start. + */ +void Ability::StartAbility(const Want &want, AbilityStartSetting abilityStartSetting) +{ + APP_LOGI("%{public}s beign.", __func__); + if (abilityInfo_ == nullptr) { + APP_LOGE("Ability::StartAbility abilityInfo_ == nullptr"); + return; + } + APP_LOGI("Ability::StartAbility called type = %{public}d", abilityInfo_->type); + if (abilityInfo_->type == AppExecFwk::AbilityType::PAGE || abilityInfo_->type == AppExecFwk::AbilityType::SERVICE) { + AbilityContext::StartAbility(want, -1, abilityStartSetting); + } + APP_LOGI("%{public}s end.", __func__); +} + +/** + * @brief Called when a key is pressed. When any component in the Ability gains focus, the key-down event for + * the component will be handled first. This callback will not be invoked if the callback triggered for the + * key-down event of the component returns true. The default implementation of this callback does nothing + * and returns false. + * + * @param keyCode Indicates the code of the key pressed. + * @param keyEvent Indicates the key-down event. + * + * @return Returns true if this event is handled and will not be passed further; returns false if this event + * is not handled and should be passed to other handlers. + */ +bool Ability::OnKeyDown(int keyCode, const KeyEvent &keyEvent) +{ + APP_LOGI("Ability::OnKeyDown called"); + APP_LOGI("Ability::OnKeyDown keyCode: %{public}d.", keyCode); + return false; +} + +/** + * @brief Called when a key is released. When any component in the Ability gains focus, the key-up event for + * the component will be handled first. This callback will not be invoked if the callback triggered for the + * key-up event of the component returns true. The default implementation of this callback does nothing and + * returns false. + * + * @param keyCode Indicates the code of the key released. + * @param keyEvent Indicates the key-up event. + * + * @return Returns true if this event is handled and will not be passed further; returns false if this event + * is not handled and should be passed to other handlers. + */ +bool Ability::OnKeyUp(int keyCode, const KeyEvent &keyEvent) +{ + APP_LOGI("Ability::OnKeyUp called"); + APP_LOGI("Ability::OnKeyUp keyCode: %{public}d.", keyCode); + switch (keyCode) { + case OHOS::KeyEventEnum::KEY_BACK: + APP_LOGI("Ability::OnKey Back key pressed."); + OnBackPressed(); + return true; + default: + break; + } + return false; +} + +/** + * @brief Called when a touch event is dispatched to this ability. The default implementation of this callback + * does nothing and returns false. + * + * @param event Indicates information about the touch event. + * + * @return Returns true if the event is handled; returns false otherwise. + */ +bool Ability::OnTouchEvent(const TouchEvent &touchEvent) +{ + APP_LOGI("Ability::OnTouchEvent called"); + APP_LOGI("Ability::OnTouchEvent action: %{public}d phase: %{public}d", + const_cast(touchEvent).GetAction(), + const_cast(touchEvent).GetPhase()); + return false; +} + +/** + * @brief Inflates UI controls by using ComponentContainer. + * You can create a ComponentContainer instance that contains multiple components. + * + * @param componentContainer Indicates a set of customized components. + */ +void Ability::SetUIContent(const ComponentContainer &componentContainer) +{} + +/** + * @brief Inflates layout resources by using the layout resource ID. + * + * @param layoutRes Indicates the layout resource ID, which cannot be a negative number. + */ +void Ability::SetUIContent(int layoutRes) +{} + +/** + * @brief Inflates UI controls by using ComponentContainer. + * You can create a ComponentContainer instance that contains multiple components. + * + * @param componentContainer Indicates the component layout defined by the user. + * @param context Indicates the context to use. + * @param typeFlag Indicates the window type. + */ +void Ability::SetUIContent( + const ComponentContainer &componentContainer, std::shared_ptr &context, int typeFlag) +{} + +/** + * @brief Inflates layout resources by using the layout resource ID. + * + * @param layoutRes Indicates the layout resource ID, which cannot be a negative number. + * @param context Indicates the context to use. + * @param typeFlag Indicates the window type. + */ +void Ability::SetUIContent(int layoutRes, std::shared_ptr &context, int typeFlag) +{} + +/** + * @brief Inflates UI controls by using WindowConfig. + * + * @param config Indicates the window config defined by the user. + */ +void Ability::SetUIContent(const sptr &config) +{ + if (abilityWindow_ == nullptr) { + APP_LOGE("Ability::SetUIContent abilityWindow_ is nullptr"); + return; + } + + APP_LOGI("%{public}s beign abilityWindow_->SetWindowConfig.", __func__); + abilityWindow_->SetWindowConfig(config); + APP_LOGI("%{public}s end abilityWindow_->SetWindowConfig.", __func__); +} + +/** + * @brief Get the window belong to the ability. + * + * @return Returns a IWindowsManager object pointer. + */ +const sptr Ability::GetWindow() +{ + if (abilityWindow_ != nullptr) { + return abilityWindow_->GetWindow(); + } else { + APP_LOGI("%{public}s abilityWindow_ is nullptr.", __func__); + return nullptr; + } +} + +/** + * @brief Obtains the type of audio whose volume is adjusted by the volume button. + * + * @return Returns the AudioManager.AudioVolumeType. + */ +int Ability::GetVolumeTypeAdjustedByKey() +{ + return 0; +} + +/** + * @brief Checks whether the main window of this ability has window focus. + * + * @return Returns true if this ability currently has window focus; returns false otherwise. + */ +bool Ability::HasWindowFocus() +{ + if (abilityInfo_ == nullptr) { + APP_LOGI("Ability::HasWindowFocus abilityInfo_ == nullptr"); + return false; + } + + if (abilityInfo_->type == AppExecFwk::AbilityType::PAGE) { + return bWindowFocus_; + } + + return false; +} + +/** + * @brief Called when a key is lone pressed. + * + * @param keyCode Indicates the code of the key long pressed. + * @param keyEvent Indicates the key-long-press event. + * + * @return Returns true if this event is handled and will not be passed further; returns false if this event + * is not handled and should be passed to other handlers. + */ +bool Ability::OnKeyPressAndHold(int keyCode, const std::shared_ptr &keyEvent) +{ + return false; +} + +/** + * @brief Called back after permissions are requested by using + * AbilityContext.requestPermissionsFromUser(java.lang.String[],int). + * + * @param requestCode Indicates the request code passed to this method from + * AbilityContext.requestPermissionsFromUser(java.lang.String[],int). + * @param permissions Indicates the list of permissions requested by using + * AbilityContext.requestPermissionsFromUser(java.lang.String[],int). This parameter cannot be null. + * @param grantResults Indicates the granting results of the corresponding permissions requested using + * AbilityContext.requestPermissionsFromUser(java.lang.String[],int). The value 0 indicates that a + * permission is granted, and the value -1 indicates not. + * + */ +void Ability::OnRequestPermissionsFromUserResult( + int requestCode, const std::vector &permissions, const std::vector &grantResults) +{} + +/** + * @brief Called when this ability is about to leave the foreground and enter the background due to a user operation, + * for example, when the user touches the Home key. + * + */ +void Ability::OnLeaveForeground() +{} + +/** + * @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 Ability::GetType(const Uri &uri) +{ + return ""; +} + +/** + * @brief Inserts a data record into the database. This method should be implemented by a Data ability. + * + * @param uri Indicates the position where the data is to insert. + * @param value Indicates the data to insert. + * + * @return Returns the index of the newly inserted data record. + */ +int Ability::Insert(const Uri &uri, const NativeRdb::ValuesBucket &value) +{ + return 0; +} + +/** + * @brief Called when the system configuration is updated. + * + * @param configuration Indicates the updated configuration information. + */ +void Ability::OnConfigurationUpdated(const Configuration &configuration) +{} + +/** + * @brief Called when the system configuration is updated. + * + * @param level Indicates the memory trim level, which shows the current memory usage status. + * + */ +void Ability::OnMemoryLevel(int level) +{} + +/** + * @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 Ability::OpenRawFile(const Uri &uri, const std::string &mode) +{ + return -1; +} + +/** + * @brief Updates one or more data records in the database. This method should be implemented by a Data ability. + * + * @param uri Indicates the database table storing the data to update. + * @param value Indicates the data to update. This parameter can be null. + * @param predicates Indicates filter criteria. If this parameter is null, all data records will be updated by default. + * + * @return Returns the number of data records updated. + */ +int Ability::Update( + const Uri &uri, const NativeRdb::ValuesBucket &value, const NativeRdb::DataAbilityPredicates &predicates) +{ + return 0; +} + +/** + * @brief get application witch the ability belong + * + * @return Returns the application ptr + */ +std::shared_ptr Ability::GetApplication() +{ + APP_LOGI("%{public}s begin.", __func__); + if (application_ == nullptr) { + APP_LOGE("Ability::GetApplication error. application_ == nullptr."); + return nullptr; + } + APP_LOGI("%{public}s end.", __func__); + return application_; +} + +/** + * @brief Obtains the class name in this ability name, without the prefixed bundle name. + * + * @return Returns the class name of this ability. + */ +std::string Ability::GetAbilityName() +{ + if (abilityInfo_ == nullptr) { + APP_LOGE("Ability::GetAbilityName abilityInfo_ is nullptr"); + return ""; + } + + return abilityInfo_->name; +} + +/** + * @brief OChecks whether the current ability is being destroyed. + * An ability is being destroyed if you called terminateAbility() on it or someone else requested to destroy it. + * + * @return Returns true if the current ability is being destroyed; returns false otherwise. + */ +bool Ability::IsTerminating() +{ + return false; +} + +/** + * @brief Called when startAbilityForResult(ohos.aafwk.content.Want,int) is called to start an ability and the result is + * returned. This method is called only on Page abilities. You can start a new ability to perform some calculations and + * use setResult (int,ohos.aafwk.content.Want) to return the calculation result. Then the system calls back the current + * method to use the returned data to execute its own logic. + * + * @param requestCode Indicates the request code returned after the ability is started. You can define the request code + * to identify the results returned by abilities. The value ranges from 0 to 65535. + * @param resultCode Indicates the result code returned after the ability is started. You can define the result code to + * identify an error. + * @param resultData Indicates the data returned after the ability is started. You can define the data returned. The + * value can be null. + * + */ +void Ability::OnAbilityResult(int requestCode, int resultCode, const Want &want) +{} + +/** + * @brief Called back when the Back key is pressed. + * The default implementation destroys the ability. You can override this method. + * + */ +void Ability::OnBackPressed() +{ + APP_LOGI("%{public}s begin.", __func__); + if (abilityInfo_ == nullptr) { + APP_LOGE("Ability::OnBackPressed abilityInfo_ is nullptr"); + return; + } + + if (abilityInfo_->isLauncherAbility == false) { + APP_LOGI("Ability::OnBackPressed the ability is not Launcher"); + TerminateAbility(); + } + APP_LOGI("%{public}s end.", __func__); +} + +/** + * @brief Called when the launch mode of an ability is set to singleInstance. This happens when you re-launch an + * ability that has been at the top of the ability stack. + * + * @param want Indicates the new Want containing information about the ability. + */ +void Ability::OnNewWant(const Want &want) +{ + APP_LOGI("Ability::OnNewWant called"); +} + +/** + * @brief Restores data and states of an ability when it is restored by the system. This method should be implemented by + * a Page ability. This method is called if an ability was destroyed at a certain time due to resource reclaim or was + * unexpectedly destroyed and the onSaveAbilityState(ohos.utils.PacMap) method was called to save its user data and + * states. Generally, this method is called after the onStart(ohos.aafwk.content.Want) method. + * + * @param inState Indicates the PacMap object used for storing data and states. This parameter can not be null. + * + */ +void Ability::OnRestoreAbilityState(const PacMap &inState) +{ + APP_LOGI("Ability::OnRestoreAbilityState called"); +} + +/** + * @brief Saves temporary data and states of this ability. This method should be implemented by a Page ability. + * This method is called when the system determines that the ability may be destroyed in an unexpected situation, for + * example, when the screen orientation changes or the user touches the Home key. Generally, this method is used only to + * save temporary states. + * + * @param outState Indicates the PacMap object used for storing user data and states. This parameter cannot be null. + * + */ +void Ability::OnSaveAbilityState(PacMap &outState) +{ + APP_LOGI("Ability::OnSaveAbilityState called"); +} + +/** + * @brief Called every time a key, touch, or trackball event is dispatched to this ability. + * You can override this callback method if you want to know that the user has interacted with + * the device in a certain way while this ability is running. This method, together with onLeaveForeground(), + * is designed to help abilities intelligently manage status bar notifications. Specifically, they help + * abilities determine when to cancel a notification. + * + */ +void Ability::OnEventDispatch() +{} + +/** + * @brief Sets the want object that can be obtained by calling getWant(). + * @param Want information of other ability + */ +void Ability::SetWant(const AAFwk::Want &want) +{ + setWant_ = std::make_shared(want); +} + +/** + * @brief Obtains the Want object that starts this ability. + * + * @return Returns the Want object that starts this ability. + */ +std::shared_ptr Ability::GetWant() +{ + return setWant_; +} + +/** + * @brief Sets the result code and data to be returned by this Page ability to the caller. + * When a Page ability is destroyed, the caller overrides the AbilitySlice#onAbilityResult(int, int, Want) method to + * receive the result set in the current method. This method can be called only after the ability has been initialized. + * + * @param resultCode Indicates the result code returned after the ability is destroyed. You can define the result code + * to identify an error. + * @param resultData Indicates the data returned after the ability is destroyed. You can define the data returned. This + * parameter can be null. + */ +void Ability::SetResult(int resultCode, const Want &resultData) +{ + APP_LOGI("%{public}s begin.", __func__); + if (abilityInfo_ == nullptr) { + APP_LOGE("Ability::SetResult abilityInfo_ == nullptr"); + return; + } + APP_LOGI("Ability::SetResult called type = %{public}d", abilityInfo_->type); + if (abilityInfo_->type == AppExecFwk::AbilityType::PAGE) { + AbilityContext::resultWant_ = resultData; + AbilityContext::resultCode_ = resultCode; + } + APP_LOGI("%{public}s end.", __func__); +} + +/** + * @brief Sets the type of audio whose volume will be adjusted by the volume button. + * + * @param volumeType Indicates the AudioManager.AudioVolumeType to set. + */ +void Ability::SetVolumeTypeAdjustedByKey(int volumeType) +{} + +/** + * @brief Called back when Service is started. + * This method can be called only by Service. You can use the StartAbility(ohos.aafwk.content.Want) method to start + * Service. Then the system calls back the current method to use the transferred want parameter to execute its own + * logic. + * + * @param want Indicates the want of Service to start. + * @param restart Indicates the startup mode. The value true indicates that Service is restarted after being destroyed, + * and the value false indicates a normal startup. + * @param startId Indicates the number of times the Service ability has been started. The startId is incremented by 1 + * every time the ability is started. For example, if the ability has been started for six times, the value of startId + * is 6. + */ +void Ability::OnCommand(const AAFwk::Want &want, bool restart, int startId) +{ + APP_LOGI("%{public}s begin restart=%{public}s,startId=%{public}d.", __func__, restart ? "true" : "false", startId); + if (abilityLifecycleExecutor_ == nullptr) { + APP_LOGE("Ability::OnCommand error. abilityLifecycleExecutor_ == nullptr."); + return; + } + abilityLifecycleExecutor_->DispatchLifecycleState(AbilityLifecycleExecutor::LifecycleState::ACTIVE); + + if (lifecycle_ == nullptr) { + APP_LOGE("Ability::OnCommand error. lifecycle_ == nullptr."); + return; + } + lifecycle_->DispatchLifecycle(LifeCycle::Event::ON_ACTIVE); + APP_LOGI("%{public}s end.", __func__); +} + +/** + * @brief dump ability info + * + * @param extra dump ability info + */ +void Ability::Dump(const std::string &extra) +{ + APP_LOGI("Ability::Dump called"); + // abilityInfo + APP_LOGI("==============AbilityInfo=============="); + if (abilityInfo_ != nullptr) { + APP_LOGI("abilityInfo: package: %{public}s", abilityInfo_->package.c_str()); + APP_LOGI("abilityInfo: name: %{public}s", abilityInfo_->name.c_str()); + APP_LOGI("abilityInfo: label: %{public}s", abilityInfo_->label.c_str()); + APP_LOGI("abilityInfo: description: %{public}s", abilityInfo_->description.c_str()); + APP_LOGI("abilityInfo: iconPath: %{public}s", abilityInfo_->iconPath.c_str()); + APP_LOGI("abilityInfo: visible: %{public}d", abilityInfo_->visible); + APP_LOGI("abilityInfo: kind: %{public}s", abilityInfo_->kind.c_str()); + APP_LOGI("abilityInfo: type: %{public}d", abilityInfo_->type); + APP_LOGI("abilityInfo: orientation: %{public}d", abilityInfo_->orientation); + APP_LOGI("abilityInfo: launchMode: %{public}d", abilityInfo_->launchMode); + for (auto permission : abilityInfo_->permissions) { + APP_LOGI("abilityInfo: permission: %{public}s", permission.c_str()); + } + APP_LOGI("abilityInfo: bundleName: %{public}s", abilityInfo_->bundleName.c_str()); + APP_LOGI("abilityInfo: applicationName: %{public}s", abilityInfo_->applicationName.c_str()); + } else { + APP_LOGI("abilityInfo is nullptr"); + } + + // lifecycle_Event + APP_LOGI("==============lifecycle_Event=============="); + if (lifecycle_ != nullptr) { + APP_LOGI("lifecycle_Event: launchMode: %{public}d", lifecycle_->GetLifecycleState()); + } else { + APP_LOGI("lifecycle is nullptr"); + } + + // lifecycle_State + APP_LOGI("==============lifecycle_State=============="); + if (abilityLifecycleExecutor_ != nullptr) { + APP_LOGI("lifecycle_State: launchMode: %{public}d", abilityLifecycleExecutor_->GetState()); + } else { + APP_LOGI("abilityLifecycleExecutor is nullptr"); + } + + // applicationInfo + APP_LOGI("==============applicationInfo=============="); + std::shared_ptr ApplicationInfoPtr = GetApplicationInfo(); + if (ApplicationInfoPtr != nullptr) { + APP_LOGI("applicationInfo: name: %{public}s", ApplicationInfoPtr->name.c_str()); + APP_LOGI("applicationInfo: bundleName: %{public}s", ApplicationInfoPtr->bundleName.c_str()); + } else { + APP_LOGI("ApplicationInfoPtr is nullptr"); + } +} + +/** + * @brief Keeps this Service ability in the background and displays a notification bar. + * To use this method, you need to request the ohos.permission.KEEP_BACKGROUND_RUNNING permission from the system. + * The ohos.permission.KEEP_BACKGROUND_RUNNING permission is of the normal level. + * This method can be called only by Service abilities after the onStart(ohos.aafwk.content.Want) method is called. + * + * @param id Identifies the notification bar information. + * @param notificationRequest Indicates the NotificationRequest instance containing information for displaying a + * notification bar. + */ +void Ability::KeepBackgroundRunning(int id, const NotificationRequest ¬ificationRequest) +{} + +/** + * @brief Cancels background running of this ability to free up system memory. + * This method can be called only by Service abilities when the onStop() method is called. + * + */ +void Ability::CancelBackgroundRunning() +{} + +/** + * @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 Ability::NormalizeUri(const Uri &uri) +{ + return uri; +} + +/** + * @brief Deletes one or more data records. This method should be implemented by a Data ability. + * + * @param uri Indicates the database table storing the data to delete. + * @param predicates Indicates filter criteria. If this parameter is null, all data records will be deleted by default. + * + * @return Returns the number of data records deleted. + */ +int Ability::Delete(const Uri &uri, const NativeRdb::DataAbilityPredicates &predicates) +{ + return 0; +} + +/** + * @brief Obtains the MIME type of files. This method should be implemented by a Data ability. + * + * @param uri Indicates the path of the files to obtain. + * @param mimeTypeFilter Indicates the MIME type of the files to obtain. This parameter cannot be set to null. + * 1. * / *: Obtains all types supported by a Data ability. + * 2. image/ *: Obtains files whose main type is image of any subtype. + * 3. * /jpg: Obtains files whose subtype is JPG of any main type. + * + * @return Returns the MIME type of the matched files; returns null if there is no type that matches the Data ability. + */ +std::vector Ability::GetFileTypes(const Uri &uri, const std::string &mimeTypeFilter) +{ + return types_; +} + +/** + * @brief Opens a file. This method should be implemented by a Data ability. + * + * @param uri Indicates the path of the file to open. + * @param mode Indicates the 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 FileDescriptor object of the file descriptor. + */ +int Ability::OpenFile(const Uri &uri, const std::string &mode) +{ + return -1; +} + +/** + * @brief Queries one or more data records in the database. This method should be implemented by a Data ability. + * + * @param uri Indicates the database table storing the data to query. + * @param columns Indicates the columns to be queried, in array, for example, {"name","age"}. You should define the + * processing logic when this parameter is null. + * @param predicates Indicates filter criteria. If this parameter is null, all data records will be queried by default. + * + * @return Returns the queried data. + */ +std::shared_ptr Ability::Query( + const Uri &uri, const std::vector &columns, const NativeRdb::DataAbilityPredicates &predicates) +{ + return nullptr; +} + +/** + * @brief Reloads data in the database. + * The default implementation of this method returns false. You must implement this method in the child class + * of your Data Ability if you want to use it. + * + * @param uri Indicates the position where the data is to reload. + * @param extras Indicates the additional parameters contained in the PacMap object to be passed in this call. + * + * @return Returns true if the data is successfully reloaded; returns false otherwise. + */ +bool Ability::Reload(const Uri &uri, const PacMap &extras) +{ + return false; +} + +/** + * @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 Ability::BatchInsert(const Uri &uri, const std::vector &values) +{ + APP_LOGI("%{public}s begin.", __func__); + int amount = 0; + for (auto it = values.begin(); it != values.end(); it++) { + if (Insert(uri, *it) >= 0) { + amount++; + } + } + APP_LOGI("%{public}s end, amount=%{public}d", __func__, amount); + return amount; +} + +/** + * @brief Migrates this ability to the given device on the same distributed network in a reversible way that allows this + * ability to be migrated back to the local device through reverseContinueAbility(). The ability to migrate and its + * ability slices must implement the IAbilityContinuation interface. Otherwise, an exception is thrown, indicating that + * the ability does not support migration. + * + * @param deviceId Indicates the ID of the target device where this ability will be migrated to. If this parameter is + * null, this method has the same effect as ContinueAbilityReversibly(). + * + */ +void Ability::ContinueAbilityReversibly(const std::string &deviceId) +{ + if (!VerifySupportForContinuation()) { + APP_LOGE("Ability::ContinueAbilityReversibly(deviceId) failed. VerifySupportForContinuation faled"); + return; + } + continuationManager_->ContinueAbility(true, deviceId); +} + +/** + * @brief public final String getOriginalDeviceId​() throws UnsupportedOperationException + * Obtains the ID of the source device from which this ability is migrated. + * + * @return Returns the source device ID. + */ +std::string Ability::GetOriginalDeviceId() +{ + return ""; +} + +/** + * @brief Obtains the migration state of this ability. + * @return Returns the migration state. + * + * @return Returns the source device ID. + */ +ContinuationState Ability::GetContinuationState() +{ + if (!VerifySupportForContinuation()) { + APP_LOGE("Ability::GetContinuationState failed. VerifySupportForContinuation faled"); + return ContinuationState::LOCAL_RUNNING; + } + return continuationManager_->GetContinuationState(); +} + +/** + * @brief Obtains the singleton AbilityPackage object to which this ability belongs. + * + * @return Returns the singleton AbilityPackage object to which this ability belongs. + */ +std::shared_ptr Ability::GetAbilityPackage() +{ + return nullptr; +} + +/** + * @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 Ability::DenormalizeUri(const Uri &uri) +{ + return uri; +} + +/** + * @brief Obtains the Lifecycle object of the current ability. + * + * @return Returns the Lifecycle object. + */ +std::shared_ptr Ability::GetLifecycle() +{ + APP_LOGI("Ability::GetLifecycle called"); + return lifecycle_; +} + +/** + * @brief Obtains the lifecycle state of this ability. + * + * @return Returns the lifecycle state of this ability. + */ +AbilityLifecycleExecutor::LifecycleState Ability::GetState() +{ + APP_LOGI("Ability::GetState called"); + + if (abilityLifecycleExecutor_ == nullptr) { + APP_LOGE("Ability::GetState error. abilityLifecycleExecutor_ == nullptr."); + return AbilityLifecycleExecutor::LifecycleState::UNINITIALIZED; + } + + return (AbilityLifecycleExecutor::LifecycleState)abilityLifecycleExecutor_->GetState(); +} + +/** + * @brief A Page or Service ability uses this method to start a specific ability. The system locates the target ability + * from installed abilities based on the value of the intent parameter and then starts it. You can specify the ability + * to start using the intent parameter. + * + * @param intent Indicates the ability to start. + */ +void Ability::StartAbility(const Want &want) +{ + APP_LOGI("%{public}s begin.", __func__); + AbilityContext::StartAbility(want, -1); + APP_LOGI("%{public}s end.", __func__); +} + +/** + * @brief Destroys this Page or Service ability. + * After a Page or Service ability performs all operations, it can use this method to destroy itself + * to free up memory. This method can be called only after the ability is initialized. + */ +void Ability::TerminateAbility() +{ + APP_LOGI("%{public}s begin.", __func__); + AbilityContext::TerminateAbility(); + APP_LOGI("%{public}s end.", __func__); +} + +/** + * @brief Destroys ability. + * + * @param want Indicates the want containing information about TerminateAbility + * + * @return Returns the result of TerminateAbility + */ +int Ability::TerminateAbility(Want &want) +{ + return -1; +} + +/** + * @brief Sets the main route for this ability. + * + * The main route, also called main entry, refers to the default AbilitySlice to present for this ability. + * This function should be called only on Feature Abilities. If this function is not called in the + * {@link OnStart(const Want &want)} function for a Feature Ability, the Feature Ability will fail to start. + * + * @param entry Indicates the main entry, which is the class name of the AbilitySlice instance to start. + * + * @return Returns the result of SetMainRouter + */ +void Ability::SetMainRoute(const std::string &entry) +{} + +/** + * @brief By binding an action, you can set different action parameters in Intent to present different initial pages. + * You must register actions in the profile file. + * + * @param action Indicates the action to bind. + * + * @param entry Indicates the entry, which is the fully qualified name of your AbilitySlice class. + * + * @return Returns the result of AddActionRoute + */ +void Ability::AddActionRoute(const std::string &action, const std::string &entry) +{} + +/** + * @brief Sets the background color of the window in RGB color mode. + * + * @param red The value ranges from 0 to 255. + * + * @param green The value ranges from 0 to 255. + * + * @param blue The value ranges from 0 to 255. + * + * @return Returns the result of SetWindowBackgroundColor + */ +int Ability::SetWindowBackgroundColor(int red, int green, int blue) +{ + return -1; +} + +/** + * @brief Connects the current ability to an ability using the AbilityInfo.AbilityType.SERVICE template. + * + * @param want Indicates the want containing information about the ability to connect + * + * @param conn Indicates the callback object when the target ability is connected. + * + * @return True means success and false means failure + */ +bool Ability::ConnectAbility(const Want &want, const sptr &conn) +{ + return AbilityContext::ConnectAbility(want, conn); +} + +/** + * @brief Disconnects the current ability from an ability + * + * @param conn Indicates the IAbilityConnection callback object passed by connectAbility after the connection + * is set up. The IAbilityConnection object uniquely identifies a connection between two abilities. + */ +void Ability::DisconnectAbility(const sptr &conn) +{ + return AbilityContext::DisconnectAbility(conn); +} + +/** + * @brief Destroys another ability that uses the AbilityInfo.AbilityType.SERVICE template. + * The current ability using either the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE + * template can call this method to destroy another ability that uses the AbilityInfo.AbilityType.SERVICE + * template. The current ability itself can be destroyed by calling the terminateAbility() method. + * + * @param want Indicates the Want containing information about the ability to destroy. + * + * @return Returns true if the ability is destroyed successfully; returns false otherwise. + */ +bool Ability::StopAbility(const AAFwk::Want &want) +{ + return AbilityContext::StopAbility(want); +} + +/** + * @brief Posts a scheduled Runnable task to a new non-UI thread. + * The task posted via this method will be executed in a new thread, which allows you to perform certain time-consuming + * operations. To use this method, you must also override the supportHighPerformanceUI() method. + * + * @param task Indicates the Runnable task to post. + * + * @param delayTime Indicates the number of milliseconds after which the task will be executed. + * + * @return - + */ +void Ability::PostTask(std::function task, long delayTime) +{ + APP_LOGI("%{public}s begin.", __func__); + TaskHandlerClient::GetInstance()->PostTask(task, delayTime); + APP_LOGI("%{public}s end.", __func__); +} + +/** + * @brief You can use the IContinuationRegisterManager object to interact with the Device+ control center, + * including registering and unregistering the ability to migrate, updating the device connection state, and + * showing the list of devices that can be selected for ability migration. + * + * @return Returns true if the migration request is successful; returns false otherwise. + */ +std::weak_ptr Ability::GetContinuationRegisterManager() +{ + if (abilityInfo_ != nullptr) { + if ((abilityInfo_->type == AbilityType::PAGE) && (continuationRegisterManager_ == nullptr)) { + std::weak_ptr context = shared_from_this(); + std::shared_ptr continuationRegisterManagerProxy = + std::make_shared(context); + continuationRegisterManager_ = std::make_shared(); + continuationRegisterManager_->Init(continuationRegisterManagerProxy); + } + } + std::weak_ptr continuationRegisterManager = + std::weak_ptr(continuationRegisterManager_); + return continuationRegisterManager; +} + +/** + * @brief Migrates this ability to the given device on the same distributed network. The ability to migrate and its + * ability slices must implement the IAbilityContinuation interface. + * + * @param deviceId Indicates the ID of the target device where this ability will be migrated to. If this parameter + * is null, this method has the same effect as continueAbility(). + * + */ +void Ability::ContinueAbility(const std::string &deviceId) +{ + if (deviceId.empty()) { + APP_LOGE("Ability::ContinueAbility(deviceId) failed. deviceId is empty"); + return; + } + + if (!VerifySupportForContinuation()) { + APP_LOGE("Ability::ContinueAbility(deviceId) failed. VerifySupportForContinuation faled"); + return; + } + continuationManager_->ContinueAbility(false, deviceId); +} + +/** + * @brief Callback function to ask the user whether to start the migration . + * + * @return If the user allows migration, it returns true; otherwise, it returns false. + */ +bool Ability::OnStartContinuation() +{ + return false; +} + +/** + * @brief Save user data of local Ability generated at runtime. + * + * @param saveData Indicates the user data to be saved. + * @return If the data is saved successfully, it returns true; otherwise, it returns false. + */ +bool Ability::OnSaveData(WantParams &saveData) +{ + return false; +} + +/** + * @brief After creating the Ability on the remote device, + * immediately restore the user data saved during the migration of the Ability on the remote device. + * @param restoreData Indicates the user data to be restored. + * @return If the data is restored successfully, it returns true; otherwise, it returns false . + */ +bool Ability::OnRestoreData(WantParams &restoreData) +{ + return false; +} + +/** + * @brief This function can be used to implement the processing logic after the migration is completed. + * + * @param result Migration result code. 0 means the migration was successful, -1 means the migration failed. + * @return None. + */ +void Ability::OnCompleteContinuation(int result) +{} + +/** + * @brief Used to notify the local Ability that the remote Ability has been destroyed. + * + * @return None. + */ +void Ability::OnRemoteTerminated() +{} +bool Ability::VerifySupportForContinuation() +{ + if (continuationManager_ == nullptr) { + APP_LOGE("Ability::VerifySupportForContinuation failed. continuationManager_ is nullptr"); + return false; + } + return true; +} + +void Ability::HandleCreateAsContinuation(const Want &want) +{ + if (!IsFlagExists(Want::FLAG_ABILITY_CONTINUATION, want.GetFlags())) { + APP_LOGI("Ability::HandleCreateAsContinuation return. This not continuated ability"); + return; + } + + // check whether it needs reversible + bool reversible = false; + reversible = IsFlagExists(Want::FLAG_ABILITY_CONTINUATION_REVERSIBLE, want.GetFlags()); + if (!VerifySupportForContinuation()) { + APP_LOGE("Ability::HandleCreateAsContinuation failed. VerifySupportForContinuation failed"); + return; + } + bool success = continuationManager_->RestoreData( + want.GetParams(), reversible, want.GetStringParam(ContinuationHandler::ORIGINAL_DEVICE_ID)); + + if (success && reversible) { + // Register this ability to receive reverse continuation callback. + std::weak_ptr ReplicaHandler = continuationHandler_; + reverseContinuationSchedulerReplica_ = sptr( + new (std::nothrow) ReverseContinuationSchedulerReplica(handler_, ReplicaHandler)); + + if (reverseContinuationSchedulerReplica_ == nullptr) { + APP_LOGE("Ability::HandleCreateAsContinuation failed, create reverseContinuationSchedulerReplica failed"); + return; + } + } + + int sessionId = want.GetIntParam(DMS_SESSION_ID, DEFAULT_DMS_SESSION_ID); + std::string originDeviceId = want.GetStringParam(DMS_ORIGIN_DEVICE_ID); + APP_LOGE("Ability::HandleCreateAsContinuationoriginDeviceId: %{public}s", originDeviceId.c_str()); + continuationManager_->NotifyCompleteContinuation( + originDeviceId, sessionId, success, reverseContinuationSchedulerReplica_); +} +bool Ability::IsFlagExists(unsigned int flag, unsigned int flagSet) +{ + return (flag & flagSet) == flag; +} + +/** + * @brief Called when this ability gains or loses window focus. + * + * @param hasFocus Specifies whether this ability has focus. + */ +void Ability::OnWindowFocusChanged(bool hasFocus) +{} + +/** + * @brief Called when this ability is moved to or removed from the top of the stack. + * + * @param topActive Specifies whether this ability is moved to or removed from the top of the stack. The value true + * indicates that it is moved to the top, and false indicates that it is removed from the top of the stack. + */ +void Ability::OnTopActiveAbilityChanged(bool topActive) +{} + +/** + * @brief Called to set caller information for the application. The default implementation returns null. + * + * @return Returns the caller information. + */ +Uri Ability::OnSetCaller() +{ + return Uri(""); +} + +/** + * @brief Call this when your ability should be closed and the mission should be completely removed as a part of + * finishing the root ability of the mission. + */ +void Ability::TerminateAndRemoveMission() +{ + APP_LOGI("%{public}s begin.", __func__); + auto state = GetState(); + if (state > AbilityLifecycleExecutor::LifecycleState::INITIAL) { + APP_LOGI("Ability::TerminateAndRemoveMission the GetState retval is %d", state); + return; + } + AbilityContext::TerminateAndRemoveMission(); + APP_LOGI("%{public}s end.", __func__); +} + +/** + * @brief Create a PostEvent timeout task. The default delay is 5000ms + * + * @return Return a smart pointer to a timeout object + */ +std::shared_ptr Ability::CreatePostEventTimeouter(std::string taskstr) +{ + return std::make_shared(taskstr, handler_); +} + +/** + * Releases an obtained form by its ID. + * + *

After this method is called, the form won't be available for use by the application, but the Form Manager + * Service still keeps the cache information about the form, so that the application can quickly obtain it based on + * the {@code formId}.

+ *

Permission: {@link ohos.security.SystemPermission#REQUIRE_FORM}

+ * + * @param formId Indicates the form ID. + * @return Returns {@code true} if the form is successfully released; returns {@code false} otherwise. + * + *
    + *
  • The passed {@code formId} is invalid. Its value must be larger than 0.
  • + *
  • The specified form has not been added by the application.
  • + *
  • An error occurred when connecting to the Form Manager Service.
  • + *
  • The application is not granted with the {@link ohos.security.SystemPermission#REQUIRE_FORM} permission.
  • + *
  • The form has been obtained by another application and cannot be released by the current application.
  • + *
  • The form is being restored.
  • + *
+ */ +bool Ability::ReleaseForm(const int64_t formId) +{ + APP_LOGI("%{public}s called.", __func__); + // release form by formId and do not release cache + return ReleaseForm(formId, false); +} + +/** + * Releases an obtained form by its ID. + * + *

After this method is called, the form won't be available for use by the application, if isReleaseCache is + * false, this method is same as {@link #releaseForm(int)}, otherwise the Form Manager Service still store this + * form in the cache.

+ *

Permission: {@link ohos.security.SystemPermission#REQUIRE_FORM}

+ * + * @param formId Indicates the form ID. + * @param isReleaseCache Indicates whether to clear cache in service. + * @return Returns {@code true} if the form is successfully released; returns {@code false} otherwise. + * + *
    + *
  • The passed {@code formId} is invalid. Its value must be larger than 0.
  • + *
  • The specified form has not been added by the application.
  • + *
  • An error occurred when connecting to the Form Manager Service.
  • + *
  • The application is not granted with the {@link ohos.security.SystemPermission#REQUIRE_FORM} permission.
  • + *
  • The form has been obtained by another application and cannot be released by the current application.
  • + *
  • The form is being restored.
  • + *
+ */ +bool Ability::ReleaseForm(const int64_t formId, const bool isReleaseCache) +{ + APP_LOGI("%{public}s called.", __func__); + // release form with formId and specifies whether to release the cache + return DeleteForm(formId, isReleaseCache ? RELEASE_CACHED_FORM : RELEASE_FORM); +} + +/** + * Deletes an obtained form by its ID. + * + *

After this method is called, the form won't be available for use by the application and the Form Manager + * Service no longer keeps the cache information about the form.

+ *

Permission: {@link ohos.security.SystemPermission#REQUIRE_FORM}

+ * + * @param formId Indicates the form ID. + * @return Returns {@code true} if the form is successfully deleted; returns {@code false} otherwise. + * + *
    + *
  • The passed {@code formId} is invalid. Its value must be larger than 0.
  • + *
  • The specified form has not been added by the application.
  • + *
  • An error occurred when connecting to the Form Manager Service.
  • + *
  • The application is not granted with the {@link ohos.security.SystemPermission#REQUIRE_FORM} permission.
  • + *
  • The form has been obtained by another application and cannot be deleted by the current application.
  • + *
  • The form is being restored.
  • + *
+ */ +bool Ability::DeleteForm(const int64_t formId) +{ + APP_LOGI("%{public}s called.", __func__); + // delete form with formId + return DeleteForm(formId, DELETE_FORM); +} + +/** + * @brief Cast temp form with formId. + * + * @param formId Indicates the form's ID. + * + * @return Returns {@code true} if the form is successfully casted; returns {@code false} otherwise. + */ +bool Ability::CastTempForm(const int64_t formId) +{ + APP_LOGI("%{public}s start", __func__); + if (formId <= 0) { + APP_LOGE("%{public}s error, passing in form id can't be negative.", __func__); + return false; + } + + APP_LOGI("%{public}s, castTempForm begin of temp form %{public}" PRId64, __func__, formId); + int result = FormMgr::GetInstance().CastTempForm(formId, FormHostClient::GetInstance()); + + if (result != ERR_OK) { + APP_LOGE("%{public}s error, some internal server occurs, error code is %{public}d.", __func__, result); + return false; + } + + userReqParams_[formId].SetParam(Constants::PARAM_FORM_TEMPORARY_KEY, false); + + APP_LOGI("%{public}s end", __func__); + return true; +} + +/** + * @brief Obtains a specified form that matches the application bundle name, module name, form name, and + * other related information specified in the passed {@code Want}. + * + *

This method is asynchronous. After the {@link FormJsInfo} instance is obtained. + * + * @param formId Indicates the form ID. + * @param want Indicates the detailed information about the form to be obtained, including the bundle name, + * module name, ability name, form name, form id, tempForm flag, form dimension, and form customize data. + * @param callback Indicates the callback to be invoked whenever the {@link FormJsInfo} instance is obtained. + * @return Returns {@code true} if the request is successfully initiated; returns {@code false} otherwise. + */ +bool Ability::AcquireForm(const int64_t formId, const Want &want, const std::shared_ptr callback) +{ + APP_LOGI("%{public}s called.", __func__); + struct timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + long currentTime = ts.tv_sec * SEC_TO_MILLISEC + ts.tv_nsec / MILLISEC_TO_NANOSEC; + APP_LOGI("%{public}s begin, current time: %{public}ld", __func__, currentTime); + + // check fms recover status + if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) { + APP_LOGE("%{public}s error, form is in recover status, can't do action on form.", __func__); + return false; + } + + // check form id + APP_LOGD("%{public}s, param of formId %{public}" PRId64 ".", __func__, formId); + if (formId < 0) { + APP_LOGE("%{public}s error, form id should not be negative.", __func__); + return false; + } + + // check param of want + if (!CheckWantValid(formId, want)) { + APP_LOGE("%{public}s error, failed to check param of want.", __func__); + return false; + }; + + ElementName elementName = want.GetElement(); + std::string bundleName = elementName.GetBundleName(); + std::string abilityName = elementName.GetAbilityName(); + APP_LOGI("%{public}s, begin to acquire form, bundleName is %{public}s, abilityName is %{public}s, formId is " + "%{public}" PRId64 ".", + __func__, + bundleName.c_str(), + abilityName.c_str(), + formId); + + // hostClient init + sptr formHostClient = FormHostClient::GetInstance(); + if (formHostClient == nullptr) { + APP_LOGE("%{public}s error, formHostClient == nullptr.", __func__); + return false; + } + + // acquire form request to fms + FormJsInfo formJsInfo; + if (FormMgr::GetInstance().AddForm(formId, want, formHostClient, formJsInfo) != ERR_OK) { + APP_LOGE("%{public}s error, acquire form for fms failed.", __func__); + return false; + } + APP_LOGI("%{public}s, end to acquire form, the formId returned from the fms is %{public}" PRId64 ".", + __func__, + formJsInfo.formId); + + // check for form presence in hostForms + if (formHostClient->ContainsForm(formJsInfo.formId)) { + APP_LOGE("%{public}s error, form has already acquired, do not support acquire twice.", __func__); + return false; + } + + // add ability of form to hostForms + std::shared_ptr thisAbility = this->shared_from_this(); + formHostClient->AddForm(thisAbility, formJsInfo.formId); + // post the async task of handleAcquireResult + PostTask([this, want, formJsInfo, callback]() { HandleAcquireResult(want, formJsInfo, callback); }, 0L); + // the acquire form is successfully + return true; +} + +/** + * @brief Updates the content of a specified JS form. + * + *

This method is called by a form provider to update JS form data as needed. + * + * @param formId Indicates the form ID. + * @param formProviderData The data used to update the JS form displayed on the client. + * @return Returns {@code true} if the request is successfully initiated; returns {@code false} otherwise. + */ +bool Ability::UpdateForm(const int64_t formId, const FormProviderData &formProviderData) +{ + APP_LOGI("%{public}s called.", __func__); + // check fms recover status + if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) { + APP_LOGE("%{public}s error, form is in recover status, can't do action on form.", __func__); + return false; + } + + // check formId + if (formId <= 0) { + APP_LOGE("%{public}s error, the passed in formId can't be negative or zero.", __func__); + return false; + } + + // check formProviderData + if (formProviderData.GetDataString().empty()) { + APP_LOGE("%{public}s error, the formProviderData is null.", __func__); + return false; + } + + // update form request to fms + if (FormMgr::GetInstance().UpdateForm(formId, abilityInfo_->bundleName, formProviderData) != ERR_OK) { + APP_LOGE("%{public}s error, update form for fms failed.", __func__); + return false; + } + + // the update form is successfully + return true; +} + +/** + * @brief Sends a notification to the form framework to make the specified forms visible. + * + *

After this method is successfully called, {@link Ability#OnVisibilityChanged(std::map)} + * will be called to notify the form provider of the form visibility change event.

+ * + * @param formIds Indicates the IDs of the forms to be made visible. + * @return Returns {@code true} if the request is successfully initiated; returns {@code false} otherwise. + */ +bool Ability::NotifyVisibleForms(const std::vector &formIds) +{ + APP_LOGI("%{public}s called.", __func__); + return NotifyWhetherVisibleForms(formIds, Constants::FORM_VISIBLE); +} + +/** + * @brief Sends a notification to the form framework to make the specified forms invisible. + * + *

After this method is successfully called, {@link Ability#OnVisibilityChanged(std::map)} + * will be called to notify the form provider of the form visibility change event.

+ * + * @param formIds Indicates the IDs of the forms to be made invisible. + * @return Returns {@code true} if the request is successfully initiated; returns {@code false} otherwise. + */ +bool Ability::NotifyInvisibleForms(const std::vector &formIds) +{ + APP_LOGI("%{public}s called.", __func__); + return NotifyWhetherVisibleForms(formIds, Constants::FORM_INVISIBLE); +} + +/** + * @brief Set form next refresh time. + * + *

This method is called by a form provider to set refresh time. + * + * @param formId Indicates the ID of the form to set refresh time. + * @param nextTime Indicates the next time gap now in seconds, can not be litter than 300 seconds. + * @return Returns {@code true} if seting succeed; returns {@code false} otherwise. + */ +bool Ability::SetFormNextRefreshTime(const int64_t formId, const int64_t nextTime) +{ + APP_LOGI("%{public}s called.", __func__); + if (nextTime < MIN_NEXT_TIME) { + APP_LOGE("next time litte than 300 seconds."); + return false; + } + + if (FormMgr::GetInstance().GetRecoverStatus() == Constants::IN_RECOVERING) { + APP_LOGE("%{public}s, formManager is in recovering", __func__); + return false; + } + + int result = FormMgr::GetInstance().SetNextRefreshTime(formId, nextTime); + if (result != ERR_OK) { + APP_LOGE("%{public}s, internal error:[%{public}d]", __func__, result); + return false; + } + + return true; +} +/** + * @brief Requests for form data update. + * + * This method must be called when the application has detected that a system setting item (such as the language, + * resolution, or screen orientation) being listened for has changed. Upon receiving the update request, the form + * provider automatically updates the form data (if there is any update) through the form framework, with the update + * process being unperceivable by the application. + * + * @param formId Indicates the ID of the form to update. + * @return Returns true if the update request is successfully initiated, returns false otherwise. + */ +bool Ability::RequestForm(const int64_t formId) +{ + APP_LOGI("%{public}s called.", __func__); + Want want; + return RequestForm(formId, want); +} + +/** + * @brief Update form. + * + * @param formJsInfo Indicates the obtained {@code FormJsInfo} instance. + */ +void Ability::ProcessFormUpdate(const FormJsInfo &formJsInfo) +{ + APP_LOGI("%{public}s called.", __func__); + // post the async task of handleFormMessage + int32_t msgCode = OHOS_FORM_UPDATE_FORM; + PostTask([this, msgCode, formJsInfo]() { HandleFormMessage(msgCode, formJsInfo); }, 0L); +} +/** + * @brief Uninstall form. + * + * @param formId Indicates the ID of the form to uninstall. + */ +void Ability::ProcessFormUninstall(const int64_t formId) +{ + APP_LOGI("%{public}s start.", __func__); + // check formId + if (formId <= 0) { + APP_LOGE("%{public}s error, the passed in formId can't be negative or zero.", __func__); + return; + } + + std::shared_ptr formCallback = nullptr; + { + std::lock_guard lock(formLock); + // get callback iterator by formId + std::map>::iterator appCallbackIterator = appCallbacks_.find(formId); + + // call the callback function when you need to be notified + if (appCallbackIterator == appCallbacks_.end()) { + APP_LOGE("%{public}s failed, callback not find, formId: %{public}" PRId64 ".", __func__, formId); + return; + } + formCallback = appCallbackIterator->second; + CleanFormResource(formId); + } + if (formCallback == nullptr) { + APP_LOGE("%{public}s failed, callback is nullptr.", __func__); + return; + } + + formCallback->OnFormUninstall(formId); + + APP_LOGI("%{public}s end.", __func__); +} + +/** + * @brief Called to return a FormProviderInfo object. + * + *

You must override this method if your ability will serve as a form provider to provide a form for clients. + * The default implementation returns nullptr.

+ * + * @param want Indicates the detailed information for creating a FormProviderInfo. + * The Want object must include the form ID, form name of the form, + * which can be obtained from Ability#PARAM_FORM_IDENTITY_KEY, + * Ability#PARAM_FORM_NAME_KEY, and Ability#PARAM_FORM_DIMENSION_KEY, + * respectively. Such form information must be managed as persistent data for further form + * acquisition, update, and deletion. + * + * @return Returns the created FormProviderInfo object. + */ +FormProviderInfo Ability::OnCreate(const Want &want) +{ + APP_LOGI("%{public}s called.", __func__); + FormProviderInfo formProviderInfo; + return formProviderInfo; +} + +/** + * @brief Called to notify the form provider that a specified form has been deleted. Override this method if + * you want your application, as the form provider, to be notified of form deletion. + * + * @param formId Indicates the ID of the deleted form. + * @return None. + */ +void Ability::OnDelete(const int64_t formId) +{} + +/** + * @brief Called to notify the form provider to update a specified form. + * + * @param formId Indicates the ID of the form to update. + * @return none. + */ +void Ability::OnUpdate(const int64_t formId) +{} + +/** + * @brief Called when the form provider is notified that a temporary form is successfully converted to a normal form. + * + * @param formId Indicates the ID of the form. + * @return None. + */ +void Ability::OnCastTemptoNormal(const int64_t formId) +{} + +/** + * @brief Called when the form provider receives form events from the fms. + * + * @param formEventsMap Indicates the form events occurred. The key in the Map object indicates the form ID, + * and the value indicates the event type, which can be either FORM_VISIBLE + * or FORM_INVISIBLE. FORM_VISIBLE means that the form becomes visible, + * and FORM_INVISIBLE means that the form becomes invisible. + * @return none. + */ +void Ability::OnVisibilityChanged(const std::map &formEventsMap) +{} +/** + * @brief Called to notify the form provider to update a specified form. + * + * @param formId Indicates the ID of the form to update. + * @param message Form event message. + */ +void Ability::OnTriggerEvent(const int64_t formId, const std::string &message) +{} +/** + * @brief Delete or release form with formId. + * + * @param formId Indicates the form's ID. + * @param deleteType Indicates the type of delete or release. + * @return Returns {@code true} if the form is successfully deleted; returns {@code false} otherwise. + */ +bool Ability::DeleteForm(const int64_t formId, const int32_t deleteType) +{ + APP_LOGI("%{public}s called.", __func__); + // check fms recover status + if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) { + APP_LOGE("%{public}s error, form is in recover status, can't do action on form.", __func__); + return false; + } + // check formId + if (formId <= 0) { + APP_LOGE("%{public}s error, the passed in formId can't be negative or zero.", __func__); + return false; + } + + APP_LOGI("%{public}s, delete form begin, formId is %{public}" PRId64 " and deleteType is %{public}d.", + __func__, + formId, + deleteType); + { + // form lock + std::lock_guard lock(formLock); + // clean form resource when form is temp form + if (std::find(lostedByReconnectTempForms_.begin(), lostedByReconnectTempForms_.end(), formId) != + lostedByReconnectTempForms_.end()) { + CleanFormResource(formId); + // the delete temp form is successfully + return true; + } + } + + // hostClient init + sptr formHostClient = FormHostClient::GetInstance(); + // delete or release request to fms + int result; + if (deleteType == DELETE_FORM) { + result = FormMgr::GetInstance().DeleteForm(formId, formHostClient); + } else { + result = FormMgr::GetInstance().ReleaseForm( + formId, formHostClient, (deleteType == RELEASE_CACHED_FORM) ? true : false); + } + if (result != ERR_OK) { + APP_LOGE("%{public}s error, some internal server occurs, error code is %{public}d.", __func__, result); + return false; + } + { + // form lock + std::lock_guard lock(formLock); + // clean form resource + CleanFormResource(formId); + } + // the delete form is successfully + return true; +} + +/** + * @brief Clean form resource with formId. + * + * @param formId Indicates the form's ID. + */ +void Ability::CleanFormResource(const int64_t formId) +{ + APP_LOGI("%{public}s called.", __func__); + // compatible with int form id + int64_t cleanId{-1L}; + for (auto param : userReqParams_) { + uint64_t unsignedFormId = static_cast(formId); + uint64_t unsignedParamFirst = static_cast(param.first); + if ((unsignedParamFirst & 0x00000000ffffffffL) == (unsignedFormId & 0x00000000ffffffffL)) { + cleanId = param.first; + break; + } + } + if (cleanId == -1L) { + return; + } + + APP_LOGD("%{public}s. clean id is %{public}" PRId64 ".", __func__, cleanId); + // remove wantParam, callback and lostedByReconnectTempForms + appCallbacks_.erase(cleanId); + userReqParams_.erase(cleanId); + auto tempForm = std::find(lostedByReconnectTempForms_.begin(), lostedByReconnectTempForms_.end(), cleanId); + if (tempForm != lostedByReconnectTempForms_.end()) { + lostedByReconnectTempForms_.erase(tempForm); + } + + // remove ability + std::shared_ptr thisAbility = this->shared_from_this(); + FormHostClient::GetInstance()->RemoveForm(thisAbility, cleanId); + + // unregister death callback when appCallbacks is empty + if (appCallbacks_.empty()) { + std::shared_ptr thisAbility = this->shared_from_this(); + FormMgr::GetInstance().UnRegisterDeathCallback(thisAbility); + } + APP_LOGI("%{public}s end.", __func__); +} + +/** + * @brief Handle acquire result of the obtained form instance. + * + * @param want Indicates the detailed information about the form to be obtained, including the bundle name, + * module name, ability name, form name, form id, tempForm flag, form dimension, and form customize data. + * @param formJsInfo Indicates the obtained {@code FormJsInfo} instance. + * @param callback Indicates the callback to be invoked whenever the {@link FormJsInfo} instance is obtained. + */ +void Ability::HandleAcquireResult( + const Want &want, const FormJsInfo &formJsInfo, const std::shared_ptr callback) +{ + APP_LOGI("%{public}s called.", __func__); + { + // form lock + std::lock_guard lock(formLock); + + // register death when userReqParams is empty + if (userReqParams_.empty()) { + std::shared_ptr thisAbility = this->shared_from_this(); + FormMgr::GetInstance().RegisterDeathCallback(thisAbility); + } + + // save wantParam and callback + userReqParams_.insert(std::make_pair(formJsInfo.formId, want)); + appCallbacks_.insert(std::make_pair(formJsInfo.formId, callback)); + } + + struct timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + long currentTime = ts.tv_sec * SEC_TO_MILLISEC + ts.tv_nsec / MILLISEC_TO_NANOSEC; + APP_LOGI("%{public}s, AcquireForm end, current time: %{public}ld", __func__, currentTime); + + // handle acquire message of the obtained form instance + callback->OnAcquired(FormCallback::OHOS_FORM_ACQUIRE_SUCCESS, formJsInfo); +} + +/** + * @brief Handle acquire message of the obtained form instance. + * + * @param msgCode Indicates the code of message type. + * @param formJsInfo Indicates the obtained {@code FormJsInfo} instance. + */ +void Ability::HandleFormMessage(const int32_t msgCode, const FormJsInfo &formJsInfo) +{ + APP_LOGI("%{public}s called.", __func__); + std::shared_ptr formCallback = nullptr; + { + std::lock_guard lock(formLock); + // get callback iterator by formId + std::map>::iterator appCallbackIterator = + appCallbacks_.find(formJsInfo.formId); + + // call the callback function when you need to be notified + if (appCallbackIterator == appCallbacks_.end()) { + APP_LOGE("%{public}s failed, callback not find, formId: %{public}" PRId64 ".", __func__, formJsInfo.formId); + return; + } + formCallback = appCallbackIterator->second; + } + if (formCallback == nullptr) { + APP_LOGE("%{public}s failed, callback is nullptr.", __func__); + return; + } + + APP_LOGI("%{public}s, call user implement of form %{public}" PRId64 ".", __func__, formJsInfo.formId); + + if (msgCode == OHOS_FORM_ACQUIRE_FORM) { + formCallback->OnAcquired(FormCallback::OHOS_FORM_ACQUIRE_SUCCESS, formJsInfo); + } else { + formCallback->OnUpdate(FormCallback::OHOS_FORM_UPDATE_SUCCESS, formJsInfo); + } +} + +/** + * @brief Notify the forms visibility change event. + * + * @param formIds Indicates the IDs of the forms to be made visible or invisible. + * @param eventType Indicates the form events occurred. FORM_VISIBLE means that the form becomes visible, + * and FORM_INVISIBLE means that the form becomes invisible. + * @return Returns {@code true} if the request is successfully initiated; returns {@code false} otherwise. + */ +bool Ability::NotifyWhetherVisibleForms(const std::vector &formIds, int32_t eventType) +{ + APP_LOGI("%{public}s called.", __func__); + if (formIds.empty() || formIds.size() > Constants::MAX_VISIBLE_NOTIFY_LIST) { + APP_LOGE("%{public}s, formIds is empty or exceed 32.", __func__); + return false; + } + + if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) { + APP_LOGE("%{public}s error, form is in recover status, can't do action on form.", __func__); + return false; + } + + int resultCode = + FormMgr::GetInstance().NotifyWhetherVisibleForms(formIds, FormHostClient::GetInstance(), eventType); + if (resultCode != ERR_OK) { + APP_LOGE("%{public}s error, internal error occurs, error code:%{public}d.", __func__, resultCode); + return false; + } + return true; +} + +/** + * @brief Check the param of want. + * + * @param formId Indicates the form's ID. + * @param want Indicates the detailed information about the form to be obtained, including the bundle name, + * module name, ability name, form name, form id, tempForm flag, form dimension, and form customize data. + * @return Returns {@code true} if the check result is ok; returns {@code false} ng. + */ +bool Ability::CheckWantValid(const int64_t formId, const Want &want) +{ + APP_LOGI("%{public}s called.", __func__); + // get want parameters + int32_t formDimension = want.GetIntParam(Constants::PARAM_FORM_DIMENSION_KEY, 1); + std::string moduleName = want.GetStringParam(Constants::PARAM_MODULE_NAME_KEY); + + ElementName elementName = want.GetElement(); + std::string bundleName = elementName.GetBundleName(); + std::string abilityName = elementName.GetAbilityName(); + + bool tempFormFlg = want.GetBoolParam(Constants::PARAM_FORM_TEMPORARY_KEY, false); + + APP_LOGD("%{public}s, param of formDimension %{public}d in want.", __func__, formDimension); + APP_LOGD("%{public}s, param of moduleName %{public}s in want.", __func__, moduleName.c_str()); + APP_LOGD("%{public}s, param of bundleName %{public}s in want.", __func__, bundleName.c_str()); + APP_LOGD("%{public}s, param of abilityName %{public}s in want.", __func__, abilityName.c_str()); + APP_LOGD("%{public}s, param of tempFormFlg %{public}d in want.", __func__, tempFormFlg); + + // check want parameters + if (bundleName.empty() || abilityName.empty() || moduleName.empty()) { + APP_LOGE("%{public}s error, bundleName or abilityName or moduleName is not set in want.", __func__); + return false; + } + if (FormHostClient::GetInstance()->ContainsForm(formId)) { + APP_LOGE("%{public}s error, form has already acquired, do not support acquire twice.", __func__); + return false; + } + if (formDimension <= 0) { + APP_LOGE("%{public}s error, dimension should not be zero or negative in want.", __func__); + return false; + } + if (tempFormFlg && formId != 0) { + APP_LOGE("%{public}s error, can not select form id when acquire temporary form.", __func__); + return false; + } + + // the check is successfully + return true; +} + +/** + * @brief Enable form update. + * + * @param formIds FormIds of hostclient. + */ +bool Ability::EnableUpdateForm(const std::vector &formIds) +{ + APP_LOGI("%{public}s called.", __func__); + return LifecycleUpdate(formIds, ENABLE_FORM_UPDATE); +} + +/** + * @brief Disable form update. + * + * @param formIds FormIds of hostclient. + */ +bool Ability::DisableUpdateForm(const std::vector &formIds) +{ + APP_LOGI("%{public}s called.", __func__); + return LifecycleUpdate(formIds, DISABLE_FORM_UPDATE); +} + +bool Ability::LifecycleUpdate(std::vector formIds, int32_t updateType) +{ + if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) { + APP_LOGE("%{public}s error, form is in recover status, can't do action on form.", __func__); + return false; + } + + // hostClient init + sptr formHostClient = FormHostClient::GetInstance(); + if (formHostClient == nullptr) { + APP_LOGE("%{public}s error, formHostClient == nullptr.", __func__); + return false; + } + + int result = FormMgr::GetInstance().LifecycleUpdate(formIds, formHostClient, updateType); + if (result != ERR_OK) { + APP_LOGE("%{public}s error, internal error.", __func__); + return false; + } + + return true; +} + +/** + * @brief Requests for form data update, by passing a set of parameters (using Want) to the form provider. + * + * This method must be called when the application has detected that a system setting item (such as the language, + * resolution, or screen orientation) being listened for has changed. Upon receiving the update request, the form + * provider automatically updates the form data (if there is any update) through the form framework, with the update + * process being unperceivable by the application. + * + * @param formId Indicates the ID of the form to update. + * @param want Indicates a set of parameters to be transparently passed to the form provider. + * @return Returns true if the update request is successfully initiated, returns false otherwise. + */ +bool Ability::RequestForm(const int64_t formId, const Want &want) +{ + APP_LOGI("%{public}s called.", __func__); + if (formId <= 0) { + APP_LOGE("%{public}s error, The passed formid is invalid. Its value must be larger than 0.", __func__); + return false; + } + + if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) { + APP_LOGE("%{public}s error, form is in recover status, can't do action on form.", __func__); + return false; + } + + // requestForm request to fms + int resultCode = FormMgr::GetInstance().RequestForm(formId, FormHostClient::GetInstance(), want); + if (resultCode != ERR_OK) { + APP_LOGE("%{public}s error, failed to notify the form service that the form user's lifecycle is updated, error " + "code is %{public}d.", + __func__, + resultCode); + return false; + } + + return true; +} + +/** + * @brief Called to reacquire form and update the form host after the death callback is received. + * + */ +void Ability::OnDeathReceived() +{ + APP_LOGI("%{public}s called.", __func__); + int64_t formId; + std::map &userReqParams = userReqParams_; + std::vector &lostedTempForms = lostedByReconnectTempForms_; + for (const auto &userReqRaram : userReqParams) { + formId = userReqRaram.first; + Want want; + { + std::lock_guard lock(formLock); + want = userReqRaram.second; + if (want.GetBoolParam(Constants::PARAM_FORM_TEMPORARY_KEY, false) && + std::find(lostedTempForms.begin(), lostedTempForms.end(), formId) == lostedTempForms.end()) { + lostedTempForms.emplace_back(formId); + continue; + } + } + + bool result = ReAcquireForm(formId, want); + if (!result) { + APP_LOGI("%{public}s error, reacquire form failed, formId:%{public}" PRId64 ".", __func__, formId); + std::shared_ptr formCallback = nullptr; + { + std::lock_guard lock(formLock); + // get callback iterator by formId + std::map>::iterator appCallbackIterator = + appCallbacks_.find(formId); + + if (appCallbackIterator == appCallbacks_.end()) { + APP_LOGW("%{public}s error, lack of form callback for form, formId:%{public}" PRId64 ".", + __func__, + formId); + continue; + } + formCallback = appCallbackIterator->second; + } + if (formCallback == nullptr) { + APP_LOGW("%{public}s failed, callback is nullptr.", __func__); + continue; + } + + FormJsInfo formJsInfo; + formJsInfo.formId = formId; + formCallback->OnAcquired(FormCallback::OHOS_FORM_RESTORE_FAILURE, formJsInfo); + } + } +} + +/** + * @brief Reacquire a specified form when the death callback is received. + * + * @param formId Indicates the form ID. + * @param want Indicates the detailed information about the form to be obtained. + * @return Returns true if the request is successfully initiated; returns false otherwise. + */ +bool Ability::ReAcquireForm(const int64_t formId, const Want &want) +{ + APP_LOGI("%{public}s called.", __func__); + + // get the form host client + sptr formHostClient = FormHostClient::GetInstance(); + if (formHostClient == nullptr) { + APP_LOGE("%{public}s error, formHostClient is nullptr, formId:%{public}" PRId64 ".", __func__, formId); + return false; + } + + // reacquire form + FormJsInfo formJsInfo; + if (FormMgr::GetInstance().AddForm(formId, want, formHostClient, formJsInfo) != ERR_OK || formJsInfo.formId <= 0 || + formJsInfo.formId != formId) { + APP_LOGE("%{public}s error, fms reacquire form failed, formId:%{public}" PRId64 ".", __func__, formId); + return false; + } + + // handle update message of the obtained form instance. + ProcessFormUpdate(formJsInfo); + + return true; +} + +/** + * @brief Check form manager service ready. + * + * @return Return true if form manager service ready; returns false otherwise. + */ +bool Ability::CheckFMSReady() +{ + APP_LOGI("%{public}s called.", __func__); + + sptr systemAbilityManager = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + auto remoteObject = systemAbilityManager->GetSystemAbility(FORM_MGR_SERVICE_ID); + if (remoteObject == nullptr) { + APP_LOGI("%{public}s, form manager service is not ready.", __func__); + return false; + } + + return true; +} + +/** + * @brief Get All FormsInfo. + * + * @param formInfos Return the forms' information of all forms provided. + * @return Return true if the request is successfully initiated; return false otherwise. + */ +bool Ability::GetAllFormsInfo(std::vector &formInfos) +{ + APP_LOGI("%{public}s called.", __func__); + sptr iBundleMgr = GetBundleMgr(); + if (iBundleMgr == nullptr) { + APP_LOGE("%{public}s error, failed to get IBundleMgr.", __func__); + return false; + } + bool isHaveFormsInfo = iBundleMgr->GetAllFormsInfo(formInfos); + if (formInfos.size() == 0 || formInfos.empty()) { + isHaveFormsInfo = false; + } + return isHaveFormsInfo; +} + +/** + * @brief Get forms info by bundle name . + * + * @param bundleName Application name. + * @param formInfos Return the forms' information of the specify application name. + * @return Return true if the request is successfully initiated; return false otherwise. + */ +bool Ability::GetFormsInfoByApp(std::string &bundleName, std::vector &formInfos) +{ + APP_LOGI("%{public}s called.", __func__); + if (bundleName.empty()) { + APP_LOGW("Failed to Get forms info, because empty bundle name"); + return false; + } + sptr iBundleMgr = GetBundleMgr(); + if (iBundleMgr == nullptr) { + APP_LOGE("%{public}s error, failed to get IBundleMgr.", __func__); + return false; + } + bool isHaveFormsInfo = iBundleMgr->GetFormsInfoByApp(bundleName, formInfos); + if (formInfos.size() == 0 || formInfos.empty()) { + isHaveFormsInfo = false; + } + return isHaveFormsInfo; +} + +/** + * @brief Get forms info by bundle name and module name. + * + * @param bundleName bundle name. + * @param moduleName Module name of hap. + * @param formInfos Return the forms' information of the specify bundle name and module name. + * @return Return true if the request is successfully initiated; return false otherwise. + */ +bool Ability::GetFormsInfoByModule(std::string &bundleName, std::string &moduleName, std::vector &formInfos) +{ + APP_LOGI("%{public}s called.", __func__); + if (bundleName.empty() || moduleName.empty()) { + APP_LOGW("Failed to Get forms info, because empty bundleName or moduleName"); + return false; + } + sptr iBundleMgr = GetBundleMgr(); + if (iBundleMgr == nullptr) { + APP_LOGE("%{public}s error, failed to get IBundleMgr.", __func__); + return false; + } + bool isHaveFormsInfo = iBundleMgr->GetFormsInfoByModule(bundleName, moduleName, formInfos); + if (formInfos.size() == 0 || formInfos.empty()) { + isHaveFormsInfo = false; + } + return isHaveFormsInfo; +} + +/** + * @brief Acquire a bundle manager, if it not existed. + * @return returns the bundle manager ipc object, or nullptr for failed. + */ +sptr Ability::GetBundleMgr() +{ + APP_LOGI("%{public}s called.", __func__); + if (iBundleMgr_ == nullptr) { + sptr systemAbilityManager = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + auto remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (remoteObject == nullptr) { + APP_LOGE("%{public}s error, failed to get bundle manager service.", __func__); + return nullptr; + } + + iBundleMgr_ = iface_cast(remoteObject); + if (iBundleMgr_ == nullptr) { + APP_LOGE("%{public}s error, failed to get bundle manager service", __func__); + return nullptr; + } + } + + return iBundleMgr_; +} + +/** + * @brief Add the bundle manager instance for debug. + * @param bundleManager the bundle manager ipc object. + */ +void Ability::SetBundleManager(const sptr &bundleManager) +{ + APP_LOGI("%{public}s called.", __func__); + + iBundleMgr_ = bundleManager; +} + +/** + * @brief Acquire a form provider remote object. + * @return Returns form provider remote object. + */ +sptr Ability::GetFormRemoteObject() +{ + APP_LOGI("%{public}s start", __func__); + if (providerRemoteObject_ == nullptr) { + sptr providerClient = new (std::nothrow) FormProviderClient(); + std::shared_ptr thisAbility = this->shared_from_this(); + if (thisAbility == nullptr) { + APP_LOGE("%{public}s failed, thisAbility is nullptr", __func__); + } + providerClient->SetOwner(thisAbility); + providerRemoteObject_ = providerClient->AsObject(); + } + APP_LOGI("%{public}s end", __func__); + return providerRemoteObject_; +} + +/** + * @brief Set the start ability setting. + * @param setting the start ability setting. + */ +void Ability::SetStartAbilitySetting(std::shared_ptr setting) +{ + APP_LOGI("%{public}s called.", __func__); + setting_ = setting; +} + +std::vector> Ability::ExecuteBatch( + const std::vector> &operations) +{ + APP_LOGI("Ability::ExecuteBatch start"); + std::vector> results; + if (abilityInfo_ == nullptr) { + APP_LOGE("Ability::ExecuteBatch abilityInfo is nullptr"); + return results; + } + if (abilityInfo_->type != AppExecFwk::AbilityType::DATA) { + APP_LOGE("Ability::ExecuteBatch data ability type failed, current type: %{public}d", abilityInfo_->type); + return results; + } + size_t len = operations.size(); + APP_LOGI("Ability::ExecuteBatch operation is nullptr, len %{public}zu", len); + for (size_t i = 0; i < len; i++) { + std::shared_ptr operation = operations[i]; + if (operation == nullptr) { + APP_LOGI("Ability::ExecuteBatch operation is nullptr, create DataAbilityResult"); + results.push_back(std::make_shared(0)); + continue; + } + ExecuteOperation(operation, results, i); + } + APP_LOGI("Ability::ExecuteBatch end, %{public}zu", results.size()); + return results; +} +void Ability::ExecuteOperation(std::shared_ptr &operation, + std::vector> &results, int index) +{ + APP_LOGI("Ability::ExecuteOperation start, index=%{public}d", index); + if (abilityInfo_->type != AppExecFwk::AbilityType::DATA) { + APP_LOGE("Ability::ExecuteOperation data ability type failed, current type: %{public}d", abilityInfo_->type); + return; + } + if (index < 0) { + APP_LOGE( + "Ability::ExecuteOperation operation result index should not below zero, current index: %{public}d", index); + return; + } + if (operation == nullptr) { + APP_LOGI("Ability::ExecuteOperation operation is nullptr, create DataAbilityResult"); + results.push_back(std::make_shared(0)); + return; + } + + int numRows = 0; + std::shared_ptr valuesBucket = ParseValuesBucketReference(results, operation, index); + std::shared_ptr predicates = + ParsePredictionArgsReference(results, operation, index); + if (operation->IsInsertOperation()) { + APP_LOGI("Ability::ExecuteOperation IsInsertOperation"); + numRows = Insert(*(operation->GetUri().get()), *valuesBucket); + } else if (operation->IsDeleteOperation()) { + APP_LOGI("Ability::ExecuteOperation IsDeleteOperation"); + numRows = Delete(*(operation->GetUri().get()), *predicates); + } else if (operation->IsUpdateOperation()) { + APP_LOGI("Ability::ExecuteOperation IsUpdateOperation"); + numRows = Update(*(operation->GetUri().get()), *valuesBucket, *predicates); + } else if (operation->IsAssertOperation()) { + APP_LOGI("Ability::ExecuteOperation IsAssertOperation"); + std::vector columns; + std::shared_ptr queryResult = + Query(*(operation->GetUri().get()), columns, *predicates); + if (queryResult == nullptr) { + APP_LOGE("Ability::ExecuteOperation Query retval is nullptr"); + results.push_back(std::make_shared(0)); + return; + } + if (queryResult->GetRowCount(numRows) != 0) { + APP_LOGE("Ability::ExecuteOperation queryResult->GetRowCount(numRows) != E_OK"); + } + if (!CheckAssertQueryResult(queryResult, operation->GetValuesBucket())) { + if (queryResult != nullptr) { + queryResult->Close(); + } + APP_LOGE("Query Result is not equal to expected value."); + } + + if (queryResult != nullptr) { + queryResult->Close(); + } + } else { + APP_LOGE("Ability::ExecuteOperation Expected bad type %{public}d", operation->GetType()); + } + if (operation->GetExpectedCount() != numRows) { + APP_LOGE("Ability::ExecuteOperation Expected %{public}d rows but actual %{public}d", + operation->GetExpectedCount(), + numRows); + } else { + if (operation->GetUri() != nullptr) { + results.push_back(std::make_shared(*operation->GetUri(), numRows)); + } else { + results.push_back(std::make_shared(Uri(std::string("")), numRows)); + } + } +} + +std::shared_ptr Ability::ParsePredictionArgsReference( + std::vector> &results, std::shared_ptr &operation, + int numRefs) +{ + if (operation == nullptr) { + APP_LOGE("Ability::ParsePredictionArgsReference intpur is nullptr"); + return nullptr; + } + + std::map predicatesBackReferencesMap = operation->GetDataAbilityPredicatesBackReferences(); + if (predicatesBackReferencesMap.empty()) { + return operation->GetDataAbilityPredicates(); + } + + std::vector strPredicatesList; + strPredicatesList.clear(); + std::shared_ptr predicates = operation->GetDataAbilityPredicates(); + if (predicates == nullptr) { + APP_LOGI("Ability::ParsePredictionArgsReference operation->GetDataAbilityPredicates is nullptr"); + } else { + APP_LOGI("Ability::ParsePredictionArgsReference operation->GetDataAbilityPredicates isn`t nullptr"); + std::vector strPredicatesList = predicates->GetWhereArgs(); + } + + if (strPredicatesList.empty()) { + APP_LOGE("Ability::ParsePredictionArgsReference operation->GetDataAbilityPredicates()->GetWhereArgs()" + "error strList is empty()"); + } + + for (auto iterMap : predicatesBackReferencesMap) { + APP_LOGI("Ability::ParsePredictionArgsReference predicatesBackReferencesMap first:%{public}d second:%{public}d", + iterMap.first, + iterMap.second); + int tempCount = ChangeRef2Value(results, numRefs, iterMap.second); + if (tempCount < 0) { + APP_LOGE("Ability::ParsePredictionArgsReference tempCount:%{public}d", tempCount); + continue; + } + std::string strPredicates = std::to_string(tempCount); + APP_LOGI("Ability::ParsePredictionArgsReference strPredicates:%{public}s", strPredicates.c_str()); + strPredicatesList.push_back(strPredicates); + APP_LOGI("Ability::ParsePredictionArgsReference push_back done"); + } + + predicates->SetWhereArgs(strPredicatesList); + + return predicates; +} + +std::shared_ptr Ability::ParseValuesBucketReference( + std::vector> &results, std::shared_ptr &operation, + int numRefs) +{ + NativeRdb::ValuesBucket retValueBucket; + if (operation == nullptr) { + APP_LOGE("Ability::ParseValuesBucketReference intpur is nullptr"); + return nullptr; + } + + if (operation->GetValuesBucketReferences() == nullptr) { + return operation->GetValuesBucket(); + } + + retValueBucket.Clear(); + if (operation->GetValuesBucket() == nullptr) { + APP_LOGI("Ability::ParseValuesBucketReference operation->GetValuesBucket is nullptr"); + } else { + APP_LOGI("Ability::ParseValuesBucketReference operation->GetValuesBucket is nullptr"); + retValueBucket = *operation->GetValuesBucket(); + } + + std::map valuesMapReferences; + operation->GetValuesBucketReferences()->GetAll(valuesMapReferences); + + for (auto itermap : valuesMapReferences) { + std::string key = itermap.first; + NativeRdb::ValueObject obj; + if (!operation->GetValuesBucketReferences()->GetObject(key, obj)) { + APP_LOGE("Ability::ParseValuesBucketReference operation->GetValuesBucketReferences()->GetObject error"); + continue; + } + switch (obj.GetType()) { + case NativeRdb::ValueObjectType::TYPE_INT: { + int val = 0; + if (obj.GetInt(val) != 0) { + APP_LOGE("Ability::ParseValuesBucketReference ValueObject->GetInt() error"); + break; + } + APP_LOGI("Ability::ParseValuesBucketReference retValueBucket->PutInt(%{public}s, %{public}d)", + key.c_str(), + val); + retValueBucket.PutInt(key, val); + } break; + case NativeRdb::ValueObjectType::TYPE_DOUBLE: { + double val = 0.0; + if (obj.GetDouble(val) != 0) { + APP_LOGE("Ability::ParseValuesBucketReference ValueObject->GetDouble() error"); + break; + } + APP_LOGI("Ability::ParseValuesBucketReference retValueBucket->PutDouble(%{public}s, %{public}f)", + key.c_str(), + val); + retValueBucket.PutDouble(key, val); + } break; + case NativeRdb::ValueObjectType::TYPE_STRING: { + std::string val = ""; + if (obj.GetString(val) != 0) { + APP_LOGE("Ability::ParseValuesBucketReference ValueObject->GetString() error"); + break; + } + APP_LOGI("Ability::ParseValuesBucketReference retValueBucket->PutString(%{public}s, %{public}s)", + key.c_str(), + val.c_str()); + retValueBucket.PutString(key, val); + } break; + case NativeRdb::ValueObjectType::TYPE_BLOB: { + std::vector val; + if (obj.GetBlob(val) != 0) { + APP_LOGE("Ability::ParseValuesBucketReference ValueObject->GetBlob() error"); + break; + } + APP_LOGI("Ability::ParseValuesBucketReference retValueBucket->PutBlob(%{public}s, %{public}zu)", + key.c_str(), + val.size()); + retValueBucket.PutBlob(key, val); + } break; + case NativeRdb::ValueObjectType::TYPE_BOOL: { + bool val = false; + if (obj.GetBool(val) != 0) { + APP_LOGE("Ability::ParseValuesBucketReference ValueObject->GetBool() error"); + break; + } + APP_LOGI("Ability::ParseValuesBucketReference retValueBucket->PutBool(%{public}s, %{public}s)", + key.c_str(), + val ? "true" : "false"); + retValueBucket.PutBool(key, val); + } break; + default: { + APP_LOGI("Ability::ParseValuesBucketReference retValueBucket->PutNull(%{public}s)", key.c_str()); + retValueBucket.PutNull(key); + } break; + } + } + + std::map valuesMap; + retValueBucket.GetAll(valuesMap); + + return std::make_shared(valuesMap); +} + +int Ability::ChangeRef2Value(std::vector> &results, int numRefs, int index) +{ + int retval = -1; + if (index >= numRefs) { + APP_LOGE("Ability::ChangeRef2Value index >= numRefs"); + return retval; + } + + if (index >= static_cast(results.size())) { + APP_LOGE("Ability::ChangeRef2Value index:%{public}d >= results.size():%{public}zu", index, results.size()); + return retval; + } + + std::shared_ptr refResult = results[index]; + if (refResult == nullptr) { + APP_LOGE("Ability::ChangeRef2Value No.%{public}d refResult is null", index); + return retval; + } + + if (refResult->GetUri().ToString().empty()) { + retval = refResult->GetCount(); + } else { + retval = DataUriUtils::GetId(refResult->GetUri()); + } + + return retval; +} + +bool Ability::CheckAssertQueryResult(std::shared_ptr &queryResult, + std::shared_ptr &&valuesBucket) +{ + if (queryResult == nullptr) { + APP_LOGE("Ability::CheckAssertQueryResult intput queryResult is null"); + return true; + } + + if (valuesBucket == nullptr) { + APP_LOGE("Ability::CheckAssertQueryResult intput valuesBucket is null"); + return true; + } + + std::map valuesMap; + valuesBucket->GetAll(valuesMap); + if (valuesMap.empty()) { + APP_LOGE("Ability::CheckAssertQueryResult valuesMap is empty"); + return true; + } + int count = 0; + if (queryResult->GetRowCount(count) != 0) { + APP_LOGE("Ability::CheckAssertQueryResult GetRowCount is 0"); + return true; + } + + for (auto iterMap : valuesMap) { + std::string strObject; + if (iterMap.second.GetString(strObject) != 0) { + APP_LOGE("Ability::CheckAssertQueryResult GetString strObject is error"); + continue; + } + if (strObject.empty()) { + APP_LOGE("Ability::CheckAssertQueryResult strObject is empty"); + continue; + } + for (int i = 0; i < count; ++i) { + std::string strName; + if (queryResult->GetString(i, strName) != 0) { + APP_LOGE("Ability::CheckAssertQueryResult GetString strName is error"); + continue; + } + if (strName.empty()) { + APP_LOGE("Ability::CheckAssertQueryResult strName is empty"); + continue; + } + if (strName.c_str() == strObject.c_str()) { + APP_LOGE("Ability::CheckAssertQueryResult strName same to strObject"); + continue; + } + + return false; + } + } + + return true; +} +} // namespace AppExecFwk +} // namespace OHOS diff --git a/interfaces/kits/js/@ohos.ability.formManager.d.ts b/interfaces/kits/js/@ohos.ability.formManager.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..c047780aea55cf5e3d930deabf07002802ec6cd3 --- /dev/null +++ b/interfaces/kits/js/@ohos.ability.formManager.d.ts @@ -0,0 +1,854 @@ +/* + * Copyright (c) 2021 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. + */ +import { AsyncCallback } from './basic'; +import { Want } from './ability/want'; + +/** + * Client to communication with FormManagerService. + * @name formManager + * @since 7 + * @sysCap AAFwk + * @devices phone, tablet + * @import import formManager from '@ohos.ability.formManager' + */ +declare namespace formManager { + /** + * Requests for form data update. + *

This method must be called when the application has detected that a system setting item (such as the language, + * resolution, or screen orientation) being listened for has changed. Upon receiving the update request, the form + * provider automatically updates the form data (if there is any update) through the form framework, with the update + * process being unperceivable by the application.

+ *

Permission: ohos.permission.REQUIRE_FORM

+ * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @param formId Indicates the ID of the form to update. + * @return - + * @systemapi hide for inner use. + */ + function requestForm(formId: string, callback: AsyncCallback): void; + function requestForm(formId: string): Promise; + + /** + * Deletes an obtained form by its ID. + * + *

After this method is called, the form won't be available for use by the application and the Form Manager + * Service no longer keeps the cache information about the form.

+ *

Permission: ohos.permission.REQUIRE_FORM

+ * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @param formId Indicates the form ID of the form to delete. + * @return - + * @systemapi hide for inner use. + */ + function deleteForm(formId: string, callback: AsyncCallback): void; + function deleteForm(formId: string): Promise; + + /** + * Releases an obtained form by its ID. + * + *

After this method is called, the form won't be available for use by the application, but the Form Manager + * Service still keeps the cache information about the form, so that the application can quickly obtain it based on + * the {@code formId}.

+ *

Permission: ohos.permission.REQUIRE_FORM

+ * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @param formId Indicates the form ID of the form to release. + * @return - + * @systemapi hide for inner use. + */ + function releaseForm(formId: string, callback: AsyncCallback): void; + function releaseForm(formId: string, isReleaseCache: boolean, callback: AsyncCallback): void; + function releaseForm(formId: string, isReleaseCache?: boolean): Promise; + + /** + * Sends a notification to the form framework to make the specified forms visible. + * + *

After this method is successfully called, Ability.onEventNotify(Map) + * will be called to notify the form provider of the form visibility change event.

+ *

Permissions: ohos.permission.REQUIRE_FORM

+ * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @param formIds Indicates the IDs of the forms to be made visible. + * @return - + * @systemapi hide for inner use. + */ + function notifyVisibleForms(formIds: Array, callback: AsyncCallback): void; + function notifyVisibleForms(formIds: Array): Promise; + + /** + * Sends a notification to the form framework to make the specified forms invisible. + * + *

After this method is successfully called, Ability.onEventNotify(Map) + * will be called to notify the form provider of the form visibility change event.

+ *

Permissions: ohos.permission.REQUIRE_FORM

+ * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @param formIds Indicates the IDs of the forms to be made invisible. + * @return - + * @systemapi hide for inner use. + */ + function notifyInvisibleForms(formIds: Array, callback: AsyncCallback): void; + function notifyInvisibleForms(formIds: Array): Promise; + + /** + * set form refresh state to true. + * + *

You can use this method to set form refresh state to true, the form can receive new + * update from service.

+ * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @param formIds the specify form id. + * @return - + * @systemapi hide for inner use. + */ + function enableFormsUpdate(formIds: Array, callback: AsyncCallback): void; + function enableFormsUpdate(formIds: Array): Promise; + + /** + * set form refresh state to false. + * + *

You can use this method to set form refresh state to false, the form do not receive + * new update from service.

+ * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @param formIds the specify form id. + * @return - + * @systemapi hide for inner use. + */ + function disableFormsUpdate(formIds: Array, callback: AsyncCallback): void; + function disableFormsUpdate(formIds: Array): Promise; + + /** + * obtain the system is ready. + * + *

You can use this method to obtain the system is ready + * new update from service.

+ * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @param - + * @return - is system ready + * @systemapi hide for inner use. + */ + function isSystemReady(callback: AsyncCallback): void; + function isSystemReady(): Promise; + + /** + * cast a temp form to normal form. + * + *

You can use this method to cast a temp form to normal form + * new update from service.

+ * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @param formId the specify form id. + * @return - + * @systemapi hide for inner use. + */ + function castTempForm(formId: string, callback: AsyncCallback): void; + function castTempForm(formId: string): Promise; + + /** + * delete invalid forms of the application in the Form Manager Service based on the list of. + * + *

You can use this method to delete invalid forms of the application + * new update from service.

+ * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @param formIds the specify form id. + * @return - the number of invalid forms deleted by the Form Manager Service + * @systemapi hide for inner use. + */ + function deleteInvalidForms(formIds: Array, callback: AsyncCallback): void; + function deleteInvalidForms(formIds: Array): Promise; + + /** + * obtains the Form state. + * + *

You can use this method to obtains the form state + * new update from service.

+ * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @param want of the form. + * @return - form state + * @systemapi hide for inner use. + */ + function acquireFormState(want: Want, callback: AsyncCallback): void; + function acquireFormState(want: Want): Promise; + + /** + * listen to the event of uninstall form. + * + *

You can use this method to listen to the event of uninstall form + * new update from service.

+ * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @param event type. + * @return - formIds of the uninstalled form. + * @systemapi hide for inner use. + */ + function on(type: "formUninstall", callback: AsyncCallback): void; + + /** + * cancel listening to the event of uninstall form. + * + *

You can use this method to cancel listening to the event of uninstall form + * new update from service.

+ * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @param event type. + * @return - + * @systemapi hide for inner use. + */ + function off(type: "formUninstall", callback?: AsyncCallback): void; + + /** + * Obtains the FormInfo objects provided by all ohos applications on the device. + * + *

Permission: ohos.permission.GET_BUNDLE_INFO_PRIVILEGED

+ * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @return - + * @systemapi hide for inner use. + */ + function getAllFormsInfo(callback: AsyncCallback>): void; + function getAllFormsInfo(): Promise>; + + /** + * Obtains the FormInfo objects provided by a specified application module on the device. + * + *

Permission: ohos.permission.GET_BUNDLE_INFO_PRIVILEGED

+ * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @param bundleName Indicates the bundle name of the ohos application. Specifically, it is the value of the + * {@code bundleName} attribute defined in the {@code app} tag of the config.json file for the Harmony + * application. + * @param moduleName Indicates the module name of the ohos application. Specifically, it is the value of the + * {@code moduleName} sub-attribute of the {@code distro} attribute defined in the {@code module} tag of the + * config.json file for the Harmony application. Optional parameter, if not set, search is performed based on + * bundleName only. + * @return - + * @systemapi hide for inner use. + */ + function getFormsInfo(bundleName: string, callback: AsyncCallback>): void; + function getFormsInfo(bundleName: string, moduleName: string, callback: AsyncCallback>): void; + function getFormsInfo(bundleName: string, moduleName?: string): Promise>; + + /** + * Provides information about a form. + * @name FormInfo + * @since 7 + */ + interface FormInfo { + /** + * Obtains the bundle name of the application to which this form belongs. + * @default - + * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + */ + bundleName: string; + + /** + * Obtains the name of the application module to which this form belongs. + * @default - + * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + */ + moduleName: string; + + /** + * Obtains the class name of the ability to which this form belongs. + * @default - + * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + */ + abilityName: string; + + /** + * Obtains the name of this form. + * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @default - + */ + name: string; + + /** + * Obtains the description of this form. + * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @default - + */ + description?: string; + + /** + * Obtains the type of this form. Currently, Java and JS forms are supported. + * a constant value, one of [FormType.JAVA, FormType.JS]. + * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @default - + */ + type: FormType; + + /** + * Obtains the JS component name of this JS form. + *

This method is valid only when the type of this form is {@code JS}.

+ * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @default - + */ + jsComponentName: string; + + /** + * Obtains the color mode of this form. + * A constant value, one of ColorMode.MODE_AUTO, ColorMode.MODE_DARK, ColorMode.MODE_LIGHT. + * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @default - + */ + colorMode: ColorMode; + + /** + * Checks whether this form is a default form. + *

A form is considered a default form if no form name is specified. Specifically, the system determines + * whether a form is a default form based on the value of the {@code isDefault} attribute defined under + * {@code forms} in the config.json file of the Harmony application.

+ * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @default - + */ + isDefault: boolean; + + /** + * Obtains the updateEnabled. + * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @default - + * @systemapi hide for inner use. + */ + updateEnabled: boolean; + + /** + * Obtains whether notify visible of this form. + * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @default - + */ + formVisibleNotify: boolean; + + /** + * Obtains the bundle relatedBundleName of the application to which this form belongs. + * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @default - + * @systemapi hide for inner use. + */ + relatedBundleName: string; + + /** + * Obtains the scheduledUpdateTime. + * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @default - + * @systemapi hide for inner use + */ + scheduledUpdateTime: string; + + /** + * Obtains the form config ability about this form. + *

A form config ability is a user-defined ability.

+ * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @default - + */ + formConfigAbility: string; + + /** + * Obtains the updateDuration. + * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @default - + * @systemapi hide for inner use + */ + updateDuration: number; + + /** + * Obtains the default grid style of this form. + * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @default - + */ + defaultDimension: number; + + /** + * Obtains the grid styles supported by this form. + * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @default - + */ + supportDimensions: Array; + + /** + * Obtains the custom data defined in this form. + * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @default - + */ + customizeData: {[key: string]: [value: string]}; + + /** + * Indicates the compatibility object. + * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @default - + */ + jsonObject: object; + } + + /** + * Provides state information about a form. + * @name FormStateInfo + * @since 7 + */ + interface FormStateInfo { + /** + * Obtains the form state. + * @default - + * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + */ + formState: FormState; + + /** + * Obtains the want form . + * @default - + * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + */ + want: Want; + } + + /** + * Provides state about a form. + * @name FormState + * @since 7 + */ + enum FormState { + /** + * Indicates that the form status is unknown due to an internal error. + * + * @since 7 + */ + UNKNOWN = -1, + /** + * Indicates that the form is in the default state. + * + * @since 7 + */ + DEFAULT = 0, + /** + * Indicates that the form is ready. + * + * @since 7 + */ + READY = 1, + } + + enum FormType { + /** + * Java form. + * @default - + * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + */ + JAVA = 0, + + /** + * Js form. + * @default - + * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + */ + JS = 1 + } + + enum FormParam { + /** + * Indicates the key specifying the ID of the form to be obtained, which is represented as + * want: { + * "parameters": { + * IDENTITY_KEY: 1L + * } + * }. + * @default - + * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @systemapi hide for inner use. + */ + IDENTITY_KEY = "ohos.extra.param.key.form_identity", + + /** + * Indicates the key specifying the grid style of the form to be obtained, which is represented as + * want: { + * "parameters": { + * DIMENSION_KEY: 1 + * } + * }. + * @default - + * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @systemapi hide for inner use. + */ + DIMENSION_KEY = "ohos.extra.param.key.form_dimension", + + /** + * Indicates the key specifying the name of the form to be obtained, which is represented as + * want: { + * "parameters": { + * NAME_KEY: "formName" + * } + * }. + * @default - + * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @systemapi hide for inner use. + */ + NAME_KEY = "ohos.extra.param.key.form_name", + + /** + * Indicates the key specifying the name of the module to which the form to be obtained belongs, which is + * represented as + * want: { + * "parameters": { + * MODULE_NAME_KEY: "formEntry" + * } + * } + * This constant is mandatory. + * @default - + * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @systemapi hide for inner use. + */ + MODULE_NAME_KEY = "ohos.extra.param.key.module_name", + + /** + * Indicates the key specifying the width of the form to be obtained, which is represented as + * want: { + * "parameters": { + * WIDTH_KEY: 800 + * } + * } + * @default - + * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @systemapi hide for inner use. + */ + WIDTH_KEY = "ohos.extra.param.key.form_width", + + /** + * Indicates the key specifying the height of the form to be obtained, which is represented as + * want: { + * "parameters": { + * HEIGHT_KEY: 400 + * } + * } + * @default - + * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @systemapi hide for inner use. + */ + HEIGHT_KEY = "ohos.extra.param.key.form_height", + + /** + * Indicates the key specifying whether a form is temporary, which is represented as + * want: { + * "parameters": { + * TEMPORARY_KEY: true + * } + * } + * @default - + * @devices phone, tablet + * @since 7 + * @sysCap AAFwk + * @systemapi hide for inner use. + */ + TEMPORARY_KEY = "ohos.extra.param.key.form_temporary" + } + + enum FormError { + /** + * A common internal error occurs during form processing. + * @since 7 + * @sysCap AAFwk + * @devices phone, tablet + */ + ERR_COMMON = 1, + + /** + * The application does not have permission to use forms. + * Ensure that the application is granted with the ohos.permission.REQUIRE_FORM + * and ohos.permission.GET_BUNDLE_INFO_PRIVILEGED permissions. + * @since 7 + * @sysCap AAFwk + * @devices phone, tablet + */ + ERR_PERMISSION_DENY = 2, + + /** + * Failed to obtain the configuration information about the form specified by the + * request parameters. Ensure that the parameters of the form to be added are + * consistent with those provided by the form provider. + * @since 7 + * @sysCap AAFwk + * @devices phone, tablet + */ + ERR_GET_INFO_FAILED = 4, + + /** + * Failed to obtain the bundle to which the form belongs based on the request parameters. + * Ensure that the bundle to which the form to be added belongs is available. + * @since 7 + * @sysCap AAFwk + * @devices phone, tablet + */ + ERR_GET_BUNDLE_FAILED = 5, + + /** + * Failed to initialize the form layout based on the request parameters. + * Ensure that the grid style of the form is supported by the form provider. + * @since 7 + * @sysCap AAFwk + * @devices phone, tablet + */ + ERR_GET_LAYOUT_FAILED = 6, + + /** + * Invalid input parameter during form operation. Ensure that all input + * parameters are valid. + * @since 7 + * @sysCap AAFwk + * @devices phone, tablet + */ + ERR_ADD_INVALID_PARAM = 7, + + /** + * The form configuration to be obtained using an existing form ID is + * different from that obtained for the first time. + * @since 7 + * @sysCap AAFwk + * @devices phone, tablet + */ + ERR_CFG_NOT_MATCH_ID = 8, + + /** + * The ID of the form to be operated does not exist in the Form Manager Service. + * @since 7 + * @sysCap AAFwk + * @devices phone, tablet + */ + ERR_NOT_EXIST_ID = 9, + + /** + * Failed to bind the Form Manager Service to the provider service. + * @since 7 + * @sysCap AAFwk + * @devices phone, tablet + */ + ERR_BIND_PROVIDER_FAILED = 10, + + /** + * The total number of added forms exceeds the maximum allowed by the system. + * @since 7 + * @sysCap AAFwk + * @devices phone, tablet + */ + ERR_MAX_SYSTEM_FORMS = 11, + + /** + * The number of form instances generated using the same form configuration + * exceeds the maximum allowed by the system. + * @since 7 + * @sysCap AAFwk + * @devices phone, tablet + */ + ERR_MAX_INSTANCES_PER_FORM = 12, + + /** + * The form being requested was added by other applications and cannot be + * operated by the current application. + * @since 7 + * @sysCap AAFwk + * @devices phone, tablet + */ + ERR_OPERATION_FORM_NOT_SELF = 13, + + /** + * The Form Manager Service failed to instruct the form provider to delete the form. + * @since 7 + * @sysCap AAFwk + * @devices phone, tablet + */ + ERR_PROVIDER_DEL_FAIL = 14, + + /** + * The total number of added forms exceeds the maximum per client. + * @since 7 + * @sysCap AAFwk + * @devices phone, tablet + */ + ERR_MAX_FORMS_PER_CLIENT = 15, + + /** + * The total number of added temp forms exceeds the maximum in system. + * @since 7 + * @sysCap AAFwk + * @devices phone, tablet + */ + ERR_MAX_SYSTEM_TEMP_FORMS = 16, + + /** + * The module can not be find in system. + * @since 7 + * @sysCap AAFwk + * @devices phone, tablet + */ + ERR_FORM_NO_SUCH_MODULE = 17, + + /** + * The ability can not be find in system. + * @since 7 + * @sysCap AAFwk + * @devices phone, tablet + */ + ERR_FORM_NO_SUCH_ABILITY = 18, + + /** + * The dimension is not exist in the form. + * @since 7 + * @sysCap AAFwk + * @devices phone, tablet + */ + ERR_FORM_NO_SUCH_DIMENSION = 19, + + /** + * The ability is not installed. + * @since 7 + * @sysCap AAFwk + * @devices phone, tablet + */ + ERR_FORM_FA_NOT_INSTALLED = 20, + + /** + * Failed to obtain the RPC object of the Form Manager Service because + * the service is not started.Please try again after the service is started. + * @since 7 + * @sysCap AAFwk + * @devices phone, tablet + */ + ERR_SYSTEM_RESPONSES_FAILED = 30, + + /** + * Failed to obtain the form requested by the client because another form + * with the same form ID is in use. Forms in use cannot have the same ID. + * To obtain and display a form that has the same configuration as an in-use + * form in the same application, you are advised to set the form ID to 0 in + * the request parameters. + * @since 7 + * @sysCap AAFwk + * @devices phone, tablet + */ + ERR_FORM_DUPLICATE_ADDED = 31, + + /** + * The form is being restored. Perform operations on the form only after + * the restoration is complete. + * @since 7 + * @sysCap AAFwk + * @devices phone, tablet + */ + ERR_IN_RECOVERY = 36 + } + + enum ColorMode { + /** + * Automatic mode. + * @since 7 + * @sysCap AAFwk + * @devices phone, tablet + * @systemapi hide for inner use. + */ + MODE_AUTO = -1, + + /** + * Dark mode. + * @since 7 + * @sysCap AAFwk + * @devices phone, tablet + * @systemapi hide for inner use. + */ + MODE_DARK = 0, + + /** + * Light mode. + * @since 7 + * @sysCap AAFwk + * @devices phone, tablet + * @systemapi hide for inner use. + */ + MODE_LIGHT = 1 + } +} +export default formManager; \ No newline at end of file diff --git a/interfaces/kits/napi/aafwk/formManager/napi_form_manager.cpp b/interfaces/kits/napi/aafwk/formManager/napi_form_manager.cpp index 14c2dba7fec0db8fbc8a94da0d92ebefb42c4045..d800a09098cf0838c411ddba001db6fa97195dc9 100644 --- a/interfaces/kits/napi/aafwk/formManager/napi_form_manager.cpp +++ b/interfaces/kits/napi/aafwk/formManager/napi_form_manager.cpp @@ -13,21 +13,128 @@ * limitations under the License. */ -#include #include "napi_form_manager.h" -#include "napi/native_api.h" -#include "napi/native_node_api.h" +#include +#include #include #include +#include "napi/native_api.h" +#include "napi/native_node_api.h" using namespace OHOS; using namespace OHOS::AAFwk; using namespace OHOS::AppExecFwk; namespace { + constexpr size_t ARGS_SIZE_ZERO = 0; constexpr size_t ARGS_SIZE_ONE = 1; constexpr size_t ARGS_SIZE_TWO = 2; constexpr size_t ARGS_SIZE_THREE = 3; + constexpr int INT_64_LENGTH = 19; + constexpr int ZERO_VALUE = 0; + constexpr int64_t INT_64_MAX_VALUE = 0x7FFFFFFFFFFFFFFF; + constexpr int DECIMAL_VALUE = 10; + constexpr int BASE_NUMBER = 9; + constexpr int REF_COUNT = 1; +} + +/** + * @brief NapiGetResut + * + * @param[in] env The environment that the Node-API call is invoked under + * + * @return napi_value + */ +napi_value NapiGetResut(napi_env env, int iResult) +{ + napi_value result; + NAPI_CALL(env, napi_create_int32(env, iResult, &result)); + return result; +} + +/** + * @brief GetGlobalAbility + * + * @param[in] env The environment that the Node-API call is invoked under + * + * @return OHOS::AppExecFwk::Ability* + */ +OHOS::AppExecFwk::Ability* GetGlobalAbility(napi_env env) +{ + // get global value + napi_value global = nullptr; + napi_get_global(env, &global); + + // get ability + napi_value abilityObj = nullptr; + napi_get_named_property(env, global, "ability", &abilityObj); + + // get ability pointer + OHOS::AppExecFwk::Ability* ability = nullptr; + napi_get_value_external(env, abilityObj, (void**)&ability); + HILOG_INFO("%{public}s, ability = [%{public}p]", __func__, ability); + + return ability; +} + +/** + * @brief Convert string to int64_t + * + * @param[in] strInfo The string information + * @param[out] int64Value Convert string to int64_t + * + * @return Return the convert result + */ +static bool ConvertStringToInt64(const std::string &strInfo, int64_t &int64Value) +{ + size_t strLength = strInfo.size(); + if (strLength == ZERO_VALUE) { + int64Value = ZERO_VALUE; + return true; + } + std::regex pattern("^0|-?[1-9][0-9]{0,18}$"); // "^-?[0-9]{1,19}$" + std::smatch match; + if (regex_match(strInfo, match, pattern)) { + HILOG_DEBUG("%{public}s, regex_match successed.", __func__); + if (strInfo.substr(ZERO_VALUE, ZERO_VALUE + 1) != "-") { // maximum: 9223372036854775807 + if (strLength < INT_64_LENGTH) { + int64Value = std::stoll(strInfo); + return true; + } + int maxSubValue = std::stoi(strInfo.substr(ZERO_VALUE, ZERO_VALUE + 1)); + if (strLength == INT_64_LENGTH && maxSubValue < BASE_NUMBER) { + int64Value = std::stoll(strInfo); + return true; + } + // Means 0x7FFFFFFFFFFFFFFF remove the first number:(2^63 - 1 - 9 * 10 ^ 19) + int SubValue = std::stoll(strInfo.substr(ZERO_VALUE + 1, INT_64_LENGTH - 1)); + if (strLength == INT_64_LENGTH && SubValue <= INT_64_MAX_VALUE - BASE_NUMBER * + pow(DECIMAL_VALUE, INT_64_LENGTH - 1)) { + int64Value = std::stoll(strInfo); + return true; + } + } + if (strLength < INT_64_LENGTH + 1) { // The minimum value: -9223372036854775808 + int64Value = std::stoll(strInfo); + return true; + } + if (strLength == INT_64_LENGTH + 1) { + int minSubValue = std::stoi(strInfo.substr(1, 1)); + if (minSubValue < BASE_NUMBER) { + int64Value = std::stoll(strInfo); + return true; + } + + // Means 0x8000000000000000 remove the first number:-(2^63 - 9 * 10 ^ 19) + if (std::stoll(strInfo.substr(ZERO_VALUE + 2, INT_64_LENGTH - 1)) <= + (INT_64_MAX_VALUE - BASE_NUMBER * pow(DECIMAL_VALUE, INT_64_LENGTH) + 1)) { + int64Value = std::stoll(strInfo); + return true; + } + } + } + HILOG_DEBUG("%{public}s, regex_match failed.", __func__); + return false; } /** @@ -129,6 +236,12 @@ static void ParseFormInfoIntoNapi(napi_env env, const FormInfo &formInfo, napi_v HILOG_DEBUG("%{public}s, defaultFlag=%{public}d.", __func__, formInfo.defaultFlag); napi_set_named_property(env, result, "isDefault", defaultFlag); + // updateEnabled + napi_value updateEnabled; + napi_create_int32(env, (int32_t)formInfo.updateEnabled, &updateEnabled); + HILOG_DEBUG("%{public}s, updateEnabled=%{public}d.", __func__, formInfo.updateEnabled); + napi_set_named_property(env, result, "updateEnabled", updateEnabled); + // formVisibleNotify napi_value formVisibleNotify; napi_create_int32(env, (int32_t)formInfo.formVisibleNotify, &formVisibleNotify); @@ -147,6 +260,12 @@ static void ParseFormInfoIntoNapi(napi_env env, const FormInfo &formInfo, napi_v HILOG_DEBUG("%{public}s, updateDuration=%{public}d.", __func__, formInfo.updateDuration); napi_set_named_property(env, result, "updateDuration", updateDuration); + // scheduledUpdateTime + napi_value scheduledUpdateTime; + napi_create_string_utf8(env, formInfo.scheduledUpateTime.c_str(), NAPI_AUTO_LENGTH, &scheduledUpdateTime); + HILOG_DEBUG("%{public}s, scheduledUpdateTime=%{public}s.", __func__, formInfo.scheduledUpateTime.c_str()); + napi_set_named_property(env, result, "scheduledUpdateTime", scheduledUpdateTime); + // defaultDimension napi_value defaultDimension; napi_create_int32(env, formInfo.defaultDimension, &defaultDimension); @@ -167,12 +286,15 @@ static void ParseFormInfoIntoNapi(napi_env env, const FormInfo &formInfo, napi_v HILOG_DEBUG("%{public}s, supportDimensions size=%{public}zu.", __func__, formInfo.supportDimensions.size()); napi_set_named_property(env, result, "supportDimensions", supportDimensions); - // customizeDatas + // metaData + napi_value metaData; + napi_create_object(env, &metaData); + + // metaData: customizeDatas napi_value customizeDatas; napi_create_array(env, &customizeDatas); int iCustomizeDatasCount = 0; for (auto customizeData : formInfo.customizeDatas) { - napi_value customizeDataOnject = nullptr; napi_create_object(env, &customizeDataOnject); @@ -192,13 +314,8 @@ static void ParseFormInfoIntoNapi(napi_env env, const FormInfo &formInfo, napi_v ++iDimensionsCount; } HILOG_DEBUG("%{public}s, customizeDatas size=%{public}zu.", __func__, formInfo.customizeDatas.size()); - napi_set_named_property(env, result, "customizeData", customizeDatas); - - // relatedBundleName - napi_value relatedBundleName; - napi_create_string_utf8(env, formInfo.relatedBundleName.c_str(), NAPI_AUTO_LENGTH, &relatedBundleName); - HILOG_DEBUG("%{public}s, relatedBundleName=%{public}s.", __func__, formInfo.relatedBundleName.c_str()); - napi_set_named_property(env, result, "relatedBundleName", relatedBundleName); + napi_set_named_property(env, metaData, "customizeData", customizeDatas); + napi_set_named_property(env, result, "metaData", metaData); return; } @@ -208,7 +325,7 @@ static void ParseFormInfoIntoNapi(napi_env env, const FormInfo &formInfo, napi_v * * @param[in] env The environment that the Node-API call is invoked under * @param[out] asyncCallbackInfo Reference, callback info via Node-API - * + * * @return void */ static void InnerDelForm(napi_env env, AsyncDelFormCallbackInfo* const asyncCallbackInfo) @@ -229,7 +346,7 @@ static void InnerDelForm(napi_env env, AsyncDelFormCallbackInfo* const asyncCall * * @param[in] env The environment that the Node-API call is invoked under * @param[out] info An opaque datatype that is passed to a callback function - * + * * @return This is an opaque pointer that is used to represent a JavaScript value */ napi_value NAPI_DeleteForm(napi_env env, napi_callback_info info) @@ -246,19 +363,6 @@ napi_value NAPI_DeleteForm(napi_env env, napi_callback_info info) } HILOG_INFO("%{public}s, argc = [%{public}zu]", __func__, argc); - // get global value - napi_value global = nullptr; - napi_get_global(env, &global); - - // get ability - napi_value abilityObj = nullptr; - napi_get_named_property(env, global, "ability", &abilityObj); - - // get ability pointer - OHOS::AppExecFwk::Ability* ability = nullptr; - napi_get_value_external(env, abilityObj, (void**)&ability); - HILOG_INFO("%{public}s, ability = [%{public}p]", __func__, ability); - // Check the value type of the arguments napi_valuetype valueType; NAPI_CALL(env, napi_typeof(env, argv[0], &valueType)); @@ -266,12 +370,16 @@ napi_value NAPI_DeleteForm(napi_env env, napi_callback_info info) expected type is string."); std::string strFormId = GetStringFromNAPI(env, argv[0]); - int64_t formId = std::stoll(strFormId); + int64_t formId; + bool isConversionSucceeded = ConvertStringToInt64(strFormId, formId); + NAPI_ASSERT(env, isConversionSucceeded, "The arguments[0] type of deleteForm is incorrect,\ + expected type is string and the content must be numeric,\ + value range is: 0x8000000000000000~0x7FFFFFFFFFFFFFFF."); - AsyncDelFormCallbackInfo *asyncCallbackInfo = new + AsyncDelFormCallbackInfo *asyncCallbackInfo = new AsyncDelFormCallbackInfo { .env = env, - .ability = ability, + .ability = GetGlobalAbility(env), .asyncWork = nullptr, .deferred = nullptr, .callback = nullptr, @@ -289,7 +397,7 @@ napi_value NAPI_DeleteForm(napi_env env, napi_callback_info info) NAPI_ASSERT(env, valueType == napi_function, "The arguments[1] type of deleteForm is incorrect,\ expected type is function."); - napi_create_reference(env, argv[1], 1, &asyncCallbackInfo->callback); + napi_create_reference(env, argv[1], REF_COUNT, &asyncCallbackInfo->callback); napi_value resourceName; napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); @@ -314,7 +422,7 @@ napi_value NAPI_DeleteForm(napi_env env, napi_callback_info info) napi_get_undefined(env, &undefined); napi_get_reference_value(env, asyncCallbackInfo->callback, &callback); napi_value callResult; - napi_call_function(env, undefined, callback, 1, &result, &callResult); + napi_call_function(env, undefined, callback, ARGS_SIZE_ONE, &result, &callResult); napi_delete_reference(env, asyncCallbackInfo->callback); } napi_delete_async_work(env, asyncCallbackInfo->asyncWork); @@ -323,10 +431,7 @@ napi_value NAPI_DeleteForm(napi_env env, napi_callback_info info) (void *)asyncCallbackInfo, &asyncCallbackInfo->asyncWork); NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); - - napi_value result; - NAPI_CALL(env, napi_create_int32(env, 1, &result)); - return result; + return NapiGetResut(env, 1); } else { HILOG_INFO("%{public}s, promise.", __func__); napi_deferred deferred; @@ -367,7 +472,7 @@ napi_value NAPI_DeleteForm(napi_env env, napi_callback_info info) * * @param[in] env The environment that the Node-API call is invoked under * @param[out] asyncCallbackInfo Reference, callback info via Node-API - * + * * @return void */ static void InnerReleaseForm(napi_env env, AsyncReleaseFormCallbackInfo* const asyncCallbackInfo) @@ -388,7 +493,7 @@ static void InnerReleaseForm(napi_env env, AsyncReleaseFormCallbackInfo* const a * * @param[in] env The environment that the Node-API call is invoked under * @param[out] info An opaque datatype that is passed to a callback function - * + * * @return This is an opaque pointer that is used to represent a JavaScript value */ napi_value NAPI_ReleaseForm(napi_env env, napi_callback_info info) @@ -412,7 +517,11 @@ napi_value NAPI_ReleaseForm(napi_env env, napi_callback_info info) expected type is string."); std::string strFormId = GetStringFromNAPI(env, argv[0]); - int64_t formId = std::stoll(strFormId); + int64_t formId; + bool isConversionSucceeded = ConvertStringToInt64(strFormId, formId); + NAPI_ASSERT(env, isConversionSucceeded, "The arguments[0] type of releaseForm is incorrect,\ + expected type is string and the content must be numeric,\ + value range is: 0x8000000000000000~0x7FFFFFFFFFFFFFFF."); valueType = napi_undefined; NAPI_CALL(env, napi_typeof(env, argv[1], &valueType)); @@ -422,23 +531,10 @@ napi_value NAPI_ReleaseForm(napi_env env, napi_callback_info info) bool isReleaseCache = false; napi_get_value_bool(env, argv[1], &isReleaseCache); - // get global value - napi_value global = nullptr; - napi_get_global(env, &global); - - // get ability - napi_value abilityObj = nullptr; - napi_get_named_property(env, global, "ability", &abilityObj); - - // get ability pointer - OHOS::AppExecFwk::Ability* ability = nullptr; - napi_get_value_external(env, abilityObj, (void**)&ability); - HILOG_INFO("%{public}s, ability = [%{public}p]", __func__, ability); - - AsyncReleaseFormCallbackInfo *asyncCallbackInfo = new + AsyncReleaseFormCallbackInfo *asyncCallbackInfo = new AsyncReleaseFormCallbackInfo { .env = env, - .ability = ability, + .ability = GetGlobalAbility(env), .asyncWork = nullptr, .deferred = nullptr, .callback = nullptr, @@ -456,7 +552,7 @@ napi_value NAPI_ReleaseForm(napi_env env, napi_callback_info info) NAPI_ASSERT(env, valueType == napi_function, "The arguments[2] type of releaseForm is incorrect,\ expected type is function."); - napi_create_reference(env, argv[2], 1, &asyncCallbackInfo->callback); + napi_create_reference(env, argv[ARGS_SIZE_TWO], REF_COUNT, &asyncCallbackInfo->callback); napi_value resourceName; napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); @@ -481,7 +577,7 @@ napi_value NAPI_ReleaseForm(napi_env env, napi_callback_info info) napi_get_undefined(env, &undefined); napi_get_reference_value(env, asyncCallbackInfo->callback, &callback); napi_value callResult; - napi_call_function(env, undefined, callback, 1, &result, &callResult); + napi_call_function(env, undefined, callback, ARGS_SIZE_ONE, &result, &callResult); napi_delete_reference(env, asyncCallbackInfo->callback); } napi_delete_async_work(env, asyncCallbackInfo->asyncWork); @@ -490,10 +586,7 @@ napi_value NAPI_ReleaseForm(napi_env env, napi_callback_info info) (void *)asyncCallbackInfo, &asyncCallbackInfo->asyncWork); NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); - - napi_value result; - NAPI_CALL(env, napi_create_int32(env, 1, &result)); - return result; + return NapiGetResut(env, 1); } else { HILOG_INFO("%{public}s, promise.", __func__); napi_deferred deferred; @@ -534,7 +627,7 @@ napi_value NAPI_ReleaseForm(napi_env env, napi_callback_info info) * * @param[in] env The environment that the Node-API call is invoked under * @param[out] asyncCallbackInfo Reference, callback info via Node-API - * + * * @return void */ static void InnerRequestForm(napi_env env, AsyncRequestFormCallbackInfo* const asyncCallbackInfo) @@ -555,7 +648,7 @@ static void InnerRequestForm(napi_env env, AsyncRequestFormCallbackInfo* const a * * @param[in] env The environment that the Node-API call is invoked under * @param[out] info An opaque datatype that is passed to a callback function - * + * * @return This is an opaque pointer that is used to represent a JavaScript value */ napi_value NAPI_RequestForm(napi_env env, napi_callback_info info) @@ -579,25 +672,16 @@ napi_value NAPI_RequestForm(napi_env env, napi_callback_info info) expected type is string."); std::string strFormId = GetStringFromNAPI(env, argv[0]); - int64_t formId = std::stoll(strFormId); - - // get global value - napi_value global = nullptr; - napi_get_global(env, &global); - - // get ability - napi_value abilityObj = nullptr; - napi_get_named_property(env, global, "ability", &abilityObj); - - // get ability pointer - OHOS::AppExecFwk::Ability* ability = nullptr; - napi_get_value_external(env, abilityObj, (void**)&ability); - HILOG_INFO("%{public}s, ability = [%{public}p]", __func__, ability); + int64_t formId; + bool isConversionSucceeded = ConvertStringToInt64(strFormId, formId); + NAPI_ASSERT(env, isConversionSucceeded, "The arguments[0] type of requestForm is incorrect,\ + expected type is string and the content must be numeric,\ + value range is: 0x8000000000000000~0x7FFFFFFFFFFFFFFF."); - AsyncRequestFormCallbackInfo *asyncCallbackInfo = new + AsyncRequestFormCallbackInfo *asyncCallbackInfo = new AsyncRequestFormCallbackInfo { .env = env, - .ability = ability, + .ability = GetGlobalAbility(env), .asyncWork = nullptr, .deferred = nullptr, .callback = nullptr, @@ -614,7 +698,7 @@ napi_value NAPI_RequestForm(napi_env env, napi_callback_info info) NAPI_ASSERT(env, valueType == napi_function, "The arguments[1] type of requestForm is incorrect,\ expected type is function."); - napi_create_reference(env, argv[1], 1, &asyncCallbackInfo->callback); + napi_create_reference(env, argv[1], REF_COUNT, &asyncCallbackInfo->callback); napi_value resourceName; napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); @@ -639,7 +723,7 @@ napi_value NAPI_RequestForm(napi_env env, napi_callback_info info) napi_get_undefined(env, &undefined); napi_get_reference_value(env, asyncCallbackInfo->callback, &callback); napi_value callResult; - napi_call_function(env, undefined, callback, 1, &result, &callResult); + napi_call_function(env, undefined, callback, ARGS_SIZE_ONE, &result, &callResult); napi_delete_reference(env, asyncCallbackInfo->callback); } napi_delete_async_work(env, asyncCallbackInfo->asyncWork); @@ -648,10 +732,7 @@ napi_value NAPI_RequestForm(napi_env env, napi_callback_info info) (void *)asyncCallbackInfo, &asyncCallbackInfo->asyncWork); NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); - - napi_value result; - NAPI_CALL(env, napi_create_int32(env, 1, &result)); - return result; + return NapiGetResut(env, 1); } else { HILOG_INFO("%{public}s, promise.", __func__); napi_deferred deferred; @@ -692,7 +773,7 @@ napi_value NAPI_RequestForm(napi_env env, napi_callback_info info) * * @param[in] env The environment that the Node-API call is invoked under * @param[out] asyncCallbackInfo Reference, callback info via Node-API - * + * * @return void */ static void InnerSetFormNextRefreshTime(napi_env env, AsyncNextRefreshTimeFormCallbackInfo* const asyncCallbackInfo) @@ -713,7 +794,7 @@ static void InnerSetFormNextRefreshTime(napi_env env, AsyncNextRefreshTimeFormCa * * @param[in] env The environment that the Node-API call is invoked under * @param[out] info An opaque datatype that is passed to a callback function - * + * * @return This is an opaque pointer that is used to represent a JavaScript value */ napi_value NAPI_SetFormNextRefreshTime(napi_env env, napi_callback_info info) @@ -737,7 +818,11 @@ napi_value NAPI_SetFormNextRefreshTime(napi_env env, napi_callback_info info) expected type is string."); std::string strFormId = GetStringFromNAPI(env, argv[0]); - int64_t formId = std::stoll(strFormId); + int64_t formId; + bool isConversionSucceeded = ConvertStringToInt64(strFormId, formId); + NAPI_ASSERT(env, isConversionSucceeded, "The arguments[0] type of setFormNextRefreshTime is incorrect,\ + expected type is string and the content must be numeric,\ + value range is: 0x8000000000000000~0x7FFFFFFFFFFFFFFF."); valueType = napi_undefined; NAPI_CALL(env, napi_typeof(env, argv[1], &valueType)); @@ -747,23 +832,10 @@ napi_value NAPI_SetFormNextRefreshTime(napi_env env, napi_callback_info info) int32_t time; napi_get_value_int32(env, argv[1], &time); - // get global value - napi_value global = nullptr; - napi_get_global(env, &global); - - // get ability - napi_value abilityObj = nullptr; - napi_get_named_property(env, global, "ability", &abilityObj); - - // get ability pointer - OHOS::AppExecFwk::Ability* ability = nullptr; - napi_get_value_external(env, abilityObj, (void**)&ability); - HILOG_INFO("%{public}s, ability = [%{public}p]", __func__, ability); - - AsyncNextRefreshTimeFormCallbackInfo *asyncCallbackInfo = new + AsyncNextRefreshTimeFormCallbackInfo *asyncCallbackInfo = new AsyncNextRefreshTimeFormCallbackInfo { .env = env, - .ability = ability, + .ability = GetGlobalAbility(env), .asyncWork = nullptr, .deferred = nullptr, .callback = nullptr, @@ -781,7 +853,7 @@ napi_value NAPI_SetFormNextRefreshTime(napi_env env, napi_callback_info info) NAPI_ASSERT(env, valueType == napi_function, "The arguments[2] type of setFormNextRefreshTime is incorrect,\ expected type is function."); - napi_create_reference(env, argv[1], 1, &asyncCallbackInfo->callback); + napi_create_reference(env, argv[1], REF_COUNT, &asyncCallbackInfo->callback); napi_value resourceName; napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); @@ -811,7 +883,7 @@ napi_value NAPI_SetFormNextRefreshTime(napi_env env, napi_callback_info info) napi_get_undefined(env, &undefined); napi_get_reference_value(env, asyncCallbackInfo->callback, &callback); napi_value callResult; - napi_call_function(env, undefined, callback, 1, &result, &callResult); + napi_call_function(env, undefined, callback, ARGS_SIZE_ONE, &result, &callResult); napi_delete_reference(env, asyncCallbackInfo->callback); } napi_delete_async_work(env, asyncCallbackInfo->asyncWork); @@ -820,10 +892,7 @@ napi_value NAPI_SetFormNextRefreshTime(napi_env env, napi_callback_info info) (void *)asyncCallbackInfo, &asyncCallbackInfo->asyncWork); NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); - - napi_value result; - NAPI_CALL(env, napi_create_int32(env, 1, &result)); - return result; + return NapiGetResut(env, 1); } else { HILOG_INFO("%{public}s, promise.", __func__); napi_deferred deferred; @@ -867,7 +936,7 @@ napi_value NAPI_SetFormNextRefreshTime(napi_env env, napi_callback_info info) * * @param[in] env The environment that the Node-API call is invoked under * @param[out] asyncCallbackInfo Reference, callback info via Node-API - * + * * @return void */ static void InnerUpdateForm(napi_env env, AsyncUpdateFormCallbackInfo* const asyncCallbackInfo) @@ -888,7 +957,7 @@ static void InnerUpdateForm(napi_env env, AsyncUpdateFormCallbackInfo* const asy * * @param[in] env The environment that the Node-API call is invoked under * @param[out] info An opaque datatype that is passed to a callback function - * + * * @return This is an opaque pointer that is used to represent a JavaScript value */ napi_value NAPI_UpdateForm(napi_env env, napi_callback_info info) @@ -912,7 +981,11 @@ napi_value NAPI_UpdateForm(napi_env env, napi_callback_info info) expected type is string."); std::string strFormId = GetStringFromNAPI(env, argv[0]); - int64_t formId = std::stoll(strFormId); + int64_t formId; + bool isConversionSucceeded = ConvertStringToInt64(strFormId, formId); + NAPI_ASSERT(env, isConversionSucceeded, "The arguments[0] type of updateForm is incorrect,\ + expected type is string and the content must be numeric,\ + value range is: 0x8000000000000000~0x7FFFFFFFFFFFFFFF."); NAPI_CALL(env, napi_typeof(env, argv[1], &valueType)); NAPI_ASSERT(env, valueType == napi_string, "The arguments[1] type of updateForm is incorrect,\ @@ -921,23 +994,10 @@ napi_value NAPI_UpdateForm(napi_env env, napi_callback_info info) OHOS::AppExecFwk::FormProviderData *formProviderData = nullptr; napi_unwrap(env, argv[1], (void**)&formProviderData); - // get global value - napi_value global = nullptr; - napi_get_global(env, &global); - - // get ability - napi_value abilityObj = nullptr; - napi_get_named_property(env, global, "ability", &abilityObj); - - // get ability pointer - OHOS::AppExecFwk::Ability* ability = nullptr; - napi_get_value_external(env, abilityObj, (void**)&ability); - HILOG_INFO("%{public}s, ability = [%{public}p]", __func__, ability); - - AsyncUpdateFormCallbackInfo *asyncCallbackInfo = new + AsyncUpdateFormCallbackInfo *asyncCallbackInfo = new AsyncUpdateFormCallbackInfo { .env = env, - .ability = ability, + .ability = GetGlobalAbility(env), .asyncWork = nullptr, .deferred = nullptr, .callback = nullptr, @@ -955,7 +1015,7 @@ napi_value NAPI_UpdateForm(napi_env env, napi_callback_info info) NAPI_ASSERT(env, valueType == napi_function, "The arguments[2] type of updateForm is incorrect,\ expected type is function."); - napi_create_reference(env, argv[1], 1, &asyncCallbackInfo->callback); + napi_create_reference(env, argv[1], REF_COUNT, &asyncCallbackInfo->callback); napi_value resourceName; napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); @@ -980,7 +1040,7 @@ napi_value NAPI_UpdateForm(napi_env env, napi_callback_info info) napi_get_undefined(env, &undefined); napi_get_reference_value(env, asyncCallbackInfo->callback, &callback); napi_value callResult; - napi_call_function(env, undefined, callback, 1, &result, &callResult); + napi_call_function(env, undefined, callback, ARGS_SIZE_ONE, &result, &callResult); napi_delete_reference(env, asyncCallbackInfo->callback); } napi_delete_async_work(env, asyncCallbackInfo->asyncWork); @@ -989,10 +1049,7 @@ napi_value NAPI_UpdateForm(napi_env env, napi_callback_info info) (void *)asyncCallbackInfo, &asyncCallbackInfo->asyncWork); NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); - - napi_value result; - NAPI_CALL(env, napi_create_int32(env, 1, &result)); - return result; + return NapiGetResut(env, 1); } else { HILOG_INFO("%{public}s, promise.", __func__); napi_deferred deferred; @@ -1033,7 +1090,7 @@ napi_value NAPI_UpdateForm(napi_env env, napi_callback_info info) * * @param[in] env The environment that the Node-API call is invoked under * @param[out] asyncCallbackInfo Reference, callback info via Node-API - * + * * @return void */ static void InnerCastTempForm(napi_env env, AsyncCastTempFormCallbackInfo* const asyncCallbackInfo) @@ -1054,7 +1111,7 @@ static void InnerCastTempForm(napi_env env, AsyncCastTempFormCallbackInfo* const * * @param[in] env The environment that the Node-API call is invoked under * @param[out] info An opaque datatype that is passed to a callback function - * + * * @return This is an opaque pointer that is used to represent a JavaScript value */ napi_value NAPI_CastTempForm(napi_env env, napi_callback_info info) @@ -1078,25 +1135,16 @@ napi_value NAPI_CastTempForm(napi_env env, napi_callback_info info) expected type is string."); std::string strFormId = GetStringFromNAPI(env, argv[0]); - int64_t formId = std::stoll(strFormId); - - // get global value - napi_value global = nullptr; - napi_get_global(env, &global); - - // get ability - napi_value abilityObj = nullptr; - napi_get_named_property(env, global, "ability", &abilityObj); - - // get ability pointer - OHOS::AppExecFwk::Ability* ability = nullptr; - napi_get_value_external(env, abilityObj, (void**)&ability); - HILOG_INFO("%{public}s, ability = [%{public}p]", __func__, ability); + int64_t formId; + bool isConversionSucceeded = ConvertStringToInt64(strFormId, formId); + NAPI_ASSERT(env, isConversionSucceeded, "The arguments[0] type of castTempForm is incorrect,\ + expected type is string and the content must be numeric,\ + value range is: 0x8000000000000000~0x7FFFFFFFFFFFFFFF."); - AsyncCastTempFormCallbackInfo *asyncCallbackInfo = new + AsyncCastTempFormCallbackInfo *asyncCallbackInfo = new AsyncCastTempFormCallbackInfo { .env = env, - .ability = ability, + .ability = GetGlobalAbility(env), .asyncWork = nullptr, .deferred = nullptr, .callback = nullptr, @@ -1113,7 +1161,7 @@ napi_value NAPI_CastTempForm(napi_env env, napi_callback_info info) NAPI_ASSERT(env, valueType == napi_function, "The arguments[1] type of castTempForm is incorrect,\ expected type is function."); - napi_create_reference(env, argv[1], 1, &asyncCallbackInfo->callback); + napi_create_reference(env, argv[1], REF_COUNT, &asyncCallbackInfo->callback); napi_value resourceName; napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); @@ -1138,7 +1186,7 @@ napi_value NAPI_CastTempForm(napi_env env, napi_callback_info info) napi_get_undefined(env, &undefined); napi_get_reference_value(env, asyncCallbackInfo->callback, &callback); napi_value callResult; - napi_call_function(env, undefined, callback, 1, &result, &callResult); + napi_call_function(env, undefined, callback, ARGS_SIZE_ONE, &result, &callResult); napi_delete_reference(env, asyncCallbackInfo->callback); } napi_delete_async_work(env, asyncCallbackInfo->asyncWork); @@ -1147,10 +1195,7 @@ napi_value NAPI_CastTempForm(napi_env env, napi_callback_info info) (void *)asyncCallbackInfo, &asyncCallbackInfo->asyncWork); NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); - - napi_value result; - NAPI_CALL(env, napi_create_int32(env, 1, &result)); - return result; + return NapiGetResut(env, 1); } else { HILOG_INFO("%{public}s, promise.", __func__); napi_deferred deferred; @@ -1191,7 +1236,7 @@ napi_value NAPI_CastTempForm(napi_env env, napi_callback_info info) * * @param[in] env The environment that the Node-API call is invoked under * @param[out] asyncCallbackInfo Reference, callback info via Node-API - * + * * @return void */ static void InnerNotifyVisibleForms(napi_env env, AsyncNotifyVisibleFormsCallbackInfo* const asyncCallbackInfo) @@ -1212,7 +1257,7 @@ static void InnerNotifyVisibleForms(napi_env env, AsyncNotifyVisibleFormsCallbac * * @param[in] env The environment that the Node-API call is invoked under * @param[out] info An opaque datatype that is passed to a callback function - * + * * @return This is an opaque pointer that is used to represent a JavaScript value */ napi_value NAPI_NotifyVisibleForms(napi_env env, napi_callback_info info) @@ -1248,28 +1293,18 @@ napi_value NAPI_NotifyVisibleForms(napi_env env, napi_callback_info info) expected type is string."); std::string strFormId = GetStringFromNAPI(env, napiFormId); - int64_t formIdValue = std::stoll(strFormId); - + int64_t formIdValue; + bool isConversionSucceeded = ConvertStringToInt64(strFormId, formIdValue); + NAPI_ASSERT(env, isConversionSucceeded, "The arguments[0] type of notifyVisibleForms is incorrect,\ + expected type is string and the content must be numeric,\ + value range is: 0x8000000000000000~0x7FFFFFFFFFFFFFFF."); formIds.push_back(formIdValue); } - // get global value - napi_value global = nullptr; - napi_get_global(env, &global); - - // get ability - napi_value abilityObj = nullptr; - napi_get_named_property(env, global, "ability", &abilityObj); - - // get ability pointer - OHOS::AppExecFwk::Ability* ability = nullptr; - napi_get_value_external(env, abilityObj, (void**)&ability); - HILOG_INFO("%{public}s, ability = [%{public}p]", __func__, ability); - - AsyncNotifyVisibleFormsCallbackInfo *asyncCallbackInfo = new + AsyncNotifyVisibleFormsCallbackInfo *asyncCallbackInfo = new AsyncNotifyVisibleFormsCallbackInfo { .env = env, - .ability = ability, + .ability = GetGlobalAbility(env), .asyncWork = nullptr, .deferred = nullptr, .callback = nullptr, @@ -1286,7 +1321,7 @@ napi_value NAPI_NotifyVisibleForms(napi_env env, napi_callback_info info) NAPI_ASSERT(env, valueType == napi_function, "The arguments[1] type of notifyVisibleForms is incorrect,\ expected type is function."); - napi_create_reference(env, argv[1], 1, &asyncCallbackInfo->callback); + napi_create_reference(env, argv[1], REF_COUNT, &asyncCallbackInfo->callback); napi_value resourceName; napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); @@ -1316,7 +1351,7 @@ napi_value NAPI_NotifyVisibleForms(napi_env env, napi_callback_info info) napi_get_undefined(env, &undefined); napi_get_reference_value(env, asyncCallbackInfo->callback, &callback); napi_value callResult; - napi_call_function(env, undefined, callback, 1, &result, &callResult); + napi_call_function(env, undefined, callback, ARGS_SIZE_ONE, &result, &callResult); napi_delete_reference(env, asyncCallbackInfo->callback); } napi_delete_async_work(env, asyncCallbackInfo->asyncWork); @@ -1325,10 +1360,7 @@ napi_value NAPI_NotifyVisibleForms(napi_env env, napi_callback_info info) (void *)asyncCallbackInfo, &asyncCallbackInfo->asyncWork); NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); - - napi_value result; - NAPI_CALL(env, napi_create_int32(env, 1, &result)); - return result; + return NapiGetResut(env, 1); } else { HILOG_INFO("%{public}s, promise.", __func__); napi_deferred deferred; @@ -1374,7 +1406,7 @@ napi_value NAPI_NotifyVisibleForms(napi_env env, napi_callback_info info) * * @param[in] env The environment that the Node-API call is invoked under * @param[out] asyncCallbackInfo Reference, callback info via Node-API - * + * * @return void */ static void InnerNotifyInvisibleForms(napi_env env, AsyncNotifyInvisibleFormsCallbackInfo* const asyncCallbackInfo) @@ -1395,7 +1427,7 @@ static void InnerNotifyInvisibleForms(napi_env env, AsyncNotifyInvisibleFormsCal * * @param[in] env The environment that the Node-API call is invoked under * @param[out] info An opaque datatype that is passed to a callback function - * + * * @return This is an opaque pointer that is used to represent a JavaScript value */ napi_value NAPI_NotifyInvisibleForms(napi_env env, napi_callback_info info) @@ -1431,28 +1463,18 @@ napi_value NAPI_NotifyInvisibleForms(napi_env env, napi_callback_info info) is incorrect, expected type is string."); std::string strFormId = GetStringFromNAPI(env, napiFormId); - int64_t formIdValue = std::stoll(strFormId); - + int64_t formIdValue; + bool isConversionSucceeded = ConvertStringToInt64(strFormId, formIdValue); + NAPI_ASSERT(env, isConversionSucceeded, "The arguments[0] type of notifyInvisibleForms is incorrect,\ + expected type is string and the content must be numeric,\ + value range is: 0x8000000000000000~0x7FFFFFFFFFFFFFFF."); formIds.push_back(formIdValue); } - // get global value - napi_value global = nullptr; - napi_get_global(env, &global); - - // get ability - napi_value abilityObj = nullptr; - napi_get_named_property(env, global, "ability", &abilityObj); - - // get ability pointer - OHOS::AppExecFwk::Ability* ability = nullptr; - napi_get_value_external(env, abilityObj, (void**)&ability); - HILOG_INFO("%{public}s, ability = [%{public}p]", __func__, ability); - - AsyncNotifyInvisibleFormsCallbackInfo *asyncCallbackInfo = new + AsyncNotifyInvisibleFormsCallbackInfo *asyncCallbackInfo = new AsyncNotifyInvisibleFormsCallbackInfo { .env = env, - .ability = ability, + .ability = GetGlobalAbility(env), .asyncWork = nullptr, .deferred = nullptr, .callback = nullptr, @@ -1469,7 +1491,7 @@ napi_value NAPI_NotifyInvisibleForms(napi_env env, napi_callback_info info) NAPI_ASSERT(env, valueType == napi_function, "The arguments[1] type of notifyInvisibleForms is incorrect,\ expected type is function."); - napi_create_reference(env, argv[1], 1, &asyncCallbackInfo->callback); + napi_create_reference(env, argv[1], REF_COUNT, &asyncCallbackInfo->callback); napi_value resourceName; napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); @@ -1499,7 +1521,7 @@ napi_value NAPI_NotifyInvisibleForms(napi_env env, napi_callback_info info) napi_get_undefined(env, &undefined); napi_get_reference_value(env, asyncCallbackInfo->callback, &callback); napi_value callResult; - napi_call_function(env, undefined, callback, 1, &result, &callResult); + napi_call_function(env, undefined, callback, ARGS_SIZE_ONE, &result, &callResult); napi_delete_reference(env, asyncCallbackInfo->callback); } napi_delete_async_work(env, asyncCallbackInfo->asyncWork); @@ -1508,10 +1530,7 @@ napi_value NAPI_NotifyInvisibleForms(napi_env env, napi_callback_info info) (void *)asyncCallbackInfo, &asyncCallbackInfo->asyncWork); NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); - - napi_value result; - NAPI_CALL(env, napi_create_int32(env, 1, &result)); - return result; + return NapiGetResut(env, 1); } else { HILOG_INFO("%{public}s, promise.", __func__); napi_deferred deferred; @@ -1557,7 +1576,7 @@ napi_value NAPI_NotifyInvisibleForms(napi_env env, napi_callback_info info) * * @param[in] env The environment that the Node-API call is invoked under * @param[out] asyncCallbackInfo Reference, callback info via Node-API - * + * * @return void */ static void InnerEnableFormsUpdate(napi_env env, AsyncEnableUpdateFormCallbackInfo* const asyncCallbackInfo) @@ -1578,7 +1597,7 @@ static void InnerEnableFormsUpdate(napi_env env, AsyncEnableUpdateFormCallbackIn * * @param[in] env The environment that the Node-API call is invoked under * @param[out] info An opaque datatype that is passed to a callback function - * + * * @return This is an opaque pointer that is used to represent a JavaScript value */ napi_value NAPI_EnableFormsUpdate(napi_env env, napi_callback_info info) @@ -1614,28 +1633,18 @@ napi_value NAPI_EnableFormsUpdate(napi_env env, napi_callback_info info) is incorrect, expected type is string."); std::string strFormId = GetStringFromNAPI(env, napiFormId); - int64_t formIdValue = std::stoll(strFormId); - + int64_t formIdValue; + bool isConversionSucceeded = ConvertStringToInt64(strFormId, formIdValue); + NAPI_ASSERT(env, isConversionSucceeded, "The arguments[0] type of enableFormsUpdate is incorrect,\ + expected type is string and the content must be numeric,\ + value range is: 0x8000000000000000~0x7FFFFFFFFFFFFFFF."); formIds.push_back(formIdValue); } - // get global value - napi_value global = nullptr; - napi_get_global(env, &global); - - // get ability - napi_value abilityObj = nullptr; - napi_get_named_property(env, global, "ability", &abilityObj); - - // get ability pointer - OHOS::AppExecFwk::Ability* ability = nullptr; - napi_get_value_external(env, abilityObj, (void**)&ability); - HILOG_INFO("%{public}s, ability = [%{public}p]", __func__, ability); - - AsyncEnableUpdateFormCallbackInfo *asyncCallbackInfo = new + AsyncEnableUpdateFormCallbackInfo *asyncCallbackInfo = new AsyncEnableUpdateFormCallbackInfo { .env = env, - .ability = ability, + .ability = GetGlobalAbility(env), .asyncWork = nullptr, .deferred = nullptr, .callback = nullptr, @@ -1652,7 +1661,7 @@ napi_value NAPI_EnableFormsUpdate(napi_env env, napi_callback_info info) NAPI_ASSERT(env, valueType == napi_function, "The arguments[1] type of enableFormsUpdate \ is incorrect, expected type is function."); - napi_create_reference(env, argv[1], 1, &asyncCallbackInfo->callback); + napi_create_reference(env, argv[1], REF_COUNT, &asyncCallbackInfo->callback); napi_value resourceName; napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); @@ -1682,7 +1691,7 @@ napi_value NAPI_EnableFormsUpdate(napi_env env, napi_callback_info info) napi_get_undefined(env, &undefined); napi_get_reference_value(env, asyncCallbackInfo->callback, &callback); napi_value callResult; - napi_call_function(env, undefined, callback, 1, &result, &callResult); + napi_call_function(env, undefined, callback, ARGS_SIZE_ONE, &result, &callResult); napi_delete_reference(env, asyncCallbackInfo->callback); } napi_delete_async_work(env, asyncCallbackInfo->asyncWork); @@ -1691,10 +1700,7 @@ napi_value NAPI_EnableFormsUpdate(napi_env env, napi_callback_info info) (void *)asyncCallbackInfo, &asyncCallbackInfo->asyncWork); NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); - - napi_value result; - NAPI_CALL(env, napi_create_int32(env, 1, &result)); - return result; + return NapiGetResut(env, 1); } else { HILOG_INFO("%{public}s, promise.", __func__); napi_deferred deferred; @@ -1739,7 +1745,7 @@ napi_value NAPI_EnableFormsUpdate(napi_env env, napi_callback_info info) * * @param[in] env The environment that the Node-API call is invoked under * @param[out] asyncCallbackInfo Reference, callback info via Node-API - * + * * @return void */ static void InnerDisableFormsUpdate(napi_env env, AsyncDisableUpdateFormCallbackInfo* const asyncCallbackInfo) @@ -1756,11 +1762,11 @@ static void InnerDisableFormsUpdate(napi_env env, AsyncDisableUpdateFormCallback } /** - * @brief The implementation of Node-API interface: disableFormsUpdate + * @brief The implementation of Node-API interface: disableFormsUpdate * * @param[in] env The environment that the Node-API call is invoked under * @param[out] info An opaque datatype that is passed to a callback function - * + * * @return This is an opaque pointer that is used to represent a JavaScript value */ napi_value NAPI_DisableFormsUpdate(napi_env env, napi_callback_info info) @@ -1796,28 +1802,18 @@ napi_value NAPI_DisableFormsUpdate(napi_env env, napi_callback_info info) is incorrect, expected type is string."); std::string strFormId = GetStringFromNAPI(env, napiFormId); - int64_t formIdValue = std::stoll(strFormId); - + int64_t formIdValue; + bool isConversionSucceeded = ConvertStringToInt64(strFormId, formIdValue); + NAPI_ASSERT(env, isConversionSucceeded, "The arguments[0] type of disableFormsUpdate is incorrect,\ + expected type is string and the content must be numeric,\ + value range is: 0x8000000000000000~0x7FFFFFFFFFFFFFFF."); formIds.push_back(formIdValue); } - // get global value - napi_value global = nullptr; - napi_get_global(env, &global); - - // get ability - napi_value abilityObj = nullptr; - napi_get_named_property(env, global, "ability", &abilityObj); - - // get ability pointer - OHOS::AppExecFwk::Ability* ability = nullptr; - napi_get_value_external(env, abilityObj, (void**)&ability); - HILOG_INFO("%{public}s, ability = [%{public}p]", __func__, ability); - - AsyncDisableUpdateFormCallbackInfo *asyncCallbackInfo = new + AsyncDisableUpdateFormCallbackInfo *asyncCallbackInfo = new AsyncDisableUpdateFormCallbackInfo { .env = env, - .ability = ability, + .ability = GetGlobalAbility(env), .asyncWork = nullptr, .deferred = nullptr, .callback = nullptr, @@ -1834,7 +1830,7 @@ napi_value NAPI_DisableFormsUpdate(napi_env env, napi_callback_info info) NAPI_ASSERT(env, valueType == napi_function, "The arguments[1] type of disableFormsUpdate \ is incorrect, expected type is function."); - napi_create_reference(env, argv[1], 1, &asyncCallbackInfo->callback); + napi_create_reference(env, argv[1], REF_COUNT, &asyncCallbackInfo->callback); napi_value resourceName; napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); @@ -1864,7 +1860,7 @@ napi_value NAPI_DisableFormsUpdate(napi_env env, napi_callback_info info) napi_get_undefined(env, &undefined); napi_get_reference_value(env, asyncCallbackInfo->callback, &callback); napi_value callResult; - napi_call_function(env, undefined, callback, 1, &result, &callResult); + napi_call_function(env, undefined, callback, ARGS_SIZE_ONE, &result, &callResult); napi_delete_reference(env, asyncCallbackInfo->callback); } napi_delete_async_work(env, asyncCallbackInfo->asyncWork); @@ -1873,10 +1869,7 @@ napi_value NAPI_DisableFormsUpdate(napi_env env, napi_callback_info info) (void *)asyncCallbackInfo, &asyncCallbackInfo->asyncWork); NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); - - napi_value result; - NAPI_CALL(env, napi_create_int32(env, 1, &result)); - return result; + return NapiGetResut(env, 1); } else { HILOG_INFO("%{public}s, promise.", __func__); napi_deferred deferred; @@ -1922,7 +1915,7 @@ napi_value NAPI_DisableFormsUpdate(napi_env env, napi_callback_info info) * * @param[in] env The environment that the Node-API call is invoked under * @param[out] asyncCallbackInfo Reference, callback info via Node-API - * + * * @return void */ static void InnerCheckFMSReady(napi_env env, AsyncCheckFMSReadyCallbackInfo* const asyncCallbackInfo) @@ -1938,7 +1931,7 @@ static void InnerCheckFMSReady(napi_env env, AsyncCheckFMSReadyCallbackInfo* con * * @param[in] env The environment that the Node-API call is invoked under * @param[out] info An opaque datatype that is passed to a callback function - * + * * @return This is an opaque pointer that is used to represent a JavaScript value */ napi_value NAPI_CheckFMSReady(napi_env env, napi_callback_info info) @@ -1955,23 +1948,10 @@ napi_value NAPI_CheckFMSReady(napi_env env, napi_callback_info info) } HILOG_INFO("%{public}s, argc = [%{public}zu]", __func__, argc); - // get global value - napi_value global = nullptr; - napi_get_global(env, &global); - - // get ability - napi_value abilityObj = nullptr; - napi_get_named_property(env, global, "ability", &abilityObj); - - // get ability pointer - OHOS::AppExecFwk::Ability* ability = nullptr; - napi_get_value_external(env, abilityObj, (void**)&ability); - HILOG_INFO("%{public}s, ability = [%{public}p]", __func__, ability); - AsyncCheckFMSReadyCallbackInfo *asyncCallbackInfo = new AsyncCheckFMSReadyCallbackInfo { .env = env, - .ability = ability, + .ability = GetGlobalAbility(env), .asyncWork = nullptr, .deferred = nullptr, .callback = nullptr, @@ -1987,7 +1967,7 @@ napi_value NAPI_CheckFMSReady(napi_env env, napi_callback_info info) NAPI_ASSERT(env, valueType == napi_function, "The arguments[0] type of checkFMSReady is incorrect,\ expected type is function."); - napi_create_reference(env, argv[0], 1, &asyncCallbackInfo->callback); + napi_create_reference(env, argv[0], REF_COUNT, &asyncCallbackInfo->callback); napi_value resourceName; napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); napi_create_async_work( @@ -2014,7 +1994,7 @@ napi_value NAPI_CheckFMSReady(napi_env env, napi_callback_info info) napi_value callback; napi_get_reference_value(env, asyncCallbackInfo->callback, &callback); napi_value callResult; - napi_call_function(env, nullptr, callback, 1, &isFMSReadyResult, &callResult); + napi_call_function(env, nullptr, callback, ARGS_SIZE_ONE, &isFMSReadyResult, &callResult); napi_delete_reference(env, asyncCallbackInfo->callback); } napi_delete_async_work(env, asyncCallbackInfo->asyncWork); @@ -2023,10 +2003,7 @@ napi_value NAPI_CheckFMSReady(napi_env env, napi_callback_info info) (void *)asyncCallbackInfo, &asyncCallbackInfo->asyncWork); NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); - - napi_value result; - NAPI_CALL(env, napi_create_int32(env, 1, &result)); - return result; + return NapiGetResut(env, 1); } else { HILOG_INFO("%{public}s, promise.", __func__); napi_deferred deferred; @@ -2072,10 +2049,10 @@ napi_value NAPI_CheckFMSReady(napi_env env, napi_callback_info info) * * @param[in] env The environment that the Node-API call is invoked under * @param[out] asyncCallbackInfo Reference, callback info via Node-API - * + * * @return void */ -static void InnerGetAllFormsInfo(napi_env env, AsyncGetAllFormsCallbackInfo* const asyncCallbackInfo) +static void InnerGetAllFormsInfo(napi_env env, AsyncGetFormsInfoCallbackInfo* const asyncCallbackInfo) { HILOG_DEBUG("%{public}s called.", __func__); OHOS::AppExecFwk::Ability *ability = asyncCallbackInfo->ability; @@ -2088,12 +2065,86 @@ static void InnerGetAllFormsInfo(napi_env env, AsyncGetAllFormsCallbackInfo* con HILOG_DEBUG("%{public}s, end", __func__); } +// NAPI_GetAllFormsInfo callback execute +auto NAPI_GetAllFormsInfoAsyncExecute = [](napi_env env, void *data) { + HILOG_INFO("NAPI_GetAllFormsInfo execute callback"); + AsyncGetFormsInfoCallbackInfo *asyncCallbackInfo = + (AsyncGetFormsInfoCallbackInfo *)data; + InnerGetAllFormsInfo(env, asyncCallbackInfo); +}; + +// NAPI_GetFormsInfo callback complete +auto NAPI_GetFormsInfoAsyncComplete = [](napi_env env, napi_status status, void *data) { + HILOG_INFO("NAPI_GetFormsInfo compeleted callback"); + AsyncGetFormsInfoCallbackInfo *asyncCallbackInfo = + (AsyncGetFormsInfoCallbackInfo *)data; + napi_value arrayFormInfos; + napi_create_array(env, &arrayFormInfos); + if (asyncCallbackInfo->result) { + int iFormInfoCount = 0; + for (auto formInfo : asyncCallbackInfo->formInfos) { + napi_value formInfoObject = nullptr; + napi_create_object(env, &formInfoObject); + ParseFormInfoIntoNapi(env, formInfo, formInfoObject); + napi_set_element(env, arrayFormInfos, iFormInfoCount, formInfoObject); + ++iFormInfoCount; + } + } + if (asyncCallbackInfo->callback != nullptr) { + napi_value callbackValues[2] = {0}; + napi_value callback; + napi_value resultCode; + napi_create_int32(env, asyncCallbackInfo->result, &resultCode); + callbackValues[0] = resultCode; + callbackValues[1] = arrayFormInfos; + napi_get_reference_value(env, asyncCallbackInfo->callback, &callback); + napi_value callResult; + napi_call_function(env, nullptr, callback, ARGS_SIZE_TWO, callbackValues, &callResult); + napi_delete_reference(env, asyncCallbackInfo->callback); + } + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + delete asyncCallbackInfo; +}; + +// NAPI_GetFormsInfo promise Complete +auto NAPI_GetFormsInfoPromiseComplete = [](napi_env env, napi_status status, void *data) { + HILOG_INFO("%{public}s, promise complete", __func__); + AsyncGetFormsInfoCallbackInfo *asyncCallbackInfo = + (AsyncGetFormsInfoCallbackInfo *)data; + if (asyncCallbackInfo->result) { + napi_value arrayFormInfos; + napi_create_array(env, &arrayFormInfos); + int iFormInfoCount = 0; + for (auto formInfo : asyncCallbackInfo->formInfos) { + napi_value formInfoObject = nullptr; + napi_create_object(env, &formInfoObject); + ParseFormInfoIntoNapi(env, formInfo, formInfoObject); + napi_set_element(env, arrayFormInfos, iFormInfoCount, formInfoObject); + ++iFormInfoCount; + } + napi_resolve_deferred( + asyncCallbackInfo->env, + asyncCallbackInfo->deferred, + arrayFormInfos); + } else { + napi_value getFormsInfoResult; + napi_create_int32(env, asyncCallbackInfo->result, &getFormsInfoResult); + napi_resolve_deferred( + asyncCallbackInfo->env, + asyncCallbackInfo->deferred, + getFormsInfoResult); + } + + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + delete asyncCallbackInfo; +}; + /** * @brief The implementation of Node-API interface: getAllFormsInfo * * @param[in] env The environment that the Node-API call is invoked under * @param[out] info An opaque datatype that is passed to a callback function - * + * * @return This is an opaque pointer that is used to represent a JavaScript value */ napi_value NAPI_GetAllFormsInfo(napi_env env, napi_callback_info info) @@ -2110,30 +2161,16 @@ napi_value NAPI_GetAllFormsInfo(napi_env env, napi_callback_info info) } HILOG_INFO("%{public}s, argc = [%{public}zu]", __func__, argc); - std::vector formInfos; - formInfos.clear(); - - // get global value - napi_value global = nullptr; - napi_get_global(env, &global); - - // get ability - napi_value abilityObj = nullptr; - napi_get_named_property(env, global, "ability", &abilityObj); - - // get ability pointer - OHOS::AppExecFwk::Ability* ability = nullptr; - napi_get_value_external(env, abilityObj, (void**)&ability); - HILOG_INFO("%{public}s, ability = [%{public}p]", __func__, ability); - - AsyncGetAllFormsCallbackInfo *asyncCallbackInfo = new - AsyncGetAllFormsCallbackInfo { + AsyncGetFormsInfoCallbackInfo *asyncCallbackInfo = new + AsyncGetFormsInfoCallbackInfo { .env = env, - .ability = ability, + .ability = GetGlobalAbility(env), .asyncWork = nullptr, .deferred = nullptr, .callback = nullptr, - .formInfos = formInfos, + .formInfos = std::vector(), + .bundleName = "", + .moduleName = "", .result = 0, }; @@ -2146,61 +2183,19 @@ napi_value NAPI_GetAllFormsInfo(napi_env env, napi_callback_info info) NAPI_ASSERT(env, valueType == napi_function, "The arguments[0] type of getAllFormsInfo is incorrect,\ expected type is function."); - napi_create_reference(env, argv[0], 1, &asyncCallbackInfo->callback); + napi_create_reference(env, argv[0], REF_COUNT, &asyncCallbackInfo->callback); napi_value resourceName; napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); napi_create_async_work( env, nullptr, resourceName, - [](napi_env env, void *data) { - HILOG_INFO("%{public}s, napi_create_async_work running", __func__); - - AsyncGetAllFormsCallbackInfo *asyncCallbackInfo = - (AsyncGetAllFormsCallbackInfo *)data; - - InnerGetAllFormsInfo(env, asyncCallbackInfo); - }, - [](napi_env env, napi_status status, void *data) { - HILOG_INFO("%{public}s, napi_create_async_work complete", __func__); - - AsyncGetAllFormsCallbackInfo *asyncCallbackInfo = - (AsyncGetAllFormsCallbackInfo *)data; - - napi_value arrayFormInfos; - napi_create_array(env, &arrayFormInfos); - if (asyncCallbackInfo->result) { - int iFormInfoCount = 0; - for (auto formInfo : asyncCallbackInfo->formInfos) { - napi_value formInfoObject = nullptr; - napi_create_object(env, &formInfoObject); - ParseFormInfoIntoNapi(env, formInfo, formInfoObject); - napi_set_element(env, arrayFormInfos, iFormInfoCount, formInfoObject); - ++iFormInfoCount; - } - } - - if (asyncCallbackInfo->callback != nullptr) { - napi_value callbackValues[2] = {0}; - napi_value callback; - napi_value resultCode; - napi_create_int32(env, asyncCallbackInfo->result, &resultCode); - callbackValues[0] = resultCode; - callbackValues[1] = arrayFormInfos; - napi_get_reference_value(env, asyncCallbackInfo->callback, &callback); - napi_value callResult; - napi_call_function(env, nullptr, callback, 2, callbackValues, &callResult); - napi_delete_reference(env, asyncCallbackInfo->callback); - } - napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - delete asyncCallbackInfo; - }, + NAPI_GetAllFormsInfoAsyncExecute, + NAPI_GetFormsInfoAsyncComplete, (void *)asyncCallbackInfo, &asyncCallbackInfo->asyncWork); NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); - napi_value result; - NAPI_CALL(env, napi_create_int32(env, 1, &result)); - return result; + return NapiGetResut(env, 1); } else { HILOG_INFO("%{public}s, promise.", __func__); napi_deferred deferred; @@ -2214,36 +2209,8 @@ napi_value NAPI_GetAllFormsInfo(napi_env env, napi_callback_info info) env, nullptr, resourceName, - [](napi_env env, void *data) { - HILOG_INFO("%{public}s, promise runnning", __func__); - - AsyncGetAllFormsCallbackInfo *asyncCallbackInfo = - (AsyncGetAllFormsCallbackInfo *)data; - - InnerGetAllFormsInfo(env, asyncCallbackInfo); - }, - [](napi_env env, napi_status status, void *data) { - HILOG_INFO("%{public}s, promise complete", __func__); - - AsyncGetAllFormsCallbackInfo *asyncCallbackInfo = - (AsyncGetAllFormsCallbackInfo *)data; - - napi_value arrayFormInfos; - napi_create_array(env, &arrayFormInfos); - if (asyncCallbackInfo->result) { - int iFormInfoCount = 0; - for (auto formInfo : asyncCallbackInfo->formInfos) { - napi_value formInfoObject = nullptr; - napi_create_object(env, &formInfoObject); - ParseFormInfoIntoNapi(env, formInfo, formInfoObject); - napi_set_element(env, arrayFormInfos, iFormInfoCount, formInfoObject); - ++iFormInfoCount; - } - } - napi_resolve_deferred(asyncCallbackInfo->env, asyncCallbackInfo->deferred, arrayFormInfos); - napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - delete asyncCallbackInfo; - }, + NAPI_GetAllFormsInfoAsyncExecute, + NAPI_GetFormsInfoPromiseComplete, (void *)asyncCallbackInfo, &asyncCallbackInfo->asyncWork); napi_queue_async_work(env, asyncCallbackInfo->asyncWork); @@ -2256,10 +2223,10 @@ napi_value NAPI_GetAllFormsInfo(napi_env env, napi_callback_info info) * * @param[in] env The environment that the Node-API call is invoked under * @param[out] asyncCallbackInfo Reference, callback info via Node-API - * + * * @return void */ -static void InnerGetFormsInfoByApp(napi_env env, AsyncGetFormsInfoByAppCallbackInfo* const asyncCallbackInfo) +static void InnerGetFormsInfoByApp(napi_env env, AsyncGetFormsInfoCallbackInfo* const asyncCallbackInfo) { HILOG_DEBUG("%{public}s called.", __func__); OHOS::AppExecFwk::Ability *ability = asyncCallbackInfo->ability; @@ -2274,188 +2241,15 @@ static void InnerGetFormsInfoByApp(napi_env env, AsyncGetFormsInfoByAppCallbackI HILOG_DEBUG("%{public}s, end", __func__); } -/** - * @brief The implementation of Node-API interface: getFormsInfoByApp - * - * @param[in] env The environment that the Node-API call is invoked under - * @param[out] info An opaque datatype that is passed to a callback function - * - * @return This is an opaque pointer that is used to represent a JavaScript value - */ -napi_value NAPI_GetFormsInfoByApp(napi_env env, napi_callback_info info) -{ - HILOG_INFO("%{public}s called.", __func__); - - // Check the number of the arguments - size_t argc = ARGS_SIZE_TWO; - napi_value argv[ARGS_SIZE_TWO] = {nullptr}; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); - if (argc > ARGS_SIZE_TWO) { - HILOG_ERROR("%{public}s, wrong number of arguments.", __func__); - return nullptr; - } - HILOG_INFO("%{public}s, argc = [%{public}zu]", __func__, argc); - - // Check the value type of the arguments - napi_valuetype valueType; - NAPI_CALL(env, napi_typeof(env, argv[0], &valueType)); - NAPI_ASSERT(env, valueType == napi_string, "The arguments[0] type of getFormsInfoByApp is incorrect,\ - expected type is string."); - - std::string bundleNameInfo = GetStringFromNAPI(env, argv[0]); - HILOG_INFO("%{public}s, bundleName=%{public}s.", __func__, bundleNameInfo.c_str()); - - std::vector formInfos; - formInfos.clear(); - - // get global value - napi_value global = nullptr; - napi_get_global(env, &global); - - // get ability - napi_value abilityObj = nullptr; - napi_get_named_property(env, global, "ability", &abilityObj); - - // get ability pointer - OHOS::AppExecFwk::Ability* ability = nullptr; - napi_get_value_external(env, abilityObj, (void**)&ability); - HILOG_INFO("%{public}s, ability = [%{public}p]", __func__, ability); - - AsyncGetFormsInfoByAppCallbackInfo *asyncCallbackInfo = new - AsyncGetFormsInfoByAppCallbackInfo { - .env = env, - .ability = ability, - .asyncWork = nullptr, - .deferred = nullptr, - .callback = nullptr, - .formInfos = formInfos, - .bundleName = bundleNameInfo, - .result = 0, - }; - - if (argc == ARGS_SIZE_TWO) { - HILOG_INFO("%{public}s, asyncCallback.", __func__); - - // Check the value type of the arguments - valueType = napi_undefined; - NAPI_CALL(env, napi_typeof(env, argv[1], &valueType)); - NAPI_ASSERT(env, valueType == napi_function, "The arguments[1] type of getFormsInfoByApp is incorrect,\ - expected type is function."); - - napi_create_reference(env, argv[1], 1, &asyncCallbackInfo->callback); - napi_value resourceName; - napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); - napi_create_async_work( - env, - nullptr, - resourceName, - [](napi_env env, void *data) { - HILOG_INFO("%{public}s, napi_create_async_work running", __func__); - - AsyncGetFormsInfoByAppCallbackInfo *asyncCallbackInfo = - (AsyncGetFormsInfoByAppCallbackInfo *)data; - - InnerGetFormsInfoByApp(env, asyncCallbackInfo); - }, - [](napi_env env, napi_status status, void *data) { - HILOG_INFO("%{public}s, napi_create_async_work complete", __func__); - - AsyncGetFormsInfoByAppCallbackInfo *asyncCallbackInfo = - (AsyncGetFormsInfoByAppCallbackInfo *)data; - - napi_value arrayFormInfos; - napi_create_array(env, &arrayFormInfos); - if (asyncCallbackInfo->result) { - int iFormInfoCount = 0; - for (auto formInfo : asyncCallbackInfo->formInfos) { - napi_value formInfoObject = nullptr; - napi_create_object(env, &formInfoObject); - ParseFormInfoIntoNapi(env, formInfo, formInfoObject); - napi_set_element(env, arrayFormInfos, iFormInfoCount, formInfoObject); - ++iFormInfoCount; - } - } - if (asyncCallbackInfo->callback != nullptr) { - napi_value callbackValues[2] = {0}; - napi_value callback; - napi_value resultCode; - napi_create_int32(env, asyncCallbackInfo->result, &resultCode); - callbackValues[0] = resultCode; - callbackValues[1] = arrayFormInfos; - napi_get_reference_value(env, asyncCallbackInfo->callback, &callback); - napi_value callResult; - napi_call_function(env, nullptr, callback, 2, callbackValues, &callResult); - napi_delete_reference(env, asyncCallbackInfo->callback); - } - napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - delete asyncCallbackInfo; - }, - (void *)asyncCallbackInfo, - &asyncCallbackInfo->asyncWork); - NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); - - napi_value result; - NAPI_CALL(env, napi_create_int32(env, 1, &result)); - return result; - } else { - HILOG_INFO("%{public}s, promise.", __func__); - napi_deferred deferred; - napi_value promise; - NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); - asyncCallbackInfo->deferred = deferred; - - napi_value resourceName; - napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); - napi_create_async_work( - env, - nullptr, - resourceName, - [](napi_env env, void *data) { - HILOG_INFO("%{public}s, promise runnning", __func__); - - AsyncGetFormsInfoByAppCallbackInfo *asyncCallbackInfo = - (AsyncGetFormsInfoByAppCallbackInfo *)data; - - InnerGetFormsInfoByApp(env, asyncCallbackInfo); - }, - [](napi_env env, napi_status status, void *data) { - HILOG_INFO("%{public}s, promise complete", __func__); - - AsyncGetFormsInfoByAppCallbackInfo *asyncCallbackInfo = - (AsyncGetFormsInfoByAppCallbackInfo *)data; - - napi_value arrayFormInfos; - napi_create_array(env, &arrayFormInfos); - if (asyncCallbackInfo->result) { - int iFormInfoCount = 0; - for (auto formInfo : asyncCallbackInfo->formInfos) { - napi_value formInfoObject = nullptr; - napi_create_object(env, &formInfoObject); - ParseFormInfoIntoNapi(env, formInfo, formInfoObject); - napi_set_element(env, arrayFormInfos, iFormInfoCount, formInfoObject); - ++iFormInfoCount; - } - } - napi_resolve_deferred(asyncCallbackInfo->env, asyncCallbackInfo->deferred, arrayFormInfos); - napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - delete asyncCallbackInfo; - }, - (void *)asyncCallbackInfo, - &asyncCallbackInfo->asyncWork); - napi_queue_async_work(env, asyncCallbackInfo->asyncWork); - return promise; - } -} - /** * @brief Call native kit function: GetFormsInfoByModule * * @param[in] env The environment that the Node-API call is invoked under * @param[out] asyncCallbackInfo Reference, callback info via Node-API - * + * * @return void */ -static void InnerGetFormsInfoByModule(napi_env env, AsyncGetFormsInfoByModuleCallbackInfo* const asyncCallbackInfo) +static void InnerGetFormsInfoByModule(napi_env env, AsyncGetFormsInfoCallbackInfo* const asyncCallbackInfo) { HILOG_DEBUG("%{public}s called.", __func__); OHOS::AppExecFwk::Ability *ability = asyncCallbackInfo->ability; @@ -2473,15 +2267,128 @@ static void InnerGetFormsInfoByModule(napi_env env, AsyncGetFormsInfoByModuleCal HILOG_DEBUG("%{public}s, end", __func__); } +// NAPI_GetFormsInfo byModule callback execute +auto NAPI_GetFormsInfoByModuleAsyncExecute = [](napi_env env, void *data) { + HILOG_INFO("NAPI_GetFormsInfo byModule execute callback"); + AsyncGetFormsInfoCallbackInfo *asyncCallbackInfo = + (AsyncGetFormsInfoCallbackInfo *)data; + InnerGetFormsInfoByModule(env, asyncCallbackInfo); +}; + +// NAPI_GetFormsInfo byApp callback execute +auto NAPI_GetFormsInfoByAppAsyncExecute = [](napi_env env, void *data) { + HILOG_INFO("NAPI_GetFormsInfo byApp execute callback"); + AsyncGetFormsInfoCallbackInfo *asyncCallbackInfo = + (AsyncGetFormsInfoCallbackInfo *)data; + InnerGetFormsInfoByApp(env, asyncCallbackInfo); +}; + +// GetFormsInfo callback +napi_value GetFormsInfoCallback(napi_env env, AsyncGetFormsInfoCallbackInfo *asyncCallbackInfo, bool isByApp) +{ + HILOG_INFO("%{public}s, callback.", __func__); + napi_value resourceName; + napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); + napi_create_async_work( + env, + nullptr, + resourceName, + isByApp ? NAPI_GetFormsInfoByAppAsyncExecute : + NAPI_GetFormsInfoByModuleAsyncExecute, + NAPI_GetFormsInfoAsyncComplete, + (void *)asyncCallbackInfo, + &asyncCallbackInfo->asyncWork); + NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); + return NapiGetResut(env, 1); +} + +// GetFormsInfo promise +napi_value GetFormsInfoPromise(napi_env env, AsyncGetFormsInfoCallbackInfo *asyncCallbackInfo, bool isByApp) +{ + HILOG_INFO("%{public}s, promise.", __func__); + napi_deferred deferred; + napi_value promise; + NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); + asyncCallbackInfo->deferred = deferred; + napi_value resourceName; + napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); + napi_create_async_work( + env, + nullptr, + resourceName, + isByApp ? NAPI_GetFormsInfoByAppAsyncExecute : + NAPI_GetFormsInfoByModuleAsyncExecute, + NAPI_GetFormsInfoPromiseComplete, + (void *)asyncCallbackInfo, + &asyncCallbackInfo->asyncWork); + napi_queue_async_work(env, asyncCallbackInfo->asyncWork); + return promise; +} + +// GetFormsInfo THREE ARGV +napi_value GetFormsInfoThreeArgv(napi_env env, napi_value *argv, AsyncGetFormsInfoCallbackInfo* const asyncCallbackInfo) +{ + HILOG_INFO("%{public}s.", __func__); + + // Check the value type of the arguments + napi_valuetype valueType; + NAPI_CALL(env, napi_typeof(env, argv[ARGS_SIZE_ONE], &valueType)); + NAPI_ASSERT(env, valueType == napi_string, "The arguments[1] type of getFormsInfo is incorrect,\ + expected type is string."); + std::string moduleNameInfo = GetStringFromNAPI(env, argv[ARGS_SIZE_ONE]); + HILOG_INFO("%{public}s, moduleNameInfo=%{public}s.", __func__, moduleNameInfo.c_str()); + asyncCallbackInfo->moduleName = moduleNameInfo; + + // Check the value type of the arguments + valueType = napi_undefined; + NAPI_CALL(env, napi_typeof(env, argv[ARGS_SIZE_TWO], &valueType)); + NAPI_ASSERT(env, valueType == napi_function, "The arguments[2] type of getFormsInfo is incorrect,\ + expected type is function."); + napi_create_reference(env, argv[ARGS_SIZE_TWO], REF_COUNT, &asyncCallbackInfo->callback); + return GetFormsInfoCallback(env, asyncCallbackInfo, false); +} + +// GetFormsInfo TWO ARGV +napi_value GetFormsInfoTwoArgv(napi_env env, napi_value *argv, AsyncGetFormsInfoCallbackInfo* const asyncCallbackInfo) +{ + HILOG_INFO("%{public}s.", __func__); + + // Check the value type of the arguments + napi_valuetype valueType; + NAPI_CALL(env, napi_typeof(env, argv[ARGS_SIZE_ONE], &valueType)); + + // GetFormsInfoByModule promise + if (valueType == napi_string) { + std::string moduleNameInfo = GetStringFromNAPI(env, argv[ARGS_SIZE_ONE]); + HILOG_INFO("%{public}s, moduleNameInfo=%{public}s.", __func__, moduleNameInfo.c_str()); + asyncCallbackInfo->moduleName = moduleNameInfo; + return GetFormsInfoPromise(env, asyncCallbackInfo, false); + } else if (valueType == napi_function) { // GetFormsInfoByApp callback + HILOG_INFO("%{public}s, asyncCallback.", __func__); + + // Check the value type of the arguments + valueType = napi_undefined; + NAPI_CALL(env, napi_typeof(env, argv[ARGS_SIZE_ONE], &valueType)); + NAPI_ASSERT(env, valueType == napi_function, "The arguments[1] type of getFormsInfo is incorrect,\ + expected type is function."); + napi_create_reference(env, argv[ARGS_SIZE_ONE], REF_COUNT, &asyncCallbackInfo->callback); + return GetFormsInfoCallback(env, asyncCallbackInfo, true); + } else { + NAPI_ASSERT(env, false, "The arguments[1] type of getFormsInfo is incorrect,\ + expected type is string or function."); + return NapiGetResut(env, 1); + } +} + /** - * @brief The implementation of Node-API interface: getFormsInfoByModule + * @brief The implementation of Node-API interface: getFormsInfo * * @param[in] env The environment that the Node-API call is invoked under * @param[out] info An opaque datatype that is passed to a callback function - * + * * @return This is an opaque pointer that is used to represent a JavaScript value */ -napi_value NAPI_GetFormsInfoByModule(napi_env env, napi_callback_info info) +napi_value NAPI_GetFormsInfo(napi_env env, napi_callback_info info) { HILOG_INFO("%{public}s called.", __func__); @@ -2497,160 +2404,34 @@ napi_value NAPI_GetFormsInfoByModule(napi_env env, napi_callback_info info) // Check the value type of the arguments napi_valuetype valueType; - NAPI_CALL(env, napi_typeof(env, argv[0], &valueType)); - NAPI_ASSERT(env, valueType == napi_string, "The arguments[0] type of getFormsInfoByModule is incorrect,\ - expected type is string."); - - std::string bundleNameInfo = GetStringFromNAPI(env, argv[0]); - HILOG_INFO("%{public}s, bundleName=%{public}s.", __func__, bundleNameInfo.c_str()); - - valueType = napi_undefined; - NAPI_CALL(env, napi_typeof(env, argv[1], &valueType)); - NAPI_ASSERT(env, valueType == napi_string, "The arguments[1] type of getFormsInfoByModule is incorrect,\ + NAPI_CALL(env, napi_typeof(env, argv[ARGS_SIZE_ZERO], &valueType)); + NAPI_ASSERT(env, valueType == napi_string, "The arguments[0] type of getFormsInfo is incorrect,\ expected type is string."); - std::string moduleNameInfo = GetStringFromNAPI(env, argv[1]); - HILOG_INFO("%{public}s, moduleName=%{public}s.", __func__, moduleNameInfo.c_str()); - - std::vector formInfos; - formInfos.clear(); - - // get global value - napi_value global = nullptr; - napi_get_global(env, &global); - - // get ability - napi_value abilityObj = nullptr; - napi_get_named_property(env, global, "ability", &abilityObj); - - // get ability pointer - OHOS::AppExecFwk::Ability* ability = nullptr; - napi_get_value_external(env, abilityObj, (void**)&ability); - HILOG_INFO("%{public}s, ability = [%{public}p]", __func__, ability); + std::string bundleNameInfo = GetStringFromNAPI(env, argv[ARGS_SIZE_ZERO]); + HILOG_INFO("%{public}s, bundleName=%{public}s.", __func__, bundleNameInfo.c_str()); - AsyncGetFormsInfoByModuleCallbackInfo *asyncCallbackInfo = new - AsyncGetFormsInfoByModuleCallbackInfo { + AsyncGetFormsInfoCallbackInfo *asyncCallbackInfo = new + AsyncGetFormsInfoCallbackInfo { .env = env, - .ability = ability, + .ability = GetGlobalAbility(env), .asyncWork = nullptr, .deferred = nullptr, .callback = nullptr, - .formInfos = formInfos, + .formInfos = std::vector(), .bundleName = bundleNameInfo, - .moduleName = moduleNameInfo, + .moduleName = "", .result = 0, }; - if (argc == ARGS_SIZE_THREE) { - HILOG_INFO("%{public}s, asyncCallback.", __func__); - - // Check the value type of the arguments - valueType = napi_undefined; - NAPI_CALL(env, napi_typeof(env, argv[2], &valueType)); - NAPI_ASSERT(env, valueType == napi_function, "The arguments[2] type of getFormsInfoByModule \ - is incorrect, expected type is function."); - - napi_create_reference(env, argv[2], 1, &asyncCallbackInfo->callback); - napi_value resourceName; - napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); - napi_create_async_work( - env, - nullptr, - resourceName, - [](napi_env env, void *data) { - HILOG_INFO("%{public}s, napi_create_async_work running", __func__); - - AsyncGetFormsInfoByModuleCallbackInfo *asyncCallbackInfo = - (AsyncGetFormsInfoByModuleCallbackInfo *)data; - - InnerGetFormsInfoByModule(env, asyncCallbackInfo); - }, - [](napi_env env, napi_status status, void *data) { - HILOG_INFO("%{public}s, napi_create_async_work complete", __func__); - - AsyncGetFormsInfoByModuleCallbackInfo *asyncCallbackInfo = - (AsyncGetFormsInfoByModuleCallbackInfo *)data; - - napi_value arrayFormInfos; - napi_create_array(env, &arrayFormInfos); - if (asyncCallbackInfo->result) { - int iFormInfoCount = 0; - for (auto formInfo : asyncCallbackInfo->formInfos) { - napi_value formInfoObject = nullptr; - napi_create_object(env, &formInfoObject); - ParseFormInfoIntoNapi(env, formInfo, formInfoObject); - napi_set_element(env, arrayFormInfos, iFormInfoCount, formInfoObject); - ++iFormInfoCount; - } - } - if (asyncCallbackInfo->callback != nullptr) { - napi_value callbackValues[2] = {0}; - napi_value callback; - napi_value resultCode; - napi_create_int32(env, asyncCallbackInfo->result, &resultCode); - callbackValues[0] = resultCode; - callbackValues[1] = arrayFormInfos; - napi_get_reference_value(env, asyncCallbackInfo->callback, &callback); - napi_value callResult; - napi_call_function(env, nullptr, callback, 2, callbackValues, &callResult); - napi_delete_reference(env, asyncCallbackInfo->callback); - } - napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - delete asyncCallbackInfo; - }, - (void *)asyncCallbackInfo, - &asyncCallbackInfo->asyncWork); - NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); - - napi_value result; - NAPI_CALL(env, napi_create_int32(env, 1, &result)); - return result; - } else { - HILOG_INFO("%{public}s, promise.", __func__); - napi_deferred deferred; - napi_value promise; - NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); - asyncCallbackInfo->deferred = deferred; - - napi_value resourceName; - napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); - napi_create_async_work( - env, - nullptr, - resourceName, - [](napi_env env, void *data) { - HILOG_INFO("%{public}s, promise runnning", __func__); - - AsyncGetFormsInfoByModuleCallbackInfo *asyncCallbackInfo = - (AsyncGetFormsInfoByModuleCallbackInfo *)data; - - InnerGetFormsInfoByModule(env, asyncCallbackInfo); - }, - [](napi_env env, napi_status status, void *data) { - HILOG_INFO("%{public}s, promise complete", __func__); - - AsyncGetFormsInfoByModuleCallbackInfo *asyncCallbackInfo = - (AsyncGetFormsInfoByModuleCallbackInfo *)data; - - napi_value arrayFormInfos; - napi_create_array(env, &arrayFormInfos); - if (asyncCallbackInfo->result) { - int iFormInfoCount = 0; - for (auto formInfo : asyncCallbackInfo->formInfos) { - napi_value formInfoObject = nullptr; - napi_create_object(env, &formInfoObject); - ParseFormInfoIntoNapi(env, formInfo, formInfoObject); - napi_set_element(env, arrayFormInfos, iFormInfoCount, formInfoObject); - ++iFormInfoCount; - } - } - napi_resolve_deferred(asyncCallbackInfo->env, asyncCallbackInfo->deferred, arrayFormInfos); - napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - delete asyncCallbackInfo; - }, - (void *)asyncCallbackInfo, - &asyncCallbackInfo->asyncWork); - napi_queue_async_work(env, asyncCallbackInfo->asyncWork); - return promise; + if (argc == ARGS_SIZE_THREE) { // GetFormsInfoByModule callback + HILOG_INFO("%{public}s, ARGS_SIZE_THREE.", __func__); + return GetFormsInfoThreeArgv(env, argv, asyncCallbackInfo); + } else if (argc == ARGS_SIZE_TWO) { + HILOG_INFO("%{public}s, ARGS_SIZE_TWO.", __func__); + return GetFormsInfoTwoArgv(env, argv, asyncCallbackInfo); + } else if (argc == ARGS_SIZE_ONE) { // GetFormsInfoByApp promise + return GetFormsInfoPromise(env, asyncCallbackInfo, false); } + return NapiGetResut(env, 1); } diff --git a/interfaces/kits/napi/aafwk/formManager/napi_form_manager.h b/interfaces/kits/napi/aafwk/formManager/napi_form_manager.h index ff8333f697a860aadecf59e77c19a16acef5b2cc..e89c2aadbec568b44257cf8292526d6e36857a7a 100755 --- a/interfaces/kits/napi/aafwk/formManager/napi_form_manager.h +++ b/interfaces/kits/napi/aafwk/formManager/napi_form_manager.h @@ -138,28 +138,7 @@ struct AsyncCheckFMSReadyCallbackInfo { bool isFMSReady; }; -struct AsyncGetAllFormsCallbackInfo { - napi_env env; - OHOS::AppExecFwk::Ability *ability; - napi_async_work asyncWork; - napi_deferred deferred; - napi_ref callback; - std::vector formInfos; - int result; -}; - -struct AsyncGetFormsInfoByAppCallbackInfo { - napi_env env; - OHOS::AppExecFwk::Ability *ability; - napi_async_work asyncWork; - napi_deferred deferred; - napi_ref callback; - std::vector formInfos; - std::string bundleName; - int result; -}; - -struct AsyncGetFormsInfoByModuleCallbackInfo { +struct AsyncGetFormsInfoCallbackInfo { napi_env env; OHOS::AppExecFwk::Ability *ability; napi_async_work asyncWork; @@ -183,7 +162,6 @@ napi_value NAPI_EnableFormsUpdate(napi_env env, napi_callback_info info); napi_value NAPI_DisableFormsUpdate(napi_env env, napi_callback_info info); napi_value NAPI_CheckFMSReady(napi_env env, napi_callback_info info); napi_value NAPI_GetAllFormsInfo(napi_env env, napi_callback_info info); -napi_value NAPI_GetFormsInfoByApp(napi_env env, napi_callback_info info); -napi_value NAPI_GetFormsInfoByModule(napi_env env, napi_callback_info info); +napi_value NAPI_GetFormsInfo(napi_env env, napi_callback_info info); #endif /* FORM_ABILITY_H_ */ diff --git a/interfaces/kits/napi/aafwk/formManager/native_module.cpp b/interfaces/kits/napi/aafwk/formManager/native_module.cpp index 664419ca19bdc103c826dd15e8c3dcdff03e5582..4cd78af39651d8a3c6b8c7316ad4c4cb3ee1c9be 100755 --- a/interfaces/kits/napi/aafwk/formManager/native_module.cpp +++ b/interfaces/kits/napi/aafwk/formManager/native_module.cpp @@ -44,10 +44,9 @@ static napi_value Init(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("notifyInvisibleForms", NAPI_NotifyInvisibleForms), DECLARE_NAPI_FUNCTION("enableFormsUpdate", NAPI_EnableFormsUpdate), DECLARE_NAPI_FUNCTION("disableFormsUpdate", NAPI_DisableFormsUpdate), - DECLARE_NAPI_FUNCTION("checkFMSReady", NAPI_CheckFMSReady), + DECLARE_NAPI_FUNCTION("isSystemReady", NAPI_CheckFMSReady), DECLARE_NAPI_FUNCTION("getAllFormsInfo", NAPI_GetAllFormsInfo), - DECLARE_NAPI_FUNCTION("getFormsInfoByApp", NAPI_GetFormsInfoByApp), - DECLARE_NAPI_FUNCTION("getFormsInfoByModule", NAPI_GetFormsInfoByModule), + DECLARE_NAPI_FUNCTION("getFormsInfo", NAPI_GetFormsInfo), }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(properties) / sizeof(properties[0]), properties)); HILOG_INFO("napi_moudule Init end..."); @@ -63,7 +62,7 @@ static napi_module _module = { .nm_flags = 0, .nm_filename = nullptr, .nm_register_func = Init, - .nm_modname = "ability.formmanager", + .nm_modname = "ability.formManager", .nm_priv = ((void *)0), .reserved = {0} };