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 "logger.h"
20
21 namespace OHOS {
22 namespace ObjectStore {
Context(std::shared_ptr<AbilityRuntime::Context> stageContext)23 Context::Context(std::shared_ptr<AbilityRuntime::Context> stageContext)
24 {
25 bundleName_ = stageContext->GetBundleName();
26 LOG_DEBUG("Stage: bundle:%{public}s", bundleName_.c_str());
27 }
28
Context(std::shared_ptr<AbilityRuntime::AbilityContext> abilityContext)29 Context::Context(std::shared_ptr<AbilityRuntime::AbilityContext> abilityContext)
30 {
31 bundleName_ = abilityContext->GetBundleName();
32 LOG_DEBUG("FA: bundle:%{public}s", bundleName_.c_str());
33 }
34
GetBundleName()35 std::string Context::GetBundleName()
36 {
37 return bundleName_;
38 }
39
GetContext(napi_env env,napi_value value)40 std::shared_ptr<Context> JSAbility::GetContext(napi_env env, napi_value value)
41 {
42 bool mode = false;
43 AbilityRuntime::IsStageContext(env, value, mode);
44 if (mode) {
45 LOG_DEBUG("Get context as stage mode.");
46 auto stageContext = AbilityRuntime::GetStageModeContext(env, value);
47 if (stageContext == nullptr) {
48 LOG_ERROR("GetStageModeContext failed.");
49 return nullptr;
50 }
51 return std::make_shared<Context>(stageContext);
52 }
53
54 LOG_DEBUG("Get context as feature ability mode.");
55 auto ability = AbilityRuntime::GetCurrentAbility(env);
56 if (ability == nullptr) {
57 LOG_ERROR("GetCurrentAbility failed.");
58 return nullptr;
59 }
60 auto abilityContext = ability->GetAbilityContext();
61 if (abilityContext == nullptr) {
62 LOG_ERROR("GetAbilityContext failed.");
63 return nullptr;
64 }
65 return std::make_shared<Context>(abilityContext);
66 }
67 } // namespace ObjectStore
68 } // namespace OHOS