!917 fence extension

Merge pull request !917 from liuxinbing/master
This commit is contained in:
openharmony_ci 2024-10-12 07:05:24 +00:00 committed by Gitee
commit d8a47590b4
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
26 changed files with 1714 additions and 0 deletions

View File

@ -89,6 +89,9 @@
"fwk_group": [ "fwk_group": [
"//base/location/frameworks/native/locator_sdk:locator_sdk", "//base/location/frameworks/native/locator_sdk:locator_sdk",
"//base/location/frameworks/js/napi:geolocation", "//base/location/frameworks/js/napi:geolocation",
"//base/location/frameworks/js/napi/fence_extension_ability:fence_extension_ability_napi",
"//base/location/frameworks/js/napi/fence_extension_ability_context:fence_extension_context_napi",
"//base/location/frameworks/native/fence_extension_ability:fence_extension_ability",
"//base/location/frameworks/js/napi:geolocationmanager", "//base/location/frameworks/js/napi:geolocationmanager",
"//base/location/frameworks/location_common/common:lbsservice_common", "//base/location/frameworks/location_common/common:lbsservice_common",
"//base/location/frameworks/native/locator_agent:locator_agent", "//base/location/frameworks/native/locator_agent:locator_agent",

View File

@ -0,0 +1,61 @@
# Copyright (c) 2023 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("//arkcompiler/ets_frontend/es2panda/es2abc_config.gni")
import("//build/ohos.gni")
es2abc_gen_abc("gen_fence_extension_ability_abc") {
src_js = rebase_path("fence_extension_ability.js")
dst_file = rebase_path(target_out_dir + "/fence_extension_ability.abc")
in_puts = [ "fence_extension_ability.js" ]
out_puts = [ target_out_dir + "/fence_extension_ability.abc" ]
extra_args = [ "--module" ]
}
gen_js_obj("fence_extension_ability_js") {
input = "fence_extension_ability.js"
output = target_out_dir + "/fence_extension_ability.o"
}
gen_js_obj("fence_extension_ability_abc") {
input = get_label_info(":gen_fence_extension_ability_abc", "target_out_dir") +
"/fence_extension_ability.abc"
output = target_out_dir + "/fence_extension_ability_abc.o"
dep = ":gen_fence_extension_ability_abc"
}
ohos_shared_library("fence_extension_ability_napi") {
sanitize = {
cfi = true
cfi_cross_dso = true
debug = false
}
branch_protector_ret = "pac_ret"
sources = [ "fence_extension_ability_module.cpp" ]
deps = [
":fence_extension_ability_abc",
":fence_extension_ability_js",
]
external_deps = [
"hilog:libhilog",
"napi:ace_napi",
]
relative_install_dir = "module/app/ability"
subsystem_name = "location"
part_name = "location"
}

View File

@ -0,0 +1,28 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let ExtensionAbility = requireNapi('app.ability.ExtensionAbility');
class FenceExtensionAbility extends ExtensionAbility {
onFenceStatusChange(transition, additions) {
console.log('js onFenceStatusChange');
}
onDestroy() {
console.log('js onDestroy');
}
}
export default FenceExtensionAbility;

View File

@ -0,0 +1,53 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "native_engine/native_engine.h"
extern const char _binary_fence_extension_ability_js_start[];
extern const char _binary_fence_extension_ability_js_end[];
extern const char _binary_fence_extension_ability_abc_start[];
extern const char _binary_fence_extension_ability_abc_end[];
static napi_module _module = {
.nm_version = 0,
.nm_filename = "app/ability/libfence_extension_ability_napi.z.so/fence_extension_ability.js",
.nm_modname = "app.ability.FenceExtensionAbility",
};
extern "C" __attribute__((constructor)) void NAPI_app_ability_FenceExtensionAbility_AutoRegister()
{
napi_module_register(&_module);
}
extern "C" __attribute__((visibility("default"))) void NAPI_app_ability_FenceExtensionAbility_GetJSCode(
const char **buf, int *bufLen)
{
if (buf != nullptr) {
*buf = _binary_fence_extension_ability_js_start;
}
if (bufLen != nullptr) {
*bufLen = _binary_fence_extension_ability_js_end - _binary_fence_extension_ability_js_start;
}
}
extern "C" __attribute__((visibility("default"))) void NAPI_app_ability_FenceExtensionAbility_GetABCCode(
const char **buf, int *buflen)
{
if (buf != nullptr) {
*buf = _binary_fence_extension_ability_abc_start;
}
if (buflen != nullptr) {
*buflen = _binary_fence_extension_ability_abc_end - _binary_fence_extension_ability_abc_start;
}
}

View File

@ -0,0 +1,57 @@
# Copyright (c) 2023 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("//arkcompiler/ets_frontend/es2panda/es2abc_config.gni")
import("//build/ohos.gni")
es2abc_gen_abc("gen_fence_extension_context_abc") {
src_js = rebase_path("fence_extension_context.js")
dst_file = rebase_path(target_out_dir + "/fence_extension_context.abc")
in_puts = [ "fence_extension_context.js" ]
out_puts = [ target_out_dir + "/fence_extension_context.abc" ]
extra_args = [ "--module" ]
}
gen_js_obj("fence_extension_context_js") {
input = "fence_extension_context.js"
output = target_out_dir + "/fence_extension_context.o"
}
gen_js_obj("fence_extension_context_abc") {
input = get_label_info(":gen_fence_extension_context_abc", "target_out_dir") +
"/fence_extension_context.abc"
output = target_out_dir + "/fence_extension_context_abc.o"
dep = ":gen_fence_extension_context_abc"
}
ohos_shared_library("fence_extension_context_napi") {
sanitize = {
cfi = true
cfi_cross_dso = true
debug = false
}
branch_protector_ret = "pac_ret"
sources = [ "fence_extension_context_module.cpp" ]
deps = [
":fence_extension_context_abc",
":fence_extension_context_js",
]
external_deps = [ "napi:ace_napi" ]
relative_install_dir = "module/app/ability"
subsystem_name = "location"
part_name = "location"
}

View File

@ -0,0 +1,30 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let ExtensionContext = requireNapi('application.ExtensionContext');
class FenceExtensionContext extends ExtensionContext {
constructor(obj) {
super(obj);
}
startAbility(want, callback) {
console.log('js startAbility');
return this.__context_impl__.startAbility(want, callback);
}
}
export default FenceExtensionContext;

View File

@ -0,0 +1,55 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "native_engine/native_engine.h"
extern const char _binary_fence_extension_context_js_start[];
extern const char _binary_fence_extension_context_js_end[];
extern const char _binary_fence_extension_context_abc_start[];
extern const char _binary_fence_extension_context_abc_end[];
static napi_module _module = {
.nm_version = 0,
.nm_filename = "app/ability/libfence_extension_context_napi.z.so/fence_extension_context.js",
.nm_modname = "app.ability.FenceExtensionContext",
};
extern "C" __attribute__((constructor)) void NAPI_app_ability_FenceExtensionContext_AutoRegister()
{
napi_module_register(&_module);
}
extern "C" __attribute__((visibility("default"))) void NAPI_app_ability_FenceExtensionContext_GetJSCode(
const char **buf, int *bufLen)
{
if (buf != nullptr) {
*buf = _binary_fence_extension_context_js_start;
}
if (bufLen != nullptr) {
*bufLen = _binary_fence_extension_context_js_end - _binary_fence_extension_context_js_start;
}
}
extern "C" __attribute__((visibility("default"))) void NAPI_app_ability_FenceExtensionContext_GetABCCode(
const char **buf, int *buflen)
{
if (buf != nullptr) {
*buf = _binary_fence_extension_context_abc_start;
}
if (buflen != nullptr) {
*buflen = _binary_fence_extension_context_abc_end - _binary_fence_extension_context_abc_start;
}
}

View File

@ -0,0 +1,121 @@
# Copyright (c) 2023 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("//base/location/config.gni")
import("//build/ohos.gni")
config("fence_extension_ability_config") {
visibility = [ ":*" ]
include_dirs = [
"$LOCATION_NATIVE_DIR/fence_extension_ability/include",
"$LOCATION_ROOT_DIR/interfaces/inner_api/include",
]
}
fence_extension_ability_sources = [
"$LOCATION_NATIVE_DIR/fence_extension_ability/src/fence_extension.cpp",
"$LOCATION_NATIVE_DIR/fence_extension_ability/src/fence_extension_context.cpp",
"$LOCATION_NATIVE_DIR/fence_extension_ability/src/fence_extension_module_loader.cpp",
"$LOCATION_NATIVE_DIR/fence_extension_ability/src/fence_extension_stub.cpp",
"$LOCATION_NATIVE_DIR/fence_extension_ability/src/fence_extension_stub_impl.cpp",
"$LOCATION_NATIVE_DIR/fence_extension_ability/src/js_fence_extension.cpp",
"$LOCATION_NATIVE_DIR/fence_extension_ability/src/js_fence_extension_context.cpp",
]
fence_extension_ability_deps = [
"$LOCATION_ROOT_DIR/frameworks/js/napi:geolocation",
"$LOCATION_ROOT_DIR/frameworks/location_common/common:lbsservice_common",
]
fence_extension_ability_external_deps = [
"ability_base:want",
"ability_runtime:ability_context_native",
"ability_runtime:ability_manager",
"ability_runtime:app_context",
"ability_runtime:extensionkit_native",
"ability_runtime:napi_common",
"ability_runtime:runtime",
"access_token:libaccesstoken_sdk",
"access_token:libprivacy_sdk",
"c_utils:utils",
"eventhandler:libeventhandler",
"ffrt:libffrt",
"hilog:libhilog",
"ipc:ipc_single",
"napi:ace_napi",
"os_account:os_account_innerkits",
"safwk:system_ability_fwk",
"samgr:samgr_proxy",
]
ohos_shared_library("fence_extension_ability") {
public_configs = [ ":fence_extension_ability_config" ]
sources = fence_extension_ability_sources
public_deps = fence_extension_ability_deps
external_deps = fence_extension_ability_external_deps
relative_install_dir = "extensionability/"
subsystem_name = "location"
part_name = "location"
sanitize = {
cfi = true
cfi_cross_dso = true
debug = false
}
cflags_cc = [
"-std=c++17",
"-fno-rtti",
]
ldflags = [
"-fPIC",
"-Wl,-E",
]
branch_protector_ret = "pac_ret"
}
# Just for test.
ohos_static_library("fence_extension_ability_static") {
public_configs = [ ":fence_extension_ability_config" ]
sources = fence_extension_ability_sources
public_deps = fence_extension_ability_deps
external_deps = fence_extension_ability_external_deps
subsystem_name = "location"
part_name = "location"
sanitize = {
cfi = true
cfi_cross_dso = true
debug = false
}
cflags_cc = [
"-std=c++17",
"-fno-rtti",
]
ldflags = [
"-fPIC",
"-Wl,-E",
]
}

View File

@ -0,0 +1,100 @@
/*
* Copyright (C) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_ABILITY_RUNTIME_FENCE_EXTENSION_H
#define OHOS_ABILITY_RUNTIME_FENCE_EXTENSION_H
#include "extension_base.h"
#include "fence_extension_context.h"
#include "constant_definition.h"
#include <map>
namespace OHOS {
namespace Location {
using namespace AbilityRuntime;
class FenceExtensionContext;
class FenceExtension;
using CreatorFunc = std::function<FenceExtension *(const std::unique_ptr<Runtime> &runtime)>;
/**
* @brief Basic vpn components.
*/
class FenceExtension : public ExtensionBase<FenceExtensionContext> {
public:
FenceExtension() = default;
virtual ~FenceExtension() = default;
/**
* @brief Create and init context.
*
* @param record the extension record.
* @param application the application info.
* @param handler the extension handler.
* @param token the remote token.
* @return The created context.
*/
virtual std::shared_ptr<FenceExtensionContext> CreateAndInitContext(
const std::shared_ptr<AbilityLocalRecord> &record, const std::shared_ptr<OHOSApplication> &application,
std::shared_ptr<AbilityHandler> &handler, const sptr<IRemoteObject> &token) override;
/**
* @brief Init the extension.
*
* @param record the extension record.
* @param application the application info.
* @param handler the extension handler.
* @param token the remote token.
*/
virtual void Init(const std::shared_ptr<AbilityLocalRecord> &record,
const std::shared_ptr<OHOSApplication> &application, std::shared_ptr<AbilityHandler> &handler,
const sptr<IRemoteObject> &token) override;
/**
* @brief Create Extension.
*
* @param runtime The runtime.
* @return The VpnExtension instance.
*/
static FenceExtension *Create(const std::unique_ptr<OHOS::AbilityRuntime::Runtime> &runtime);
/**
* @brief Set a creator function.
*
* @param creator The function for create a vpn-extension ability.
*/
static void SetCreator(const CreatorFunc &creator);
/**
* @brief called back when geofence status is change.
*
* @param fenceId enter fence id
* @param fenceType enter fence type
* @param extraData other extra data
*/
FenceExtensionErrCode OnFenceStatusChange(std::map<std::string, std::string> extraData);
/**
* @brief destroy this extension
*/
void OnDestroy();
};
} // namespace Location
} // namespace OHOS
#endif // OHOS_ABILITY_RUNTIME_FENCE_EXTENSION_H

