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 #include "js_ability.h"
16 
17 #include "extension_context.h"
18 #include "log_print.h"
19 
20 namespace OHOS {
21 namespace PreferencesJsKit {
22 namespace JSAbility {
GetContextMode(napi_env env,napi_value value)23 CONTEXT_MODE GetContextMode(napi_env env, napi_value value)
24 {
25     if (gContextNode == INIT) {
26         bool isStageMode;
27         napi_status status = AbilityRuntime::IsStageContext(env, value, isStageMode);
28         if (status == napi_ok) {
29             gContextNode = isStageMode ? STAGE : FA;
30         }
31         LOG_INFO("set gContextNode: %{public}d, status: %{public}d,", gContextNode, status);
32     }
33     return gContextNode;
34 }
35 
GetContextInfo(napi_env env,napi_value value,const std::string & dataGroupId,ContextInfo & contextInfo)36 std::shared_ptr<JSError> GetContextInfo(napi_env env, napi_value value,
37     const std::string &dataGroupId, ContextInfo &contextInfo)
38 {
39     if (GetContextMode(env, value) == STAGE) {
40         if (auto stageContext = AbilityRuntime::GetStageModeContext(env, value)) {
41             int errcode = stageContext->GetSystemPreferencesDir(dataGroupId, false, contextInfo.preferencesDir);
42             if (errcode != 0) {
43                 LOG_ERROR("GetSystemPreferencesDir fails, rc = %{public}d", errcode);
44                 return std::make_shared<InnerError>(E_DATA_GROUP_ID_INVALID);
45             }
46             contextInfo.bundleName = stageContext->GetBundleName();
47             return nullptr;
48         } else {
49             LOG_ERROR("failed to get the context of the stage model.");
50             return std::make_shared<ParamTypeError>("The context is invalid.");
51         }
52     }
53 
54     if (!dataGroupId.empty()) {
55         LOG_ERROR("dataGroupId should be empty in fa mode");
56         return std::make_shared<InnerError>(E_NOT_STAGE_MODE);
57     }
58 
59     auto ability = AbilityRuntime::GetCurrentAbility(env);
60     if (ability == nullptr) {
61         LOG_ERROR("failed to get current ability.");
62         return std::make_shared<ParamTypeError>("The context is invalid.");
63     }
64 
65     auto abilityContext = ability->GetAbilityContext();
66     if (ability == nullptr) {
67         LOG_ERROR("failed to get ability context.");
68         return std::make_shared<ParamTypeError>("The context is invalid.");
69     }
70     abilityContext->GetSystemPreferencesDir("", false, contextInfo.preferencesDir);
71     return nullptr;
72 }
73 } // namespace JSAbility
74 } // namespace PreferencesJsKit
75 } // namespace OHOS
76