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

[PM] Add loading state to BackgroundTabLoadingPolicy

Track PageNodes that are in to_load/initiated_load/loading states.

Used to restrict maximum simultaneous load in future cl.

Bug: 1059341
Change-Id: I76f02d2620fd2fb22c34358453750b20503bc921
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2118177
Commit-Queue: Hailey Wang <haileywang@google.com>
Reviewed-by: default avatarFrançois Doray <fdoray@chromium.org>
Cr-Commit-Position: refs/heads/master@{#754939}
parent 2ac248d4
...@@ -55,16 +55,48 @@ BackgroundTabLoadingPolicy::~BackgroundTabLoadingPolicy() { ...@@ -55,16 +55,48 @@ BackgroundTabLoadingPolicy::~BackgroundTabLoadingPolicy() {
g_background_tab_loading_policy = nullptr; g_background_tab_loading_policy = nullptr;
} }
void BackgroundTabLoadingPolicy::OnPassedToGraph(Graph* graph) {} void BackgroundTabLoadingPolicy::OnPassedToGraph(Graph* graph) {
graph->AddPageNodeObserver(this);
}
void BackgroundTabLoadingPolicy::OnTakenFromGraph(Graph* graph) {
graph->RemovePageNodeObserver(this);
}
void BackgroundTabLoadingPolicy::OnTakenFromGraph(Graph* graph) {} void BackgroundTabLoadingPolicy::OnIsLoadingChanged(const PageNode* page_node) {
if (!page_node->IsLoading()) {
// Once the PageNode finishes loading, stop tracking it within this policy.
RemovePageNode(page_node);
return;
}
// The PageNode started loading, either because of this policy or because of
// external factors (e.g. user-initiated). In either case, remove the PageNode
// from the set of PageNodes for which a load needs to be initiated and from
// the set of PageNodes for which a load has been initiated but hasn't
// started.
base::Erase(page_nodes_to_load_, page_node);
base::Erase(page_nodes_load_initiated_, page_node);
// Keep track of all PageNodes that are loading, even when the load isn't
// initiated by this policy.
DCHECK(!base::Contains(page_nodes_loading_, page_node));
page_nodes_loading_.push_back(page_node);
}
void BackgroundTabLoadingPolicy::OnBeforePageNodeRemoved(
const PageNode* page_node) {
RemovePageNode(page_node);
}
void BackgroundTabLoadingPolicy::ScheduleLoadForRestoredTabs( void BackgroundTabLoadingPolicy::ScheduleLoadForRestoredTabs(
std::vector<PageNode*> page_nodes) { std::vector<PageNode*> page_nodes) {
for (auto* page_node : page_nodes) { for (auto* page_node : page_nodes) {
// Put the |page_node| in the queue for loading.
DCHECK(!base::Contains(page_nodes_to_load_, page_node));
page_nodes_to_load_.push_back(page_node);
DCHECK( DCHECK(
TabPropertiesDecorator::Data::FromPageNode(page_node)->IsInTabStrip()); TabPropertiesDecorator::Data::FromPageNode(page_node)->IsInTabStrip());
page_loader_->LoadPageNode(page_node); InitiateLoad(page_node);
} }
} }
...@@ -77,6 +109,23 @@ BackgroundTabLoadingPolicy* BackgroundTabLoadingPolicy::GetInstance() { ...@@ -77,6 +109,23 @@ BackgroundTabLoadingPolicy* BackgroundTabLoadingPolicy::GetInstance() {
return g_background_tab_loading_policy; return g_background_tab_loading_policy;
} }
void BackgroundTabLoadingPolicy::InitiateLoad(PageNode* page_node) {
// Mark |page_node| as load initiated. Ensure that InitiateLoad is only called
// for a PageNode that is tracked by the policy.
size_t num_removed = base::Erase(page_nodes_to_load_, page_node);
DCHECK_EQ(num_removed, 1U);
page_nodes_load_initiated_.push_back(page_node);
// Make the call to load |page_node|.
page_loader_->LoadPageNode(page_node);
}
void BackgroundTabLoadingPolicy::RemovePageNode(const PageNode* page_node) {
base::Erase(page_nodes_to_load_, page_node);
base::Erase(page_nodes_load_initiated_, page_node);
base::Erase(page_nodes_loading_, page_node);
}
} // namespace policies } // namespace policies
} // namespace performance_manager } // namespace performance_manager
...@@ -21,7 +21,8 @@ namespace policies { ...@@ -21,7 +21,8 @@ namespace policies {
// This policy manages loading of background tabs created by session restore. It // This policy manages loading of background tabs created by session restore. It
// is responsible for assigning priorities and controlling the load of // is responsible for assigning priorities and controlling the load of
// background tab loading at all times. // background tab loading at all times.
class BackgroundTabLoadingPolicy : public GraphOwned { class BackgroundTabLoadingPolicy : public GraphOwned,
public PageNode::ObserverDefaultImpl {
public: public:
BackgroundTabLoadingPolicy(); BackgroundTabLoadingPolicy();
~BackgroundTabLoadingPolicy() override; ~BackgroundTabLoadingPolicy() override;
...@@ -33,6 +34,10 @@ class BackgroundTabLoadingPolicy : public GraphOwned { ...@@ -33,6 +34,10 @@ class BackgroundTabLoadingPolicy : public GraphOwned {
void OnPassedToGraph(Graph* graph) override; void OnPassedToGraph(Graph* graph) override;
void OnTakenFromGraph(Graph* graph) override; void OnTakenFromGraph(Graph* graph) override;
// PageNodeObserver implementation:
void OnIsLoadingChanged(const PageNode* page_node) override;
void OnBeforePageNodeRemoved(const PageNode* page_node) override;
// Schedules the PageNodes in |page_nodes| to be loaded when appropriate. // Schedules the PageNodes in |page_nodes| to be loaded when appropriate.
void ScheduleLoadForRestoredTabs(std::vector<PageNode*> page_nodes); void ScheduleLoadForRestoredTabs(std::vector<PageNode*> page_nodes);
...@@ -42,7 +47,28 @@ class BackgroundTabLoadingPolicy : public GraphOwned { ...@@ -42,7 +47,28 @@ class BackgroundTabLoadingPolicy : public GraphOwned {
static BackgroundTabLoadingPolicy* GetInstance(); static BackgroundTabLoadingPolicy* GetInstance();
private: private:
// Move the PageNode from |page_nodes_to_load_| to
// |page_nodes_load_initiated_| and make the call to load the PageNode.
void InitiateLoad(PageNode* page_node);
// Removes the PageNode from all the sets of PageNodes that the policy is
// tracking.
void RemovePageNode(const PageNode* page_node);
// The mechanism used to load the pages.
std::unique_ptr<performance_manager::mechanism::PageLoader> page_loader_; std::unique_ptr<performance_manager::mechanism::PageLoader> page_loader_;
// The set of PageNodes that have been restored for which we need to schedule
// loads.
std::vector<const PageNode*> page_nodes_to_load_;
// The set of PageNodes that BackgroundTabLoadingPolicy has initiated loading,
// and for which we are waiting for the loading to actually start. This signal
// will be received from |OnIsLoadingChanged|.
std::vector<const PageNode*> page_nodes_load_initiated_;
// The set of PageNodes that are currently loading.
std::vector<const PageNode*> page_nodes_loading_;
}; };
} // namespace policies } // namespace policies
......
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