• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 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 "ability_mgr_service.h"
17 
18 #include "ability_errors.h"
19 #ifdef __LITEOS_M__
20 #include "ability_service.h"
21 #endif
22 #include "ability_service_interface.h"
23 #include "adapter.h"
24 #include "ohos_init.h"
25 #include "samgr_lite.h"
26 #include "util/abilityms_log.h"
27 
28 namespace OHOS {
29 const int QUEUE_SIZE = 20;
30 #ifdef __LITEOS_M__
31 const int BYTE_OFFSET = 16;
32 #endif
33 
AbilityMgrService()34 AbilityMgrService::AbilityMgrService() : Service(), identity_()
35 {
36     this->Service::GetName = AbilityMgrService::GetServiceName;
37     this->Service::Initialize = AbilityMgrService::ServiceInitialize;
38     this->Service::MessageHandle = AbilityMgrService::ServiceMessageHandle;
39     this->Service::GetTaskConfig = AbilityMgrService::GetServiceTaskConfig;
40 }
41 
Init()42 static void Init()
43 {
44     SamgrLite *sm = SAMGR_GetInstance();
45     CHECK_NULLPTR_RETURN(sm, "AbilityManagerService", "get samgr error");
46     BOOL result = sm->RegisterService(AbilityMgrService::GetInstance());
47     PRINTI("AbilityManagerService", "ams starts %{public}s", result ? "successfully" : "unsuccessfully");
48 }
49 SYSEX_SERVICE_INIT(Init);
50 
GetServiceName(Service * service)51 const char *AbilityMgrService::GetServiceName(Service *service)
52 {
53     (void)service;
54     return AMS_SERVICE;
55 }
56 
GetIdentity()57 const Identity *AbilityMgrService::GetIdentity()
58 {
59     return &identity_;
60 }
61 
ServiceInitialize(Service * service,Identity identity)62 BOOL AbilityMgrService::ServiceInitialize(Service *service, Identity identity)
63 {
64     if (service == nullptr) {
65         return FALSE;
66     }
67     AbilityMgrService *abilityManagerService = static_cast<AbilityMgrService *>(service);
68     abilityManagerService->identity_ = identity;
69     return TRUE;
70 }
71 
ServiceMessageHandle(Service * service,Request * request)72 BOOL AbilityMgrService::ServiceMessageHandle(Service *service, Request *request)
73 {
74     if (request == nullptr) {
75         return FALSE;
76     }
77 #ifdef __LITEOS_M__
78     int ret = ERR_OK;
79     if (request->msgId == START_ABILITY) {
80         ret = AbilityService::GetInstance().StartAbility(AbilityService::GetInstance().want_);
81         AbilityService::GetInstance().CleanWant();
82         AbilityService::GetInstance().curTask_ = 0;
83     } else if  (request->msgId == ABILITY_TRANSACTION_DONE) {
84         int token = request->msgValue & 0xFFFF;
85         int state = (request->msgValue >> BYTE_OFFSET) & 0xFFFF;
86         ret = AbilityService::GetInstance().SchedulerLifecycleDone(token, state);
87     } else if (request->msgId == TERMINATE_ABILITY) {
88         ret = AbilityService::GetInstance().TerminateAbility(request->msgValue);
89     } else if (request->msgId == TERMINATE_APP) {
90         ret = AbilityService::GetInstance().ForceStopBundle(request->msgValue);
91     } else if (request->msgId == TERMINATE_APP_BY_BUNDLENAME) {
92         char* bundleName = reinterpret_cast<char *>(request->data);
93         ret = AbilityService::GetInstance().ForceStop(bundleName);
94     }
95     return ret == ERR_OK;
96 #else
97     return TRUE;
98 #endif
99 }
100 
GetServiceTaskConfig(Service * service)101 TaskConfig AbilityMgrService::GetServiceTaskConfig(Service *service)
102 {
103     TaskConfig config = {LEVEL_HIGH, PRI_NORMAL, AMS_TASK_STACK_SIZE, QUEUE_SIZE, SINGLE_TASK};
104     return config;
105 }
106 } // namespace OHOS
107