blob: 5b37fce305f2aec8ec7dcb928926ab8c740365bc [file] [log] [blame]
Jack Hsiehaba560282022-12-12 17:36:531// Copyright 2022 The Chromium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/hid/hid_connection_tracker.h"
6
7#include <string>
8
9#include "base/notreached.h"
10#include "base/strings/strcat.h"
11#include "base/strings/utf_string_conversions.h"
12#include "build/build_config.h"
13#include "chrome/browser/browser_process.h"
Jack Hsiehea76b94b52022-12-13 16:37:0514#include "chrome/browser/hid/hid_system_tray_icon.h"
Jack Hsiehaba560282022-12-12 17:36:5315#include "chrome/browser/notifications/notification_display_service.h"
16#include "chrome/browser/profiles/profile.h"
17#include "chrome/browser/ui/chrome_pages.h"
18#include "chrome/grit/chromium_strings.h"
19#include "chrome/grit/generated_resources.h"
20#include "components/vector_icons/vector_icons.h"
21#include "extensions/buildflags/buildflags.h"
22#include "ui/base/l10n/l10n_util.h"
23#include "ui/message_center/public/cpp/notification.h"
24#include "ui/message_center/public/cpp/notification_delegate.h"
25#include "url/origin.h"
26
27#if BUILDFLAG(ENABLE_EXTENSIONS)
28#include "extensions/browser/extension_registry.h"
29#include "extensions/common/constants.h"
30#endif // BUILDFLAG(ENABLE_EXTENSIONS)
31
Jack Hsiehaba560282022-12-12 17:36:5332HidConnectionTracker::HidConnectionTracker(Profile* profile)
33 : profile_(profile) {}
34
35HidConnectionTracker::~HidConnectionTracker() {
36 CleanUp();
37}
38
Jack Hsiehe777b292023-04-15 07:26:4539void HidConnectionTracker::IncrementConnectionCount(const url::Origin& origin) {
Jack Hsiehe1371152023-04-15 10:04:2240 CHECK_GE(origins_[origin], 0);
41 origins_[origin]++;
42 total_connection_count_++;
43
Jack Hsiehea76b94b52022-12-13 16:37:0544 auto* hid_system_tray_icon = g_browser_process->hid_system_tray_icon();
45 if (!hid_system_tray_icon) {
46 return;
47 }
Jack Hsiehe1371152023-04-15 10:04:2248 if (total_connection_count_ == 1) {
Jack Hsieh2ba757f2023-03-25 03:50:4349 hid_system_tray_icon->StageProfile(profile_);
Jack Hsiehea76b94b52022-12-13 16:37:0550 } else {
51 hid_system_tray_icon->NotifyConnectionCountUpdated(profile_);
52 }
Jack Hsiehaba560282022-12-12 17:36:5353}
54
Jack Hsiehe777b292023-04-15 07:26:4555void HidConnectionTracker::DecrementConnectionCount(const url::Origin& origin) {
Jack Hsiehe1371152023-04-15 10:04:2256 auto it = origins_.find(origin);
57 CHECK(it != origins_.end());
58 auto& connection_count = it->second;
59 CHECK_GT(connection_count, 0);
60
61 connection_count--;
62 total_connection_count_--;
63 if (connection_count == 0) {
64 origins_.erase(it);
65 }
66
Jack Hsiehea76b94b52022-12-13 16:37:0567 auto* hid_system_tray_icon = g_browser_process->hid_system_tray_icon();
68 if (!hid_system_tray_icon) {
69 return;
70 }
Jack Hsiehe1371152023-04-15 10:04:2271 if (total_connection_count_ == 0) {
Jack Hsieh2ba757f2023-03-25 03:50:4372 hid_system_tray_icon->UnstageProfile(profile_, /*immediate=*/false);
Jack Hsiehea76b94b52022-12-13 16:37:0573 } else {
74 hid_system_tray_icon->NotifyConnectionCountUpdated(profile_);
75 }
Jack Hsiehaba560282022-12-12 17:36:5376}
77
Jack Hsiehe1371152023-04-15 10:04:2278void HidConnectionTracker::ShowContentSettingsExceptions() {
Jack Hsiehaba560282022-12-12 17:36:5379 chrome::ShowContentSettingsExceptionsForProfile(
80 profile_, ContentSettingsType::HID_CHOOSER_DATA);
81}
82
83void HidConnectionTracker::ShowSiteSettings(const url::Origin& origin) {
84 chrome::ShowSiteSettings(profile_, origin.GetURL());
85}
86
87void HidConnectionTracker::CleanUp() {
Jack Hsiehe1371152023-04-15 10:04:2288 if (!origins_.empty()) {
89 origins_.clear();
90 total_connection_count_ = 0;
Jack Hsiehea76b94b52022-12-13 16:37:0591 auto* hid_system_tray_icon = g_browser_process->hid_system_tray_icon();
Jack Hsiehe1371152023-04-15 10:04:2292 if (hid_system_tray_icon) {
Jack Hsieh2ba757f2023-03-25 03:50:4393 hid_system_tray_icon->UnstageProfile(profile_, /*immediate=*/true);
Jack Hsiehe1371152023-04-15 10:04:2294 }
Jack Hsiehaba560282022-12-12 17:36:5395 }
96}