Commit 685baf97 authored by Matt Menke's avatar Matt Menke Committed by Commit Bot

Delete the media cache(s) when using a single combined HTTP cache.

This is so that I can try increasing the media cache size in experiments
without increasing Chrome's profile size (Except possibly for profiles
that have never been used to play video).

This is a relant of
https://chromium-review.googlesource.com/c/chromium/src/+/1130162, with
re-worked tests.

Bug: 789657
Change-Id: I057e2e657572e21732610aefc468550e58fabf77
Reviewed-on: https://chromium-review.googlesource.com/1145137Reviewed-by: default avatarMaks Orlovich <morlovich@chromium.org>
Commit-Queue: Matt Menke <mmenke@chromium.org>
Cr-Commit-Position: refs/heads/master@{#577595}
parent c04a9a27
...@@ -9,6 +9,8 @@ ...@@ -9,6 +9,8 @@
#include <memory> #include <memory>
#include "base/command_line.h" #include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/files/file_path_watcher.h"
#include "base/files/file_util.h" #include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h" #include "base/files/scoped_temp_dir.h"
#include "base/json/json_reader.h" #include "base/json/json_reader.h"
...@@ -20,6 +22,7 @@ ...@@ -20,6 +22,7 @@
#include "base/sequenced_task_runner.h" #include "base/sequenced_task_runner.h"
#include "base/synchronization/waitable_event.h" #include "base/synchronization/waitable_event.h"
#include "base/task_scheduler/task_scheduler.h" #include "base/task_scheduler/task_scheduler.h"
#include "base/test/bind_test_util.h"
#include "base/test/scoped_feature_list.h" #include "base/test/scoped_feature_list.h"
#include "base/threading/thread_restrictions.h" #include "base/threading/thread_restrictions.h"
#include "base/values.h" #include "base/values.h"
...@@ -35,6 +38,7 @@ ...@@ -35,6 +38,7 @@
#include "chrome/browser/ui/tabs/tab_strip_model.h" #include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_constants.h" #include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_features.h" #include "chrome/common/chrome_features.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/pref_names.h" #include "chrome/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h" #include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h" #include "chrome/test/base/ui_test_utils.h"
...@@ -846,3 +850,157 @@ IN_PROC_BROWSER_TEST_F(ProfileWithoutMediaCacheBrowserTest, ...@@ -846,3 +850,157 @@ IN_PROC_BROWSER_TEST_F(ProfileWithoutMediaCacheBrowserTest,
net::LOAD_ONLY_FROM_CACHE); net::LOAD_ONLY_FROM_CACHE);
url_fetcher_delegate3.WaitForCompletion(); url_fetcher_delegate3.WaitForCompletion();
} }
namespace {
// Watches for the destruction of the specified path (Which, in the tests that
// use it, is typically a directory), and expects the parent directory not to be
// deleted.
//
// This is used the the media cache deletion tests, so handles all the possible
// orderings of events that could happen:
//
// * In PRE_* tests, the media cache could deleted before the test completes, by
// the task posted on Profile / isolated app URLRequestContext creation.
//
// * In the followup test, the media cache could be deleted by the off-thread
// delete media cache task before the FileDestructionWatcher starts watching for
// deletion, or even before it's created.
//
// * In the followup test, the media cache could be deleted after the
// FileDestructionWatcher starts watching.
//
// It also may be possible to get a notification of the media cache being
// created from the the previous test, so this allows multiple watch events to
// happen, before the path is actually deleted.
//
// The public methods are called on the UI thread, the private ones called on a
// separate SequencedTaskRunner.
class FileDestructionWatcher {
public:
explicit FileDestructionWatcher(const base::FilePath& watched_file_path)
: watched_file_path_(watched_file_path) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
}
void WaitForDestruction() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DCHECK(!watcher_);
base::CreateSequencedTaskRunnerWithTraits({base::MayBlock()})
->PostTask(FROM_HERE,
base::BindOnce(&FileDestructionWatcher::StartWatchingPath,
base::Unretained(this)));
run_loop_.Run();
// The watcher should be destroyed before quitting the run loop, once the
// file has been destroyed.
DCHECK(!watcher_);
// Double check that the file was destroyed, and that the parent directory
// was not.
base::ScopedAllowBlockingForTesting allow_blocking;
EXPECT_FALSE(base::PathExists(watched_file_path_));
EXPECT_TRUE(base::PathExists(watched_file_path_.DirName()));
}
private:
void StartWatchingPath() {
DCHECK(!watcher_);
watcher_ = std::make_unique<base::FilePathWatcher>();
// Start watching before checking if the file exists, as the file could be
// destroyed between the existence check and when we start watching, if the
// order were reversed.
EXPECT_TRUE(watcher_->Watch(
watched_file_path_, false /* recursive */,
base::BindRepeating(&FileDestructionWatcher::OnPathChanged,
base::Unretained(this))));
CheckIfPathExists();
}
void OnPathChanged(const base::FilePath& path, bool error) {
EXPECT_EQ(watched_file_path_, path);
EXPECT_FALSE(error);
CheckIfPathExists();
}
// Checks if the path exists, and if so, destroys the watcher and quits
// |run_loop_|.
void CheckIfPathExists() {
if (!base::PathExists(watched_file_path_)) {
watcher_.reset();
run_loop_.Quit();
return;
}
}
base::RunLoop run_loop_;
const base::FilePath watched_file_path_;
// Created and destroyed off of the UI thread, on the sequence used to watch
// for changes.
std::unique_ptr<base::FilePathWatcher> watcher_;
DISALLOW_COPY_AND_ASSIGN(FileDestructionWatcher);
};
} // namespace
// Create a media cache file, and make sure it's deleted by the time the next
// test runs.
IN_PROC_BROWSER_TEST_F(ProfileWithoutMediaCacheBrowserTest,
PRE_DeleteMediaCache) {
base::FilePath media_cache_path =
browser()->profile()->GetPath().Append(chrome::kMediaCacheDirname);
base::ScopedAllowBlockingForTesting allow_blocking;
EXPECT_TRUE(base::CreateDirectory(media_cache_path));
std::string data = "foo";
base::WriteFile(media_cache_path.AppendASCII("foo"), data.c_str(),
data.size());
}
IN_PROC_BROWSER_TEST_F(ProfileWithoutMediaCacheBrowserTest, DeleteMediaCache) {
base::FilePath media_cache_path =
browser()->profile()->GetPath().Append(chrome::kMediaCacheDirname);
base::ScopedAllowBlockingForTesting allow_blocking;
FileDestructionWatcher destruction_watcher(media_cache_path);
destruction_watcher.WaitForDestruction();
}
// Create a media cache file, and make sure it's deleted by initializing an
// extension browser context.
IN_PROC_BROWSER_TEST_F(ProfileWithoutMediaCacheBrowserTest,
PRE_DeleteIsolatedAppMediaCache) {
scoped_refptr<const extensions::Extension> app =
BuildTestApp(browser()->profile());
content::StoragePartition* extension_partition =
content::BrowserContext::GetStoragePartitionForSite(
browser()->profile(),
extensions::Extension::GetBaseURLFromExtensionId(app->id()));
base::FilePath extension_media_cache_path =
extension_partition->GetPath().Append(chrome::kMediaCacheDirname);
base::ScopedAllowBlockingForTesting allow_blocking;
EXPECT_TRUE(base::CreateDirectory(extension_media_cache_path));
std::string data = "foo";
base::WriteFile(extension_media_cache_path.AppendASCII("foo"), data.c_str(),
data.size());
}
IN_PROC_BROWSER_TEST_F(ProfileWithoutMediaCacheBrowserTest,
DeleteIsolatedAppMediaCache) {
scoped_refptr<const extensions::Extension> app =
BuildTestApp(browser()->profile());
content::StoragePartition* extension_partition =
content::BrowserContext::GetStoragePartitionForSite(
browser()->profile(),
extensions::Extension::GetBaseURLFromExtensionId(app->id()));
base::FilePath extension_media_cache_path =
extension_partition->GetPath().Append(chrome::kMediaCacheDirname);
FileDestructionWatcher destruction_watcher(extension_media_cache_path);
destruction_watcher.WaitForDestruction();
}
...@@ -12,6 +12,8 @@ ...@@ -12,6 +12,8 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/command_line.h" #include "base/command_line.h"
#include "base/feature_list.h" #include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/metrics/field_trial.h" #include "base/metrics/field_trial.h"
...@@ -19,6 +21,7 @@ ...@@ -19,6 +21,7 @@
#include "base/stl_util.h" #include "base/stl_util.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/task_scheduler/post_task.h" #include "base/task_scheduler/post_task.h"
#include "base/task_scheduler/task_traits.h"
#include "base/time/default_clock.h" #include "base/time/default_clock.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.h"
...@@ -113,6 +116,19 @@ net::BackendType ChooseCacheBackendType(const base::CommandLine& command_line) { ...@@ -113,6 +116,19 @@ net::BackendType ChooseCacheBackendType(const base::CommandLine& command_line) {
return net::CACHE_BACKEND_DEFAULT; return net::CACHE_BACKEND_DEFAULT;
} }
void MaybeDeleteMediaCache(const base::FilePath& media_cache_path) {
if (!base::FeatureList::IsEnabled(features::kUseSameCacheForMedia) ||
media_cache_path.empty()) {
return;
}
base::PostTaskWithTraits(
FROM_HERE,
{base::TaskPriority::BACKGROUND, base::MayBlock(),
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
base::BindOnce(base::IgnoreResult(&base::DeleteFile), media_cache_path,
true /* recursive */));
}
} // namespace } // namespace
using content::BrowserThread; using content::BrowserThread;
...@@ -522,6 +538,8 @@ void ProfileImplIOData::OnMainRequestContextCreated( ...@@ -522,6 +538,8 @@ void ProfileImplIOData::OnMainRequestContextCreated(
InitializeExtensionsRequestContext(profile_params); InitializeExtensionsRequestContext(profile_params);
#endif #endif
MaybeDeleteMediaCache(lazy_params_->media_cache_path);
// Create a media request context based on the main context, but using a // Create a media request context based on the main context, but using a
// media cache. It shares the same job factory as the main context. // media cache. It shares the same job factory as the main context.
StoragePartitionDescriptor details(profile_path_, false); StoragePartitionDescriptor details(profile_path_, false);
...@@ -555,6 +573,11 @@ net::URLRequestContext* ProfileImplIOData::InitializeAppRequestContext( ...@@ -555,6 +573,11 @@ net::URLRequestContext* ProfileImplIOData::InitializeAppRequestContext(
protocol_handler_interceptor, protocol_handler_interceptor,
content::ProtocolHandlerMap* protocol_handlers, content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector request_interceptors) const { content::URLRequestInterceptorScopedVector request_interceptors) const {
if (!partition_descriptor.in_memory) {
MaybeDeleteMediaCache(
partition_descriptor.path.Append(chrome::kMediaCacheDirname));
}
// Copy most state from the main context. // Copy most state from the main context.
AppRequestContext* context = new AppRequestContext(); AppRequestContext* context = new AppRequestContext();
context->CopyFrom(main_context); context->CopyFrom(main_context);
......
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