View File

@ -0,0 +1,42 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FENCEEXTENSION_FENCE_EXTENSION_CONTEXT_H
#define FENCEEXTENSION_FENCE_EXTENSION_CONTEXT_H
#include "extension_context.h"
#include "want.h"
namespace OHOS {
namespace Location {
/**
* @brief context supply for fence
*
*/
class FenceExtensionContext : public AbilityRuntime::ExtensionContext {
public:
FenceExtensionContext();
virtual ~FenceExtensionContext() override;
ErrCode StartServiceExtensionAbility(const AAFwk::Want &want, int32_t accountId = -1) const;
protected:
bool CheckCallerIsSystemApp() const;
};
} // namespace Location
} // namespace OHOS
#endif // FENCEEXTENSION_FENCE_EXTENSION_CONTEXT_H

View File

@ -0,0 +1,46 @@
/*
* Copyright (C) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FENCEEXTENSION_FENCE_EXTENSION_MODULE_LOADER_H
#define FENCEEXTENSION_FENCE_EXTENSION_MODULE_LOADER_H
#include <map>
#include "extension_module_loader.h"
namespace OHOS {
namespace Location {
class FenceExtensionModuleLoader : public AbilityRuntime::ExtensionModuleLoader,
public Singleton<FenceExtensionModuleLoader> {
DECLARE_SINGLETON(FenceExtensionModuleLoader);
public:
/**
* @brief Create fence extension.
*
* @param runtime The runtime.
* @return The fence extension instance.
*/
virtual AbilityRuntime::Extension *Create(const std::unique_ptr<AbilityRuntime::Runtime> &runtime) const override;
/**
* @brief Get fence extension info.
*
* @return The fence extension info.
*/
virtual std::map<std::string, std::string> GetParams() override;
};
} // namespace Location
} // namespace OHOS
#endif // FENCEEXTENSION_FENCE_EXTENSION_MODULE_LOADER_H

