File: website_metrics.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 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 (701 lines) | stat: -rw-r--r-- 24,393 bytes parent folder | download | duplicates (3)
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/apps/app_service/metrics/website_metrics.h"

#include <random>

#include "base/containers/contains.h"
#include "base/json/values_util.h"
#include "base/rand_util.h"
#include "chrome/browser/apps/app_service/metrics/app_platform_metrics_utils.h"
#include "chrome/browser/apps/browser_instance/web_contents_instance_id_utils.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/web_applications/web_app_helpers.h"
#include "components/history/core/browser/history_types.h"
#include "components/webapps/browser/banners/installable_web_app_check_result.h"
#include "components/webapps/browser/banners/web_app_banner_data.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 "third_party/blink/public/common/manifest/manifest_util.h"
#include "third_party/blink/public/mojom/installation/installation.mojom.h"
#include "third_party/blink/public/mojom/manifest/manifest.mojom.h"
#include "ui/aura/window.h"
#include "ui/wm/core/window_util.h"
#include "ui/wm/public/activation_client.h"

namespace {

const double mean = 1.0;
const double stddev = 0.025;

std::default_random_engine random_generator(base::RandDouble());
std::normal_distribution<double> distribution(mean, stddev);

// Generate random noise following normal_distribution.
double GetRandomNoise() {
  return distribution(random_generator);
}

// Checks if a given browser is running a windowed app. It will return true for
// web apps, hosted apps, and packaged V1 apps.
bool IsAppBrowser(const Browser* browser) {
  return (browser->is_type_app() || browser->is_type_app_popup()) &&
         !web_app::GetAppIdFromApplicationName(browser->app_name()).empty();
}

aura::Window* GetWindowWithBrowser(Browser* browser) {
  if (!browser) {
    return nullptr;
  }
  BrowserWindow* browser_window = browser->window();
  // In some test cases, browser window might be skipped.
  return browser_window ? browser_window->GetNativeWindow() : nullptr;
}

aura::Window* GetWindowWithTabStripModel(TabStripModel* tab_strip_model) {
  for (Browser* browser : *BrowserList::GetInstance()) {
    if (browser->tab_strip_model() == tab_strip_model) {
      return GetWindowWithBrowser(browser);
    }
  }
  return nullptr;
}

wm::ActivationClient* GetActivationClient(aura::Window* window) {
  if (!window) {
    return nullptr;
  }

  aura::Window* root_window = window->GetRootWindow();
  if (!root_window) {
    return nullptr;
  }

  return wm::GetActivationClient(root_window);
}

bool IsSupportedUrl(const GURL& url) {
  return !url.is_empty() && url.SchemeIsHTTPOrHTTPS();
}

}  // namespace

