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 #include "napi_preferences_error.h"
20
21 namespace OHOS {
22 namespace PreferencesJsKit {
IsStageContext(napi_env env,napi_value value,bool & isStageMode)23 napi_status JSAbility::IsStageContext(napi_env env, napi_value value, bool &isStageMode)
24 {
25 napi_status status = AbilityRuntime::IsStageContext(env, value, isStageMode);
26 return status;
27 }
28
GetContextInfo(napi_env env,napi_value value,const std::string & dataGroupId,const bool & isStageMode,ContextInfo & contextInfo)29 int JSAbility::GetContextInfo(napi_env env, napi_value value, const std::string &dataGroupId, const bool &isStageMode,
30 ContextInfo &contextInfo)
31 {
32 if (isStageMode) {
33 auto stageContext = AbilityRuntime::GetStageModeContext(env, value);
34 if (stageContext == nullptr) {
35 LOG_ERROR("failed to get stage mode context.");
36 return E_INNER_ERROR;
37 }
38
39 int errcode = stageContext->GetSystemPreferencesDir(dataGroupId, false, contextInfo.preferencesDir);
40 if (errcode != 0) {
41 return E_DATA_GROUP_ID_INVALID;
42 }
43 contextInfo.bundleName = stageContext->GetBundleName();
44 return OK;
45 }
46
47 if (!dataGroupId.empty()) {
48 return E_NOT_STAGE_MODE;
49 }
50
51 auto ability = AbilityRuntime::GetCurrentAbility(env);
52 if (ability == nullptr) {
53 LOG_ERROR("failed to get current ability.");
54 return E_INNER_ERROR;
55 }
56
57 auto abilityContext = ability->GetAbilityContext();
58 if (ability == nullptr) {
59 LOG_ERROR("failed to get ability context.");
60 return E_INNER_ERROR;
61 }
62 abilityContext->GetSystemPreferencesDir("", false, contextInfo.preferencesDir);
63 return OK;
64 }
65 } // namespace PreferencesJsKit
66 } // namespace OHOS
67