Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors |
Sebastien Marchand | 578251d | 2020-12-11 03:44:43 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
François Doray | 9a27b01b | 2024-03-12 22:23:21 | [diff] [blame] | 5 | #include "components/performance_manager/freezing/freezer.h" |
Sebastien Marchand | 578251d | 2020-12-11 03:44:43 | [diff] [blame] | 6 | |
Avi Drissman | 9269d4ed | 2023-01-07 01:38:06 | [diff] [blame] | 7 | #include "base/functional/bind.h" |
Joe Mason | 6bd35761 | 2024-06-04 22:05:55 | [diff] [blame^] | 8 | #include "base/memory/weak_ptr.h" |
Sebastien Marchand | 578251d | 2020-12-11 03:44:43 | [diff] [blame] | 9 | #include "base/task/task_traits.h" |
Sebastien Marchand | 578251d | 2020-12-11 03:44:43 | [diff] [blame] | 10 | #include "components/content_settings/core/common/content_settings_types.h" |
| 11 | #include "components/performance_manager/public/graph/page_node.h" |
François Doray | 9a27b01b | 2024-03-12 22:23:21 | [diff] [blame] | 12 | #include "content/public/browser/browser_context.h" |
Sebastien Marchand | 578251d | 2020-12-11 03:44:43 | [diff] [blame] | 13 | #include "content/public/browser/browser_task_traits.h" |
| 14 | #include "content/public/browser/browser_thread.h" |
Illia Klimov | 57d65b8a | 2022-03-25 20:27:43 | [diff] [blame] | 15 | #include "content/public/browser/permission_controller.h" |
Charlene Yan | 753d746 | 2021-01-26 23:39:52 | [diff] [blame] | 16 | #include "content/public/browser/visibility.h" |
Sebastien Marchand | 578251d | 2020-12-11 03:44:43 | [diff] [blame] | 17 | #include "content/public/browser/web_contents.h" |
Andy Paicu | a6d6d85 | 2022-04-28 18:08:36 | [diff] [blame] | 18 | #include "third_party/blink/public/common/permissions/permission_utils.h" |
Sebastien Marchand | 578251d | 2020-12-11 03:44:43 | [diff] [blame] | 19 | |
| 20 | namespace performance_manager { |
Sebastien Marchand | 578251d | 2020-12-11 03:44:43 | [diff] [blame] | 21 | namespace { |
| 22 | |
| 23 | // Try to freeze a page on the UI thread. |
Joe Mason | 6bd35761 | 2024-06-04 22:05:55 | [diff] [blame^] | 24 | void MaybeFreezePageOnUIThread(base::WeakPtr<content::WebContents> contents) { |
Sebastien Marchand | 578251d | 2020-12-11 03:44:43 | [diff] [blame] | 25 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
François Doray | daea98c | 2024-02-22 17:09:29 | [diff] [blame] | 26 | if (!contents) { |
Sebastien Marchand | bed434c | 2021-03-25 19:37:24 | [diff] [blame] | 27 | return; |
François Doray | daea98c | 2024-02-22 17:09:29 | [diff] [blame] | 28 | } |
Sebastien Marchand | 578251d | 2020-12-11 03:44:43 | [diff] [blame] | 29 | |
Illia Klimov | 57d65b8a | 2022-03-25 20:27:43 | [diff] [blame] | 30 | content::PermissionController* permission_controller = |
| 31 | contents->GetBrowserContext()->GetPermissionController(); |
Illia Klimov | 7709f0c | 2021-07-05 09:42:39 | [diff] [blame] | 32 | |
Sebastien Marchand | 578251d | 2020-12-11 03:44:43 | [diff] [blame] | 33 | // 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 Klimov | 57d65b8a | 2022-03-25 20:27:43 | [diff] [blame] | 37 | if (permission_controller->GetPermissionStatusForCurrentDocument( |
Dave Tapuska | 6aea6e2 | 2022-06-06 21:21:51 | [diff] [blame] | 38 | blink::PermissionType::NOTIFICATIONS, |
| 39 | contents->GetPrimaryMainFrame()) == |
Illia Klimov | 57d65b8a | 2022-03-25 20:27:43 | [diff] [blame] | 40 | blink::mojom::PermissionStatus::GRANTED) { |
Sebastien Marchand | bed434c | 2021-03-25 19:37:24 | [diff] [blame] | 41 | return; |
Illia Klimov | 57d65b8a | 2022-03-25 20:27:43 | [diff] [blame] | 42 | } |
Sebastien Marchand | 578251d | 2020-12-11 03:44:43 | [diff] [blame] | 43 | |
Dave Tapuska | 7e8c023b | 2023-11-09 02:09:08 | [diff] [blame] | 44 | // A visible page should not be frozen. |
| 45 | if (contents->GetVisibility() == content::Visibility::VISIBLE) { |
| 46 | return; |
| 47 | } |
| 48 | |
Sebastien Marchand | 578251d | 2020-12-11 03:44:43 | [diff] [blame] | 49 | contents->SetPageFrozen(true); |
| 50 | } |
| 51 | |
Joe Mason | 6bd35761 | 2024-06-04 22:05:55 | [diff] [blame^] | 52 | void UnfreezePageOnUIThread(base::WeakPtr<content::WebContents> contents) { |
Sebastien Marchand | 578251d | 2020-12-11 03:44:43 | [diff] [blame] | 53 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
Joe Mason | 6bd35761 | 2024-06-04 22:05:55 | [diff] [blame^] | 54 | if (!contents) { |
Sebastien Marchand | 578251d | 2020-12-11 03:44:43 | [diff] [blame] | 55 | return; |
François Doray | daea98c | 2024-02-22 17:09:29 | [diff] [blame] | 56 | } |
Sebastien Marchand | 578251d | 2020-12-11 03:44:43 | [diff] [blame] | 57 | |
Charlene Yan | 753d746 | 2021-01-26 23:39:52 | [diff] [blame] | 58 | // A visible page is automatically unfrozen. |
Joe Mason | 6bd35761 | 2024-06-04 22:05:55 | [diff] [blame^] | 59 | if (contents->GetVisibility() == content::Visibility::VISIBLE) { |
Charlene Yan | 753d746 | 2021-01-26 23:39:52 | [diff] [blame] | 60 | return; |
François Doray | daea98c | 2024-02-22 17:09:29 | [diff] [blame] | 61 | } |
Charlene Yan | 753d746 | 2021-01-26 23:39:52 | [diff] [blame] | 62 | |
Joe Mason | 6bd35761 | 2024-06-04 22:05:55 | [diff] [blame^] | 63 | contents->SetPageFrozen(false); |
Sebastien Marchand | 578251d | 2020-12-11 03:44:43 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | } // namespace |
| 67 | |
François Doray | daea98c | 2024-02-22 17:09:29 | [diff] [blame] | 68 | void Freezer::MaybeFreezePageNode(const PageNode* page_node) { |
Sebastien Marchand | 578251d | 2020-12-11 03:44:43 | [diff] [blame] | 69 | DCHECK(page_node); |
| 70 | content::GetUIThreadTaskRunner({})->PostTask( |
Joe Mason | 6bd35761 | 2024-06-04 22:05:55 | [diff] [blame^] | 71 | FROM_HERE, |
| 72 | base::BindOnce(&MaybeFreezePageOnUIThread, page_node->GetWebContents())); |
Sebastien Marchand | 578251d | 2020-12-11 03:44:43 | [diff] [blame] | 73 | } |
| 74 | |
François Doray | daea98c | 2024-02-22 17:09:29 | [diff] [blame] | 75 | void Freezer::UnfreezePageNode(const PageNode* page_node) { |
Sebastien Marchand | 578251d | 2020-12-11 03:44:43 | [diff] [blame] | 76 | DCHECK(page_node); |
| 77 | content::GetUIThreadTaskRunner({})->PostTask( |
| 78 | FROM_HERE, |
Joe Mason | 6bd35761 | 2024-06-04 22:05:55 | [diff] [blame^] | 79 | base::BindOnce(&UnfreezePageOnUIThread, page_node->GetWebContents())); |
Sebastien Marchand | 578251d | 2020-12-11 03:44:43 | [diff] [blame] | 80 | } |
| 81 | |
Sebastien Marchand | 578251d | 2020-12-11 03:44:43 | [diff] [blame] | 82 | } // namespace performance_manager |