1 /*
2  * Copyright (c) 2020-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 #include "dft_impl.h"
17 
18 #if ((OHOS_ACELITE_PRODUCT_WATCH == 1) || (FEATURE_CUSTOM_ENTRY_PAGE == 1))
19 #include "ace_log.h"
20 #include "js_app_context.h"
21 
22 namespace OHOS {
23 namespace ACELite {
GetInstance()24 DftImpl *DftImpl::GetInstance()
25 {
26     static DftImpl instance;
27     return &instance;
28 }
29 
GetPagePath()30 char *DftImpl::GetPagePath()
31 {
32     const char * const pagePath = JsAppContext::GetInstance()->GetCurrentJsPath();
33     if (pagePath == nullptr) {
34         return nullptr;
35     }
36 
37     // remove suffix from js
38     // pages/index/index.js -> pages/index/index
39     size_t suffixLen = 3;
40     size_t len = strlen(pagePath) - suffixLen;
41     char *path = static_cast<char *>(ace_malloc(len + 1));
42     if (path == nullptr) {
43         HILOG_ERROR(HILOG_MODULE_ACE, "malloc buffer for path failed.");
44         return nullptr;
45     }
46     if (memcpy_s(path, len, pagePath, len) != EOK) {
47         ace_free(path);
48         path = nullptr;
49         return nullptr;
50     }
51     path[len] = '\0';
52     return path;
53 }
54 
RegisterPageReplaced(PageReplacedCallback handler)55 void DftImpl::RegisterPageReplaced(PageReplacedCallback handler)
56 {
57     pageReplacedCallback_ = handler;
58 }
59 
RegisterPageChangedCallback(PageInfoFunc pageInfoFunc)60 void DftImpl::RegisterPageChangedCallback(PageInfoFunc pageInfoFunc)
61 {
62     pageInfoFunc_ = pageInfoFunc;
63 }
64 
CallbackPageReplaced(int state,const char * param)65 void DftImpl::CallbackPageReplaced(int state, const char *param)
66 {
67     if (pageReplacedCallback_ != nullptr) {
68         char *currentPath = DftImpl::GetPagePath();
69         pageReplacedCallback_(currentPath, state);
70         ACE_FREE(currentPath);
71     }
72     if (pageInfoFunc_ == nullptr) {
73         return;
74     }
75     Param value;
76     char *paramValue = nullptr;
77     jerry_value_t global = jerry_get_global_object();
78     if (jerryx_has_property_str(global, ROUTER_PAGE)) {
79         jerry_value_t param = jerryx_get_property_str(global, ROUTER_PAGE);
80         jerry_value_t paramJson = jerry_json_stringify(param);
81         paramValue = MallocStringOf(paramJson);
82         ReleaseJerryValue(paramJson, param, VA_ARG_END_FLAG);
83     }
84     jerry_release_value(global);
85     value.routerParam = paramValue;
86     value.path = JsAppContext::GetInstance()->GetCurrentJsPath();
87     value.routerPath = DftImpl::GetPagePath();
88     pageInfoFunc_(value);
89     ACE_FREE(paramValue);
90 }
91 } // namespace ACELite
92 } // namespace OHOS
93 #endif
94