Commit eaf5460a authored by Hailey Wang's avatar Hailey Wang Committed by Commit Bot

[PM] Implementation of PageLoader mechanism

Used by background tab loading policy to load a PageNode.

Bug: 1059341
Change-Id: Ifa11aab18abea4521016a5d9d5c14aff4b8ad88a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2106016
Commit-Queue: Hailey Wang <haileywang@google.com>
Reviewed-by: default avatarFrançois Doray <fdoray@chromium.org>
Reviewed-by: default avatarSébastien Marchand <sebmarchand@chromium.org>
Cr-Commit-Position: refs/heads/master@{#751108}
parent 8ff66c4b
......@@ -3352,7 +3352,8 @@ jumbo_static_library("browser") {
"performance_manager/graph/policies/urgent_page_discarding_policy.h",
"performance_manager/mechanisms/page_discarder.cc",
"performance_manager/mechanisms/page_discarder.h",
"performance_manager/mechanisms/tab_loader.h",
"performance_manager/mechanisms/page_loader.cc",
"performance_manager/mechanisms/page_loader.h",
"performance_manager/persistence/site_data/exponential_moving_average.cc",
"performance_manager/persistence/site_data/exponential_moving_average.h",
"performance_manager/persistence/site_data/feature_usage.h",
......
......@@ -4,14 +4,14 @@
#include "chrome/browser/performance_manager/graph/policies/background_tab_loading_policy.h"
#include "chrome/browser/performance_manager/mechanisms/tab_loader.h"
#include "chrome/browser/performance_manager/mechanisms/page_loader.h"
namespace performance_manager {
namespace policies {
BackgroundTabLoadingPolicy::BackgroundTabLoadingPolicy()
: tab_loader_(std::make_unique<mechanism::TabLoader>()) {}
: page_loader_(std::make_unique<mechanism::PageLoader>()) {}
BackgroundTabLoadingPolicy::~BackgroundTabLoadingPolicy() = default;
void BackgroundTabLoadingPolicy::OnPassedToGraph(Graph* graph) {}
......@@ -22,13 +22,13 @@ void BackgroundTabLoadingPolicy::RestoreTabs(
std::vector<PageNode*> page_nodes) {
// TODO(https://crbug.com/1059341): DCHECK that |page_node| is in a tab strip.
for (auto* page_node : page_nodes) {
tab_loader_->LoadPageNode(page_node);
page_loader_->LoadPageNode(page_node);
}
}
void BackgroundTabLoadingPolicy::SetMockLoaderForTesting(
std::unique_ptr<mechanism::TabLoader> loader) {
tab_loader_ = std::move(loader);
std::unique_ptr<mechanism::PageLoader> loader) {
page_loader_ = std::move(loader);
}
} // namespace policies
......
......@@ -13,7 +13,7 @@
namespace performance_manager {
namespace mechanism {
class TabLoader;
class PageLoader;
} // namespace mechanism
namespace policies {
......@@ -36,10 +36,10 @@ class BackgroundTabLoadingPolicy : public GraphOwned {
// Schedules the PageNodes in |page_nodes| to be loaded when appropriate.
void RestoreTabs(std::vector<PageNode*> page_nodes);
void SetMockLoaderForTesting(std::unique_ptr<mechanism::TabLoader> loader);
void SetMockLoaderForTesting(std::unique_ptr<mechanism::PageLoader> loader);
private:
std::unique_ptr<performance_manager::mechanism::TabLoader> tab_loader_;
std::unique_ptr<performance_manager::mechanism::PageLoader> page_loader_;
};
} // namespace policies
......
......@@ -6,7 +6,7 @@
#include <vector>
#include "chrome/browser/performance_manager/mechanisms/tab_loader.h"
#include "chrome/browser/performance_manager/mechanisms/page_loader.h"
#include "components/performance_manager/graph/page_node_impl.h"
#include "components/performance_manager/test_support/graph_test_harness.h"
#include "testing/gmock/include/gmock/gmock.h"
......@@ -16,17 +16,18 @@ namespace performance_manager {
namespace policies {
// Mock version of a performance_manager::mechanism::TabLoader.
class LenientMockTabLoader : public performance_manager::mechanism::TabLoader {
// Mock version of a performance_manager::mechanism::PageLoader.
class LenientMockPageLoader
: public performance_manager::mechanism::PageLoader {
public:
LenientMockTabLoader() = default;
~LenientMockTabLoader() override = default;
LenientMockTabLoader(const LenientMockTabLoader& other) = delete;
LenientMockTabLoader& operator=(const LenientMockTabLoader&) = delete;
LenientMockPageLoader() = default;
~LenientMockPageLoader() override = default;
LenientMockPageLoader(const LenientMockPageLoader& other) = delete;
LenientMockPageLoader& operator=(const LenientMockPageLoader&) = delete;
MOCK_METHOD1(LoadPageNode, void(const PageNode* page_node));
};
using MockTabLoader = ::testing::StrictMock<LenientMockTabLoader>;
using MockPageLoader = ::testing::StrictMock<LenientMockPageLoader>;
class BackgroundTabLoadingPolicyTest : public GraphTestHarness {
public:
......@@ -43,8 +44,8 @@ class BackgroundTabLoadingPolicyTest : public GraphTestHarness {
policy_ = policy.get();
graph()->PassToGraph(std::move(policy));
// Make the policy use a mock TabLoader.
auto mock_loader = std::make_unique<MockTabLoader>();
// Make the policy use a mock PageLoader.
auto mock_loader = std::make_unique<MockPageLoader>();
mock_loader_ = mock_loader.get();
policy_->SetMockLoaderForTesting(std::move(mock_loader));
}
......@@ -53,11 +54,11 @@ class BackgroundTabLoadingPolicyTest : public GraphTestHarness {
protected:
BackgroundTabLoadingPolicy* policy() { return policy_; }
MockTabLoader* loader() { return mock_loader_; }
MockPageLoader* loader() { return mock_loader_; }
private:
BackgroundTabLoadingPolicy* policy_;
MockTabLoader* mock_loader_;
MockPageLoader* mock_loader_;
};
TEST_F(BackgroundTabLoadingPolicyTest, RestoreTabs) {
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/performance_manager/mechanisms/page_loader.h"
#include "base/task/post_task.h"
#include "components/performance_manager/public/decorators/tab_properties_decorator.h"
#include "components/performance_manager/public/graph/page_node.h"
#include "components/performance_manager/public/web_contents_proxy.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/web_contents.h"
namespace performance_manager {
namespace mechanism {
namespace {
// Load a page on the UI thread.
void LoadPageOnUIThread(const WebContentsProxy& contents_proxy) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
content::WebContents* const contents = contents_proxy.Get();
if (!contents)
return;
contents->GetController().LoadIfNecessary();
}
} // namespace
void PageLoader::LoadPageNode(const PageNode* page_node) {
DCHECK(page_node);
DCHECK(TabPropertiesDecorator::Data::FromPageNode(page_node)->IsInTabStrip());
base::PostTask(
FROM_HERE, {content::BrowserThread::UI},
base::BindOnce(&LoadPageOnUIThread, page_node->GetContentsProxy()));
}
} // namespace mechanism
} // namespace performance_manager
......@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_PERFORMANCE_MANAGER_MECHANISMS_TAB_LOADER_H_
#define CHROME_BROWSER_PERFORMANCE_MANAGER_MECHANISMS_TAB_LOADER_H_
#ifndef CHROME_BROWSER_PERFORMANCE_MANAGER_MECHANISMS_PAGE_LOADER_H_
#define CHROME_BROWSER_PERFORMANCE_MANAGER_MECHANISMS_PAGE_LOADER_H_
#include "base/macros.h"
......@@ -13,20 +13,20 @@ class PageNode;
namespace mechanism {
// Mechanism that allows loading of the tab associated with a PageNode.
class TabLoader {
// Mechanism that allows loading of the Page associated with a PageNode.
class PageLoader {
public:
TabLoader() = default;
virtual ~TabLoader() = default;
TabLoader(const TabLoader& other) = delete;
TabLoader& operator=(const TabLoader&) = delete;
PageLoader() = default;
virtual ~PageLoader() = default;
PageLoader(const PageLoader& other) = delete;
PageLoader& operator=(const PageLoader&) = delete;
// Starts loading |page_node| if not already loaded.
virtual void LoadPageNode(const PageNode* page_node) {}
virtual void LoadPageNode(const PageNode* page_node);
};
} // namespace mechanism
} // namespace performance_manager
#endif // CHROME_BROWSER_PERFORMANCE_MANAGER_MECHANISMS_TAB_LOADER_H_
#endif // CHROME_BROWSER_PERFORMANCE_MANAGER_MECHANISMS_PAGE_LOADER_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment