• 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 APP_PLATFORM_WATCHGT
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 #ifndef APP_PLATFORM_WATCHGT
30 const int STACK_SIZE = 0x800;
31 #else
32 const int STACK_SIZE = 0xE00;
33 #endif
34 const int QUEUE_SIZE = 20;
35 const int BYTE_OFFSET = 8;
36 
AbilityMgrService()37 AbilityMgrService::AbilityMgrService() : Service(), identity_()
38 {
39     this->Service::GetName = AbilityMgrService::GetServiceName;
40     this->Service::Initialize = AbilityMgrService::ServiceInitialize;
41     this->Service::MessageHandle = AbilityMgrService::ServiceMessageHandle;
42     this->Service::GetTaskConfig = AbilityMgrService::GetServiceTaskConfig;
43 }
44 
Init()45 static void Init()
46 {
47     SamgrLite *sm = SAMGR_GetInstance();
48     CHECK_NULLPTR_RETURN(sm, "AbilityManagerService", "get samgr error");
49     BOOL result = sm->RegisterService(AbilityMgrService::GetInstance());
50     PRINTI("AbilityManagerService", "ams starts %{public}s", result ? "successfully" : "unsuccessfully");
51 }
52 SYSEX_SERVICE_INIT(Init);
53 
GetServiceName(Service * service)54 const char *AbilityMgrService::GetServiceName(Service *service)
55 {
56     (void)service;
57     return AMS_SERVICE;
58 }
59 
GetIdentity()60 const Identity *AbilityMgrService::GetIdentity()
61 {
62     return &identity_;
63 }
64 
ServiceInitialize(Service * service,Identity identity)65 BOOL AbilityMgrService::ServiceInitialize(Service *service, Identity identity)
66 {
67     if (service == nullptr) {
68         return FALSE;
69     }
70     AbilityMgrService *abilityManagerService = static_cast<AbilityMgrService *>(service);
71     abilityManagerService->identity_ = identity;
72     return TRUE;
73 }
74 
ServiceMessageHandle(Service * service,Request * request)75 BOOL AbilityMgrService::ServiceMessageHandle(Service *service, Request *request)
76 {
77     if (request == nullptr) {
78         return FALSE;
79     }
80 #ifdef APP_PLATFORM_WATCHGT
81     int ret = ERR_OK;
82     if (request->msgId == START_ABILITY) {
83         ret = AbilityService::GetInstance().StartAbility(AbilityService::GetInstance().want_);
84         AbilityService::GetInstance().CleanWant();
85     } else if  (request->msgId == ABILITY_TRANSACTION_DONE) {
86         int token = request->msgValue & 0xFF;
87         int state = (request->msgValue >> BYTE_OFFSET) & 0xFF;
88         ret = AbilityService::GetInstance().SchedulerLifecycleDone(token, state);
89     } else if (request->msgId == TERMINATE_ABILITY) {
90         ret = AbilityService::GetInstance().TerminateAbility(request->msgValue);
91     } else if (request->msgId == TERMINATE_APP) {
92         ret = AbilityService::GetInstance().ForceStopBundle(request->msgValue);
93     } else if (request->msgId == TERMINATE_APP_BY_BUNDLENAME) {
94         char* bundleName = reinterpret_cast<char *>(request->data);
95         ret = AbilityService::GetInstance().ForceStop(bundleName);
96     }
97     return ret == ERR_OK;
98 #else
99     return TRUE;
100 #endif
101 }
102 
GetServiceTaskConfig(Service * service)103 TaskConfig AbilityMgrService::GetServiceTaskConfig(Service *service)
104 {
105     TaskConfig config = {LEVEL_HIGH, PRI_NORMAL, STACK_SIZE, QUEUE_SIZE, SINGLE_TASK};
106     return config;
107 }
108 } // namespace OHOS
109