Commit 414d24f7 authored by Francois Doray's avatar Francois Doray Committed by Commit Bot

Add resource_coordinator::TabLifecycleUnit.

A TabLifecycleUnit represents a tab.

The implementation will be in a separate class (TabLifecyleUnitImpl)
because we want to expose only part of the it outside of
chrome/browser/resource_coordinator/.

E.g. the extensions system is allowed call:

TabLifecycleUnit::FromWebContents(contents)->Discard(condition);

but we don't want it to change the TabStripModel associated with
a tab (TabLifecycleUnitImpl::set_tab_strip_model()).

Bug: 775644
Change-Id: Idf2ad85bf2969871be1dc09eb1eb3776bc922959
Reviewed-on: https://chromium-review.googlesource.com/774979Reviewed-by: default avatarChris Hamilton <chrisha@chromium.org>
Commit-Queue: François Doray <fdoray@chromium.org>
Cr-Commit-Position: refs/heads/master@{#518410}
parent 52d21598
...@@ -2557,6 +2557,8 @@ split_static_library("browser") { ...@@ -2557,6 +2557,8 @@ split_static_library("browser") {
"resource_coordinator/discard_condition.h", "resource_coordinator/discard_condition.h",
"resource_coordinator/lifecycle_unit.cc", "resource_coordinator/lifecycle_unit.cc",
"resource_coordinator/lifecycle_unit.h", "resource_coordinator/lifecycle_unit.h",
"resource_coordinator/tab_lifecycle_unit.cc",
"resource_coordinator/tab_lifecycle_unit.h",
"resource_coordinator/tab_lifetime_observer.cc", "resource_coordinator/tab_lifetime_observer.cc",
"resource_coordinator/tab_lifetime_observer.h", "resource_coordinator/tab_lifetime_observer.h",
"resource_coordinator/tab_manager.cc", "resource_coordinator/tab_manager.cc",
......
// Copyright 2017 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/resource_coordinator/tab_lifecycle_unit.h"
#include "base/macros.h"
#include "content/public/browser/web_contents_user_data.h"
namespace resource_coordinator {
namespace {
class TabLifecycleUnitHolder
: public content::WebContentsUserData<TabLifecycleUnitHolder> {
public:
explicit TabLifecycleUnitHolder(content::WebContents*) {}
TabLifecycleUnit* tab_lifecycle_unit() const { return tab_lifecycle_unit_; }
void set_tab_lifecycle_unit(TabLifecycleUnit* tab_lifecycle_unit) {
tab_lifecycle_unit_ = tab_lifecycle_unit;
}
private:
TabLifecycleUnit* tab_lifecycle_unit_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(TabLifecycleUnitHolder);
};
} // namespace
// static
TabLifecycleUnit* TabLifecycleUnit::FromWebContents(
content::WebContents* web_contents) {
TabLifecycleUnitHolder* holder =
TabLifecycleUnitHolder::FromWebContents(web_contents);
return holder ? holder->tab_lifecycle_unit() : nullptr;
}
// static
void TabLifecycleUnit::SetForWebContents(content::WebContents* web_contents,
TabLifecycleUnit* tab_lifecycle_unit) {
TabLifecycleUnitHolder::CreateForWebContents(web_contents);
TabLifecycleUnitHolder::FromWebContents(web_contents)
->set_tab_lifecycle_unit(tab_lifecycle_unit);
}
} // namespace resource_coordinator
DEFINE_WEB_CONTENTS_USER_DATA_KEY(resource_coordinator::TabLifecycleUnitHolder);
// Copyright 2017 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.
#ifndef CHROME_BROWSER_RESOURCE_COORDINATOR_TAB_LIFECYCLE_UNIT_H_
#define CHROME_BROWSER_RESOURCE_COORDINATOR_TAB_LIFECYCLE_UNIT_H_
#include "chrome/browser/resource_coordinator/lifecycle_unit.h"
namespace content {
class RenderProcessHost;
class WebContents;
} // namespace content
namespace resource_coordinator {
// Represents a tab.
class TabLifecycleUnit : public LifecycleUnit {
public:
// Returns the TabLifecycleUnit associated with |web_contents|, or nullptr if
// the WebContents is not associated with a tab.
static TabLifecycleUnit* FromWebContents(content::WebContents* web_contents);
// Returns the WebContents associated with this tab.
virtual content::WebContents* GetWebContents() const = 0;
// Returns the RenderProcessHost associated with this tab.
virtual content::RenderProcessHost* GetRenderProcessHost() const = 0;
// Returns true if the tab is playing audio, has played audio recently, is
// accessing the microphone, is accessing the camera or is being mirrored.
virtual bool IsMediaTab() const = 0;
// Returns true if this tab can be automatically discarded.
virtual bool IsAutoDiscardable() const = 0;
// Allows/disallows this tab to be automatically discarded.
virtual void SetAutoDiscardable(bool auto_discardable) = 0;
protected:
// Sets the TabLifecycleUnit associated with |web_contents|.
static void SetForWebContents(content::WebContents* web_contents,
TabLifecycleUnit* tab_lifecycle_unit);
};
} // namespace resource_coordinator
#endif // CHROME_BROWSER_RESOURCE_COORDINATOR_TAB_LIFECYCLE_UNIT_H_
// Copyright 2017 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/resource_coordinator/tab_lifecycle_unit.h"
#include "base/macros.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace resource_coordinator {
namespace {
class DummyTabLifecycleUnit : public TabLifecycleUnit {
public:
explicit DummyTabLifecycleUnit(content::WebContents* contents)
: contents_(contents) {
TabLifecycleUnit::SetForWebContents(contents_, this);
}
~DummyTabLifecycleUnit() override {
TabLifecycleUnit::SetForWebContents(contents_, nullptr);
}
// TabLifecycleUnit:
content::WebContents* GetWebContents() const override { return nullptr; }
content::RenderProcessHost* GetRenderProcessHost() const override {
return nullptr;
}
bool IsMediaTab() const override { return false; }
bool IsAutoDiscardable() const override { return false; }
void SetAutoDiscardable(bool) override {}
// LifecycleUnit:
base::string16 GetTitle() const override { return base::string16(); }
std::string GetIconURL() const override { return std::string(); }
SortKey GetSortKey() const override { return SortKey(); }
State GetState() const override { return State::LOADED; }
int GetEstimatedMemoryFreedOnDiscardKB() const override { return 0; }
bool CanDiscard(DiscardCondition) const override { return false; }
bool Discard(DiscardCondition) override { return false; }
private:
content::WebContents* const contents_;
DISALLOW_COPY_AND_ASSIGN(DummyTabLifecycleUnit);
};
} // namespace
using TabLifecycleUnitTest = ChromeRenderViewHostTestHarness;
TEST_F(TabLifecycleUnitTest, FromWebContentsNullByDefault) {
EXPECT_FALSE(TabLifecycleUnit::FromWebContents(web_contents()));
}
TEST_F(TabLifecycleUnitTest, SetForWebContents) {
{
DummyTabLifecycleUnit tab_lifecycle_unit(web_contents());
EXPECT_EQ(&tab_lifecycle_unit,
TabLifecycleUnit::FromWebContents(web_contents()));
}
EXPECT_FALSE(TabLifecycleUnit::FromWebContents(web_contents()));
{
DummyTabLifecycleUnit tab_lifecycle_unit(web_contents());
EXPECT_EQ(&tab_lifecycle_unit,
TabLifecycleUnit::FromWebContents(web_contents()));
}
EXPECT_FALSE(TabLifecycleUnit::FromWebContents(web_contents()));
}
} // namespace resource_coordinator
...@@ -2720,6 +2720,7 @@ test("unit_tests") { ...@@ -2720,6 +2720,7 @@ test("unit_tests") {
"../browser/page_load_metrics/observers/session_restore_page_load_metrics_observer_unittest.cc", "../browser/page_load_metrics/observers/session_restore_page_load_metrics_observer_unittest.cc",
"../browser/resource_coordinator/background_tab_navigation_throttle_unittest.cc", "../browser/resource_coordinator/background_tab_navigation_throttle_unittest.cc",
"../browser/resource_coordinator/lifecycle_unit_unittest.cc", "../browser/resource_coordinator/lifecycle_unit_unittest.cc",
"../browser/resource_coordinator/tab_lifecycle_unit_unittest.cc",
"../browser/resource_coordinator/tab_manager_delegate_chromeos_unittest.cc", "../browser/resource_coordinator/tab_manager_delegate_chromeos_unittest.cc",
"../browser/resource_coordinator/tab_manager_stats_collector_unittest.cc", "../browser/resource_coordinator/tab_manager_stats_collector_unittest.cc",
"../browser/resource_coordinator/tab_manager_unittest.cc", "../browser/resource_coordinator/tab_manager_unittest.cc",
......
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