View File

@ -0,0 +1,35 @@
/*
* Copyright (C) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_FENCEEXTENSION_FENCE_EXTENSION_STUB_H
#define OHOS_FENCEEXTENSION_FENCE_EXTENSION_STUB_H
#include "i_remote_fence_extension.h"
#include "iremote_stub.h"
namespace OHOS {
namespace Location {
class FenceExtensionStub : public IRemoteStub<IRemoteFenceExtension> {
public:
FenceExtensionStub() = default;
virtual ~FenceExtensionStub() = default;
int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
};
} // namespace Location
} // namespace OHOS
#endif // OHOS_FENCEEXTENSION_FENCE_EXTENSION_STUB_H

View File

@ -0,0 +1,40 @@
/*
* Copyright (C) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_FENCEEXTENSION_FENCE_EXTENSION_STUB_IMPL_H
#define OHOS_FENCEEXTENSION_FENCE_EXTENSION_STUB_IMPL_H
#include <memory>
#include "js_fence_extension.h"
#include "fence_extension_stub.h"
#include "constant_definition.h"
#include <map>
namespace OHOS {
namespace Location {
class FenceExtensionStubImpl : public FenceExtensionStub {
public:
explicit FenceExtensionStubImpl(const std::shared_ptr<JsFenceExtension> &extension);
virtual ~FenceExtensionStubImpl();
virtual FenceExtensionErrCode OnFenceStatusChange(std::map<std::string, std::string> extraData) override;
private:
std::weak_ptr<JsFenceExtension> extension_;
};
} // namespace Location
} // namespace OHOS
#endif // OHOS_FENCEEXTENSION_FENCE_EXTENSION_STUB_IMPL_H

View File

@ -0,0 +1,128 @@
/*
* Copyright (C) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FENCEEXTENSION_JS_FENCE_EXTENSION_H
#define FENCEEXTENSION_JS_FENCE_EXTENSION_H
#include "async_context.h"
#include "js_runtime.h"
#include "napi/native_api.h"
#include "native_engine/native_reference.h"
#include "native_engine/native_value.h"
#include "runtime.h"
#include "fence_extension.h"
#include <map>
namespace OHOS {
namespace Location {
class JsFenceExtension : public FenceExtension {
public:
explicit JsFenceExtension(AbilityRuntime::JsRuntime &jsRuntime);
virtual ~JsFenceExtension() override;
/**
* @brief Create JsFenceExtension.
*
* @param runtime The runtime.
* @return The JsFenceExtension instance.
*/
static JsFenceExtension *Create(const std::unique_ptr<AbilityRuntime::Runtime> &runtime);
/**
* @brief Init the extension.
*
* @param record the extension record.
* @param application the application info.
* @param handler the extension handler.
* @param token the remote token.
*/
virtual void Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &record,
const std::shared_ptr<AppExecFwk::OHOSApplication> &application,
std::shared_ptr<AppExecFwk::AbilityHandler> &handler, const sptr<IRemoteObject> &token) override;
/**
* @brief Called when this remoteLocation extension is started. You must override this function if you want to
* perform some initialization operations during extension startup.
*
* This function can be called only once in the entire lifecycle of an extension.
* @param Want Indicates the {@link Want} structure containing startup information about the extension.
*/
virtual void OnStart(const AAFwk::Want &want) override;
/**
* @brief Called when this remoteLocation extension 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 Notification
* extension.
* @return Returns a pointer to the <b>sid</b> of the connected remoteLocation extension.
*/
virtual sptr<IRemoteObject> OnConnect(const AAFwk::Want &want) override;
/**
* @brief Called when all abilities connected to this remoteLocation extension are disconnected.
*
* You can override this function to implement your own processing logic.
*
*/
virtual void OnDisconnect(const AAFwk::Want &want) override;
/**
* @brief Called when this remoteLocation extension enters the <b>STATE_STOP</b> state.
*
* The extension in the <b>STATE_STOP</b> is being destroyed.
* You can override this function to implement your own processing logic.
*/
virtual void OnStop() override;
/**
* @brief called back when geofence status is change.
*
* @param fenceId enter fence id
* @param fenceType enter fence type
* @param extraData other extra data
*/
FenceExtensionErrCode OnFenceStatusChange(std::map<std::string, std::string> extraData);
void OnDestroy();
private:
AbilityRuntime::JsRuntime &jsRuntime_;
std::unique_ptr<NativeReference> jsObj_ = nullptr;
std::shared_ptr<NativeReference> shellContextRef_ = nullptr;
/**
* @brief BindContext
* @param [IN] &env napi_env
* @param [IN] &obj napi_value
*/
void BindContext(const napi_env &env, const napi_value &obj);
/**
* @brief get extension src
* @param [IN] &srcPath
*/
void GetSrcPath(std::string &srcPath);
std::string GetAndDeleteFromMap(std::map<std::string, std::string> &param, std::string key);
::napi_value GetMethod(
AbilityRuntime::JsRuntime &jsRuntime, const std::unique_ptr<NativeReference> &jsObj, const std::string &name);
napi_status SetValueUtf8String(const napi_env &env, const char *fieldStr, const char *str, napi_value &result);
napi_status SetValueInt32(const napi_env &env, const char *fieldStr, const int intValue, napi_value &result);
FenceExtensionErrCode CallToUiThread(std::map<std::string, std::string> extraData);
};
} // namespace Location
} // namespace OHOS
#endif // FENCEEXTENSION_JS_FENCE_EXTENSION_H

View File

