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