Move PackageFromLocation to freestanding function.

Bug: 159987908
Change-Id: Ie4d09e5be2a3d3a6ab4abbe8121b4c570ae3f5b5
diff --git a/Android.bp b/Android.bp
index 96bd295..ec55b37 100644
--- a/Android.bp
+++ b/Android.bp
@@ -6967,6 +6967,7 @@
     "src/trace_processor/importers/proto/packet_sequence_state.cc",
     "src/trace_processor/importers/proto/profile_module.cc",
     "src/trace_processor/importers/proto/profile_packet_utils.cc",
+    "src/trace_processor/importers/proto/profiler_util.cc",
     "src/trace_processor/importers/proto/proto_importer_module.cc",
     "src/trace_processor/importers/proto/proto_trace_parser.cc",
     "src/trace_processor/importers/proto/proto_trace_tokenizer.cc",
diff --git a/BUILD b/BUILD
index 93176cb..287870b 100644
--- a/BUILD
+++ b/BUILD
@@ -1081,6 +1081,8 @@
         "src/trace_processor/importers/proto/profile_module.h",
         "src/trace_processor/importers/proto/profile_packet_utils.cc",
         "src/trace_processor/importers/proto/profile_packet_utils.h",
+        "src/trace_processor/importers/proto/profiler_util.cc",
+        "src/trace_processor/importers/proto/profiler_util.h",
         "src/trace_processor/importers/proto/proto_importer_module.cc",
         "src/trace_processor/importers/proto/proto_importer_module.h",
         "src/trace_processor/importers/proto/proto_incremental_state.h",
diff --git a/src/trace_processor/BUILD.gn b/src/trace_processor/BUILD.gn
index 1d401a7..5ed58a1 100644
--- a/src/trace_processor/BUILD.gn
+++ b/src/trace_processor/BUILD.gn
@@ -98,6 +98,8 @@
     "importers/proto/profile_module.h",
     "importers/proto/profile_packet_utils.cc",
     "importers/proto/profile_packet_utils.h",
+    "importers/proto/profiler_util.cc",
+    "importers/proto/profiler_util.h",
     "importers/proto/proto_importer_module.cc",
     "importers/proto/proto_importer_module.h",
     "importers/proto/proto_incremental_state.h",
diff --git a/src/trace_processor/importers/proto/heap_graph_tracker.cc b/src/trace_processor/importers/proto/heap_graph_tracker.cc
index c798318..a582837 100644
--- a/src/trace_processor/importers/proto/heap_graph_tracker.cc
+++ b/src/trace_processor/importers/proto/heap_graph_tracker.cc
@@ -18,6 +18,7 @@
 
 #include "perfetto/ext/base/string_splitter.h"
 #include "perfetto/ext/base/string_utils.h"
+#include "src/trace_processor/importers/proto/profiler_util.h"
 #include "src/trace_processor/tables/profiler_tables.h"
 
 #include <set>
