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
28 #include "ipc_skeleton.h"
29
30 namespace OHOS::CalendarApi {
31
GetContext(napi_env env,napi_value value)32 napi_status CalendarEnvNapi::GetContext(napi_env env, napi_value value)
33 {
34 bool isStageMode = false;
35 auto status = AbilityRuntime::IsStageContext(env, value, isStageMode);
36 if (status != napi_ok || !isStageMode) {
37 LOG_ERROR("No support FA Model; No support get calendarMgr in worker thread");
38 return napi_generic_failure;
39 }
40 m_context = OHOS::AbilityRuntime::GetStageModeContext(env, value);
41 if (m_context == nullptr) {
42 LOG_ERROR("GetStageModeContext contextRtm == nullptr.");
43 return napi_generic_failure;
44 }
45 return napi_ok;
46 }
47
Init(napi_env env,napi_value value)48 void CalendarEnvNapi::Init(napi_env env, napi_value value)
49 {
50 const std::string CALENDAR_DATA_URI = "datashare:///calendardata";
51 if (hasInited) {
52 return;
53 }
54 LOG_INFO("CalendarEnvNapi Init.");
55 auto status = GetContext(env, value);
56 if (status != napi_ok) {
57 return;
58 }
59 std::string bundleName = m_context->GetBundleName();
60 uint64_t tokenId = IPCSkeleton::GetSelfTokenID();
61 CalendarEnv::GetInstance().Init(bundleName, tokenId);
62 auto dataShareHelper = DataShare::DataShareHelper::Creator(m_context->GetToken(), CALENDAR_DATA_URI);
63 DataShareHelperManager::GetInstance().SetDataShareHelper(dataShareHelper);
64 hasInited = true;
65 }
66 };
67