1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef ABILITY_RUNTIME_JS_ABILITY_H 17 #define ABILITY_RUNTIME_JS_ABILITY_H 18 19 #include "ability.h" 20 #include "ability_delegator_infos.h" 21 22 class NativeReference; 23 class NativeValue; 24 25 namespace OHOS { 26 namespace AbilityRuntime { 27 class JsRuntime; 28 29 using Ability = AppExecFwk::Ability; 30 using AbilityHandler = AppExecFwk::AbilityHandler; 31 using AbilityInfo = AppExecFwk::AbilityInfo; 32 using OHOSApplication = AppExecFwk::OHOSApplication; 33 using Want = AppExecFwk::Want; 34 using AbilityStartSetting = AppExecFwk::AbilityStartSetting; 35 using Configuration = AppExecFwk::Configuration; 36 37 struct JsAbilityDeleterObject { 38 std::shared_ptr<NativeReference> shellContextRef_ = nullptr; 39 std::unique_ptr<NativeReference> jsAbilityObj_ = nullptr; 40 std::shared_ptr<AbilityRuntime::AbilityContext> abilityContext_ = nullptr; 41 }; 42 43 class JsAbility : public Ability { 44 public: 45 static Ability *Create(const std::unique_ptr<Runtime> &runtime); 46 47 JsAbility(JsRuntime &jsRuntime); 48 ~JsAbility() override; 49 50 void Init(const std::shared_ptr<AbilityInfo> &abilityInfo, const std::shared_ptr<OHOSApplication> &application, 51 std::shared_ptr<AbilityHandler> &handler, const sptr<IRemoteObject> &token) override; 52 53 void OnStart(const Want &want) override; 54 void OnStop() override; 55 #ifdef SUPPORT_GRAPHICS 56 void OnSceneCreated() override; 57 void onSceneDestroyed() override; 58 void OnSceneRestored() override; 59 60 void OnForeground(const Want &want) override; 61 void OnBackground() override; 62 #endif 63 int32_t OnContinue(WantParams &wantParams) override; 64 void OnConfigurationUpdated(const Configuration &configuration) override; 65 void UpdateContextConfiguration() override; 66 void OnNewWant(const Want &want) override; 67 68 void OnAbilityResult(int requestCode, int resultCode, const Want &resultData) override; 69 void OnRequestPermissionsFromUserResult( 70 int requestCode, const std::vector<std::string> &permissions, const std::vector<int> &grantResults) override; 71 72 sptr<IRemoteObject> CallRequest() override; 73 74 protected: 75 #ifdef SUPPORT_GRAPHICS 76 void DoOnForeground(const Want &want) override; 77 void RequsetFocus(const Want &want) override; 78 #endif 79 private: 80 void CallObjectMethod(const char *name, NativeValue *const *argv = nullptr, size_t argc = 0); 81 std::unique_ptr<NativeReference> CreateAppWindowStage(); 82 #ifdef SUPPORT_GRAPHICS 83 void GetPageStackFromWant(const Want &want, std::string &pageStack); 84 #endif 85 std::shared_ptr<AppExecFwk::ADelegatorAbilityProperty> CreateADelegatorAbilityProperty(); 86 87 JsRuntime &jsRuntime_; 88 std::shared_ptr<NativeReference> shellContextRef_; 89 std::unique_ptr<NativeReference> jsAbilityObj_; 90 sptr<IRemoteObject> remoteCallee_; 91 }; 92 } // namespace AbilityRuntime 93 } // namespace OHOS 94 #endif // ABILITY_RUNTIME_JS_ABILITY_H 95