1 /*
2 * Copyright (C) 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "src/trace_redaction/find_package_uid.h"
18 #include "perfetto/ext/base/string_view.h"
19 #include "src/trace_redaction/trace_redaction_framework.h"
20
21 #include "protos/perfetto/trace/android/packages_list.pbzero.h"
22 #include "protos/perfetto/trace/trace_packet.pbzero.h"
23
24 namespace perfetto::trace_redaction {
25
Begin(Context * context) const26 base::Status FindPackageUid::Begin(Context* context) const {
27 if (context->package_name.empty()) {
28 return base::ErrStatus("FindPackageUid: missing package name.");
29 }
30
31 if (context->package_uid.has_value()) {
32 return base::ErrStatus("FindPackageUid: package uid already found.");
33 }
34
35 return base::OkStatus();
36 }
37
Collect(const protos::pbzero::TracePacket::Decoder & packet,Context * context) const38 base::Status FindPackageUid::Collect(
39 const protos::pbzero::TracePacket::Decoder& packet,
40 Context* context) const {
41 // If a package has been found in a pervious iteration, stop.
42 if (context->package_uid.has_value()) {
43 return base::OkStatus();
44 }
45
46 if (!packet.has_packages_list()) {
47 return base::OkStatus();
48 }
49
50 protos::pbzero::PackagesList::Decoder packages_list_decoder(
51 packet.packages_list());
52
53 for (auto package = packages_list_decoder.packages(); package; ++package) {
54 protos::pbzero::PackagesList::PackageInfo::Decoder info(*package);
55
56 if (!info.has_name() || !info.uid()) {
57 continue;
58 }
59
60 // Package names should be lowercase, but this check is meant to be more
61 // forgiving.
62 base::StringView expected_name(context->package_name.data(),
63 context->package_name.size());
64 base::StringView actual_name(info.name().data, info.name().size);
65 if (!actual_name.CaseInsensitiveEq(expected_name)) {
66 continue;
67 }
68
69 // See "trace_redaction_framework.cc" for info.uid() must be normalized.
70 context->package_uid = NormalizeUid(info.uid());
71 return base::OkStatus();
72 }
73
74 // Nothing was found. There should only be one package list, but we keep
75 // looking just incase. The error case will be handled in End().
76 return base::OkStatus();
77 }
78
End(Context * context) const79 base::Status FindPackageUid::End(Context* context) const {
80 if (!context->package_uid.has_value()) {
81 return base::ErrStatus("FindPackageUid: did not find package uid.");
82 }
83
84 return base::OkStatus();
85 }
86
87 } // namespace perfetto::trace_redaction
88