app_source_url_recorder.cc 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/ukm/app_source_url_recorder.h"
  5. #include "base/atomic_sequence_num.h"
  6. #include "components/crx_file/id_util.h"
  7. #include "services/metrics/public/cpp/delegating_ukm_recorder.h"
  8. #include "services/metrics/public/cpp/ukm_source_id.h"
  9. #include "url/gurl.h"
  10. namespace ukm {
  11. SourceId AssignNewAppId() {
  12. static base::AtomicSequenceNumber seq;
  13. return ConvertToSourceId(seq.GetNext() + 1, SourceIdType::APP_ID);
  14. }
  15. SourceId AppSourceUrlRecorder::GetSourceIdForChromeApp(
  16. const std::string& app_id) {
  17. DCHECK(!app_id.empty());
  18. GURL url("app://" + app_id);
  19. return GetSourceIdForUrl(url, AppType::kChromeApp);
  20. }
  21. SourceId AppSourceUrlRecorder::GetSourceIdForChromeExtension(
  22. const std::string& id) {
  23. GURL url("chrome-extension://" + id);
  24. return GetSourceIdForUrl(url, AppType::kExtension);
  25. }
  26. SourceId AppSourceUrlRecorder::GetSourceIdForArcPackageName(
  27. const std::string& package_name) {
  28. DCHECK(!package_name.empty());
  29. GURL url("app://" + package_name);
  30. return GetSourceIdForUrl(url, AppType::kArc);
  31. }
  32. SourceId AppSourceUrlRecorder::GetSourceIdForArc(
  33. const std::string& package_name) {
  34. const std::string package_name_hash =
  35. crx_file::id_util::GenerateId(package_name);
  36. GURL url("app://play/" + package_name_hash);
  37. return GetSourceIdForUrl(url, AppType::kArc);
  38. }
  39. SourceId AppSourceUrlRecorder::GetSourceIdForPWA(const GURL& url) {
  40. return GetSourceIdForUrl(url, AppType::kPWA);
  41. }
  42. SourceId AppSourceUrlRecorder::GetSourceIdForBorealis(const std::string& app) {
  43. GURL url("app://borealis/" + app);
  44. return GetSourceIdForUrl(url, AppType::kBorealis);
  45. }
  46. SourceId AppSourceUrlRecorder::GetSourceIdForCrostini(
  47. const std::string& desktop_id,
  48. const std::string& app_name) {
  49. GURL url("app://" + desktop_id + "/" + app_name);
  50. return GetSourceIdForUrl(url, AppType::kCrostini);
  51. }
  52. SourceId AppSourceUrlRecorder::GetSourceIdForUrl(const GURL& url,
  53. AppType app_type) {
  54. ukm::DelegatingUkmRecorder* const recorder =
  55. ukm::DelegatingUkmRecorder::Get();
  56. if (!recorder)
  57. return kInvalidSourceId;
  58. const SourceId source_id = AssignNewAppId();
  59. if (base::FeatureList::IsEnabled(kUkmAppLogging)) {
  60. recorder->UpdateAppURL(source_id, url, app_type);
  61. }
  62. return source_id;
  63. }
  64. void AppSourceUrlRecorder::MarkSourceForDeletion(SourceId source_id) {
  65. if (GetSourceIdType(source_id) != SourceIdType::APP_ID) {
  66. DLOG(FATAL) << "AppSourceUrlRecorder::MarkSourceForDeletion invoked on "
  67. << "non-APP_ID type SourceId: " << source_id;
  68. return;
  69. }
  70. ukm::DelegatingUkmRecorder* const recorder =
  71. ukm::DelegatingUkmRecorder::Get();
  72. if (recorder)
  73. recorder->MarkSourceForDeletion(source_id);
  74. }
  75. } // namespace ukm