blob: 62bf9ecaa6c650305481dcbc35e2b36fd1059910 [file] [log] [blame]
Sebastien Marchand578251d2020-12-11 03:44:431// Copyright 2020 The Chromium Authors. All rights reserved.
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/performance_manager/mechanisms/page_freezer.h"
6
7#include "base/bind.h"
8#include "base/task/task_traits.h"
Sebastien Marchand578251d2020-12-11 03:44:439#include "chrome/browser/profiles/profile.h"
10#include "components/content_settings/core/common/content_settings_types.h"
11#include "components/performance_manager/public/graph/page_node.h"
12#include "components/performance_manager/public/web_contents_proxy.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 {
21namespace mechanism {
22namespace {
23
24// Try to freeze a page on the UI thread.
Sebastien Marchandbed434c2021-03-25 19:37:2425void MaybeFreezePageOnUIThread(const WebContentsProxy& contents_proxy) {
Sebastien Marchand578251d2020-12-11 03:44:4326 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
27 content::WebContents* const contents = contents_proxy.Get();
28 if (!contents)
Sebastien Marchandbed434c2021-03-25 19:37:2429 return;
Sebastien Marchand578251d2020-12-11 03:44:4330
Illia Klimov57d65b8a2022-03-25 20:27:4331 content::PermissionController* permission_controller =
32 contents->GetBrowserContext()->GetPermissionController();
Illia Klimov7709f0c2021-07-05 09:42:3933
Sebastien Marchand578251d2020-12-11 03:44:4334 // Page with the notification permission shouldn't be frozen as this is a
35 // strong signal that the user wants to receive updates from this page while
36 // it's in background. This information isn't available in the PM graph, this
37 // has to be checked on the UI thread.
Illia Klimov57d65b8a2022-03-25 20:27:4338 if (permission_controller->GetPermissionStatusForCurrentDocument(
Dave Tapuska6aea6e22022-06-06 21:21:5139 blink::PermissionType::NOTIFICATIONS,
40 contents->GetPrimaryMainFrame()) ==
Illia Klimov57d65b8a2022-03-25 20:27:4341 blink::mojom::PermissionStatus::GRANTED) {
Sebastien Marchandbed434c2021-03-25 19:37:2442 return;
Illia Klimov57d65b8a2022-03-25 20:27:43