1 /*
2 * Copyright (c) 2023 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
17 #include "napi_env.h"
18 #include "calendar_log.h"
19 #include "calendar_env.h"
20 #include "ability.h"
21 #include "ability_context.h"
22 #include "context.h"
23 #include "napi_base_context.h"
24 #include "data_share_helper_manager.h"
25 #include "datashare_helper.h"
26 #include "datashare_predicates.h"
27 #include "accesstoken_kit.h"
28
29 #include "ipc_skeleton.h"
30
31 namespace OHOS::CalendarApi {
32
GetContext(napi_env env,napi_value value)33 napi_status CalendarEnvNapi::GetContext(napi_env env, napi_value value)
34 {
35 bool isStageMode = false;
36 auto status = AbilityRuntime::IsStageContext(env, value, isStageMode);
37 if (status != napi_ok || !isStageMode) {
38 LOG_ERROR("No support FA Model; No support get calendarMgr in worker thread");
39 return napi_generic_failure;
40 }
41 m_context = OHOS::AbilityRuntime::GetStageModeContext(env, value);
42 if (m_context == nullptr) {
43 LOG_ERROR("GetStageModeContext contextRtm == nullptr.");
44 return napi_generic_failure;
45 }
46 return napi_ok;
47 }
48
Init(napi_env env,napi_value value)49 void CalendarEnvNapi::Init(napi_env env, napi_value value)
50 {
51 const std::string CALENDAR_DATA_URI = "datashare:///calendardata";
52 const std::string CALENDAR_DATA_WHOLE_URI = "datashare:///calendardata_whole";
53 const std::string PERMISSION_NAME = "ohos.permission.READ_WHOLE_CALENDAR";
54 if (hasInited) {
55 return;
56 }
57 LOG_INFO("CalendarEnvNapi Init.");
58 auto status = GetContext(env, value);
59 if (status != napi_ok) {
60 return;
61 }
62 std::string bundleName = m_context->GetBundleName();
63 uint64_t tokenId = IPCSkeleton::GetSelfTokenID();
64 CalendarEnv::GetInstance().Init(bundleName, tokenId);
65
66 int32_t ret = Security::AccessToken::AccessTokenKit::VerifyAccessToken(IPCSkeleton::GetCallingTokenID(),
67 PERMISSION_NAME);
68 LOG_INFO("verify access result=%{public}d", ret);
69 if (ret == Security::AccessToken::PERMISSION_GRANTED) {
70 auto dataShareHelper = DataShare::DataShareHelper::Creator(m_context->GetToken(), CALENDAR_DATA_WHOLE_URI);
71 LOG_INFO("dataShareHelper high create result=%{public}d", dataShareHelper != nullptr);
72 DataShareHelperManager::GetInstance().SetDataShareHelper(dataShareHelper);
73 } else {
74 auto dataShareHelper = DataShare::DataShareHelper::Creator(m_context->GetToken(), CALENDAR_DATA_URI);
75 LOG_INFO("dataShareHelper low create result=%{public}d", dataShareHelper != nullptr);
76 DataShareHelperManager::GetInstance().SetDataShareHelper(dataShareHelper);
77 }
78 hasInited = true;
79 }
80 };
81