File: device_connection_tracker.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 (147 lines) | stat: -rw-r--r-- 4,438 bytes parent folder | download | duplicates (6)
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
// Copyright 2023 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/device_notifications/device_connection_tracker.h"

#include <string>

#include "base/containers/contains.h"
#include "base/notreached.h"
#include "build/build_config.h"
#include "chrome/browser/device_notifications/device_system_tray_icon.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/chrome_pages.h"
#include "content/public/browser/browser_thread.h"
#include "extensions/buildflags/buildflags.h"
#include "url/origin.h"

#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "extensions/browser/extension_registry.h"
#include "extensions/common/constants.h"
#endif  // BUILDFLAG(ENABLE_EXTENSIONS)

namespace {

using base::TimeTicks;

std::string GetOriginName(Profile* profile, const url::Origin& origin) {
#if BUILDFLAG(ENABLE_EXTENSIONS)
  if (origin.scheme() == extensions::kExtensionScheme) {
    const auto* extension_registry =
        extensions::ExtensionRegistry::Get(profile);
    CHECK(extension_registry);
    const extensions::Extension* extension =
        extension_registry->GetExtensionById(
            origin.host(), extensions::ExtensionRegistry::EVERYTHING);
    // The extension must be installed if we are generating the name.
    CHECK(extension);
    return extension->name();
  }
#endif  // BUILDFLAG(ENABLE_EXTENSIONS)
  NOTREACHED();
}

}  // namespace

DeviceConnectionTracker::DeviceConnectionTracker(Profile* profile)
    : profile_(profile) {}

DeviceConnectionTracker::~DeviceConnectionTracker() {
  CleanUp();
}

void DeviceConnectionTracker::IncrementConnectionCount(
    const url::Origin& origin) {
  if (base::Contains(whitelisted_origins_, origin)) {
    return;
  }
  bool to_stage_profile = origins_.empty();
  auto& state = origins_[origin];

  CHECK_GE(state.count, 0);
  if (state.count == 0) {
    state.name = GetOriginName(profile_, origin);
  }
  ++state.count;
  state.timestamp = TimeTicks::Now();
  ++total_connection_count_;

  auto* system_tray_icon = GetSystemTrayIcon();
  if (!system_tray_icon) {
    return;
  }
  if (to_stage_profile) {
    system_tray_icon->StageProfile(profile_);
  } else {
    system_tray_icon->NotifyConnectionCountUpdated(profile_);
  }
}

void DeviceConnectionTracker::DecrementConnectionCount(
    const url::Origin& origin) {
  if (base::Contains(whitelisted_origins_, origin)) {
    return;
  }
  auto it = origins_.find(origin);
  CHECK(it != origins_.end());

  auto& state = it->second;
  CHECK_GT(state.count, 0);
  --state.count;
  state.timestamp = TimeTicks::Now();
  --total_connection_count_;
  if (state.count == 0) {
    content::BrowserThread::GetTaskRunnerForThread(content::BrowserThread::UI)
        ->PostDelayedTask(
            FROM_HERE,
            base::BindOnce(&DeviceConnectionTracker::CleanUpOrigin,
                           weak_factory_.GetWeakPtr(), origin, state.timestamp),
            kOriginInactiveTime);
  }

  auto* system_tray_icon = GetSystemTrayIcon();
  if (!system_tray_icon) {
    return;
  }
  system_tray_icon->NotifyConnectionCountUpdated(profile_);
}

void DeviceConnectionTracker::ShowSiteSettings(const url::Origin& origin) {
  chrome::ShowSiteSettings(profile_, origin.GetURL());
}

void DeviceConnectionTracker::CleanUp() {
  if (!origins_.empty()) {
    origins_.clear();
    total_connection_count_ = 0;
    auto* system_tray_icon = GetSystemTrayIcon();
    if (system_tray_icon) {
      system_tray_icon->UnstageProfile(profile_, /*immediate=*/true);
    }
  }
}

void DeviceConnectionTracker::CleanUpOrigin(const url::Origin& origin,
                                            const TimeTicks& timestamp) {
  auto it = origins_.find(origin);
  if (it == origins_.end()) {
    // This can happen if the connection bounces within 1 microsecond, which is
    // the base unit of base::TimeTicks. The first CleanUpOrigin call will clear
    // the origin because it sees the timestamp as the same.
    return;
  }
  auto& state = it->second;
  if (state.count == 0 && state.timestamp == timestamp) {
    origins_.erase(it);
    auto* system_tray_icon = GetSystemTrayIcon();
    if (!system_tray_icon) {
      return;
    }
    if (origins_.empty()) {
      system_tray_icon->UnstageProfile(profile_, /*immediate=*/true);
    } else {
      system_tray_icon->NotifyConnectionCountUpdated(profile_);
    }
  }
}