namespace apps {

constexpr char kWebsiteUsageTime[] = "app_platform_metrics.website_usage_time";
constexpr char kRunningTimeKey[] = "time";
constexpr char kPromotableKey[] = "promotable";

WebsiteMetrics::ActiveTabWebContentsObserver::ActiveTabWebContentsObserver(
    content::WebContents* contents,
    WebsiteMetrics* owner)
    : content::WebContentsObserver(contents), owner_(owner) {}

WebsiteMetrics::ActiveTabWebContentsObserver::~ActiveTabWebContentsObserver() =
    default;

void WebsiteMetrics::ActiveTabWebContentsObserver::OnPrimaryPageChanged() {
  owner_->OnWebContentsUpdated(web_contents());

  if (app_banner_manager_observer_.IsObserving()) {
    return;
  }

  auto* app_banner_manager =
      webapps::AppBannerManager::FromWebContents(web_contents());
  // In some test cases, AppBannerManager might be null.
  if (app_banner_manager) {
    app_banner_manager_observer_.Observe(app_banner_manager);
  }
}

void WebsiteMetrics::ActiveTabWebContentsObserver::PrimaryPageChanged(
    content::Page& page) {
  OnPrimaryPageChanged();
}

void WebsiteMetrics::ActiveTabWebContentsObserver::WebContentsDestroyed() {
  app_banner_manager_observer_.Reset();
}

void WebsiteMetrics::ActiveTabWebContentsObserver::
    OnInstallableWebAppStatusUpdated(
        webapps::InstallableWebAppCheckResult result,
        const std::optional<webapps::WebAppBannerData>& data) {
  owner_->OnInstallableWebAppStatusUpdated(web_contents(), result, data);
}

WebsiteMetrics::UrlInfo::UrlInfo(const base::Value& value) {
  const base::Value::Dict* data_dict = value.GetIfDict();
  if (!data_dict) {
    return;
  }

  std::optional<base::TimeDelta> running_time_value =
      base::ValueToTimeDelta(data_dict->Find(kRunningTimeKey));
  if (!running_time_value.has_value()) {
    return;
  }

  auto promotable_value = data_dict->FindBool(kPromotableKey);
  if (!promotable_value.has_value()) {
    return;
  }

  running_time_in_two_hours = running_time_value.value();
  promotable = promotable_value.value();
}

base::Value::Dict WebsiteMetrics::UrlInfo::ConvertToDict() const {
  base::Value::Dict usage_time_dict;
  usage_time_dict.Set(kRunningTimeKey,
                      base::TimeDeltaToValue(running_time_in_two_hours));
  usage_time_dict.Set(kPromotableKey, promotable);
  return usage_time_dict;
}

WebsiteMetrics::WebsiteMetrics(Profile* profile, int user_type_by_device_type)
    : profile_(profile),
      browser_tab_strip_tracker_(this, nullptr),
      user_type_by_device_type_(user_type_by_device_type) {
  BrowserList::GetInstance()->AddObserver(this);
  browser_tab_strip_tracker_.Init();
  history::HistoryService* history_service =
      HistoryServiceFactory::GetForProfileWithoutCreating(profile);
  if (history_service) {
    history_observation_.Observe(history_service);
  }
}

WebsiteMetrics::~WebsiteMetrics() {
  BrowserList::RemoveObserver(this);

  // Also notify observers.
  for (auto& observer : observers_) {
    observer.OnWebsiteMetricsDestroyed();
  }
}

void WebsiteMetrics::OnBrowserAdded(Browser* browser) {
  if (IsAppBrowser(browser)) {
    return;
  }

  auto* window = GetWindowWithBrowser(browser);
  if (window) {
    observed_windows_.AddObservation(window);
    window_to_web_contents_[window] = nullptr;
  }
}

void WebsiteMetrics::OnTabStripModelChanged(
    TabStripModel* tab_strip_model,
    const TabStripModelChange& change,
    const TabStripSelectionChange& selection) {
  DCHECK(tab_strip_model);
  auto* window = GetWindowWithTabStripModel(tab_strip_model);
  if (!window || !base::Contains(window_to_web_contents_, window)) {
    // Skip the app browser window.
    return;
  }

  switch (change.type()) {
    case TabStripModelChange::kInserted:
      OnTabStripModelChangeInsert(window, tab_strip_model, *change.GetInsert(),
                                  selection);
      break;
    case TabStripModelChange::kRemoved:
      OnTabStripModelChangeRemove(window, tab_strip_model, *change.GetRemove(),
                                  selection);
      break;
    case TabStripModelChange::kReplaced:
      OnTabStripModelChangeReplace(*change.GetReplace());
      break;
    case TabStripModelChange::kMoved:
    case TabStripModelChange::kSelectionOnly:
      break;
  }

  if (selection.active_tab_changed()) {
    OnActiveTabChanged(window, selection.old_contents, selection.new_contents);
  }
}

void WebsiteMetrics::OnWindowActivated(ActivationReason reason,
                                       aura::Window* gained_active,
                                       aura::Window* lost_active) {
  SetWindowInActivated(lost_active);
  SetWindowActivated(gained_active);
}

void WebsiteMetrics::OnHistoryDeletions(
    history::HistoryService* history_service,
    const history::DeletionInfo& deletion_info) {
  if (deletion_info.is_from_expiration()) {
    // This is an auto-expiration of history that happens after 90 days. Any
    // data recorded here must be newer than this threshold, so ignore the
    // expiration.
    return;
  }

  // To simplify the implementation, remove all recorded urls no matter whatever
  // `deletion_info`.
  webcontents_to_ukm_key_.clear();
  url_infos_.clear();

  profile_->GetPrefs()->SetDict(kWebsiteUsageTime, base::Value::Dict());
}

void WebsiteMetrics::OnWindowDestroying(aura::Window* window) {
  if (base::Contains(window_to_web_contents_, window)) {
    window_to_web_contents_.erase(window);
  }
  observed_windows_.RemoveObservation(window);
}

void WebsiteMetrics::HistoryServiceBeingDeleted(
    history::HistoryService* history_service) {
  DCHECK(history_observation_.IsObservingSource(history_service));
  history_observation_.Reset();
}

void WebsiteMetrics::OnFiveMinutes() {
  // When the user logs in, there might be usage time for some websites saved in
  // the user pref for the last login, and they haven't been recorded yet. So
  // for the first five minutes, read the usage time saved in the user pref, and
  // record the UKM, then save the new usage time to the user pref.
  if (should_record_ukm_from_pref_) {
    RecordUsageTimeFromPref();
    should_record_ukm_from_pref_ = false;
  }

  SaveUsageTime();
}

void WebsiteMetrics::OnTwoHours() {
  RecordUsageTime();

  std::map<GURL, UrlInfo> url_infos;
  for (const auto& it : webcontents_to_ukm_key_) {
    if (!base::Contains(url_infos, it.second) && !it.second.is_empty() &&
        it.second.SchemeIsHTTPOrHTTPS()) {
      url_infos[it.second] = std::move(url_infos_[it.second]);
    }
  }
  url_infos.swap(url_infos_);
}

void WebsiteMetrics::AddObserver(WebsiteMetrics::Observer* observer) {
  observers_.AddObserver(observer);
}

void WebsiteMetrics::RemoveObserver(WebsiteMetrics::Observer* observer) {
  observers_.RemoveObserver(observer);
}

void WebsiteMetrics::MaybeObserveWindowActivationClient(aura::Window* window) {
  auto* activation_client = GetActivationClient(window);
  if (!activation_client) {
    return;
  }

  activation_client_to_windows_[activation_client].insert(window);

  if (!activation_client_observations_.IsObservingSource(activation_client)) {
    activation_client_observations_.AddObservation(activation_client);
  }
}

void WebsiteMetrics::MaybeRemoveObserveWindowActivationClient(
    aura::Window* window) {
  auto* activation_client = GetActivationClient(window);
  if (!activation_client) {
    return;
  }

  activation_client_to_windows_[activation_client].erase(window);

  // If there are other windows share the `activation_client`, we can't remove
  // the activation client observation. E.g. for browser windows in Ash, there
  // is only 1 root window, and 1 activation_client. Only when all browser
  // windows will be closed, we can remove the observation.
  if (!activation_client_to_windows_[activation_client].empty()) {
    return;
  }

  activation_client_to_windows_.erase(activation_client);
  if (activation_client_observations_.IsObservingSource(activation_client)) {
    activation_client_observations_.RemoveObservation(activation_client);
  }
}

void WebsiteMetrics::OnTabStripModelChangeInsert(
    aura::Window* window,
    TabStripModel* tab_strip_model,
    const TabStripModelChange::Insert& insert,
    const TabStripSelectionChange& selection) {
  if (insert.contents.size() == 0) {
    return;
  }

  // First tab attached.
  if (tab_strip_model->count() == static_cast<int>(insert.contents.size())) {
    MaybeObserveWindowActivationClient(window);
  }

  for (const auto& inserted_tab : insert.contents) {
    content::WebContents* contents = inserted_tab.contents;
    // The tab is new.
    if (!base::Contains(webcontents_to_observer_map_, contents)) {
      webcontents_to_observer_map_[contents] =
          std::make_unique<WebsiteMetrics::ActiveTabWebContentsObserver>(
              contents, this);
    }
  }
}

void WebsiteMetrics::OnTabStripModelChangeRemove(
    aura::Window* window,
    TabStripModel* tab_strip_model,
    const TabStripModelChange::Remove& remove,
    const TabStripSelectionChange& selection) {
  bool active_tab_removed = false;
  const auto window_it = window_to_web_contents_.find(window);
  for (const auto& removed_tab : remove.contents) {
    ::content::WebContents* const removed_contents = removed_tab.contents;
    OnTabClosed(removed_contents);
    if (window_it != window_to_web_contents_.end() &&
        window_it->second == removed_contents) {
      active_tab_removed = true;
    }
  }

  // Last tab detached.
  if (tab_strip_model->count() == 0) {
    // The browser window will be closed, so remove the window and the web
    // contents.
    if (window_it != window_to_web_contents_.end()) {
      // Only trigger `OnTabClosed` if it has not been already triggered.
      if (!active_tab_removed && window_it->second) {
        OnTabClosed(window_it->second);
      }
      window_to_web_contents_.erase(window_it);
    }
    MaybeRemoveObserveWindowActivationClient(window);
  }
}

void WebsiteMetrics::OnTabStripModelChangeReplace(
    const TabStripModelChange::Replace& replace) {
  OnTabClosed(replace.old_contents);
}

void WebsiteMetrics::OnActiveTabChanged(aura::Window* window,
                                        content::WebContents* old_contents,
                                        content::WebContents* new_contents) {
  if (old_contents) {
    SetTabInActivated(old_contents);

    // Clear `old_contents` from `window_to_web_contents_`.
    auto it = window_to_web_contents_.find(window);
    if (it != window_to_web_contents_.end()) {
      it->second = nullptr;
    }
  }

  if (new_contents) {
    window_to_web_contents_[window] = new_contents;
    // When the tab is drag to a new browser window, PrimaryPageChanged might
    // not be called, so `webcontents_to_ukm_key_` doesn't include
    // `new_contents`. So call PrimaryPageChanged to update web contents and add
    // the website url.
    if (!base::Contains(webcontents_to_ukm_key_, new_contents)) {
      auto it = webcontents_to_observer_map_.find(new_contents);
      if (it != webcontents_to_observer_map_.end()) {
        it->second->OnPrimaryPageChanged();

        auto* app_banner_manager =
            webapps::AppBannerManager::FromWebContents(new_contents);
        // In some test cases, AppBannerManager might be null.
        if (app_banner_manager) {
          it->second->OnInstallableWebAppStatusUpdated(
              app_banner_manager->GetInstallableWebAppCheckResult(),
              app_banner_manager->GetCurrentWebAppBannerData());
        }
      }
      return;
    }
    if (wm::IsActiveWindow(window)) {
      SetTabActivated(new_contents);
    }
  }
}

void WebsiteMetrics::OnTabClosed(content::WebContents* web_contents) {
  SetTabInActivated(web_contents);
  webcontents_to_ukm_key_.erase(web_contents);
  webcontents_to_observer_map_.erase(web_contents);

  // Also notify observers.
  const GURL& url = web_contents->GetVisibleURL();
  for (auto& observer : observers_) {
    observer.OnUrlClosed(url, web_contents);
  }
}

void WebsiteMetrics::OnWebContentsUpdated(content::WebContents* web_contents) {
  // If there is an app for the url, we don't need to record the url, because
  // the app metrics can record the usage time metrics. We need to ensure we
  // notify observers of previous URL being closed if we happen to be tracking
  // it.
  if (GetInstanceAppIdForWebContents(web_contents).has_value()) {
    if (const auto web_contents_it = webcontents_to_ukm_key_.find(web_contents);
        web_contents_it != webcontents_to_ukm_key_.end()) {
      for (auto& observer : observers_) {
        observer.OnUrlClosed(web_contents_it->second, web_contents);
      }
      webcontents_to_ukm_key_.erase(web_contents);
    }
    return;
  }

  auto* const window =
      GetWindowWithBrowser(tabs::TabInterface::GetFromContents(web_contents)
                               ->GetBrowserWindowInterface()
                               ->GetBrowserForMigrationOnly());
  if (!window) {
    return;
  }

  // When the primary page of `web_contents` is changed, call SetTabInActivated
  // to calculate the usage time for the previous ukm key url.
  SetTabInActivated(web_contents);
  const GURL& url = web_contents->GetVisibleURL();

  // User could have either opened the URL in a new `WebContents` or navigated
  // from a different URL in a pre-existing `WebContents`. We check for both
  // scenarios and notify observers accordingly.
  const auto web_contents_it = webcontents_to_ukm_key_.find(web_contents);
  if (web_contents_it == webcontents_to_ukm_key_.end() && IsSupportedUrl(url)) {
    // URL opened in a new `WebContent`.
    for (auto& observer : observers_) {
      observer.OnUrlOpened(url, web_contents);
    }
  }
  if (web_contents_it != webcontents_to_ukm_key_.end() &&
      web_contents_it->second != url) {
    // Content navigation in a pre-existing `WebContents`.
    const GURL& previous_url = web_contents_it->second;
    if (IsSupportedUrl(previous_url)) {
      for (auto& observer : observers_) {
        observer.OnUrlClosed(previous_url, web_contents);
      }
    }
    if (IsSupportedUrl(url)) {
      for (auto& observer : observers_) {
        observer.OnUrlOpened(url, web_contents);
      }
    }
  }

  // When the primary page of `web_contents` is changed called by
  // contents::WebContentsObserver::PrimaryPageChanged(), set the visible url as
  // default value for the ukm key url.
  webcontents_to_ukm_key_[web_contents] = url;
  if (!IsSupportedUrl(url)) {
    return;
  }

  auto it = window_to_web_contents_.find(window);
  bool is_activated = wm::IsActiveWindow(window) &&
                      it != window_to_web_contents_.end() &&
                      it->second == web_contents;
  AddUrlInfo(url, web_contents->GetPrimaryMainFrame()->GetPageUkmSourceId(),
             base::TimeTicks::Now(), is_activated, /*promotable=*/false);
}

void WebsiteMetrics::OnInstallableWebAppStatusUpdated(
    content::WebContents* web_contents,
    webapps::InstallableWebAppCheckResult result,
    const std::optional<webapps::WebAppBannerData>& data) {
  auto it = webcontents_to_ukm_key_.find(web_contents);
  if (it == webcontents_to_ukm_key_.end()) {
    // If the `web_contents` has been removed or replaced, we don't need to set
    // the url.
    return;
  }
  // WebContents in app windows are filtered out in OnBrowserAdded. Installed
  // web apps opened in tabs are filtered out too. So every WebContents here
  // must be a website not installed.
  if (result == webapps::InstallableWebAppCheckResult::kYes_Promotable) {
    UpdateUrlInfo(it->second, /*promotable=*/true);
  }
}

void WebsiteMetrics::AddUrlInfo(const GURL& url,
                                ukm::SourceId source_id,
                                const base::TimeTicks& start_time,
                                bool is_activated,
                                bool promotable) {
  auto& url_info = url_infos_[url];
  url_info.source_id = source_id;
  url_info.start_time = start_time;
  url_info.is_activated = is_activated;
  url_info.promotable = promotable;
}

void WebsiteMetrics::UpdateUrlInfo(const GURL& url, bool promotable) {
  auto it = url_infos_.find(url);
  if (it != url_infos_.end()) {
    it->second.promotable = promotable;
  }
}

void WebsiteMetrics::SetWindowActivated(aura::Window* window) {
  auto it = window_to_web_contents_.find(window);
  if (it != window_to_web_contents_.end() && it->second) {
    SetTabActivated(it->second);
  }
}

void WebsiteMetrics::SetWindowInActivated(aura::Window* window) {
  auto it = window_to_web_contents_.find(window);
  if (it != window_to_web_contents_.end() && it->second) {
    SetTabInActivated(it->second);
  }
}

void WebsiteMetrics::SetTabActivated(content::WebContents* web_contents) {
  auto web_contents_it = webcontents_to_ukm_key_.find(web_contents);
  if (web_contents_it == webcontents_to_ukm_key_.end()) {
    return;
  }
  auto url_it = url_infos_.find(web_contents_it->second);
  if (url_it == url_infos_.end()) {
    return;
  }
  url_it->second.start_time = base::TimeTicks::Now();
  url_it->second.is_activated = true;
}

void WebsiteMetrics::SetTabInActivated(content::WebContents* web_contents) {
  auto web_contents_it = webcontents_to_ukm_key_.find(web_contents);
  if (web_contents_it == webcontents_to_ukm_key_.end()) {
    return;
  }

  // Check whether `web_contents` is activated. If yes, calculate the running
  // time based on the start time set when `web_contents` is activated.
  auto it = url_infos_.find(web_contents_it->second);
  if (it == url_infos_.end() || !it->second.is_activated) {
    return;
  }

  const auto current_time = base::TimeTicks::Now();
  DCHECK_GE(current_time, it->second.start_time);
  it->second.running_time_in_five_minutes +=
      current_time - it->second.start_time;
  it->second.is_activated = false;
}

void WebsiteMetrics::SaveUsageTime() {
  base::Value::Dict dict;
  for (auto& it : url_infos_) {
    if (it.second.is_activated) {
      // Continued usage of active web content.
      const auto current_time = base::TimeTicks::Now();
      DCHECK_GE(current_time, it.second.start_time);
      it.second.running_time_in_five_minutes +=
          current_time - it.second.start_time;
      it.second.start_time = current_time;
    }

    if (!it.second.running_time_in_five_minutes.is_zero()) {
      // Notify observers before we normalize raw usage data.
      for (auto& observer : observers_) {
        observer.OnUrlUsage(it.first, it.second.running_time_in_five_minutes);
      }

      // Based on the privacy review result, randomly multiply a noise factor to
      // the raw data collected in a 5 minutes slot.
      it.second.running_time_in_two_hours +=
          GetRandomNoise() * it.second.running_time_in_five_minutes;
      it.second.running_time_in_five_minutes = base::TimeDelta();
    }
    // Save all urls running time in the past two hours to the user pref.
    if (!it.second.running_time_in_two_hours.is_zero()) {
      dict.Set(it.first.spec(), it.second.ConvertToDict());
    }
  }

  profile_->GetPrefs()->SetDict(kWebsiteUsageTime, std::move(dict));
}

void WebsiteMetrics::RecordUsageTime() {
  for (auto& it : url_infos_) {
    if (!it.second.running_time_in_two_hours.is_zero()) {
      EmitUkm(it.second.source_id,
              it.second.running_time_in_two_hours.InMilliseconds(),
              it.second.promotable,
              /*is_from_last_login=*/false);
      it.second.running_time_in_two_hours = base::TimeDelta();
    }
  }

  // The app usage time AppKMs have been recorded, so clear the saved usage time
  // in the user pref.
  profile_->GetPrefs()->SetDict(kWebsiteUsageTime, base::Value::Dict());
}

void WebsiteMetrics::RecordUsageTimeFromPref() {
  const base::Value::Dict& usage_time =
      profile_->GetPrefs()->GetDict(kWebsiteUsageTime);

  for (const auto [urlstr, url_info_value] : usage_time) {
    if (urlstr.empty()) {
      continue;
    }
    auto url = GURL(urlstr);
    if (!url.SchemeIsHTTPOrHTTPS()) {
      continue;
    }
    auto url_info = std::make_unique<UrlInfo>(url_info_value);
    if (!url_info->running_time_in_two_hours.is_zero()) {
      // For the URL records dump from the user pref, since the web_contents
      // doesn't exist due to logout/login, we can't call GetPageUkmSourceId to
      // get the source id with the web_contents. So call
      // GetSourceIdForChromeOSWebsiteURL to generate the UKM source id to log
      // saved URLs from the last login session.
      auto source_id = ukm::UkmRecorder::GetSourceIdForChromeOSWebsiteURL(
          base::PassKey<WebsiteMetrics>(), url);
      EmitUkm(source_id, url_info->running_time_in_two_hours.InMilliseconds(),
              url_info->promotable,
              /*is_from_last_login=*/true);
    }
  }
}

void WebsiteMetrics::EmitUkm(ukm::SourceId source_id,
                             int64_t usage_time,
                             bool promotable,
                             bool is_from_last_login) {
  if (source_id == ukm::kInvalidSourceId) {
    DVLOG(1) << "WebsiteMetrics::EmitUkm source id is invalid.";
    return;
  }

  ukm::builders::ChromeOS_WebsiteUsageTime builder(source_id);
  builder.SetDuration(usage_time)
      .SetIsFromLastLogin(is_from_last_login)
      .SetPromotable(promotable)
      .SetUserDeviceMatrix(user_type_by_device_type_)
      .Record(ukm::UkmRecorder::Get());
}

}  // namespace apps