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 "ability_debug_deal.h"
17 
18 #include "ability_record.h"
19 #include "hilog_tag_wrapper.h"
20 #include "hitrace_meter.h"
21 #include "in_process_call_wrapper.h"
22 
23 namespace OHOS {
24 namespace AAFwk {
RegisterAbilityDebugResponse()25 void AbilityDebugDeal::RegisterAbilityDebugResponse()
26 {
27     abilityDebugResponse_ = new (std::nothrow) AbilityDebugResponse(weak_from_this());
28     if (abilityDebugResponse_ == nullptr) {
29         TAG_LOGE(AAFwkTag::ABILITYMGR, "Ability debug response is nullptr.");
30         return;
31     }
32 
33     IN_PROCESS_CALL_WITHOUT_RET(
34         DelayedSingleton<AppScheduler>::GetInstance()->RegisterAbilityDebugResponse(abilityDebugResponse_));
35 }
36 
OnAbilitysDebugStarted(const std::vector<sptr<IRemoteObject>> & tokens)37 void AbilityDebugDeal::OnAbilitysDebugStarted(const std::vector<sptr<IRemoteObject>> &tokens)
38 {
39     TAG_LOGD(AAFwkTag::ABILITYMGR, "called");
40     for (auto &token : tokens) {
41         auto abilityRecord = Token::GetAbilityRecordByToken(token);
42         if (abilityRecord == nullptr) {
43             TAG_LOGE(AAFwkTag::ABILITYMGR, "Ability record is nullptr.");
44             continue;
45         }
46         abilityRecord->SetAttachDebug(true);
47     }
48 }
49 
OnAbilitysDebugStoped(const std::vector<sptr<IRemoteObject>> & tokens)50 void AbilityDebugDeal::OnAbilitysDebugStoped(const std::vector<sptr<IRemoteObject>> &tokens)
51 {
52     TAG_LOGD(AAFwkTag::ABILITYMGR, "called");
53     for (auto &token : tokens) {
54         auto abilityRecord = Token::GetAbilityRecordByToken(token);
55         if (abilityRecord == nullptr) {
56             TAG_LOGE(AAFwkTag::ABILITYMGR, "Ability record is nullptr.");
57             continue;
58         }
59         abilityRecord->SetAttachDebug(false);
60     }
61 }
62 
OnAbilitysAssertDebugChange(const std::vector<sptr<IRemoteObject>> & tokens,bool isAssertDebug)63 void AbilityDebugDeal::OnAbilitysAssertDebugChange(const std::vector<sptr<IRemoteObject>> &tokens, bool isAssertDebug)
64 {
65     TAG_LOGD(AAFwkTag::ABILITYMGR, "Called. %{public}s", isAssertDebug ? "start assert debug" : "stop assert debug");
66     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
67     for (const auto &token : tokens) {
68         auto abilityRecord = Token::GetAbilityRecordByToken(token);
69         if (abilityRecord == nullptr) {
70             TAG_LOGE(AAFwkTag::ABILITYMGR, "Ability record is nullptr.");
71             continue;
72         }
73         abilityRecord->SetAssertDebug(isAssertDebug);
74     }
75 }
76 
OnAbilitysDebugStarted(const std::vector<sptr<IRemoteObject>> & tokens)77 void AbilityDebugResponse::OnAbilitysDebugStarted(const std::vector<sptr<IRemoteObject>> &tokens)
78 {
79     if (tokens.empty()) {
80         TAG_LOGW(AAFwkTag::ABILITYMGR, "Tokens is empty.");
81         return;
82     }
83 
84     auto deal = abilityDebugDeal_.lock();
85     if (deal == nullptr) {
86         TAG_LOGE(AAFwkTag::ABILITYMGR, "Ability debug deal object is nullptr.");
87         return;
88     }
89     deal->OnAbilitysDebugStarted(tokens);
90 }
91 
OnAbilitysDebugStoped(const std::vector<sptr<IRemoteObject>> & tokens)92 void AbilityDebugResponse::OnAbilitysDebugStoped(const std::vector<sptr<IRemoteObject>> &tokens)
93 {
94     if (tokens.empty()) {
95         TAG_LOGW(AAFwkTag::ABILITYMGR, "Tokens is empty.");
96         return;
97     }
98 
99     auto deal = abilityDebugDeal_.lock();
100     if (deal == nullptr) {
101         TAG_LOGE(AAFwkTag::ABILITYMGR, "Ability debug deal object is nullptr.");
102         return;
103     }
104     deal->OnAbilitysDebugStoped(tokens);
105 }
106 
OnAbilitysAssertDebugChange(const std::vector<sptr<IRemoteObject>> & tokens,bool isAssertDebug)107 void AbilityDebugResponse::OnAbilitysAssertDebugChange(
108     const std::vector<sptr<IRemoteObject>> &tokens, bool isAssertDebug)
109 {
110     if (tokens.empty()) {
111         TAG_LOGW(AAFwkTag::ABILITYMGR, "Tokens is empty.");
112         return;
113     }
114 
115     auto deal = abilityDebugDeal_.lock();
116     if (deal == nullptr) {
117         TAG_LOGW(AAFwkTag::ABILITYMGR, "Ability debug deal object is nullptr.");
118         return;
119     }
120     deal->OnAbilitysAssertDebugChange(tokens, isAssertDebug);
121 }
122 } // namespace AAFwk
123 } // namespace OHOS
124