summaryrefslogtreecommitdiff
path: root/chromium/components/ukm/content/app_source_url_recorder.cc
blob: ea69b854596f4223080f23584cba84166920b7cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/ukm/content/app_source_url_recorder.h"

#include "base/atomic_sequence_num.h"
#include "services/metrics/public/cpp/delegating_ukm_recorder.h"
#include "services/metrics/public/cpp/ukm_recorder.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "url/gurl.h"

namespace ukm {

SourceId AssignNewAppId() {
  static base::AtomicSequenceNumber seq;
  return ConvertToSourceId(seq.GetNext() + 1, SourceIdType::APP_ID);
}

SourceId AppSourceUrlRecorder::GetSourceIdForApp(AppType type,
                                                 const std::string& id) {
  GURL url;
  if (type == AppType::kArc)
    url = GURL("app://play/" + id);
  else if (type == AppType::kChromeExtension)
    url = GURL("chrome-extension://" + id);
  else
    return kInvalidSourceId;

  return GetSourceIdForUrl(url);
}

SourceId AppSourceUrlRecorder::GetSourceIdForPWA(const GURL& url) {
  return GetSourceIdForUrl(url);
}

SourceId AppSourceUrlRecorder::GetSourceIdForUrl(const GURL& url) {
  ukm::DelegatingUkmRecorder* const recorder =
      ukm::DelegatingUkmRecorder::Get();
  if (!recorder)
    return kInvalidSourceId;

  const SourceId source_id = AssignNewAppId();
  if (base::FeatureList::IsEnabled(kUkmAppLogging)) {
    recorder->UpdateAppURL(source_id, url);
  }
  return source_id;
}

}  // namespace ukm