Commit 4ce33da6 authored by Evan Stade's avatar Evan Stade Committed by Chromium LUCI CQ

Remove ImportantSitesUsageCounter.

It's been unused since eff032d4

Bug: none
Change-Id: I8bff98f5f75dbc87593097904f225a5ee936ff6a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2599884Reviewed-by: default avatarcalamity <calamity@chromium.org>
Commit-Queue: Evan Stade <estade@chromium.org>
Cr-Commit-Position: refs/heads/master@{#840247}
parent 45ddb2c3
......@@ -476,8 +476,6 @@ static_library("browser") {
"endpoint_fetcher/endpoint_fetcher.h",
"engagement/history_aware_site_engagement_service.cc",
"engagement/history_aware_site_engagement_service.h",
"engagement/important_sites_usage_counter.cc",
"engagement/important_sites_usage_counter.h",
"engagement/important_sites_util.cc",
"engagement/important_sites_util.h",
"engagement/site_engagement_helper.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/engagement/important_sites_usage_counter.h"
#include <utility>
#include "base/bind.h"
#include "base/threading/thread_task_runner_handle.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/dom_storage_context.h"
namespace site_engagement {
using ImportantDomainInfo = ImportantSitesUtil::ImportantDomainInfo;
// static
void ImportantSitesUsageCounter::GetUsage(
std::vector<ImportantSitesUtil::ImportantDomainInfo> sites,
storage::QuotaManager* quota_manager,
content::DOMStorageContext* dom_storage_context,
UsageCallback callback) {
(new ImportantSitesUsageCounter(std::move(sites), quota_manager,
dom_storage_context, std::move(callback)))
->RunAndDestroySelfWhenFinished();
}
ImportantSitesUsageCounter::ImportantSitesUsageCounter(
std::vector<ImportantDomainInfo> sites,
storage::QuotaManager* quota_manager,
content::DOMStorageContext* dom_storage_context,
UsageCallback callback)
: callback_(std::move(callback)),
sites_(std::move(sites)),
quota_manager_(quota_manager),
dom_storage_context_(dom_storage_context),
tasks_(0) {
for (ImportantDomainInfo& site : sites_)
site.usage = 0;
}
ImportantSitesUsageCounter::~ImportantSitesUsageCounter() {}
void ImportantSitesUsageCounter::RunAndDestroySelfWhenFinished() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
tasks_ += 1;
content::GetIOThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(&ImportantSitesUsageCounter::GetQuotaUsageOnIOThread,
base::Unretained(this)));
tasks_ += 1;
dom_storage_context_->GetLocalStorageUsage(
base::BindOnce(&ImportantSitesUsageCounter::ReceiveLocalStorageUsage,
base::Unretained(this)));
}
void ImportantSitesUsageCounter::GetQuotaUsageOnIOThread() {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
quota_manager_->GetUsageInfo(
base::BindOnce(&ImportantSitesUsageCounter::ReceiveQuotaUsageOnIOThread,
base::Unretained(this)));
}
void ImportantSitesUsageCounter::ReceiveQuotaUsageOnIOThread(
std::vector<storage::UsageInfo> usage_infos) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
content::GetUIThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(&ImportantSitesUsageCounter::ReceiveQuotaUsage,
base::Unretained(this), std::move(usage_infos)));
}
void ImportantSitesUsageCounter::ReceiveQuotaUsage(
std::vector<storage::UsageInfo> usage_infos) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
for (const storage::UsageInfo& info : usage_infos) {
IncrementUsage(
ImportantSitesUtil::GetRegisterableDomainOrIPFromHost(info.host),
info.usage);
}
Done();
}
void ImportantSitesUsageCounter::ReceiveLocalStorageUsage(
const std::vector<content::StorageUsageInfo>& storage_infos) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
for (const content::StorageUsageInfo& info : storage_infos) {
IncrementUsage(
ImportantSitesUtil::GetRegisterableDomainOrIP(info.origin.GetURL()),
info.total_size_bytes);
}
Done();
}
// Look up the corresponding ImportantDomainInfo for |url| and increase its
// usage by |size|.
void ImportantSitesUsageCounter::IncrementUsage(const std::string& domain,
int64_t size) {
// Use a linear search over sites_ because it only has up to 10 entries.
auto it = std::find_if(sites_.begin(), sites_.end(),
[domain](ImportantDomainInfo& info) {
return info.registerable_domain == domain;
});
if (it != sites_.end())
it->usage += size;
}
void ImportantSitesUsageCounter::Done() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DCHECK_GT(tasks_, 0);
if (--tasks_ == 0) {
std::move(callback_).Run(std::move(sites_));
delete this;
}
}
} // namespace site_engagement
// 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_ENGAGEMENT_IMPORTANT_SITES_USAGE_COUNTER_H_
#define CHROME_BROWSER_ENGAGEMENT_IMPORTANT_SITES_USAGE_COUNTER_H_
#include <string>
#include <vector>
#include "base/callback.h"
#include "base/macros.h"
#include "base/sequenced_task_runner_helpers.h"
#include "chrome/browser/engagement/important_sites_util.h"
#include "content/public/browser/storage_usage_info.h"
#include "storage/browser/quota/quota_manager.h"
namespace content {
class DOMStorageContext;
}
namespace storage {
class QuotaManager;
}
namespace site_engagement {
// A helper class for important storage that retrieves the localstorage and
// quota usage for each domain in |ImportantDomainInfo|, populates
// |ImportantDomainInfo::usage| in the |sites| entries and return the result via
// |callback|.
class ImportantSitesUsageCounter {
public:
using UsageCallback = base::OnceCallback<void(
std::vector<ImportantSitesUtil::ImportantDomainInfo> sites)>;
// Populates the ImportantDomainInfo::usage field of each site with the amount
// of localstorage and quota bytes used. |callback| is asynchronously invoked
// on the UI thread with the |sites| vector (populated with usage) as an
// argument.
static void GetUsage(
std::vector<ImportantSitesUtil::ImportantDomainInfo> sites,
storage::QuotaManager* quota_manager,
content::DOMStorageContext* dom_storage_context,
UsageCallback callback);
private:
friend class base::DeleteHelper<ImportantSitesUsageCounter>;
ImportantSitesUsageCounter(
std::vector<ImportantSitesUtil::ImportantDomainInfo> sites,
storage::QuotaManager* quota_manager,
content::DOMStorageContext* dom_storage_context,
UsageCallback callback);
~ImportantSitesUsageCounter();
void RunAndDestroySelfWhenFinished();
void GetQuotaUsageOnIOThread();
void ReceiveQuotaUsageOnIOThread(std::vector<storage::UsageInfo> usage_infos);
void ReceiveQuotaUsage(std::vector<storage::UsageInfo> usage_infos);
void ReceiveLocalStorageUsage(
const std::vector<content::StorageUsageInfo>& storage_infos);
// Look up the corresponding ImportantDomainInfo for |url| and increase its
// usage by |size|.
void IncrementUsage(const std::string& domain, int64_t size);
void Done();
UsageCallback callback_;
std::vector<ImportantSitesUtil::ImportantDomainInfo> sites_;
storage::QuotaManager* quota_manager_;
content::DOMStorageContext* dom_storage_context_;
int tasks_;
DISALLOW_IMPLICIT_CONSTRUCTORS(ImportantSitesUsageCounter);
};
} // namespace site_engagement
#endif // CHROME_BROWSER_ENGAGEMENT_IMPORTANT_SITES_USAGE_COUNTER_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/engagement/important_sites_usage_counter.h"
#include <memory>
#include <utility>
#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/scoped_refptr.h"
#include "base/run_loop.h"
#include "base/test/metrics/histogram_tester.h"
#include "chrome/browser/bookmarks/bookmark_model_factory.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_utils.h"
#include "storage/browser/quota/quota_client_type.h"
#include "storage/browser/quota/quota_manager_proxy.h"
#include "storage/browser/test/mock_quota_client.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/mojom/quota/quota_types.mojom-shared.h"
namespace site_engagement {
using ImportantDomainInfo = ImportantSitesUtil::ImportantDomainInfo;
using content::DOMStorageContext;
using storage::QuotaManager;
class ImportantSitesUsageCounterTest : public testing::Test {
public:
void SetUp() override {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
run_loop_ = std::make_unique<base::RunLoop>();
}
void TearDown() override {
// Release the quota manager and wait for the database to be closed.
quota_manager_.reset();
content::RunAllTasksUntilIdle();
}
TestingProfile* profile() { return &profile_; }
QuotaManager* CreateQuotaManager() {
quota_manager_ = base::MakeRefCounted<QuotaManager>(
/*is_incognito=*/false, temp_dir_.GetPath(),
content::GetIOThreadTaskRunner({}).get(),
/*quota_change_callback=*/base::DoNothing(),
/*special_storage_policy=*/nullptr, storage::GetQuotaSettingsFunc());
return quota_manager_.get();
}
void RegisterClient(base::span<const storage::MockOriginData> origin_data) {
auto client = base::MakeRefCounted<storage::MockQuotaClient>(
quota_manager_->proxy(), origin_data,
storage::QuotaClientType::kFileSystem);
quota_manager_->proxy()->RegisterClient(
client, storage::QuotaClientType::kFileSystem,
{blink::mojom::StorageType::kTemporary,
blink::mojom::StorageType::kPersistent});
client->TouchAllOriginsAndNotify();
}
void CreateLocalStorage(
base::Time creation_time,
int length,
const base::FilePath::StringPieceType& storage_origin) {
// Note: This test depends on details of how the dom_storage library
// stores data in the host file system.
base::FilePath storage_path =
profile()->GetPath().AppendASCII("Local Storage");
base::CreateDirectory(storage_path);
std::string data(length, ' ');
// Write file to local storage.
base::FilePath file_path = storage_path.Append(storage_origin);
base::WriteFile(file_path, data.c_str(), length);
base::TouchFile(file_path, creation_time, creation_time);
}
void FetchCompleted(std::vector<ImportantDomainInfo> domain_info) {
domain_info_ = std::move(domain_info);
run_loop_->Quit();
}
void WaitForResult() {
run_loop_->Run();
run_loop_ = std::make_unique<base::RunLoop>();
}
const std::vector<ImportantDomainInfo>& domain_info() { return domain_info_; }
private:
base::ScopedTempDir temp_dir_;
content::BrowserTaskEnvironment task_environment_;
TestingProfile profile_;
scoped_refptr<QuotaManager> quota_manager_;
std::vector<ImportantDomainInfo> domain_info_;
std::unique_ptr<base::RunLoop> run_loop_;
};
TEST_F(ImportantSitesUsageCounterTest, PopulateUsage) {
std::vector<ImportantDomainInfo> important_sites;
ImportantDomainInfo i1;
i1.registerable_domain = "example.com";
ImportantDomainInfo i2;
i2.registerable_domain = "somethingelse.com";
important_sites.push_back(std::move(i1));
important_sites.push_back(std::move(i2));
static const storage::MockOriginData kOrigins[] = {
{"http://example.com/", blink::mojom::StorageType::kTemporary, 1},
{"https://example.com/", blink::mojom::StorageType::kTemporary, 2},
{"https://maps.example.com/", blink::mojom::StorageType::kTemporary, 4},
{"http://google.com/", blink::mojom::StorageType::kPersistent, 8},
};
QuotaManager* quota_manager = CreateQuotaManager();
RegisterClient(kOrigins);
base::Time now = base::Time::Now();
CreateLocalStorage(now, 16,
FILE_PATH_LITERAL("https_example.com_443.localstorage"));
CreateLocalStorage(now, 32,
FILE_PATH_LITERAL("https_bing.com_443.localstorage"));
DOMStorageContext* dom_storage_context =
content::BrowserContext::GetDefaultStoragePartition(profile())
->GetDOMStorageContext();
ImportantSitesUsageCounter::GetUsage(
std::move(important_sites), quota_manager, dom_storage_context,
base::BindOnce(&ImportantSitesUsageCounterTest::FetchCompleted,
base::Unretained(this)));
WaitForResult();
EXPECT_EQ(2U, domain_info().size());
// The first important site is example.com. It uses 1B quota storage for
// http://example.com/, 2B for https://example.com and 4B for
// https://maps.example.com. On top of that it uses 16B local storage.
EXPECT_EQ("example.com", domain_info()[0].registerable_domain);
EXPECT_EQ(1 + 2 + 4 + 16, domain_info()[0].usage);
// The second important site is somethingelse.com but it doesn't use any
// quota. We still expect it to be returned and not dropped.
EXPECT_EQ("somethingelse.com", domain_info()[1].registerable_domain);
EXPECT_EQ(0, domain_info()[1].usage);
}
} // namespace site_engagement
......@@ -43,9 +43,6 @@ class ImportantSitesUtil {
GURL example_origin;
double engagement_score = 0;
int32_t reason_bitfield = 0;
// |usage| has to be initialized by ImportantSitesUsageCounter before it
// will contain the number of bytes used for quota and localstorage.
int64_t usage = 0;
// Only set if the domain belongs to an installed app.
base::Optional<std::string> app_name;
};
......
......@@ -3492,7 +3492,6 @@ test("unit_tests") {
"../browser/download/download_ui_controller_unittest.cc",
"../browser/download/offline_item_utils_unittest.cc",
"../browser/endpoint_fetcher/endpoint_fetcher_unittest.cc",
"../browser/engagement/important_sites_usage_counter_unittest.cc",
"../browser/engagement/important_sites_util_unittest.cc",
"../browser/engagement/site_engagement_helper_unittest.cc",
"../browser/engagement/site_engagement_score_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