Foromo Daniel Soromou | 67860ff | 2025-05-05 15:38:33 | [diff] [blame] | 1 | // Copyright 2025 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/win/installer_downloader/installer_downloader_controller.h" |
| 6 | |
| 7 | #include <memory> |
| 8 | #include <optional> |
| 9 | #include <utility> |
| 10 | |
Foromo Daniel Soromou | 3646aa8 | 2025-05-22 17:07:27 | [diff] [blame] | 11 | #include "base/base_paths.h" |
Foromo Daniel Soromou | 615461c | 2025-05-16 14:05:31 | [diff] [blame] | 12 | #include "base/check_deref.h" |
Foromo Daniel Soromou | cfa8f7b | 2025-05-23 19:25:38 | [diff] [blame] | 13 | #include "base/check_is_test.h" |
Foromo Daniel Soromou | 67860ff | 2025-05-05 15:38:33 | [diff] [blame] | 14 | #include "base/files/file_path.h" |
Foromo Daniel Soromou | badfb80 | 2025-05-05 16:22:04 | [diff] [blame] | 15 | #include "base/functional/bind.h" |
| 16 | #include "base/functional/callback.h" |
Foromo Daniel Soromou | cfa8f7b | 2025-05-23 19:25:38 | [diff] [blame] | 17 | #include "base/metrics/histogram_functions.h" |
Foromo Daniel Soromou | 3646aa8 | 2025-05-22 17:07:27 | [diff] [blame] | 18 | #include "base/path_service.h" |
Foromo Daniel Soromou | ce65be0 | 2025-05-22 14:14:06 | [diff] [blame] | 19 | #include "base/strings/string_util.h" |
| 20 | #include "base/uuid.h" |
Foromo Daniel Soromou | ed9d588 | 2025-05-22 14:23:52 | [diff] [blame] | 21 | #include "chrome/browser/profiles/keep_alive/profile_keep_alive_types.h" |
| 22 | #include "chrome/browser/profiles/keep_alive/scoped_profile_keep_alive.h" |
| 23 | #include "chrome/browser/profiles/profile.h" |
Foromo Daniel Soromou | 615461c | 2025-05-16 14:05:31 | [diff] [blame] | 24 | #include "chrome/browser/ui/browser_window/public/browser_window_interface.h" |
| 25 | #include "chrome/browser/ui/browser_window/public/browser_window_interface_iterator.h" |
Foromo Daniel Soromou | ce65be0 | 2025-05-22 14:14:06 | [diff] [blame] | 26 | #include "chrome/browser/win/installer_downloader/installer_downloader_feature.h" |
Foromo Daniel Soromou | 9c11601 | 2025-05-23 16:02:09 | [diff] [blame] | 27 | #include "chrome/browser/win/installer_downloader/installer_downloader_infobar_window_active_tab_tracker.h" |
Foromo Daniel Soromou | 67860ff | 2025-05-05 15:38:33 | [diff] [blame] | 28 | #include "chrome/browser/win/installer_downloader/installer_downloader_model.h" |
| 29 | #include "chrome/browser/win/installer_downloader/installer_downloader_model_impl.h" |
Foromo Daniel Soromou | f0d01d5 | 2025-05-08 15:25:41 | [diff] [blame] | 30 | #include "chrome/browser/win/installer_downloader/system_info_provider_impl.h" |
Foromo Daniel Soromou | 9c11601 | 2025-05-23 16:02:09 | [diff] [blame] | 31 | #include "components/infobars/content/content_infobar_manager.h" |
| 32 | #include "components/infobars/core/confirm_infobar_delegate.h" |
| 33 | #include "components/infobars/core/infobar.h" |
Foromo Daniel Soromou | 615461c | 2025-05-16 14:05:31 | [diff] [blame] | 34 | #include "components/tabs/public/tab_interface.h" |
Foromo Daniel Soromou | ce65be0 | 2025-05-22 14:14:06 | [diff] [blame] | 35 | #include "content/public/browser/browser_context.h" |
| 36 | #include "content/public/browser/download_manager.h" |
Foromo Daniel Soromou | 615461c | 2025-05-16 14:05:31 | [diff] [blame] | 37 | #include "content/public/browser/web_contents.h" |
Foromo Daniel Soromou | ce65be0 | 2025-05-22 14:14:06 | [diff] [blame] | 38 | #include "url/gurl.h" |
Foromo Daniel Soromou | 67860ff | 2025-05-05 15:38:33 | [diff] [blame] | 39 | |
| 40 | namespace installer_downloader { |
| 41 | |
Foromo Daniel Soromou | 615461c | 2025-05-16 14:05:31 | [diff] [blame] | 42 | namespace { |
| 43 | |
| 44 | content::WebContents* GetActiveWebContents() { |
| 45 | for (BrowserWindowInterface* browser : GetAllBrowserWindowInterfaces()) { |
| 46 | if (!browser->IsActive() || |
| 47 | browser->GetType() != BrowserWindowInterface::Type::TYPE_NORMAL) { |
| 48 | continue; |
| 49 | } |
| 50 | |
| 51 | return CHECK_DEREF(browser->GetActiveTabInterface()).GetContents(); |
| 52 | } |
| 53 | |
| 54 | return nullptr; |
Foromo Daniel Soromou | 67860ff | 2025-05-05 15:38:33 | [diff] [blame] | 55 | } |
| 56 | |
Foromo Daniel Soromou | ce65be0 | 2025-05-22 14:14:06 | [diff] [blame] | 57 | std::optional<GURL> BuildInstallerDownloadUrl(bool is_metrics_enabled) { |
| 58 | std::string installer_url_template = kInstallerUrlTemplateParam.Get(); |
| 59 | |
| 60 | base::ReplaceFirstSubstringAfterOffset( |
| 61 | &installer_url_template, /*start_offset=*/0, "IIDGUID", |
| 62 | base::Uuid::GenerateRandomV4().AsLowercaseString()); |
| 63 | |
| 64 | base::ReplaceFirstSubstringAfterOffset(&installer_url_template, |
| 65 | /*start_offset=*/0, "STATS", |
| 66 | is_metrics_enabled ? "1" : "0"); |
| 67 | |
| 68 | GURL installer_url(installer_url_template); |
| 69 | |
| 70 | return installer_url.is_valid() |
| 71 | ? std::optional<GURL>(std::move(installer_url)) |
| 72 | : std::nullopt; |
| 73 | } |
| 74 | |
Foromo Daniel Soromou | 615461c | 2025-05-16 14:05:31 | [diff] [blame] | 75 | } // namespace |
| 76 | |
| 77 | InstallerDownloaderController::InstallerDownloaderController( |
Foromo Daniel Soromou | ce65be0 | 2025-05-22 14:14:06 | [diff] [blame] | 78 | ShowInfobarCallback show_infobar_callback, |
| 79 | base::RepeatingCallback<bool()> is_metrics_enabled_callback) |
| 80 | : is_metrics_enabled_callback_(std::move(is_metrics_enabled_callback)), |
| 81 | show_infobar_callback_(std::move(show_infobar_callback)), |
Foromo Daniel Soromou | 615461c | 2025-05-16 14:05:31 | [diff] [blame] | 82 | model_(std::make_unique<InstallerDownloaderModelImpl>( |
| 83 | std::make_unique<SystemInfoProviderImpl>())), |
| 84 | get_active_web_contents_callback_( |
Foromo Daniel Soromou | 9c11601 | 2025-05-23 16:02:09 | [diff] [blame] | 85 | base::BindRepeating(&GetActiveWebContents)) { |
| 86 | RegisterBrowserWindowEvents(); |
| 87 | } |
Foromo Daniel Soromou | 615461c | 2025-05-16 14:05:31 | [diff] [blame] | 88 | |
| 89 | InstallerDownloaderController::InstallerDownloaderController( |
| 90 | ShowInfobarCallback show_infobar_callback, |
Foromo Daniel Soromou | ce65be0 | 2025-05-22 14:14:06 | [diff] [blame] | 91 | base::RepeatingCallback<bool()> is_metrics_enabled_callback, |
Foromo Daniel Soromou | 615461c | 2025-05-16 14:05:31 | [diff] [blame] | 92 | std::unique_ptr<InstallerDownloaderModel> model) |
Foromo Daniel Soromou | ce65be0 | 2025-05-22 14:14:06 | [diff] [blame] | 93 | : is_metrics_enabled_callback_(std::move(is_metrics_enabled_callback)), |
| 94 | show_infobar_callback_(std::move(show_infobar_callback)), |
Foromo Daniel Soromou | 615461c | 2025-05-16 14:05:31 | [diff] [blame] | 95 | model_(std::move(model)), |
| 96 | get_active_web_contents_callback_( |
Foromo Daniel Soromou | 9c11601 | 2025-05-23 16:02:09 | [diff] [blame] | 97 | base::BindRepeating(&GetActiveWebContents)) { |
| 98 | RegisterBrowserWindowEvents(); |
| 99 | } |
| 100 | |
| 101 | void InstallerDownloaderController::RegisterBrowserWindowEvents() { |
| 102 | active_window_subscription_ = |
| 103 | window_tracker_.RegisterActiveWindowChangedCallback(base::BindRepeating( |
| 104 | &InstallerDownloaderController::OnActiveBrowserWindowChanged, |
| 105 | base::Unretained(this))); |
| 106 | |
| 107 | removed_window_subscription_ = |
| 108 | window_tracker_.RegisterRemovedWindowCallback(base::BindRepeating( |
| 109 | &InstallerDownloaderController::OnRemovedBrowserWindow, |
| 110 | base::Unretained(this))); |
| 111 | } |
Foromo Daniel Soromou | 615461c | 2025-05-16 14:05:31 | [diff] [blame] | 112 | |
Foromo Daniel Soromou | 67860ff | 2025-05-05 15:38:33 | [diff] [blame] | 113 | InstallerDownloaderController::~InstallerDownloaderController() = default; |
| 114 | |
Foromo Daniel Soromou | 9c11601 | 2025-05-23 16:02:09 | [diff] [blame] | 115 | void InstallerDownloaderController::OnActiveBrowserWindowChanged( |
| 116 | BrowserWindowInterface* bwi) { |
| 117 | // This can be null during the startup or when the last window is closed. |
| 118 | if (!bwi) { |
| 119 | return; |
| 120 | } |
Foromo Daniel Soromou | badfb80 | 2025-05-05 16:22:04 | [diff] [blame] | 121 | |
Foromo Daniel Soromou | 9c11601 | 2025-05-23 16:02:09 | [diff] [blame] | 122 | if (bwi_and_active_tab_tracker_map_.contains(bwi)) { |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | bwi_and_active_tab_tracker_map_[bwi] = |
| 127 | std::make_unique<InstallerDownloaderInfobarWindowActiveTabTracker>( |
| 128 | bwi, |
| 129 | base::BindRepeating(&InstallerDownloaderController::MaybeShowInfoBar, |
| 130 | base::Unretained(this))); |
| 131 | } |
| 132 | |
| 133 | void InstallerDownloaderController::OnRemovedBrowserWindow( |
| 134 | BrowserWindowInterface* bwi) { |
| 135 | if (!bwi_and_active_tab_tracker_map_.contains(bwi)) { |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | bwi_and_active_tab_tracker_map_.erase(bwi); |
| 140 | } |
| 141 | |
| 142 | void InstallerDownloaderController::MaybeShowInfoBar() { |
Foromo Daniel Soromou | badfb80 | 2025-05-05 16:22:04 | [diff] [blame] | 143 | // The max show count of the infobar have been reached. Eligibility check is |
| 144 | // no longer needed. |
| 145 | if (model_->IsMaxShowCountReached()) { |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | model_->CheckEligibility( |
| 150 | base::BindOnce(&InstallerDownloaderController::OnEligibilityReady, |
| 151 | base::Unretained(this))); |
Foromo Daniel Soromou | 67860ff | 2025-05-05 15:38:33 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | void InstallerDownloaderController::OnEligibilityReady( |
Foromo Daniel Soromou | 3646aa8 | 2025-05-22 17:07:27 | [diff] [blame] | 155 | std::optional<base::FilePath> destination) { |
Foromo Daniel Soromou | 9c11601 | 2025-05-23 16:02:09 | [diff] [blame] | 156 | if (infobar_closed_) { |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | auto* contents = get_active_web_contents_callback_.Run(); |
| 161 | |
| 162 | if (!contents) { |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | if (visible_infobars_web_contents_.contains(contents)) { |
| 167 | return; |
| 168 | } |
| 169 | |
Foromo Daniel Soromou | 3646aa8 | 2025-05-22 17:07:27 | [diff] [blame] | 170 | // Early return when we have no destination and bypass is not allowed. |
| 171 | if (!destination.has_value() && !model_->ShouldByPassEligibilityCheck()) { |
Foromo Daniel Soromou | 615461c | 2025-05-16 14:05:31 | [diff] [blame] | 172 | return; |
| 173 | } |
| 174 | |
Foromo Daniel Soromou | 3646aa8 | 2025-05-22 17:07:27 | [diff] [blame] | 175 | // Compute a fallback destination (user’s Desktop) when bypassing eligibility. |
| 176 | if (!destination) { |
| 177 | base::FilePath desktop_path; |
| 178 | if (!base::PathService::Get(base::DIR_USER_DESKTOP, &desktop_path)) { |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | destination = std::move(desktop_path); |
| 183 | } |
| 184 | |
Foromo Daniel Soromou | 9c11601 | 2025-05-23 16:02:09 | [diff] [blame] | 185 | infobars::ContentInfoBarManager* infobar_manager = |
| 186 | infobars::ContentInfoBarManager::FromWebContents(contents); |
Foromo Daniel Soromou | 615461c | 2025-05-16 14:05:31 | [diff] [blame] | 187 | |
| 188 | // Installer Downloader is a global feature, therefore it's guaranteed that |
| 189 | // InstallerDownloaderController will be alive at any point during the browser |
| 190 | // runtime. |
Foromo Daniel Soromou | 9c11601 | 2025-05-23 16:02:09 | [diff] [blame] | 191 | infobars::InfoBar* infobar = show_infobar_callback_.Run( |
| 192 | infobar_manager, |
Muhammad Salmaan | dadc2298 | 2025-05-20 20:31:26 | [diff] [blame] | 193 | base::BindOnce(&InstallerDownloaderController::OnDownloadRequestAccepted, |
Foromo Daniel Soromou | ce65be0 | 2025-05-22 14:14:06 | [diff] [blame] | 194 | base::Unretained(this), destination.value()), |
Muhammad Salmaan | dadc2298 | 2025-05-20 20:31:26 | [diff] [blame] | 195 | base::BindOnce(&InstallerDownloaderController::OnInfoBarDismissed, |
| 196 | base::Unretained(this))); |
Foromo Daniel Soromou | 9c11601 | 2025-05-23 16:02:09 | [diff] [blame] | 197 | |
| 198 | if (!infobar) { |
| 199 | return; |
| 200 | } |
| 201 | |
Foromo Daniel Soromou | cfa8f7b | 2025-05-23 19:25:38 | [diff] [blame] | 202 | if (infobar_manager) { |
| 203 | infobar_manager->AddObserver(this); |
| 204 | } else { |
| 205 | CHECK_IS_TEST(); |
| 206 | } |
| 207 | |
Foromo Daniel Soromou | 9c11601 | 2025-05-23 16:02:09 | [diff] [blame] | 208 | visible_infobars_web_contents_[contents] = infobar; |
Foromo Daniel Soromou | cfa8f7b | 2025-05-23 19:25:38 | [diff] [blame] | 209 | |
| 210 | // This is the first show in this browser session. |
| 211 | if (visible_infobars_web_contents_.size() == 1u) { |
| 212 | model_->IncrementShowCount(); |
| 213 | base::UmaHistogramBoolean("Windows.InstallerDownloader.InfobarShown", |
| 214 | /*shown=*/true); |
| 215 | } |
Foromo Daniel Soromou | 9c11601 | 2025-05-23 16:02:09 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | void InstallerDownloaderController::OnInfoBarRemoved(infobars::InfoBar* infobar, |
| 219 | bool animate) { |
| 220 | auto it = std::find_if( |
| 221 | visible_infobars_web_contents_.begin(), |
| 222 | visible_infobars_web_contents_.end(), |
| 223 | [infobar](const auto& entry) { return entry.second == infobar; }); |
| 224 | |
| 225 | if (it == visible_infobars_web_contents_.end()) { |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | it->second->owner()->RemoveObserver(this); |
| 230 | visible_infobars_web_contents_.erase(it); |
| 231 | |
| 232 | if (!user_initiated_info_bar_close_pending_) { |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | for (auto [contents, infobar_instance] : visible_infobars_web_contents_) { |
| 237 | infobar_instance->owner()->RemoveObserver(this); |
| 238 | infobar_instance->RemoveSelf(); |
| 239 | } |
| 240 | |
| 241 | visible_infobars_web_contents_.clear(); |
| 242 | infobar_closed_ = true; |
Foromo Daniel Soromou | 67860ff | 2025-05-05 15:38:33 | [diff] [blame] | 243 | } |
| 244 | |
Foromo Daniel Soromou | ce65be0 | 2025-05-22 14:14:06 | [diff] [blame] | 245 | void InstallerDownloaderController::OnDownloadRequestAccepted( |
| 246 | const base::FilePath& destination) { |
Foromo Daniel Soromou | cfa8f7b | 2025-05-23 19:25:38 | [diff] [blame] | 247 | base::UmaHistogramBoolean("Windows.InstallerDownloader.RequestAccepted", |
| 248 | true); |
| 249 | |
Foromo Daniel Soromou | 9c11601 | 2025-05-23 16:02:09 | [diff] [blame] | 250 | user_initiated_info_bar_close_pending_ = true; |
Foromo Daniel Soromou | 67860ff | 2025-05-05 15:38:33 | [diff] [blame] | 251 | // User have explicitly gave download consent. Therefore, a background |
| 252 | // download should be issued. |
Foromo Daniel Soromou | ce65be0 | 2025-05-22 14:14:06 | [diff] [blame] | 253 | auto* contents = get_active_web_contents_callback_.Run(); |
| 254 | |
| 255 | if (!contents) { |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | std::optional<GURL> installer_url = |
| 260 | BuildInstallerDownloadUrl(is_metrics_enabled_callback_.Run()); |
| 261 | |
| 262 | if (!installer_url.has_value()) { |
| 263 | return; |
| 264 | } |
| 265 | |
Foromo Daniel Soromou | ed9d588 | 2025-05-22 14:23:52 | [diff] [blame] | 266 | // Keep the profile alive until the download completes. |
| 267 | auto* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
| 268 | auto keep_alive = std::make_unique<ScopedProfileKeepAlive>( |
| 269 | profile, ProfileKeepAliveOrigin::kDownloadInProgress); |
| 270 | |
Foromo Daniel Soromou | ce65be0 | 2025-05-22 14:14:06 | [diff] [blame] | 271 | model_->StartDownload( |
| 272 | installer_url.value(), destination, |
Foromo Daniel Soromou | ed9d588 | 2025-05-22 14:23:52 | [diff] [blame] | 273 | CHECK_DEREF(profile->GetDownloadManager()), |
Foromo Daniel Soromou | ce65be0 | 2025-05-22 14:14:06 | [diff] [blame] | 274 | base::BindOnce(&InstallerDownloaderController::OnDownloadCompleted, |
Foromo Daniel Soromou | ed9d588 | 2025-05-22 14:23:52 | [diff] [blame] | 275 | base::Unretained(this), std::move(keep_alive))); |
Foromo Daniel Soromou | 67860ff | 2025-05-05 15:38:33 | [diff] [blame] | 276 | } |
| 277 | |
Foromo Daniel Soromou | ed9d588 | 2025-05-22 14:23:52 | [diff] [blame] | 278 | void InstallerDownloaderController::OnDownloadCompleted( |
| 279 | std::unique_ptr<ScopedProfileKeepAlive> keep_alive, |
| 280 | bool success) { |
Foromo Daniel Soromou | cfa8f7b | 2025-05-23 19:25:38 | [diff] [blame] | 281 | base::UmaHistogramBoolean("Windows.InstallerDownloader.DownloadSucceed", |
| 282 | success); |
Foromo Daniel Soromou | 67860ff | 2025-05-05 15:38:33 | [diff] [blame] | 283 | } |
| 284 | |
Foromo Daniel Soromou | 615461c | 2025-05-16 14:05:31 | [diff] [blame] | 285 | void InstallerDownloaderController::SetActiveWebContentsCallbackForTesting( |
| 286 | GetActiveWebContentsCallback callback) { |
| 287 | get_active_web_contents_callback_ = std::move(callback); |
| 288 | } |
| 289 | |
Muhammad Salmaan | dadc2298 | 2025-05-20 20:31:26 | [diff] [blame] | 290 | void InstallerDownloaderController::OnInfoBarDismissed() { |
Foromo Daniel Soromou | cfa8f7b | 2025-05-23 19:25:38 | [diff] [blame] | 291 | base::UmaHistogramBoolean("Windows.InstallerDownloader.RequestAccepted", |
| 292 | false); |
Foromo Daniel Soromou | 9c11601 | 2025-05-23 16:02:09 | [diff] [blame] | 293 | user_initiated_info_bar_close_pending_ = true; |
Muhammad Salmaan | dadc2298 | 2025-05-20 20:31:26 | [diff] [blame] | 294 | } |
| 295 | |
Foromo Daniel Soromou | 67860ff | 2025-05-05 15:38:33 | [diff] [blame] | 296 | } // namespace installer_downloader |