blob: a74ad20b3df93e73a72fba43ec9bd6c3d5e404af [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2020 The Chromium Authors
Sebastien Marchand578251d2020-12-11 03:44:432// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
François Doray9a27b01b2024-03-12 22:23:215#include "components/performance_manager/freezing/freezer.h"
Sebastien Marchand578251d2020-12-11 03:44:436
Avi Drissman9269d4ed2023-01-07 01:38:067#include "base/functional/bind.h"
Joe Mason6bd357612024-06-04 22:05:558#include "base/memory/weak_ptr.h"
Sebastien Marchand578251d2020-12-11 03:44:439#include "base/task/task_traits.h"
Sebastien Marchand578251d2020-12-11 03:44:4310#include "components/content_settings/core/common/content_settings_types.h"
11#include "components/performance_manager/public/graph/page_node.h"
François Doray9a27b01b2024-03-12 22:23:2112#include "content/public/browser/browser_context.h"
Sebastien Marchand578251d2020-12-11 03:44:4313#include "content/public/browser/browser_task_traits.h"
14#include "content/public/browser/browser_thread.h"
Illia Klimov57d65b8a2022-03-25 20:27:4315#include "content/public/browser/permission_controller.h"
Charlene Yan753d7462021-01-26 23:39:5216#include "content/public/browser/visibility.h"
Sebastien Marchand578251d2020-12-11 03:44:4317#include "content/public/browser/web_contents.h"
Andy Paicua6d6d852022-04-28 18:08:3618#include "third_party/blink/public/common/permissions/permission_utils.h"
Sebastien Marchand578251d2020-12-11 03:44:4319
20namespace performance_manager {
Sebastien Marchand578251d2020-12-11 03:44:4321namespace {
22
23// Try to freeze a page on the UI thread.
Joe Mason6bd357612024-06-04 22:05:5524void MaybeFreezePageOnUIThread(base::WeakPtr<content::WebContents> contents) {
Sebastien Marchand578251d2020-12-11 03:44:4325 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
François Doraydaea98c2024-02-22 17:09:2926 if (!contents) {
Sebastien Marchandbed434c2021-03-25 19:37:2427 return;
François Doraydaea98c2024-02-22 17:09:2928 }
Sebastien Marchand578251d2020-12-11 03:44:4329
Illia Klimov57d65b8a2022-03-25 20:27:4330 content::PermissionController* permission_controller =
31 contents->GetBrowserContext()->GetPermissionController();
Illia Klimov7709f0c2021-07-05 09:42:3932
Sebastien Marchand578251d2020-12-11 03:44:4333 // Page with the notification permission shouldn't be frozen as this is a
34 // strong signal that the user wants to receive updates from this page while
35 // it's in background. This information isn't available in the PM graph, this
36 // has to be checked on the UI thread.
Illia Klimov57d65b8a2022-03-25 20:27:4337 if (permission_controller->GetPermissionStatusForCurrentDocument(
Dave Tapuska6aea6e22022-06-06 21:21:5138 blink::PermissionType::NOTIFICATIONS,
39 contents->GetPrimaryMainFrame()) ==
Illia Klimov57d65b8a2022-03-25 20:27:4340 blink::mojom::PermissionStatus::GRANTED) {
Sebastien Marchandbed434c2021-03-25 19:37:2441 return;
Illia Klimov57d65b8a2022-03-25 20:27:4342 }
Sebastien Marchand578251d2020-12-11 03:44:4343
Dave Tapuska7e8c023b2023-11-09 02:09:0844 // A visible page should not be frozen.
45 if (contents->GetVisibility() == content::Visibility::VISIBLE) {
46 return;
47 }
48
Sebastien Marchand578251d2020-12-11 03:44:4349 contents->SetPageFrozen(true);
50}
51
Joe Mason6bd357612024-06-04 22:05:5552void UnfreezePageOnUIThread(base::WeakPtr<content::WebContents> contents) {
Sebastien Marchand578251d2020-12-11 03:44:4353 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
Joe Mason6bd357612024-06-04 22:05:5554 if (!contents) {
Sebastien Marchand578251d2020-12-11 03:44:4355 return;
François Doraydaea98c2024-02-22 17:09:2956 }
Sebastien Marchand578251d2020-12-11 03:44:4357
Charlene Yan753d7462021-01-26 23:39:5258 // A visible page is automatically unfrozen.
Joe Mason6bd357612024-06-04 22:05:5559 if (contents->GetVisibility() == content::Visibility::VISIBLE) {
Charlene Yan753d7462021-01-26 23:39:5260 return;
François Doraydaea98c2024-02-22 17:09:2961 }
Charlene Yan753d7462021-01-26 23:39:5262
Joe Mason6bd357612024-06-04 22:05:5563 contents->SetPageFrozen(false);
Sebastien Marchand578251d2020-12-11 03:44:4364}
65
66} // namespace
67
François Doraydaea98c2024-02-22 17:09:2968void Freezer::MaybeFreezePageNode(const PageNode* page_node) {
Sebastien Marchand578251d2020-12-11 03:44:4369 DCHECK(page_node);
70 content::GetUIThreadTaskRunner({})->PostTask(
Joe Mason6bd357612024-06-04 22:05:5571 FROM_HERE,
72 base::BindOnce(&MaybeFreezePageOnUIThread, page_node->GetWebContents()));
Sebastien Marchand578251d2020-12-11 03:44:4373}
74
François Doraydaea98c2024-02-22 17:09:2975void Freezer::UnfreezePageNode(const PageNode* page_node) {
Sebastien Marchand578251d2020-12-11 03:44:4376 DCHECK(page_node);
77 content::GetUIThreadTaskRunner({})->PostTask(
78 FROM_HERE,
Joe Mason6bd357612024-06-04 22:05:5579 base::BindOnce(&UnfreezePageOnUIThread, page_node->GetWebContents()));
Sebastien Marchand578251d2020-12-11 03:44:4380}
81
Sebastien Marchand578251d2020-12-11 03:44:4382} // namespace performance_manager