@ -0,0 +1,57 @@
/*
* Copyright (C) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FENCEEXTENSION_JS_FENCE_EXTENSION_CONTEXT_H
#define FENCEEXTENSION_JS_FENCE_EXTENSION_CONTEXT_H
#include "fence_extension_context.h"
#include "napi/native_api.h"
#include "native_engine/native_engine.h"
#include "js_extension_context.h"
#include "js_runtime_utils.h"
#include "js_error_utils.h"
#include "js_runtime.h"
#include "napi/native_api.h"
#include "napi_common_want.h"
#include "want.h"
#include "ability_business_error.h"
namespace OHOS {
namespace Location {
using namespace OHOS::AbilityRuntime;
class JsFenceExtensionContext {
public:
explicit JsFenceExtensionContext(const std::shared_ptr<FenceExtensionContext> &context);
~JsFenceExtensionContext();
static ::napi_value CreateJsFenceExtensionContext(
const ::napi_env &env, std::shared_ptr<FenceExtensionContext> context);
static ::napi_value StartAbility(napi_env env, napi_callback_info info)
{
GET_NAPI_INFO_AND_CALL(env, info, JsFenceExtensionContext, OnStartAbility);
}
private:
::napi_value OnStartExtensionAbility(AAFwk::Want want, napi_env env, AbilityRuntime::NapiCallbackInfo &info);
::napi_value OnStartAbility(napi_env env, AbilityRuntime::NapiCallbackInfo &info);
static void Finalizer(::napi_env env, void *data, void *hint);
std::weak_ptr<FenceExtensionContext> context_;
};
} // namespace Location
} // namespace OHOS
#endif // FENCEEXTENSION_JS_FENCE_EXTENSION_CONTEXT_H

View File

@ -0,0 +1,6 @@
1.0 {
global:
*;
local:
*;
};

View File

@ -0,0 +1,72 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "fence_extension.h"
#include "js_fence_extension.h"
#include "location_log.h"
#include "runtime.h"
namespace OHOS {
namespace Location {
using namespace OHOS::AppExecFwk;
FenceExtension *FenceExtension::Create(const std::unique_ptr<OHOS::AbilityRuntime::Runtime> &runtime)
{
if (!runtime) {
return new FenceExtension();
}
LBSLOGI(FENCE_EXTENSION, "FenceExtension:Create");
switch (runtime->GetLanguage()) {
case OHOS::AbilityRuntime::Runtime::Language::JS:
return JsFenceExtension::Create(runtime);
default:
return new FenceExtension();
}
}
void FenceExtension::Init(const std::shared_ptr<AbilityLocalRecord> &record,
const std::shared_ptr<OHOSApplication> &application, std::shared_ptr<AbilityHandler> &handler,
const sptr<IRemoteObject> &token)
{
ExtensionBase<FenceExtensionContext>::Init(record, application, handler, token);
LBSLOGI(FENCE_EXTENSION, "FenceExtension:Init");
}
std::shared_ptr<FenceExtensionContext> FenceExtension::CreateAndInitContext(
const std::shared_ptr<AbilityLocalRecord> &record, const std::shared_ptr<OHOSApplication> &application,
std::shared_ptr<AbilityHandler> &handler, const sptr<IRemoteObject> &token)
{
std::shared_ptr<FenceExtensionContext> context =
ExtensionBase<FenceExtensionContext>::CreateAndInitContext(record, application, handler, token);
if (context == nullptr) {
LBSLOGE(FENCE_EXTENSION, "FenceExtension:context is null");
return context;
}
LBSLOGI(FENCE_EXTENSION, "FenceExtension:CreateAndInitContext");
return context;
}
FenceExtensionErrCode FenceExtension::OnFenceStatusChange(std::map<std::string, std::string> extraData)
{
LBSLOGI(FENCE_EXTENSION, "FenceExtension:OnFenceStatusChange");
return FenceExtensionErrCode::EXTENSION_SUCCESS;
}
void FenceExtension::OnDestroy()
{
LBSLOGI(FENCE_EXTENSION, "FenceExtension:OnDestroy");
}
} // namespace Location
} // namespace OHOS

View File

@ -0,0 +1,65 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "fence_extension_context.h"
#include "ability_business_error.h"
#include "ability_manager_client.h"
#include "ability_manager_errors.h"
#include "accesstoken_kit.h"
#include "ipc_skeleton.h"
#include "tokenid_kit.h"
#include "location_log.h"
namespace OHOS {
namespace Location {
namespace {
constexpr const char *PERMISSION_START_ABILITIES_FROM_BACKGROUND = "ohos.permission.START_ABILITIES_FROM_BACKGROUND";
}
FenceExtensionContext::FenceExtensionContext(){};
FenceExtensionContext::~FenceExtensionContext(){};
ErrCode FenceExtensionContext::StartServiceExtensionAbility(const AAFwk::Want &want, int32_t accountId) const
{
ErrCode err = ERR_OK;
std::string callerBundleName = GetBundleName();
std::string calledBundleName = want.GetBundle();
if (calledBundleName != callerBundleName) {
LBSLOGE(FENCE_EXTENSION, "This application won't start no-self-ability.");
err = AAFwk::ERR_NOT_SELF_APPLICATION;
return err;
}
LBSLOGI(FENCE_EXTENSION, "start %{public}s begin.", __func__);
err = AAFwk::AbilityManagerClient::GetInstance()->StartExtensionAbility(
want, token_, accountId, AppExecFwk::ExtensionAbilityType::SERVICE);
if (err != ERR_OK) {
LBSLOGE(FENCE_EXTENSION, "FenceExtensionContext::StartServiceExtensionAbility is failed %{public}d", err);
}
return err;
}
bool FenceExtensionContext::CheckCallerIsSystemApp() const
{
auto selfToken = IPCSkeleton::GetSelfTokenID();
if (!Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(selfToken)) {
return false;
}
return true;
}
} // namespace Location
} // namespace OHOS

View File

@ -0,0 +1,49 @@
/*
* Copyright (C) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "fence_extension_module_loader.h"
#include "fence_extension.h"
#include "runtime.h"
#include "js_runtime.h"
#include "js_runtime_utils.h"
using namespace OHOS::AbilityRuntime;
namespace OHOS {
namespace Location {
FenceExtensionModuleLoader::FenceExtensionModuleLoader() = default;
FenceExtensionModuleLoader::~FenceExtensionModuleLoader() = default;
Extension *FenceExtensionModuleLoader::Create(const std::unique_ptr<OHOS::AbilityRuntime::Runtime> &runtime) const
{
return FenceExtension::Create(runtime);
}
std::map<std::string, std::string> FenceExtensionModuleLoader::GetParams()
{
std::map<std::string, std::string> params;
// type means extension type in ExtensionAbilityType of extension_ability_info.h, 24 means fence.
params.insert(std::pair<std::string, std::string>("type", "24"));
// extension name
params.insert(std::pair<std::string, std::string>("name", "FenceExtension"));
return params;
}
extern "C" __attribute__((visibility("default"))) void *OHOS_EXTENSION_GetExtensionModule()
{
return &FenceExtensionModuleLoader::GetInstance();
}
} // namespace Location
} // namespace OHOS

View File

@ -0,0 +1,57 @@
/*
* Copyright (C) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "fence_extension_stub.h"
#include "ipc_types.h"
#include "location_log.h"
namespace OHOS {
namespace Location {
int FenceExtensionStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
{
LBSLOGI(FENCE_EXTENSION, "stub on message:%{public}d", code);
if (data.ReadInterfaceToken() != GetDescriptor()) {
return ERR_TRANSACTION_FAILED;
}
switch (code) {
case ON_FENCE_STATUS_CHANGE: {
std::vector<std::string> dataKeys;
data.ReadStringVector(&dataKeys);
std::vector<std::string> dataValues;
data.ReadStringVector(&dataValues);
std::map<std::string, std::string> extraData;
auto keysItr = dataKeys.begin();
auto valuesItr = dataValues.begin();
while (keysItr != dataKeys.end() && valuesItr != dataValues.end()) {
extraData[*keysItr] = *valuesItr;
keysItr++;
valuesItr++;
}
FenceExtensionErrCode result = OnFenceStatusChange(extraData);
LBSLOGI(FENCE_EXTENSION, "stub on message end:%{public}d", result);
if (result != FenceExtensionErrCode::EXTENSION_SUCCESS) {
return ERR_TRANSACTION_FAILED;
}
return ERR_NONE;
}
default:
return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
}
LBSLOGE(FENCE_EXTENSION, "stub on message trans failed");
return ERR_TRANSACTION_FAILED;
}
} // namespace Location
} // namespace OHOS

View File

@ -0,0 +1,41 @@
/*
* Copyright (C) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "fence_extension_stub_impl.h"
#include "location_log.h"
namespace OHOS {
namespace Location {
FenceExtensionStubImpl::FenceExtensionStubImpl(const std::shared_ptr<JsFenceExtension> &extension)
: extension_(extension)
{}
FenceExtensionStubImpl::~FenceExtensionStubImpl()
{}
FenceExtensionErrCode FenceExtensionStubImpl::OnFenceStatusChange(std::map<std::string, std::string> extraData)
{
LBSLOGI(FENCE_EXTENSION, "stub impl OnFenceStatusChange");
auto extension = extension_.lock();
if (extension != nullptr) {
extension->OnFenceStatusChange(extraData);
} else {
return FenceExtensionErrCode::EXTENSION_REMOTE_STUB_IS_NULL;
}
LBSLOGI(FENCE_EXTENSION, "stub impl end");
return FenceExtensionErrCode::EXTENSION_SUCCESS;
}
} // namespace Location
} // namespace OHOS

View File

@ -0,0 +1,386 @@
/*
* Copyright (C) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "js_fence_extension.h"
#include "js_fence_extension_context.h"
#include "fence_extension_stub_impl.h"
#include "ability_handler.h"
#include "ability_info.h"
#include "hilog_wrapper.h"
#include "js_extension_common.h"
#include "js_extension_context.h"
#include "runtime.h"
#include "js_runtime.h"
#include "js_runtime_utils.h"
#include "display_manager.h"
#include "napi/native_api.h"
#include "napi/native_node_api.h"
#include "napi_common_configuration.h"
#include "napi_common_want.h"
#include "napi_remote_object.h"
#include "location_log.h"
#include "geofence_definition.h"
#include <thread>
#include "napi/native_api.h"
#include "uv.h"
using namespace OHOS::AbilityRuntime;
namespace OHOS {
namespace Location {
const size_t ARGC_ONE = 1;
const size_t ARGC_TWO = 2;
const size_t LOAD_SYSTEM_MODULE_ARGC = 1;
const std::string CONTEXT_MODULE_PATH = "app.ability.FenceExtensionContext";
using namespace OHOS::AppExecFwk;
napi_value AttachFenceExtensionContext(napi_env env, void *value, void *)
{
LBSLOGI(FENCE_EXTENSION, "AttachFenceExtensionContext");
if (value == nullptr) {
LBSLOGE(FENCE_EXTENSION, "invalid parameter");
return nullptr;
}
auto ptr = reinterpret_cast<std::weak_ptr<FenceExtensionContext> *>(value)->lock();
if (ptr == nullptr) {
LBSLOGE(FENCE_EXTENSION, "invalid context");
return nullptr;
}
napi_value object = JsFenceExtensionContext::CreateJsFenceExtensionContext(env, ptr);
auto napiContextObj =
AbilityRuntime::JsRuntime::LoadSystemModuleByEngine(env, CONTEXT_MODULE_PATH, &object, LOAD_SYSTEM_MODULE_ARGC)
->GetNapiValue();
napi_coerce_to_native_binding_object(
env, napiContextObj, AbilityRuntime::DetachCallbackFunc, AttachFenceExtensionContext, value, nullptr);
auto workContext = new (std::nothrow) std::weak_ptr<FenceExtensionContext>(ptr);
if (workContext == nullptr) {
LBSLOGE(FENCE_EXTENSION, "invalid extension context");
return nullptr;
}
auto retStatus = ::napi_wrap(
env,
napiContextObj,
workContext,
[](napi_env, void *data, void *) {
LBSLOGI(FENCE_EXTENSION, "Finalizer for weak_ptr extension context is called");
delete static_cast<std::weak_ptr<FenceExtensionContext> *>(data);
},
nullptr,
nullptr);
if (retStatus != ::napi_status::napi_ok) {
LBSLOGE(FENCE_EXTENSION, "Napi wrap context error");
return nullptr;
}
LBSLOGI(FENCE_EXTENSION, "AttachFenceExtensionContext end");
return napiContextObj;
}
JsFenceExtension *JsFenceExtension::Create(const std::unique_ptr<AbilityRuntime::Runtime> &runtime)
{
return new (std::nothrow) JsFenceExtension(static_cast<AbilityRuntime::JsRuntime &>(*runtime));
}
JsFenceExtension::JsFenceExtension(AbilityRuntime::JsRuntime &jsRuntime) : jsRuntime_(jsRuntime)
{}
JsFenceExtension::~JsFenceExtension()
{
LBSLOGD(FENCE_EXTENSION, "Js extension destructor");
auto context = GetContext();
if (context) {
context->Unbind();
}
jsRuntime_.FreeNativeReference(std::move(jsObj_));
jsRuntime_.FreeNativeReference(std::move(shellContextRef_));
}
void JsFenceExtension::Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &record,
const std::shared_ptr<AppExecFwk::OHOSApplication> &application,
std::shared_ptr<AppExecFwk::AbilityHandler> &handler, const sptr<IRemoteObject> &token)
{
FenceExtension::Init(record, application, handler, token);
if (Extension::abilityInfo_->srcEntrance.empty()) {
LBSLOGE(FENCE_EXTENSION, "srcEntrance of abilityInfo is empty");
return;
}
std::string srcPath = "";
GetSrcPath(srcPath);
if (srcPath.empty()) {
LBSLOGE(FENCE_EXTENSION, "Failed to get srcPath");
return;
}
std::string moduleName(Extension::abilityInfo_->moduleName);
moduleName.append("::").append(abilityInfo_->name);
LBSLOGW(FENCE_EXTENSION,
"JsFenceExtension::Init module:%{public}s,srcPath:%{public}s",
moduleName.c_str(),
srcPath.c_str());
AbilityRuntime::HandleScope handleScope(jsRuntime_);
jsObj_ = jsRuntime_.LoadModule(
moduleName, srcPath, abilityInfo_->hapPath, abilityInfo_->compileMode == CompileMode::ES_MODULE);
if (jsObj_ == nullptr) {
LBSLOGE(FENCE_EXTENSION, "Failed to load ability module");
return;
}
napi_value obj = jsObj_->GetNapiValue();
if (obj == nullptr) {
LBSLOGE(FENCE_EXTENSION, "Failed to get extension object");
return;
}
napi_env env = jsRuntime_.GetNapiEnv();
LBSLOGI(FENCE_EXTENSION, "Init end");
BindContext(env, obj);
}
void JsFenceExtension::BindContext(const ::napi_env &env, const ::napi_value &obj)
{
LBSLOGI(FENCE_EXTENSION, "BindContext start");
auto context = GetContext();
if (context == nullptr) {
LBSLOGE(FENCE_EXTENSION, "Failed to get context");
return;
}
napi_value contextObj = JsFenceExtensionContext::CreateJsFenceExtensionContext(env, context);
auto shellContextRef_ =
AbilityRuntime::JsRuntime::LoadSystemModuleByEngine(env, CONTEXT_MODULE_PATH, &contextObj, ARGC_ONE);
if (shellContextRef_ == nullptr) {
LBSLOGE(FENCE_EXTENSION, "Failed to get shell context from system module");
return;
}
napi_value nativeObj = shellContextRef_->GetNapiValue();
if (nativeObj == nullptr) {
LBSLOGE(FENCE_EXTENSION, "Failed to load context module");
return;
}
auto workContext = new (std::nothrow) std::weak_ptr<FenceExtensionContext>(context);
if (workContext == nullptr) {
LBSLOGE(FENCE_EXTENSION, "invalid extension context");
return;
}
napi_coerce_to_native_binding_object(
env, nativeObj, AbilityRuntime::DetachCallbackFunc, AttachFenceExtensionContext, workContext, nullptr);
context->Bind(jsRuntime_, shellContextRef_.release());
napi_set_named_property(env, obj, "context", nativeObj);
napi_wrap(
env,
nativeObj,
workContext,
[](napi_env, void *data, void *) {
LBSLOGI(FENCE_EXTENSION, "Finalizer for weak_ptr extension context is called");
delete static_cast<std::weak_ptr<FenceExtensionContext> *>(data);
},
nullptr,
nullptr);
LBSLOGI(FENCE_EXTENSION, "BindContext end");
}
void JsFenceExtension::OnStart(const AAFwk::Want &want)
{
AbilityRuntime::Extension::OnStart(want);
}
void JsFenceExtension::OnStop()
{
LBSLOGI(FENCE_EXTENSION, "OnStop called");
AbilityRuntime::HandleScope handleScope(jsRuntime_);
if (jsObj_ == nullptr) {
LBSLOGE(FENCE_EXTENSION, "js fence extension obj is null");
return;
}
::napi_value method = GetMethod(jsRuntime_, jsObj_, "onDestroy");
if (method == nullptr) {
LBSLOGE(FENCE_EXTENSION, "Call function method is null");
return;
}
napi_value argv[0];
::napi_value undefined;
::napi_env env = jsRuntime_.GetNapiEnv();
::napi_value value = jsObj_->GetNapiValue();
::napi_status retStatus = ::napi_call_function(env, value, method, 0, argv, &undefined);
if (retStatus != ::napi_status::napi_ok) {
LBSLOGE(FENCE_EXTENSION, "Call function error");
}
LBSLOGI(FENCE_EXTENSION, "Call onDestroy end");
AbilityRuntime::Extension::OnStop();
}
sptr<IRemoteObject> JsFenceExtension::OnConnect(const AAFwk::Want &want)
{
LBSLOGI(FENCE_EXTENSION, "OnConnect");
AbilityRuntime::Extension::OnConnect(want);
sptr<FenceExtensionStubImpl> remoteObject =
new (std::nothrow) FenceExtensionStubImpl(std::static_pointer_cast<JsFenceExtension>(shared_from_this()));
if (remoteObject == nullptr) {
LBSLOGE(FENCE_EXTENSION, "failed to create FenceExtensionStubImpl");
return nullptr;
}
return remoteObject->AsObject();
}
void JsFenceExtension::OnDisconnect(const AAFwk::Want &want)
{
LBSLOGI(FENCE_EXTENSION, "OnDisconnect");
Extension::OnDisconnect(want);
}
FenceExtensionErrCode JsFenceExtension::OnFenceStatusChange(std::map<std::string, std::string> extraData)
{
LBSLOGI(FENCE_EXTENSION, "js fence extension:OnFenceStatusChange");
if (jsObj_ == nullptr) {
LBSLOGE(FENCE_EXTENSION, "js fence extension obj is null");
return FenceExtensionErrCode::EXTENSION_JS_OBJ_IS_NULL;
}
auto task = [=]() {
LBSLOGI(FENCE_EXTENSION, "call js function start");
JsFenceExtension::CallToUiThread(extraData);
};
if (handler_ == nullptr) {
LBSLOGE(FENCE_EXTENSION, "PostTask call js function start");
return FenceExtensionErrCode::EXTENSION_JS_CALL_FAILED;
}
handler_->PostTask(task, "FenceExtension OnFenceStatusChange Task");
LBSLOGI(FENCE_EXTENSION, "PostTask call js function start");
return FenceExtensionErrCode::EXTENSION_SUCCESS;
}
void JsFenceExtension::GetSrcPath(std::string &srcPath)
{
if (!Extension::abilityInfo_->srcEntrance.empty()) {
srcPath.append(Extension::abilityInfo_->moduleName);
srcPath.append("/");
srcPath.append(Extension::abilityInfo_->srcEntrance);
srcPath.erase(srcPath.rfind('.'));
srcPath.append(".abc");
}
}
FenceExtensionErrCode JsFenceExtension::CallToUiThread(std::map<std::string, std::string> extraData)
{
LBSLOGI(FENCE_EXTENSION, "js fence extension:OnFenceStatusChange");
if (jsObj_ == nullptr) {
return FenceExtensionErrCode::EXTENSION_JS_OBJ_IS_NULL;
}
AbilityRuntime::HandleScope handleScope(jsRuntime_);
::napi_value method = GetMethod(jsRuntime_, jsObj_, "onFenceStatusChange");
if (method == nullptr) {
return FenceExtensionErrCode::EXTENSION_JS_NOT_FOUND_METHOD;
}
auto fenceId = std::atoi(GetAndDeleteFromMap(extraData, EXTENSION_PARAM_KEY_FENCE_ID).c_str());
auto fenceEvent = std::atoi(GetAndDeleteFromMap(extraData, EXTENSION_PARAM_KEY_FENCE_EVENT).c_str());
::napi_value transitionObj;
::napi_env env = jsRuntime_.GetNapiEnv();
napi_status retTransition = ::napi_create_object(env, &transitionObj);
if (retTransition != napi_ok) {
return FenceExtensionErrCode::EXTENSION_JS_CREATE_PARAM_ERROR;
}
SetValueInt32(env, "geofenceId", fenceId, transitionObj);
SetValueInt32(env, "transitionEvent", static_cast<int>(fenceEvent), transitionObj);
::napi_value addtionsRecord;
if (extraData.size() > 0) {
::napi_status status = napi_create_object(env, &addtionsRecord);
for (const auto &pair : extraData) {
napi_value key, value;
status = napi_create_string_utf8(env, pair.first.c_str(), NAPI_AUTO_LENGTH, &key);
if (status != napi_ok) {
break;
}
status = napi_create_string_utf8(env, pair.second.c_str(), NAPI_AUTO_LENGTH, &value);
if (status != napi_ok) {
break;
}
status = napi_set_property(env, addtionsRecord, key, value);
if (status != napi_ok) {
break;
}
}
} else {
::napi_status status = napi_get_undefined(env, &addtionsRecord);
}
::napi_value argv[PARAM2];
argv[PARAM0] = transitionObj;
argv[PARAM1] = addtionsRecord;
::napi_value abilityObj = jsObj_->GetNapiValue();
::napi_value undefined;
::napi_status callStatus = ::napi_call_function(env, abilityObj, method, ARGC_TWO, argv, &undefined);
return FenceExtensionErrCode::EXTENSION_SUCCESS;
}
napi_status JsFenceExtension::SetValueUtf8String(
const napi_env &env, const char *fieldStr, const char *str, napi_value &result)
{
napi_value value = nullptr;
NAPI_CALL_BASE(env, napi_create_string_utf8(env, str, NAPI_AUTO_LENGTH, &value), napi_generic_failure);
NAPI_CALL_BASE(env, napi_set_named_property(env, result, fieldStr, value), napi_generic_failure);
return napi_ok;
}
napi_status JsFenceExtension::SetValueInt32(
const napi_env &env, const char *fieldStr, const int intValue, napi_value &result)
{
napi_value value = nullptr;
NAPI_CALL_BASE(env, napi_create_int32(env, intValue, &value), napi_generic_failure);
NAPI_CALL_BASE(env, napi_set_named_property(env, result, fieldStr, value), napi_generic_failure);
return napi_ok;
}
std::string JsFenceExtension::GetAndDeleteFromMap(std::map<std::string, std::string> &param, std::string key)
{
auto mapItr = param.find(key);
std::string value;
if (mapItr != param.end()) {
value = mapItr->second;
param.erase(key);
}
return value;
}
::napi_value JsFenceExtension::GetMethod(
AbilityRuntime::JsRuntime &jsRuntime, const std::unique_ptr<NativeReference> &jsObj, const std::string &name)
{
if (!jsObj) {
LBSLOGE(FENCE_EXTENSION, "Not found Extension.js");
return nullptr;
}
::napi_value obj = jsObj->GetNapiValue();
if (obj == nullptr) {
LBSLOGE(FENCE_EXTENSION, "Failed to get Extension object");
return nullptr;
}
::napi_env env = jsRuntime.GetNapiEnv();
::napi_value method;
::napi_status ret = ::napi_get_named_property(env, obj, name.c_str(), &method);
if (ret != ::napi_status::napi_ok) {
LBSLOGE(FENCE_EXTENSION, "napi get name fail(%{public}d)", ret);
return nullptr;
}
::napi_valuetype type = ::napi_valuetype::napi_undefined;
::napi_valuetype functionType = ::napi_valuetype::napi_function;
::napi_status retType = ::napi_typeof(env, method, &functionType);
if (retType != ::napi_status::napi_ok) {
LBSLOGE(FENCE_EXTENSION, "Parse napi object type fail(%{public}d)", retType);
return nullptr;
}
return method;
}
} // namespace Location
} // namespace OHOS

View File

@ -0,0 +1,114 @@
/*
* Copyright (C) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "js_fence_extension_context.h"
#include "location_log.h"
namespace OHOS {
namespace Location {
constexpr size_t ARGC_ZERO = 0;
constexpr size_t ARGC_ONE = 1;
constexpr size_t ARGC_TWO = 2;
JsFenceExtensionContext::JsFenceExtensionContext(const std::shared_ptr<FenceExtensionContext> &context)
: context_(context)
{}
JsFenceExtensionContext::~JsFenceExtensionContext()
{}
::napi_value JsFenceExtensionContext::CreateJsFenceExtensionContext(
const ::napi_env &env, std::shared_ptr<FenceExtensionContext> context)
{
LBSLOGI(FENCE_EXTENSION, "Begin");
std::shared_ptr<OHOS::AppExecFwk::AbilityInfo> abilityInfo = nullptr;
if (context) {
abilityInfo = context->GetAbilityInfo();
} else {
return nullptr;
}
::napi_value objValue = AbilityRuntime::CreateJsExtensionContext(env, context, abilityInfo);
std::unique_ptr<JsFenceExtensionContext> jsContext = std::make_unique<JsFenceExtensionContext>(context);
::napi_status retStatus =
::napi_wrap(env, objValue, jsContext.release(), JsFenceExtensionContext::Finalizer, nullptr, nullptr);
if (retStatus != ::napi_status::napi_ok) {
LBSLOGI(FENCE_EXTENSION, "Bind native context to js context error");
}
const char *moduleName = "JsFenceExtensionContext";
AbilityRuntime::BindNativeFunction(
env, objValue, "startAbility", moduleName, JsFenceExtensionContext::StartAbility);
LBSLOGI(FENCE_EXTENSION, "End");
return objValue;
}
napi_value JsFenceExtensionContext::OnStartAbility(napi_env env, AbilityRuntime::NapiCallbackInfo &info)
{
LBSLOGI(FENCE_EXTENSION, "OnStartAbility");
if (info.argc < ARGC_ONE) {
LBSLOGE(FENCE_EXTENSION, "invalid argc");
AbilityRuntime::ThrowTooFewParametersError(env);
return AbilityRuntime::CreateJsUndefined(env);
}
AAFwk::Want want;
if (!AppExecFwk::UnwrapWant(env, info.argv[ARGC_ZERO], want)) {
AbilityRuntime::ThrowInvalidParamError(env, "Parse param want failed, must be a Want.");
return AbilityRuntime::CreateJsUndefined(env);
}
auto isStartService = want.GetBoolParam("startService", true);
if (isStartService) {
return JsFenceExtensionContext::OnStartExtensionAbility(want, env, info);
}
LBSLOGE(FENCE_EXTENSION, "not support start ui ability");
AbilityRuntime::ThrowInvalidParamError(env, "Not support start ui ability.");
return AbilityRuntime::CreateJsUndefined(env);
}
napi_value JsFenceExtensionContext::OnStartExtensionAbility(
AAFwk::Want want, napi_env env, AbilityRuntime::NapiCallbackInfo &info)
{
LBSLOGI(FENCE_EXTENSION, "OnStartExtensionAbility");
AbilityRuntime::NapiAsyncTask::CompleteCallback complete =
[weak = context_, want](napi_env env, AbilityRuntime::NapiAsyncTask &task, int32_t status) {
auto context = weak.lock();
if (!context) {
LBSLOGW(FENCE_EXTENSION, "context is released");
task.Reject(env, CreateJsError(env, AbilityRuntime::AbilityErrorCode::ERROR_CODE_INVALID_CONTEXT));
return;
}
auto innerErrorCode = context->StartServiceExtensionAbility(want);
if (innerErrorCode == 0) {
task.Resolve(env, AbilityRuntime::CreateJsUndefined(env));
} else {
task.Reject(env, AbilityRuntime::CreateJsErrorByNativeErr(env, innerErrorCode));
}
};
napi_value lastParam = (info.argc <= ARGC_ONE) ? nullptr : info.argv[ARGC_ONE];
napi_value result = nullptr;
AbilityRuntime::NapiAsyncTask::ScheduleHighQos("JsFenceExtensionContext::OnStartExtensionAbility",
env,
CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
return result;
}
void JsFenceExtensionContext::Finalizer(::napi_env env, void *data, void *hint)
{
std::unique_ptr<JsFenceExtensionContext>(static_cast<JsFenceExtensionContext *>(data));
}
} // namespace Location
} // namespace OHOS

View File

@ -58,6 +58,11 @@ const int MODE_MS_BASED = 2;
const int MODE_MS_ASSISTED = 3; const int MODE_MS_ASSISTED = 3;
const int DEFAULT_CALLBACK_WAIT_TIME = 10000; const int DEFAULT_CALLBACK_WAIT_TIME = 10000;
const int COORDINATE_SYSTEM_TYPE_SIZE = 2; const int COORDINATE_SYSTEM_TYPE_SIZE = 2;
// fence extension const start
constexpr const char *EXTENSION_SURVIVE_TIME = "EXTENSION_SURVIVE_TIME";
constexpr const char *EXTENSION_PARAM_KEY_FENCE_ID = "FENCE_ID";
constexpr const char *EXTENSION_PARAM_KEY_FENCE_EVENT = "FENCE_EVENT";
// fence extension const end
enum { enum {
SCENE_UNSET = 0x0300, SCENE_UNSET = 0x0300,
@ -128,6 +133,27 @@ enum LocationErrCode {
ERRCODE_GEOFENCE_INCORRECT_ID = 3301602 /* Failed to delete a geofence due to an incorrect ID. */ ERRCODE_GEOFENCE_INCORRECT_ID = 3301602 /* Failed to delete a geofence due to an incorrect ID. */
}; };
enum FenceExtensionErrCode {
EXTENSION_SUCCESS = 0,
EXTENSION_IS_NOT_EXIST,
CONNECT_ABILITY_FAILED,
CONNECT_ABILITY_TIMEOUT,
CONNECT_PROXY_IS_NULL,
CONNECT_EXTENSION_PROXY_IS_NULL,
PARAM_ERROR,
NOT_FOUND_ABILITY_BY_TID,
DISCONNECT_ABILITY_FAILED,
DISCONNECT_ABILITY_TIMEOUT,
EXTENSION_REMOTE_WRITE_FAILED,
EXTENSION_REMOTE_SEND_FAILED,
EXTENSION_REMOTE_STUB_IS_NULL,
EXTENSION_JS_OBJ_IS_NULL,
EXTENSION_JS_RUNTIME_IS_NULL,
EXTENSION_JS_NOT_FOUND_METHOD,
EXTENSION_JS_CREATE_PARAM_ERROR,
EXTENSION_JS_CALL_FAILED
};
enum SatelliteConstellation { enum SatelliteConstellation {
SV_CONSTELLATION_CATEGORY_UNKNOWN = 0, SV_CONSTELLATION_CATEGORY_UNKNOWN = 0,
SV_CONSTELLATION_CATEGORY_GPS, SV_CONSTELLATION_CATEGORY_GPS,

View File

@ -0,0 +1,39 @@
/*
* Copyright (C) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_FENCEEXTENSION_IREMOTE_FENCE_EXTENSION_H
#define OHOS_FENCEEXTENSION_IREMOTE_FENCE_EXTENSION_H
#include "iremote_broker.h"
#include "constant_definition.h"
#include <map>
namespace OHOS {
namespace Location {
class IRemoteFenceExtension : public IRemoteBroker {
public:
DECLARE_INTERFACE_DESCRIPTOR(u"ohos.FenceExtension.IRemoteFenceExtension");
enum { ON_FENCE_STATUS_CHANGE = 0 };
/**
* @brief called back when geofence status is change.
*
* @param extraData other extra data
*/
virtual FenceExtensionErrCode OnFenceStatusChange(std::map<std::string, std::string> extraData) = 0;
};
} // namespace Location
} // namespace OHOS
#endif // OHOS_FENCEEXTENSION_IREMOTE_FENCE_EXTENSION_H

View File

@ -119,6 +119,9 @@ static constexpr OHOS::HiviewDFX::HiLogLabel GEOFENCE_SDK = {
}; };
static constexpr OHOS::HiviewDFX::HiLogLabel GEOFENCE_SDK_TEST = {LOG_CORE, LOCATION_LOG_DOMAIN, "GeofenceSdkTest"}; static constexpr OHOS::HiviewDFX::HiLogLabel GEOFENCE_SDK_TEST = {LOG_CORE, LOCATION_LOG_DOMAIN, "GeofenceSdkTest"};
static constexpr OHOS::HiviewDFX::HiLogLabel LOCATION_CAPI = {LOG_CORE, LOCATION_LOG_DOMAIN, "LocationCapi"}; static constexpr OHOS::HiviewDFX::HiLogLabel LOCATION_CAPI = {LOG_CORE, LOCATION_LOG_DOMAIN, "LocationCapi"};
static constexpr OHOS::HiviewDFX::HiLogLabel FENCE_EXTENSION = {
LOG_CORE, LOCATION_LOG_DOMAIN, "FenceExtension"
};
} // namespace Location } // namespace Location
} // namespace OHOS } // namespace OHOS
#endif // LOCATION_LOG_H #endif // LOCATION_LOG_H