diff --git a/frameworks/include/base/product_adapter.h b/frameworks/include/base/product_adapter.h index adb928a..c068192 100644 --- a/frameworks/include/base/product_adapter.h +++ b/frameworks/include/base/product_adapter.h @@ -120,6 +120,7 @@ public: static void SetScreenSize(uint16_t width, uint16_t height); static void RegSetScreenOnVisibleHandler(SetScreenOnVisibleHandler handler); static void RegExtraPresetModulesHook(ExtraPresetModulesHook hook); + static void ConfigPrivateDataRootPath(const char *appDataRoot); // wrapper functions, for ace internal calling static void PrintEventTrace(uint8_t info2, uint8_t info3, uint8_t info4); @@ -137,6 +138,7 @@ public: static bool SetScreenOnVisible(bool visible); static void LoadExtraPresetModules(); static void UnloadExtraPresetModules(); + static const char *GetPrivateDataRootPath(); }; } // namespace ACELite } // namespace OHOS diff --git a/frameworks/src/core/base/product_adapter.cpp b/frameworks/src/core/base/product_adapter.cpp index 83ab507..4cf2dbd 100644 --- a/frameworks/src/core/base/product_adapter.cpp +++ b/frameworks/src/core/base/product_adapter.cpp @@ -20,6 +20,7 @@ #include "js_async_work.h" #include "message_queue_utils.h" #include "module_manager.h" +#include "securec.h" namespace OHOS { namespace ACELite { @@ -59,6 +60,10 @@ static uint8_t g_defaultFontSize = 30; static uint16_t g_screenWidth = 454; static uint16_t g_screenHeight = 454; +// default app private data root path +const static char *DEFAULT_APP_DATA_PATH = "user/ace/data/"; +static const char *g_defaultDataRootPath = DEFAULT_APP_DATA_PATH; + // indicating if the ace application is on forground static bool g_isRenderTickAcceptable = false; @@ -253,5 +258,22 @@ void ProductAdapter::UnloadExtraPresetModules() g_extraPresetModulesHooks.unloadingHandler(); } } + +void ProductAdapter::ConfigPrivateDataRootPath(const char *appDataRoot) +{ + if (appDataRoot == nullptr) { + return; + } + size_t pathLen = strlen(appDataRoot); + if (pathLen == 0 || pathLen >= UINT8_MAX) { + return; + } + g_defaultDataRootPath = appDataRoot; +} + +const char *ProductAdapter::GetPrivateDataRootPath() +{ + return g_defaultDataRootPath; +} } // namespace ACELite } // namespace OHOS diff --git a/frameworks/src/core/base/string_util.cpp b/frameworks/src/core/base/string_util.cpp index 5650417..e1ebae3 100755 --- a/frameworks/src/core/base/string_util.cpp +++ b/frameworks/src/core/base/string_util.cpp @@ -58,13 +58,14 @@ char *StringUtil::Slice(const char *sequence, const int32_t start, const int32_t return nullptr; } uint32_t size = strlen(sequence); - if (size == 0) { + if (size == 0 || size >= UINT16_MAX) { return nullptr; } int32_t startIdx = (start < 0) ? (start + size) : start; startIdx = (startIdx < 0) ? 0 : startIdx; int32_t endIdx = (end < 0) ? (end + size) : end; - if (startIdx < endIdx || endIdx < 0) { + endIdx = (endIdx > static_cast(size)) ? size : endIdx; + if (startIdx > endIdx || endIdx < 0) { return nullptr; } int32_t diffSize = endIdx - startIdx; diff --git a/frameworks/src/core/components/test/unittest/common/image_src_path_tdd_test.cpp b/frameworks/src/core/components/test/unittest/common/image_src_path_tdd_test.cpp index f60bd04..dbceca0 100755 --- a/frameworks/src/core/components/test/unittest/common/image_src_path_tdd_test.cpp +++ b/frameworks/src/core/components/test/unittest/common/image_src_path_tdd_test.cpp @@ -83,11 +83,12 @@ HWTEST_F(ImageSrcPathTddTest, PrivateDataPathSupport004, TestSize.Level0) /** * @tc.steps: step1. pass empty string for handling */ + JsAppContext::GetInstance()->SetCurrentAbilityInfo("", "com.example.test", 0); char *resourcePath = JsAppContext::GetInstance()->GetResourcePath("internal://app/abc.png"); /** * @tc.steps: step2. check if handling failed */ - EXPECT_EQ(resourcePath, nullptr); + EXPECT_STREQ(resourcePath, "com.example.test/abc.png"); } /** @@ -100,11 +101,12 @@ HWTEST_F(ImageSrcPathTddTest, PrivateDataPathSupport005, TestSize.Level0) /** * @tc.steps: step1. pass empty string for handling */ + JsAppContext::GetInstance()->SetCurrentAbilityInfo("", "com.example.test", 0); char *resourcePath = JsAppContext::GetInstance()->GetResourcePath("internal://app/testa/testb/testc/abc.png"); /** * @tc.steps: step2. check if handling failed */ - EXPECT_EQ(resourcePath, nullptr); + EXPECT_STREQ(resourcePath, "com.example.test/testa/testb/testc/abc.png"); } /** diff --git a/frameworks/src/core/context/js_app_context.cpp b/frameworks/src/core/context/js_app_context.cpp index f1c6555..ba67b70 100755 --- a/frameworks/src/core/context/js_app_context.cpp +++ b/frameworks/src/core/context/js_app_context.cpp @@ -28,6 +28,7 @@ #include "js_app_environment.h" #include "js_profiler.h" #include "platform_adapter.h" +#include "product_adapter.h" #include "securec.h" #include "string_util.h" #include "task_manager.h" @@ -40,10 +41,6 @@ namespace OHOS { namespace ACELite { constexpr char URI_PREFIX_DATA[] = "internal://app"; constexpr uint8_t URI_PREFIX_DATA_LENGTH = 14; -#ifdef OHOS_ACELITE_PRODUCT_WATCH -constexpr char APP_DATA_DIR_PATH[] = "app/ace/data/"; -constexpr uint8_t APP_DATA_DIR_PATH_LENGTH = 13; -#endif void JsAppContext::ClearContext() { // reset current ability path and uuid @@ -318,25 +315,14 @@ char *JsAppContext::GetResourcePath(const char *uri) const return nullptr; } #ifdef OHOS_ACELITE_PRODUCT_WATCH - uint16_t dataPathSize = APP_DATA_DIR_PATH_LENGTH + strlen(currentBundleName_) + size - URI_PREFIX_DATA_LENGTH; - char *dataPath = StringUtil::Malloc(dataPathSize); - if (dataPath == nullptr) { - HILOG_ERROR(HILOG_MODULE_ACE, "fail to get resource path."); - ACE_FREE(path); - return nullptr; - } - if (sprintf_s(dataPath, dataPathSize + 1, "%s%s%s", APP_DATA_DIR_PATH, currentBundleName_, path) < 0) { - HILOG_ERROR(HILOG_MODULE_ACE, "fail to get resource path."); - ACE_FREE(path); - ACE_FREE(dataPath); - return nullptr; - } + // no GetDataPath API provided on watch, contact the path by the product configuration insteadly + char *relocatedPath = ProcessResourcePathByConfiguration(size, path); #else const char *dataPath = GetDataPath(); -#endif + if (dataPath == nullptr || strlen(dataPath) == 0) { + dataPath = currentBundleName_; + } char *relocatedPath = RelocateResourceFilePath(dataPath, path); -#ifdef OHOS_ACELITE_PRODUCT_WATCH - ACE_FREE(dataPath); #endif ACE_FREE(path); return relocatedPath; @@ -344,6 +330,36 @@ char *JsAppContext::GetResourcePath(const char *uri) const return RelocateResourceFilePath(currentAbilityPath_, uri); } +char *JsAppContext::ProcessResourcePathByConfiguration(size_t origUriLength, const char *slicedFilePath) const +{ + const char *appDataRoot = ProductAdapter::GetPrivateDataRootPath(); + if (appDataRoot == nullptr || origUriLength == 0 || slicedFilePath == nullptr) { + return nullptr; + } + size_t rootPathLen = strlen(appDataRoot); + if (rootPathLen == 0) { + return nullptr; + } + size_t dataPathSize = rootPathLen + strlen(currentBundleName_) + origUriLength - URI_PREFIX_DATA_LENGTH + 1; + char *dataPath = StringUtil::Malloc(dataPathSize); + if (dataPath == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "fail to malloc data path buffer."); + return nullptr; + } + const char *fmtStr = "%s%s"; + if (appDataRoot[rootPathLen - 1] != RESOURCE_SEPARATOR) { + fmtStr = "%s/%s"; + } + if (sprintf_s(dataPath, dataPathSize + 1, fmtStr, appDataRoot, currentBundleName_) < 0) { + HILOG_ERROR(HILOG_MODULE_ACE, "fail to get resource path."); + ACE_FREE(dataPath); + return nullptr; + } + char *relocatedPath = RelocateResourceFilePath(dataPath, slicedFilePath); + ACE_FREE(dataPath); + return relocatedPath; +} + void JsAppContext::LoadApiVersion() { #ifdef FEATURE_API_VERSION diff --git a/frameworks/src/core/context/js_app_context.h b/frameworks/src/core/context/js_app_context.h index ce93ccb..4c13f23 100755 --- a/frameworks/src/core/context/js_app_context.h +++ b/frameworks/src/core/context/js_app_context.h @@ -163,6 +163,7 @@ private: */ char *EvaluateFile(bool &isSnapshotMode, uint32_t &outLength, char *fullPathPath, size_t fullPathLength) const; void CheckSnapshotVersion(const char *bcFileContent, uint32_t contentLength) const; + char *ProcessResourcePathByConfiguration(size_t origUriLength, const char *filePath) const; char *currentBundleName_ = nullptr; char *currentAbilityPath_ = nullptr; char *currentJsPath_ = nullptr;