1 /* 2 * Copyright (C) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FENCEEXTENSION_JS_FENCE_EXTENSION_H 17 #define FENCEEXTENSION_JS_FENCE_EXTENSION_H 18 19 #include "async_context.h" 20 #include "js_runtime.h" 21 #include "napi/native_api.h" 22 #include "native_engine/native_reference.h" 23 #include "native_engine/native_value.h" 24 #include "runtime.h" 25 #include "fence_extension.h" 26 #include <map> 27 28 namespace OHOS { 29 namespace Location { 30 class JsFenceExtension : public FenceExtension { 31 public: 32 explicit JsFenceExtension(AbilityRuntime::JsRuntime &jsRuntime); 33 34 virtual ~JsFenceExtension() override; 35 36 /** 37 * @brief Create JsFenceExtension. 38 * 39 * @param runtime The runtime. 40 * @return The JsFenceExtension instance. 41 */ 42 static JsFenceExtension *Create(const std::unique_ptr<AbilityRuntime::Runtime> &runtime); 43 44 /** 45 * @brief Init the extension. 46 * 47 * @param record the extension record. 48 * @param application the application info. 49 * @param handler the extension handler. 50 * @param token the remote token. 51 */ 52 virtual void Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &record, 53 const std::shared_ptr<AppExecFwk::OHOSApplication> &application, 54 std::shared_ptr<AppExecFwk::AbilityHandler> &handler, const sptr<IRemoteObject> &token) override; 55 56 /** 57 * @brief Called when this remoteLocation extension is started. You must override this function if you want to 58 * perform some initialization operations during extension startup. 59 * 60 * This function can be called only once in the entire lifecycle of an extension. 61 * @param Want Indicates the {@link Want} structure containing startup information about the extension. 62 */ 63 virtual void OnStart(const AAFwk::Want &want) override; 64 65 /** 66 * @brief Called when this remoteLocation extension is connected for the first time. 67 * 68 * You can override this function to implement your own processing logic. 69 * 70 * @param want Indicates the {@link Want} structure containing connection information about the Notification 71 * extension. 72 * @return Returns a pointer to the <b>sid</b> of the connected remoteLocation extension. 73 */ 74 virtual sptr<IRemoteObject> OnConnect(const AAFwk::Want &want) override; 75 76 /** 77 * @brief Called when all abilities connected to this remoteLocation extension are disconnected. 78 * 79 * You can override this function to implement your own processing logic. 80 * 81 */ 82 virtual void OnDisconnect(const AAFwk::Want &want) override; 83 84 /** 85 * @brief Called when this remoteLocation extension enters the <b>STATE_STOP</b> state. 86 * 87 * The extension in the <b>STATE_STOP</b> is being destroyed. 88 * You can override this function to implement your own processing logic. 89 */ 90 virtual void OnStop() override; 91 92 /** 93 * @brief called back when geofence status is change. 94 * 95 * @param fenceId enter fence id 96 * @param fenceType enter fence type 97 * @param extraData other extra data 98 */ 99 FenceExtensionErrCode OnFenceStatusChange(std::map<std::string, std::string> extraData); 100 101 void OnDestroy(); 102 103 private: 104 AbilityRuntime::JsRuntime &jsRuntime_; 105 std::unique_ptr<NativeReference> jsObj_ = nullptr; 106 std::shared_ptr<NativeReference> shellContextRef_ = nullptr; 107 /** 108 * @brief BindContext 109 * @param [IN] &env napi_env 110 * @param [IN] &obj napi_value 111 */ 112 void BindContext(const napi_env &env, const napi_value &obj); 113 /** 114 * @brief get extension src 115 * @param [IN] &srcPath 116 */ 117 void GetSrcPath(std::string &srcPath); 118 std::string GetAndDeleteFromMap(std::map<std::string, std::string> ¶m, std::string key); 119 ::napi_value GetMethod( 120 AbilityRuntime::JsRuntime &jsRuntime, const std::unique_ptr<NativeReference> &jsObj, const std::string &name); 121 napi_status SetValueUtf8String(const napi_env &env, const char *fieldStr, const char *str, napi_value &result); 122 napi_status SetValueInt32(const napi_env &env, const char *fieldStr, const int intValue, napi_value &result); 123 FenceExtensionErrCode CallToUiThread(std::map<std::string, std::string> extraData); 124 }; 125 } // namespace Location 126 } // namespace OHOS 127 128 #endif // FENCEEXTENSION_JS_FENCE_EXTENSION_H