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 "bundle_mgr_service.h"
17
18 #include "bundle_service_interface.h"
19 #include "bundlems_log.h"
20 #include "gt_bundle_manager_service.h"
21 #include "ohos_init.h"
22 #include "samgr_lite.h"
23
24 namespace OHOS {
25 const int STACK_SIZE = 0x1C00;
26 const int QUEUE_SIZE = 20;
27 extern Bmsbuff *g_bmsbuff;
28
BundleMgrService()29 BundleMgrService::BundleMgrService() : Service(), identity_()
30 {
31 this->Service::GetName = BundleMgrService::GetServiceName;
32 this->Service::Initialize = BundleMgrService::ServiceInitialize;
33 this->Service::MessageHandle = BundleMgrService::ServiceMessageHandle;
34 this->Service::GetTaskConfig = BundleMgrService::GetServiceTaskConfig;
35 }
36
Init()37 static void Init()
38 {
39 SamgrLite *sm = SAMGR_GetInstance();
40 CHECK_NULLPTR_RETURN(sm, "BundleManagerService", "get samgr error");
41 #ifdef __LITEOS_M__
42 sm->RegisterService(BundleMgrService::GetInstance());
43 #else
44 BOOL result = sm->RegisterService(BundleMgrService::GetInstance());
45 PRINTI("BundleManagerService", "bms starts %{public}s", result ? "successfully" : "unsuccessfully");
46 #endif
47 }
48 SYSEX_SERVICE_INIT(Init);
49
GetServiceName(Service * service)50 const char *BundleMgrService::GetServiceName(Service *service)
51 {
52 (void)service;
53 return BMS_SERVICE;
54 }
55
ServiceInitialize(Service * service,Identity identity)56 BOOL BundleMgrService::ServiceInitialize(Service *service, Identity identity)
57 {
58 if (service == nullptr) {
59 return FALSE;
60 }
61 BundleMgrService *bundleManagerService = static_cast<BundleMgrService *>(service);
62 bundleManagerService->identity_ = identity;
63 Request request = {
64 .msgId = BMS_SCAN_PACKAGE_MSG,
65 .len = 0,
66 .data = nullptr,
67 .msgValue = 0,
68 };
69 (void) SAMGR_SendRequest(bundleManagerService->GetIdentity(), &request, nullptr);
70 return TRUE;
71 }
72
GetIdentity()73 Identity *BundleMgrService::GetIdentity()
74 {
75 return &identity_;
76 }
77
ServiceMessageHandle(Service * service,Request * request)78 BOOL BundleMgrService::ServiceMessageHandle(Service *service, Request *request)
79 {
80 if (request == nullptr) {
81 return FALSE;
82 }
83 if (request->msgId == BMS_INSTALL_MSG && g_bmsbuff != nullptr) {
84 OHOS::GtManagerService::GetInstance().Install(g_bmsbuff->bundleParameter, nullptr,
85 g_bmsbuff->bundleInstallerCallback);
86 } else if (request->msgId == BMS_UNINSTALL_MSG && g_bmsbuff != nullptr) {
87 OHOS::GtManagerService::GetInstance().Uninstall(g_bmsbuff->bundleParameter, nullptr,
88 g_bmsbuff->bundleInstallerCallback);
89 } else if (request->msgId == BMS_SCAN_PACKAGE_MSG) {
90 OHOS::GtManagerService::GetInstance().ScanPackages();
91 } else if (request->msgId == BMS_REGISTER_CALLBACK_MSG && g_bmsbuff != nullptr) {
92 OHOS::GtManagerService::GetInstance().RegisterInstallerCallback(g_bmsbuff->bundleInstallerCallback);
93 } else {
94 return FALSE;
95 }
96 return TRUE;
97 }
98
GetServiceTaskConfig(Service * service)99 TaskConfig BundleMgrService::GetServiceTaskConfig(Service *service)
100 {
101 #ifdef __LITEOS_M__
102 TaskConfig config = {LEVEL_HIGH, PRI_ABOVE_NORMAL, STACK_SIZE, QUEUE_SIZE, SINGLE_TASK};
103 #else
104 TaskConfig config = {LEVEL_HIGH, PRI_BELOW_NORMAL, STACK_SIZE, QUEUE_SIZE, SINGLE_TASK};
105 #endif
106 return config;
107 }
108 } // namespace OHOS
109