10fbfc30aSopenharmony_ci/* 20fbfc30aSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 30fbfc30aSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 40fbfc30aSopenharmony_ci * you may not use this file except in compliance with the License. 50fbfc30aSopenharmony_ci * You may obtain a copy of the License at 60fbfc30aSopenharmony_ci * 70fbfc30aSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 80fbfc30aSopenharmony_ci * 90fbfc30aSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 100fbfc30aSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 110fbfc30aSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 120fbfc30aSopenharmony_ci * See the License for the specific language governing permissions and 130fbfc30aSopenharmony_ci * limitations under the License. 140fbfc30aSopenharmony_ci */ 150fbfc30aSopenharmony_ci 160fbfc30aSopenharmony_ci 170fbfc30aSopenharmony_ci#include "napi_env.h" 180fbfc30aSopenharmony_ci#include "calendar_log.h" 190fbfc30aSopenharmony_ci#include "calendar_env.h" 200fbfc30aSopenharmony_ci#include "ability.h" 210fbfc30aSopenharmony_ci#include "ability_context.h" 220fbfc30aSopenharmony_ci#include "context.h" 230fbfc30aSopenharmony_ci#include "napi_base_context.h" 240fbfc30aSopenharmony_ci#include "data_share_helper_manager.h" 250fbfc30aSopenharmony_ci#include "datashare_helper.h" 260fbfc30aSopenharmony_ci#include "datashare_predicates.h" 270fbfc30aSopenharmony_ci#include "accesstoken_kit.h" 280fbfc30aSopenharmony_ci 290fbfc30aSopenharmony_ci#include "ipc_skeleton.h" 300fbfc30aSopenharmony_ci 310fbfc30aSopenharmony_cinamespace OHOS::CalendarApi { 320fbfc30aSopenharmony_ci 330fbfc30aSopenharmony_cinapi_status CalendarEnvNapi::GetContext(napi_env env, napi_value value) 340fbfc30aSopenharmony_ci{ 350fbfc30aSopenharmony_ci bool isStageMode = false; 360fbfc30aSopenharmony_ci auto status = AbilityRuntime::IsStageContext(env, value, isStageMode); 370fbfc30aSopenharmony_ci if (status != napi_ok || !isStageMode) { 380fbfc30aSopenharmony_ci LOG_ERROR("No support FA Model; No support get calendarMgr in worker thread"); 390fbfc30aSopenharmony_ci return napi_generic_failure; 400fbfc30aSopenharmony_ci } 410fbfc30aSopenharmony_ci m_context = OHOS::AbilityRuntime::GetStageModeContext(env, value); 420fbfc30aSopenharmony_ci if (m_context == nullptr) { 430fbfc30aSopenharmony_ci LOG_ERROR("GetStageModeContext contextRtm == nullptr."); 440fbfc30aSopenharmony_ci return napi_generic_failure; 450fbfc30aSopenharmony_ci } 460fbfc30aSopenharmony_ci return napi_ok; 470fbfc30aSopenharmony_ci} 480fbfc30aSopenharmony_ci 490fbfc30aSopenharmony_civoid CalendarEnvNapi::Init(napi_env env, napi_value value) 500fbfc30aSopenharmony_ci{ 510fbfc30aSopenharmony_ci const std::string CALENDAR_DATA_URI = "datashare:///calendardata"; 520fbfc30aSopenharmony_ci const std::string CALENDAR_DATA_WHOLE_URI = "datashare:///calendardata_whole"; 530fbfc30aSopenharmony_ci const std::string PERMISSION_NAME = "ohos.permission.READ_WHOLE_CALENDAR"; 540fbfc30aSopenharmony_ci if (hasInited) { 550fbfc30aSopenharmony_ci return; 560fbfc30aSopenharmony_ci } 570fbfc30aSopenharmony_ci LOG_INFO("CalendarEnvNapi Init."); 580fbfc30aSopenharmony_ci auto status = GetContext(env, value); 590fbfc30aSopenharmony_ci if (status != napi_ok) { 600fbfc30aSopenharmony_ci return; 610fbfc30aSopenharmony_ci } 620fbfc30aSopenharmony_ci std::string bundleName = m_context->GetBundleName(); 630fbfc30aSopenharmony_ci uint64_t tokenId = IPCSkeleton::GetSelfTokenID(); 640fbfc30aSopenharmony_ci CalendarEnv::GetInstance().Init(bundleName, tokenId); 650fbfc30aSopenharmony_ci 660fbfc30aSopenharmony_ci int32_t ret = Security::AccessToken::AccessTokenKit::VerifyAccessToken(IPCSkeleton::GetCallingTokenID(), 670fbfc30aSopenharmony_ci PERMISSION_NAME); 680fbfc30aSopenharmony_ci LOG_INFO("verify access result=%{public}d", ret); 690fbfc30aSopenharmony_ci if (ret == Security::AccessToken::PERMISSION_GRANTED) { 700fbfc30aSopenharmony_ci auto dataShareHelper = DataShare::DataShareHelper::Creator(m_context->GetToken(), CALENDAR_DATA_WHOLE_URI); 710fbfc30aSopenharmony_ci LOG_INFO("dataShareHelper high create result=%{public}d", dataShareHelper != nullptr); 720fbfc30aSopenharmony_ci DataShareHelperManager::GetInstance().SetDataShareHelper(dataShareHelper); 730fbfc30aSopenharmony_ci } else { 740fbfc30aSopenharmony_ci auto dataShareHelper = DataShare::DataShareHelper::Creator(m_context->GetToken(), CALENDAR_DATA_URI); 750fbfc30aSopenharmony_ci LOG_INFO("dataShareHelper low create result=%{public}d", dataShareHelper != nullptr); 760fbfc30aSopenharmony_ci DataShareHelperManager::GetInstance().SetDataShareHelper(dataShareHelper); 770fbfc30aSopenharmony_ci } 780fbfc30aSopenharmony_ci hasInited = true; 790fbfc30aSopenharmony_ci} 800fbfc30aSopenharmony_ci}; 81