• 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 
16 #include "js_ability.h"
17 
18 #include "extension_context.h"
19 #include "js_logger.h"
20 
21 namespace OHOS {
22 namespace PreferencesJsKit {
Context(std::shared_ptr<AbilityRuntime::Context> stageContext)23 Context::Context(std::shared_ptr<AbilityRuntime::Context> stageContext)
24 {
25     preferencesDir_ = stageContext->GetPreferencesDir();
26     auto extensionContext = AbilityRuntime::Context::ConvertTo<AbilityRuntime::ExtensionContext>(stageContext);
27     if (extensionContext != nullptr) {
28         auto abilityInfo = extensionContext->GetAbilityInfo();
29     }
30 }
31 
Context(std::shared_ptr<AbilityRuntime::AbilityContext> abilityContext)32 Context::Context(std::shared_ptr<AbilityRuntime::AbilityContext> abilityContext)
33 {
34     preferencesDir_ = abilityContext->GetPreferencesDir();
35     auto abilityInfo = abilityContext->GetAbilityInfo();
36 }
37 
GetPreferencesDir()38 std::string Context::GetPreferencesDir()
39 {
40     return preferencesDir_;
41 }
42 
CheckContext(napi_env env,napi_callback_info info)43 bool JSAbility::CheckContext(napi_env env, napi_callback_info info)
44 {
45     size_t argc = 1;
46     napi_value args[1] = { 0 };
47     bool mode = false;
48     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
49     napi_status status = AbilityRuntime::IsStageContext(env, args[0], mode);
50     LOG_DEBUG("Check context as stage mode, mode is %{public}d, status is %{public}d", mode, status == napi_ok);
51     return status == napi_ok;
52 }
53 
GetContext(napi_env env,napi_value value)54 std::shared_ptr<Context> JSAbility::GetContext(napi_env env, napi_value value)
55 {
56     bool mode = false;
57     AbilityRuntime::IsStageContext(env, value, mode);
58     if (mode) {
59         LOG_DEBUG("Get context as stage mode.");
60         auto stageContext = AbilityRuntime::GetStageModeContext(env, value);
61         if (stageContext == nullptr) {
62             LOG_ERROR("GetStageModeContext failed.");
63             return nullptr;
64         }
65         return std::make_shared<Context>(stageContext);
66     }
67 
68     LOG_DEBUG("Get context as feature ability mode.");
69     auto ability = AbilityRuntime::GetCurrentAbility(env);
70     if (ability == nullptr) {
71         LOG_ERROR("GetCurrentAbility failed.");
72         return nullptr;
73     }
74     auto abilityContext = ability->GetAbilityContext();
75     if (abilityContext == nullptr) {
76         LOG_ERROR("GetAbilityContext failed.");
77         return nullptr;
78     }
79     return std::make_shared<Context>(abilityContext);
80 }
81 } // namespace PreferencesJsKit
82 } // namespace OHOS
83