Jack Hsieh | aba56028 | 2022-12-12 17:36:53 | [diff] [blame] | 1 | // 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 Hsieh | ea76b94b5 | 2022-12-13 16:37:05 | [diff] [blame] | 14 | #include "chrome/browser/hid/hid_system_tray_icon.h" |
Jack Hsieh | aba56028 | 2022-12-12 17:36:53 | [diff] [blame] | 15 | #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 Hsieh | aba56028 | 2022-12-12 17:36:53 | [diff] [blame] | 32 | HidConnectionTracker::HidConnectionTracker(Profile* profile) |
| 33 | : profile_(profile) {} |
| 34 | |
| 35 | HidConnectionTracker::~HidConnectionTracker() { |
| 36 | CleanUp(); |
| 37 | } |
| 38 | |
Jack Hsieh | e777b29 | 2023-04-15 07:26:45 | [diff] [blame] | 39 | void HidConnectionTracker::IncrementConnectionCount(const url::Origin& origin) { |
Jack Hsieh | e137115 | 2023-04-15 10:04:22 | [diff] [blame^] | 40 | CHECK_GE(origins_[origin], 0); |
| 41 | origins_[origin]++; |
| 42 | total_connection_count_++; |
| 43 | |
Jack Hsieh | ea76b94b5 | 2022-12-13 16:37:05 | [diff] [blame] | 44 | auto* hid_system_tray_icon = g_browser_process->hid_system_tray_icon(); |
| 45 | if (!hid_system_tray_icon) { |
| 46 | return; |
| 47 | } |
Jack Hsieh | e137115 | 2023-04-15 10:04:22 | [diff] [blame^] | 48 | if (total_connection_count_ == 1) { |
Jack Hsieh | 2ba757f | 2023-03-25 03:50:43 | [diff] [blame] | 49 | hid_system_tray_icon->StageProfile(profile_); |
Jack Hsieh | ea76b94b5 | 2022-12-13 16:37:05 | [diff] [blame] | 50 | } else { |
| 51 | hid_system_tray_icon->NotifyConnectionCountUpdated(profile_); |
| 52 | } |
Jack Hsieh | aba56028 | 2022-12-12 17:36:53 | [diff] [blame] | 53 | } |
| 54 | |
Jack Hsieh | e777b29 | 2023-04-15 07:26:45 | [diff] [blame] | 55 | void HidConnectionTracker::DecrementConnectionCount(const url::Origin& origin) { |
Jack Hsieh | e137115 | 2023-04-15 10:04:22 | [diff] [blame^] | 56 | 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 Hsieh | ea76b94b5 | 2022-12-13 16:37:05 | [diff] [blame] | 67 | auto* hid_system_tray_icon = g_browser_process->hid_system_tray_icon(); |
| 68 | if (!hid_system_tray_icon) { |
| 69 | return; |
| 70 | } |
Jack Hsieh | e137115 | 2023-04-15 10:04:22 | [diff] [blame^] | 71 | if (total_connection_count_ == 0) { |
Jack Hsieh | 2ba757f | 2023-03-25 03:50:43 | [diff] [blame] | 72 | hid_system_tray_icon->UnstageProfile(profile_, /*immediate=*/false); |
Jack Hsieh | ea76b94b5 | 2022-12-13 16:37:05 | [diff] [blame] | 73 | } else { |
| 74 | hid_system_tray_icon->NotifyConnectionCountUpdated(profile_); |
| 75 | } |
Jack Hsieh | aba56028 | 2022-12-12 17:36:53 | [diff] [blame] | 76 | } |
| 77 | |
Jack Hsieh | e137115 | 2023-04-15 10:04:22 | [diff] [blame^] | 78 | void HidConnectionTracker::ShowContentSettingsExceptions() { |
Jack Hsieh | aba56028 | 2022-12-12 17:36:53 | [diff] [blame] | 79 | chrome::ShowContentSettingsExceptionsForProfile( |
| 80 | profile_, ContentSettingsType::HID_CHOOSER_DATA); |
| 81 | } |
| 82 | |
| 83 | void HidConnectionTracker::ShowSiteSettings(const url::Origin& origin) { |
| 84 | chrome::ShowSiteSettings(profile_, origin.GetURL()); |
| 85 | } |
| 86 | |
| 87 | void HidConnectionTracker::CleanUp() { |
Jack Hsieh | e137115 | 2023-04-15 10:04:22 | [diff] [blame^] | 88 | if (!origins_.empty()) { |
| 89 | origins_.clear(); |
| 90 | total_connection_count_ = 0; |
Jack Hsieh | ea76b94b5 | 2022-12-13 16:37:05 | [diff] [blame] | 91 | auto* hid_system_tray_icon = g_browser_process->hid_system_tray_icon(); |
Jack Hsieh | e137115 | 2023-04-15 10:04:22 | [diff] [blame^] | 92 | if (hid_system_tray_icon) { |
Jack Hsieh | 2ba757f | 2023-03-25 03:50:43 | [diff] [blame] | 93 | hid_system_tray_icon->UnstageProfile(profile_, /*immediate=*/true); |
Jack Hsieh | e137115 | 2023-04-15 10:04:22 | [diff] [blame^] | 94 | } |
Jack Hsieh | aba56028 | 2022-12-12 17:36:53 | [diff] [blame] | 95 | } |
| 96 | } |