1 /*
2  * Copyright (c) 2023-2024 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 "auto_startup_callback_stub.h"
17 
18 #include "ability_manager_ipc_interface_code.h"
19 #include "auto_startup_info.h"
20 #include "event_handler.h"
21 #include "hilog_tag_wrapper.h"
22 #include "ipc_types.h"
23 #include "message_parcel.h"
24 
25 namespace OHOS {
26 namespace AbilityRuntime {
27 using namespace OHOS::AAFwk;
AutoStartupCallBackStub()28 AutoStartupCallBackStub::AutoStartupCallBackStub()
29 {
30     Init();
31 }
32 
~AutoStartupCallBackStub()33 AutoStartupCallBackStub::~AutoStartupCallBackStub() {}
34 
Init()35 void AutoStartupCallBackStub::Init() {}
36 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)37 int AutoStartupCallBackStub::OnRemoteRequest(
38     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
39 {
40     std::u16string autoStartUpCallBackDescriptor = AutoStartupCallBackStub::GetDescriptor();
41     std::u16string remoteDescriptor = data.ReadInterfaceToken();
42     if (autoStartUpCallBackDescriptor != remoteDescriptor) {
43         TAG_LOGE(AAFwkTag::AUTO_STARTUP, "invalid descriptor");
44         return ERR_INVALID_STATE;
45     }
46 
47     switch (code) {
48         case static_cast<uint32_t>(AbilityManagerInterfaceCode::ON_AUTO_STARTUP_ON):
49             return OnAutoStartupOnInner(data, reply);
50             break;
51         case static_cast<uint32_t>(AbilityManagerInterfaceCode::ON_AUTO_STARTUP_OFF):
52             return OnAutoStartupOffInner(data, reply);
53             break;
54     }
55 
56     TAG_LOGW(AAFwkTag::AUTO_STARTUP, "Default case");
57     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
58 }
59 
OnAutoStartupOnInner(MessageParcel & data,MessageParcel & reply)60 int32_t AutoStartupCallBackStub::OnAutoStartupOnInner(MessageParcel &data, MessageParcel &reply)
61 {
62     sptr<AutoStartupInfo> info = data.ReadParcelable<AutoStartupInfo>();
63     if (info == nullptr) {
64         TAG_LOGE(AAFwkTag::AUTO_STARTUP, "Read info failed");
65         return ERR_INVALID_VALUE;
66     }
67 
68     std::shared_ptr<AppExecFwk::EventHandler> handler =
69         std::make_shared<AppExecFwk::EventHandler>(AppExecFwk::EventRunner::GetMainEventRunner());
70     wptr<AutoStartupCallBackStub> weak = this;
71     handler->PostSyncTask([weak, info]() {
72         auto autoStartUpCallBackStub = weak.promote();
73         if (autoStartUpCallBackStub == nullptr) {
74             TAG_LOGE(AAFwkTag::AUTO_STARTUP, "null autoStartUpCallBackStub");
75             return;
76         }
77         autoStartUpCallBackStub->OnAutoStartupOn(*info);
78         }, "AutoStartupCallBackStub::OnAutoStartupOnInner");
79 
80     return NO_ERROR;
81 }
82 
OnAutoStartupOffInner(MessageParcel & data,MessageParcel & reply)83 int32_t AutoStartupCallBackStub::OnAutoStartupOffInner(MessageParcel &data, MessageParcel &reply)
84 {
85     sptr<AutoStartupInfo> info = data.ReadParcelable<AutoStartupInfo>();
86     if (info == nullptr) {
87         TAG_LOGE(AAFwkTag::AUTO_STARTUP, "Read info failed");
88         return ERR_INVALID_VALUE;
89     }
90 
91     std::shared_ptr<AppExecFwk::EventHandler> handler =
92         std::make_shared<AppExecFwk::EventHandler>(AppExecFwk::EventRunner::GetMainEventRunner());
93     wptr<AutoStartupCallBackStub> weak = this;
94     handler->PostSyncTask([weak, info]() {
95         auto autoStartUpCallBackStub = weak.promote();
96         if (autoStartUpCallBackStub == nullptr) {
97             TAG_LOGE(AAFwkTag::AUTO_STARTUP, "null autoStartUpCallBackStub");
98             return;
99         }
100         autoStartUpCallBackStub->OnAutoStartupOff(*info);
101         }, "AutoStartupCallBackStub::OnAutoStartupOffInner");
102 
103     return NO_ERROR;
104 }
105 } // namespace AbilityRuntime
106 } // namespace OHOS
107