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