@@ -26,24 +27,6 @@
 namespace trace_processor {
 
 namespace {
-base::Optional<base::StringView> PackageFromApp(base::StringView location) {
-  location = location.substr(base::StringView("/data/app/").size());
-  size_t slash = location.find('/');
-  if (slash == std::string::npos) {
-    return base::nullopt;
-  }
-  size_t second_slash = location.find('/', slash + 1);
-  if (second_slash == std::string::npos) {
-    location = location.substr(0, slash);
-  } else {
-    location = location.substr(slash + 1, second_slash - slash);
-  }
-  size_t minus = location.find('-');
-  if (minus == std::string::npos) {
-    return base::nullopt;
-  }
-  return location.substr(0, minus);
-}
 
 std::set<tables::HeapGraphObjectTable::Id> GetChildren(
     const TraceStorage& storage,
@@ -251,100 +234,6 @@
 HeapGraphTracker::HeapGraphTracker(TraceProcessorContext* context)
     : context_(context) {}
 
-base::Optional<std::string> HeapGraphTracker::PackageFromLocation(
-    base::StringView location) {
-  // List of some hardcoded apps that do not follow the scheme used in
-  // PackageFromApp. Ask for yours to be added.
-  //
-  // TODO(b/153632336): Get rid of the hardcoded list of system apps.
-  base::StringView sysui(
-      "/system_ext/priv-app/SystemUIGoogle/SystemUIGoogle.apk");
-  if (location.size() >= sysui.size() &&
-      location.substr(0, sysui.size()) == sysui) {
-    return "com.android.systemui";
-  }
-
-  base::StringView phonesky("/product/priv-app/Phonesky/Phonesky.apk");
-  if (location.size() >= phonesky.size() &&
-      location.substr(0, phonesky.size()) == phonesky) {
-    return "com.android.vending";
-  }
-
-  base::StringView maps("/product/app/Maps/Maps.apk");
-  if (location.size() >= maps.size() &&
-      location.substr(0, maps.size()) == maps) {
-    return "com.google.android.apps.maps";
-  }
-
-  base::StringView launcher(
-      "/system_ext/priv-app/NexusLauncherRelease/NexusLauncherRelease.apk");
-  if (location.size() >= launcher.size() &&
-      location.substr(0, launcher.size()) == launcher) {
-    return "com.google.android.apps.nexuslauncher";
-  }
-
-  base::StringView photos("/product/app/Photos/Photos.apk");
-  if (location.size() >= photos.size() &&
-      location.substr(0, photos.size()) == photos) {
-    return "com.google.android.apps.photos";
-  }
-
-  base::StringView wellbeing(
-      "/product/priv-app/WellbeingPrebuilt/WellbeingPrebuilt.apk");
-  if (location.size() >= wellbeing.size() &&
-      location.substr(0, wellbeing.size()) == wellbeing) {
-    return "com.google.android.apps.wellbeing";
-  }
-
-  base::StringView matchmaker("MatchMaker");
-  if (location.size() >= matchmaker.size() &&
-      location.find(matchmaker) != base::StringView::npos) {
-    return "com.google.android.as";
-  }
-
-  base::StringView gm("/product/app/PrebuiltGmail/PrebuiltGmail.apk");
-  if (location.size() >= gm.size() && location.substr(0, gm.size()) == gm) {
-    return "com.google.android.gm";
-  }
-
-  base::StringView gmscore("/product/priv-app/PrebuiltGmsCore/PrebuiltGmsCore");
-  if (location.size() >= gmscore.size() &&
-      location.substr(0, gmscore.size()) == gmscore) {
-    return "com.google.android.gms";
-  }
-
-  base::StringView velvet("/product/priv-app/Velvet/Velvet.apk");
-  if (location.size() >= velvet.size() &&
-      location.substr(0, velvet.size()) == velvet) {
-    return "com.google.android.googlequicksearchbox";
-  }
-
-  base::StringView inputmethod(
-      "/product/app/LatinIMEGooglePrebuilt/LatinIMEGooglePrebuilt.apk");
-  if (location.size() >= inputmethod.size() &&
-      location.substr(0, inputmethod.size()) == inputmethod) {
-    return "com.google.android.inputmethod.latin";
-  }
-
-  base::StringView messaging("/product/app/PrebuiltBugle/PrebuiltBugle.apk");
-  if (location.size() >= messaging.size() &&
-      location.substr(0, messaging.size()) == messaging) {
-    return "com.google.android.apps.messaging";
-  }
-
-  base::StringView data_app("/data/app/");
-  if (location.substr(0, data_app.size()) == data_app) {
-    auto package = PackageFromApp(location);
-    if (!package) {
-      PERFETTO_DLOG("Failed to parse %s", location.ToStdString().c_str());
-      context_->storage->IncrementStats(stats::heap_graph_location_parse_error);
-      return base::nullopt;
-    }
-    return package->ToStdString();
-  }
-  return base::nullopt;
-}
-
 HeapGraphTracker::SequenceState& HeapGraphTracker::GetOrCreateSequence(
     uint32_t seq_id) {
   return sequence_state_[seq_id];
@@ -593,7 +482,8 @@
 
     if (location_name && !is_base_apk) {
       base::Optional<std::string> package_name =
-          PackageFromLocation(context_->storage->GetString(*location_name));
+          PackageFromLocation(context_->storage.get(),
+                              context_->storage->GetString(*location_name));
       if (package_name) {
         class_to_rows_[std::make_pair(
                            context_->storage->InternString(
diff --git a/src/trace_processor/importers/proto/heap_graph_tracker.h b/src/trace_processor/importers/proto/heap_graph_tracker.h
index b30ad59..b49f547 100644
--- a/src/trace_processor/importers/proto/heap_graph_tracker.h
+++ b/src/trace_processor/importers/proto/heap_graph_tracker.h
@@ -61,7 +61,6 @@
                       tables::HeapGraphObjectTable::Id id,
                       PathFromRoot* path);
 
-base::Optional<std::string> PackageFromLocation(base::StringView location);
 base::Optional<base::StringView> GetStaticClassTypeName(base::StringView type);
 size_t NumberOfArrays(base::StringView type);
 NormalizedType GetNormalizedType(base::StringView type);
@@ -143,9 +142,6 @@
       const int64_t current_ts,
       const UniquePid current_upid);
 
-  // public for testing.
-  base::Optional<std::string> PackageFromLocation(base::StringView location);
-
  private:
   struct InternedField {
     StringPool::Id name;
diff --git a/src/trace_processor/importers/proto/heap_graph_tracker_unittest.cc b/src/trace_processor/importers/proto/heap_graph_tracker_unittest.cc
index 3930310..1c7825e 100644
--- a/src/trace_processor/importers/proto/heap_graph_tracker_unittest.cc
+++ b/src/trace_processor/importers/proto/heap_graph_tracker_unittest.cc
@@ -16,6 +16,8 @@
 
 #include "src/trace_processor/importers/proto/heap_graph_tracker.h"
 
+#include "src/trace_processor/importers/proto/profiler_util.h"
+
 #include "perfetto/base/logging.h"
 #include "test/gtest_and_gmock.h"
 
@@ -26,14 +28,14 @@
 using ::testing::UnorderedElementsAre;
 
 TEST(HeapGraphTrackerTest, PackageFromLocationApp) {
-  TraceProcessorContext context;
-  context.storage.reset(new TraceStorage);
-  HeapGraphTracker tracker(&context);
-  EXPECT_EQ(tracker.PackageFromLocation(
-                "/data/app/~~ASDFGH1234QWerT==/"
-                "com.twitter.android-MNBVCX7890SDTst6==/test.apk"),
-            "com.twitter.android");
-  EXPECT_EQ(tracker.PackageFromLocation(
+  TraceStorage storage;
+  EXPECT_EQ(
+      PackageFromLocation(&storage,
+                          "/data/app/~~ASDFGH1234QWerT==/"
+                          "com.twitter.android-MNBVCX7890SDTst6==/test.apk"),
+      "com.twitter.android");
+  EXPECT_EQ(PackageFromLocation(
+                &storage,
                 "/data/app/com.google.android.webview-6XfQhnaSkFwGK0sYL9is0G==/"
                 "base.apk"),
             "com.google.android.webview");
diff --git a/src/trace_processor/importers/proto/profiler_util.cc b/src/trace_processor/importers/proto/profiler_util.cc
new file mode 100644
index 0000000..c6b5c6f
--- /dev/null
+++ b/src/trace_processor/importers/proto/profiler_util.cc
@@ -0,0 +1,141 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "src/trace_processor/importers/proto/profiler_util.h"
+
+#include "src/trace_processor/storage/trace_storage.h"
+
+namespace perfetto {
+namespace trace_processor {
+namespace {
+
+base::Optional<base::StringView> PackageFromApp(base::StringView location) {
+  location = location.substr(base::StringView("/data/app/").size());
+  size_t slash = location.find('/');
+  if (slash == std::string::npos) {
+    return base::nullopt;
+  }
+  size_t second_slash = location.find('/', slash + 1);
+  if (second_slash == std::string::npos) {
+    location = location.substr(0, slash);
+  } else {
+    location = location.substr(slash + 1, second_slash - slash);
+  }
+  size_t minus = location.find('-');
+  if (minus == std::string::npos) {
+    return base::nullopt;
+  }
+  return location.substr(0, minus);
+}
+
+}  // namespace
+
+base::Optional<std::string> PackageFromLocation(TraceStorage* storage,
+                                                base::StringView location) {
+  // List of some hardcoded apps that do not follow the scheme used in
+  // PackageFromApp. Ask for yours to be added.
+  //
+  // TODO(b/153632336): Get rid of the hardcoded list of system apps.
+  base::StringView sysui(
+      "/system_ext/priv-app/SystemUIGoogle/SystemUIGoogle.apk");
+  if (location.size() >= sysui.size() &&
+      location.substr(0, sysui.size()) == sysui) {
+    return "com.android.systemui";
+  }
+
+  base::StringView phonesky("/product/priv-app/Phonesky/Phonesky.apk");
+  if (location.size() >= phonesky.size() &&
+      location.substr(0, phonesky.size()) == phonesky) {
+    return "com.android.vending";
+  }
+
+  base::StringView maps("/product/app/Maps/Maps.apk");
+  if (location.size() >= maps.size() &&
+      location.substr(0, maps.size()) == maps) {
+    return "com.google.android.apps.maps";
+  }
+
+  base::StringView launcher(
+      "/system_ext/priv-app/NexusLauncherRelease/NexusLauncherRelease.apk");
+  if (location.size() >= launcher.size() &&
+      location.substr(0, launcher.size()) == launcher) {
+    return "com.google.android.apps.nexuslauncher";
+  }
+
+  base::StringView photos("/product/app/Photos/Photos.apk");
+  if (location.size() >= photos.size() &&
+      location.substr(0, photos.size()) == photos) {
+    return "com.google.android.apps.photos";
+  }
+
+  base::StringView wellbeing(
+      "/product/priv-app/WellbeingPrebuilt/WellbeingPrebuilt.apk");
+  if (location.size() >= wellbeing.size() &&
+      location.substr(0, wellbeing.size()) == wellbeing) {
+    return "com.google.android.apps.wellbeing";
+  }
+
+  base::StringView matchmaker("MatchMaker");
+  if (location.size() >= matchmaker.size() &&
+      location.find(matchmaker) != base::StringView::npos) {
+    return "com.google.android.as";
+  }
+
+  base::StringView gm("/product/app/PrebuiltGmail/PrebuiltGmail.apk");
+  if (location.size() >= gm.size() && location.substr(0, gm.size()) == gm) {
+    return "com.google.android.gm";
+  }
+
+  base::StringView gmscore("/product/priv-app/PrebuiltGmsCore/PrebuiltGmsCore");
+  if (location.size() >= gmscore.size() &&
+      location.substr(0, gmscore.size()) == gmscore) {
+    return "com.google.android.gms";
+  }
+
+  base::StringView velvet("/product/priv-app/Velvet/Velvet.apk");
+  if (location.size() >= velvet.size() &&
+      location.substr(0, velvet.size()) == velvet) {
+    return "com.google.android.googlequicksearchbox";
+  }
+
+  base::StringView inputmethod(
+      "/product/app/LatinIMEGooglePrebuilt/LatinIMEGooglePrebuilt.apk");
+  if (location.size() >= inputmethod.size() &&
+      location.substr(0, inputmethod.size()) == inputmethod) {
+    return "com.google.android.inputmethod.latin";
+  }
+
+  base::StringView messaging("/product/app/PrebuiltBugle/PrebuiltBugle.apk");
+  if (location.size() >= messaging.size() &&
+      location.substr(0, messaging.size()) == messaging) {
+    return "com.google.android.apps.messaging";
+  }
+
+  base::StringView data_app("/data/app/");
+  if (location.substr(0, data_app.size()) == data_app) {
+    auto package = PackageFromApp(location);
+    if (!package) {
+      PERFETTO_DLOG("Failed to parse %s", location.ToStdString().c_str());
+      storage->IncrementStats(stats::heap_graph_location_parse_error);
+      return base::nullopt;
+    }
+    return package->ToStdString();
+  }
+  return base::nullopt;
+}
+
+}  // namespace trace_processor
+}  // namespace perfetto
diff --git a/src/trace_processor/importers/proto/profiler_util.h b/src/trace_processor/importers/proto/profiler_util.h
new file mode 100644
index 0000000..5a1d984
--- /dev/null
+++ b/src/trace_processor/importers/proto/profiler_util.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_PROFILER_UTIL_H_
+#define SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_PROFILER_UTIL_H_
+
+#include <string>
+
+#include "perfetto/ext/base/optional.h"
+#include "perfetto/ext/base/string_view.h"
+
+#include "src/trace_processor/storage/trace_storage.h"
+
+namespace perfetto {
+namespace trace_processor {
+
+base::Optional<std::string> PackageFromLocation(TraceStorage* storage,
+                                                base::StringView location);
+
+}
+}  // namespace perfetto
+
+#endif  // SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_PROFILER_UTIL_H_