File: source_url_recorder.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (350 lines) | stat: -rw-r--r-- 14,446 bytes parent folder | download | duplicates (9)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

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

#include <utility>

#include "base/containers/flat_map.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/field_trial_params.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"
#include "services/metrics/public/cpp/delegating_ukm_recorder.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "services/metrics/public/cpp/ukm_recorder.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "url/gurl.h"

namespace ukm {

namespace internal {

int64_t CreateUniqueTabId() {
  static int64_t unique_id_counter = 0;
  return ++unique_id_counter;
}

// SourceUrlRecorderWebContentsObserver is responsible for recording UKM source
// URLs, for all (any only) main frame navigations in a given WebContents.
// SourceUrlRecorderWebContentsObserver records both the final URL for a
// navigation, and, if the navigation was redirected, the initial URL as well.
class SourceUrlRecorderWebContentsObserver
    : public content::WebContentsObserver,
      public content::WebContentsUserData<
          SourceUrlRecorderWebContentsObserver> {
 public:
  SourceUrlRecorderWebContentsObserver(
      const SourceUrlRecorderWebContentsObserver&) = delete;
  SourceUrlRecorderWebContentsObserver& operator=(
      const SourceUrlRecorderWebContentsObserver&) = delete;

  // content::WebContentsObserver:
  void DidStartNavigation(
      content::NavigationHandle* navigation_handle) override;
  void DidFinishNavigation(
      content::NavigationHandle* navigation_handle) override;
  void DidOpenRequestedURL(content::WebContents* new_contents,
                           content::RenderFrameHost* source_render_frame_host,
                           const GURL& url,
                           const content::Referrer& referrer,
                           WindowOpenDisposition disposition,
                           ui::PageTransition transition,
                           bool started_from_context_menu,
                           bool renderer_initiated) override;
  void WebContentsDestroyed() override;

  ukm::SourceId GetLastCommittedSourceId() const;
  ukm::SourceId GetLastCommittedFullNavigationOrSameDocumentSourceId() const;

 private:
  explicit SourceUrlRecorderWebContentsObserver(
      content::WebContents* web_contents);
  friend class content::WebContentsUserData<
      SourceUrlRecorderWebContentsObserver>;

  void HandleSameDocumentNavigation(
      content::NavigationHandle* navigation_handle);
  void HandleDifferentDocumentNavigation(
      content::NavigationHandle* navigation_handle,
      const GURL& initial_url);

  void MaybeRecordUrl(content::NavigationHandle* navigation_handle,
                      const GURL& initial_url);

  // Whether URLs should be recorded in UKM Sources.
  bool ShouldRecordURLs() const;

  // Map from navigation ID to the initial URL for that navigation.
  base::flat_map<int64_t, GURL> pending_navigations_;

  // The source id of the last committed full navigation (where a full
  // navigation is a non-same-document navigation).
  SourceId last_committed_full_navigation_source_id_;

  // The source id of the last committed navigation, either full navigation or
  // same document.
  SourceId last_committed_full_navigation_or_same_document_source_id_;

  // The source id of the last committed source in the tab that opened this tab.
  // Will be set to kInvalidSourceId after the first navigation in this tab is
  // finished.
  SourceId opener_source_id_;

  const int64_t tab_id_;

  int num_same_document_sources_for_full_navigation_source_;

  WEB_CONTENTS_USER_DATA_KEY_DECL();
};

WEB_CONTENTS_USER_DATA_KEY_IMPL(SourceUrlRecorderWebContentsObserver);

SourceUrlRecorderWebContentsObserver::SourceUrlRecorderWebContentsObserver(
    content::WebContents* web_contents)
    : content::WebContentsObserver(web_contents),
      content::WebContentsUserData<SourceUrlRecorderWebContentsObserver>(
          *web_contents),
      last_committed_full_navigation_source_id_(ukm::kInvalidSourceId),
      last_committed_full_navigation_or_same_document_source_id_(
          ukm::kInvalidSourceId),
      opener_source_id_(ukm::kInvalidSourceId),
      tab_id_(CreateUniqueTabId()),
      num_same_document_sources_for_full_navigation_source_(0) {}

bool SourceUrlRecorderWebContentsObserver::ShouldRecordURLs() const {
  // TODO(crbug.com/40689292): ensure we only record URLs for tabs in a tab
  // strip.

  // If there is an outer WebContents, then this WebContents is embedded into
  // another one (e.g it is a portal or a Chrome App <webview>).
  return web_contents()->GetOuterWebContents() == nullptr;
}

void SourceUrlRecorderWebContentsObserver::DidStartNavigation(
    content::NavigationHandle* navigation_handle) {
  // UKM only records URLs for main frame (web page) navigations, so ignore
  // non-main frame navs. Additionally, at least for the time being, we don't
  // track metrics for same-document navigations (e.g. changes in URL fragment,
  // or URL changes due to history.pushState) in UKM.
  if (!navigation_handle->IsInPrimaryMainFrame() ||
      navigation_handle->IsSameDocument()) {
    return;
  }

  // UKM doesn't want to record URLs for downloads. However, at the point a
  // navigation is started, we don't yet know if the navigation will result in a
  // download. Thus, we record the URL at the time a navigation was initiated,
  // and only record it later, once we verify that the navigation didn't result
  // in a download.
  pending_navigations_.insert(std::make_pair(
      navigation_handle->GetNavigationId(), navigation_handle->GetURL()));
}

void SourceUrlRecorderWebContentsObserver::DidFinishNavigation(
    content::NavigationHandle* navigation_handle) {
  auto it = pending_navigations_.find(navigation_handle->GetNavigationId());
  if (!navigation_handle->IsInPrimaryMainFrame()) {
    DCHECK(it == pending_navigations_.end());
    return;
  }

  if (navigation_handle->IsSameDocument()) {
    DCHECK(it == pending_navigations_.end());
    HandleSameDocumentNavigation(navigation_handle);
    return;
  }

  if (it != pending_navigations_.end()) {
    GURL initial_url = std::move(it->second);
    pending_navigations_.erase(it);
    HandleDifferentDocumentNavigation(navigation_handle, initial_url);
  }
}

void SourceUrlRecorderWebContentsObserver::HandleSameDocumentNavigation(
    content::NavigationHandle* navigation_handle) {
  if (!navigation_handle->HasCommitted())
    return;

  // Only record same-document sources if we were also recording the associated
  // full source.
  if (last_committed_full_navigation_source_id_ == ukm::kInvalidSourceId) {
    return;
  }

  // Since the navigation has committed, inform the UKM recorder that the
  // previous same-document source (if applicable) is no longer needed to be
  // kept alive in memory since we had navigated away. If the previous
  // navigation was a full navigation, we do not mark its source id since events
  // could be continued to be reported for it until the next full navigation
  // source is committed.
  ukm::DelegatingUkmRecorder* ukm_recorder = ukm::DelegatingUkmRecorder::Get();
  if (ukm_recorder &&
      GetLastCommittedSourceId() !=
          GetLastCommittedFullNavigationOrSameDocumentSourceId()) {
    ukm_recorder->MarkSourceForDeletion(
        GetLastCommittedFullNavigationOrSameDocumentSourceId());
  }

  MaybeRecordUrl(navigation_handle, GURL());

  last_committed_full_navigation_or_same_document_source_id_ =
      ukm::ConvertToSourceId(navigation_handle->GetNavigationId(),
                             ukm::SourceIdType::NAVIGATION_ID);
  ++num_same_document_sources_for_full_navigation_source_;
}

void SourceUrlRecorderWebContentsObserver::HandleDifferentDocumentNavigation(
    content::NavigationHandle* navigation_handle,
    const GURL& initial_url) {
  // UKM doesn't want to record URLs for navigations that result in downloads.
  if (navigation_handle->IsDownload())
    return;

  // If a new full navigation has been committed, there will be no more events
  // associated with previous navigation sources, so we mark them as obsolete.
  ukm::DelegatingUkmRecorder* ukm_recorder = ukm::DelegatingUkmRecorder::Get();
  if (navigation_handle->HasCommitted() && ukm_recorder) {
    // Source id of the previous full navigation.
    ukm_recorder->MarkSourceForDeletion(GetLastCommittedSourceId());
    // Source id of the previous navigation. If the previous navigation is a
    // full navigation, marking it again has no additional effect.
    ukm_recorder->MarkSourceForDeletion(
        GetLastCommittedFullNavigationOrSameDocumentSourceId());
  }

  MaybeRecordUrl(navigation_handle, initial_url);

  if (navigation_handle->HasCommitted()) {
    last_committed_full_navigation_source_id_ = ukm::ConvertToSourceId(
        navigation_handle->GetNavigationId(), ukm::SourceIdType::NAVIGATION_ID);
    last_committed_full_navigation_or_same_document_source_id_ =
        last_committed_full_navigation_source_id_;
    num_same_document_sources_for_full_navigation_source_ = 0;
  }

  // Reset the opener source id. Only the first source in a tab should have an
  // opener.
  opener_source_id_ = kInvalidSourceId;
}

void SourceUrlRecorderWebContentsObserver::DidOpenRequestedURL(
    content::WebContents* new_contents,
    content::RenderFrameHost* source_render_frame_host,
    const GURL& url,
    const content::Referrer& referrer,
    WindowOpenDisposition disposition,
    ui::PageTransition transition,
    bool started_from_context_menu,
    bool renderer_initiated) {
  // Ensure that a source recorder exists at this point, since it is possible
  // that this is called before tab helpers are added in //chrome, especially on
  // Android. See crbug.com/1024952 for more details.
  InitializeSourceUrlRecorderForWebContents(new_contents);
  SourceUrlRecorderWebContentsObserver::FromWebContents(new_contents)
      ->opener_source_id_ = GetLastCommittedSourceId();
}

void SourceUrlRecorderWebContentsObserver::WebContentsDestroyed() {
  // Inform the UKM recorder that the previous source is no longer needed to
  // be kept alive in memory since the tab has been closed or discarded. In case
  // of same-document navigation, a new source id would have been created
  // similarly to full-navigation, thus we are marking the last committed source
  // id regardless of which case it came from.
  ukm::DelegatingUkmRecorder* ukm_recorder = ukm::DelegatingUkmRecorder::Get();
  if (ukm_recorder) {
    ukm_recorder->MarkSourceForDeletion(
        GetLastCommittedFullNavigationOrSameDocumentSourceId());
  }
}

ukm::SourceId SourceUrlRecorderWebContentsObserver::GetLastCommittedSourceId()
    const {
  return last_committed_full_navigation_source_id_;
}

ukm::SourceId SourceUrlRecorderWebContentsObserver::
    GetLastCommittedFullNavigationOrSameDocumentSourceId() const {
  return last_committed_full_navigation_or_same_document_source_id_;
}

void SourceUrlRecorderWebContentsObserver::MaybeRecordUrl(
    content::NavigationHandle* navigation_handle,
    const GURL& initial_url) {
  DCHECK(navigation_handle->IsInPrimaryMainFrame());

  // TODO(crbug.com/40689295): If ShouldRecordURLs is false, we should still
  // create a UKM source, but not add any URLs to it.
  if (!ShouldRecordURLs())
    return;

  ukm::DelegatingUkmRecorder* ukm_recorder = ukm::DelegatingUkmRecorder::Get();
  if (!ukm_recorder)
    return;

  UkmSource::NavigationData navigation_data;
  const GURL& final_url = navigation_handle->GetURL();
  // TODO(crbug.com/40587196): This check isn't quite correct, as self
  // redirecting is possible. This may also be changed to include the entire
  // redirect chain. Additionally, since same-document navigations don't have
  // initial URLs, ignore empty initial URLs.
  if (!initial_url.is_empty() && final_url != initial_url)
    navigation_data.urls = {initial_url};
  navigation_data.urls.push_back(final_url);

  navigation_data.is_same_document_navigation =
      navigation_handle->IsSameDocument();

  navigation_data.same_origin_status = UkmSource::NavigationData::
      SourceSameOriginStatus::SOURCE_SAME_ORIGIN_STATUS_UNSET;
  // Only set the same origin flag for committed non-error,
  // non-same-document navigations.
  if (navigation_handle->HasCommitted() && !navigation_handle->IsErrorPage() &&
      !navigation_handle->IsSameDocument()) {
    navigation_data.same_origin_status =
        navigation_handle->IsSameOrigin()
            ? UkmSource::NavigationData::SourceSameOriginStatus::
                  SOURCE_SAME_ORIGIN
            : UkmSource::NavigationData::SourceSameOriginStatus::
                  SOURCE_CROSS_ORIGIN;
  }
  navigation_data.is_renderer_initiated =
      navigation_handle->IsRendererInitiated();
  navigation_data.is_error_page = navigation_handle->IsErrorPage();

  navigation_data.previous_source_id =
      last_committed_full_navigation_source_id_;

  navigation_data.navigation_time = navigation_handle->NavigationStart();

  // If the last_committed_full_navigation_or_same_document_source_id_ isn't
  // equal to the last_committed_full_navigation_source_id_, it indicates the
  // previous source was a same document navigation.
  const bool previous_source_was_same_document_navigation =
      last_committed_full_navigation_or_same_document_source_id_ !=
      last_committed_full_navigation_source_id_;
  if (previous_source_was_same_document_navigation) {
    navigation_data.previous_same_document_source_id =
        last_committed_full_navigation_or_same_document_source_id_;
  }
  navigation_data.opener_source_id = opener_source_id_;
  navigation_data.tab_id = tab_id_;

  const ukm::SourceId source_id = ukm::ConvertToSourceId(
      navigation_handle->GetNavigationId(), ukm::SourceIdType::NAVIGATION_ID);
  ukm_recorder->RecordNavigation(source_id, navigation_data);
}

}  // namespace internal

void InitializeSourceUrlRecorderForWebContents(
    content::WebContents* web_contents) {
  internal::SourceUrlRecorderWebContentsObserver::CreateForWebContents(
      web_contents);
}

}  // namespace ukm