Commit d897b6f7 authored by Bo Liu's avatar Bo Liu Committed by Commit Bot

weblayer: Use cache dir

Looking at chrome and android webview, looks like cache dir is used for
http cache and code cache. Use these in weblayer as well. Note before
these, neither disk cache are enabled, so there is no need to consider
any migration issue.

Bug: 1027627
Change-Id: Ib6e615e975a9cd8f7e456c6f5af1c0fac4bceea7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1937087
Commit-Queue: Bo <boliu@chromium.org>
Reviewed-by: default avatarJohn Abd-El-Malek <jam@chromium.org>
Cr-Commit-Position: refs/heads/master@{#719271}
parent 68d4ef85
......@@ -17,6 +17,7 @@
#include "components/version_info/version_info.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/devtools_manager_delegate.h"
#include "content/public/browser/generated_code_cache_settings.h"
#include "content/public/browser/navigation_throttle.h"
#include "content/public/browser/network_service_instance.h"
#include "content/public/common/content_switches.h"
......@@ -35,6 +36,7 @@
#include "url/origin.h"
#include "weblayer/browser/browser_main_parts_impl.h"
#include "weblayer/browser/i18n_util.h"
#include "weblayer/browser/profile_impl.h"
#include "weblayer/browser/ssl_error_handler.h"
#include "weblayer/browser/tab_impl.h"
#include "weblayer/browser/weblayer_content_browser_overlay_manifest.h"
......@@ -194,6 +196,8 @@ ContentBrowserClientImpl::CreateNetworkContext(
base::FilePath cookie_path = context->GetPath();
cookie_path = cookie_path.Append(FILE_PATH_LITERAL("Cookies"));
context_params->cookie_path = cookie_path;
context_params->http_cache_path =
ProfileImpl::GetCachePath(context).Append(FILE_PATH_LITERAL("Cache"));
}
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
if (command_line->HasSwitch(switches::kProxyServer)) {
......@@ -311,6 +315,17 @@ ContentBrowserClientImpl::CreateThrottlesForNavigation(
return throttles;
}
content::GeneratedCodeCacheSettings
ContentBrowserClientImpl::GetGeneratedCodeCacheSettings(
content::BrowserContext* context) {
DCHECK(context);
// If we pass 0 for size, disk_cache will pick a default size using the
// heuristics based on available disk size. These are implemented in
// disk_cache::PreferredCacheSize in net/disk_cache/cache_util.cc.
return content::GeneratedCodeCacheSettings(
true, 0, ProfileImpl::GetCachePath(context));
}
#if defined(OS_LINUX) || defined(OS_ANDROID)
void ContentBrowserClientImpl::GetAdditionalMappedFilesForChildProcess(
const base::CommandLine& command_line,
......
......@@ -68,6 +68,8 @@ class ContentBrowserClientImpl : public content::ContentBrowserClient {
bool* no_javascript_access) override;
std::vector<std::unique_ptr<content::NavigationThrottle>>
CreateThrottlesForNavigation(content::NavigationHandle* handle) override;
content::GeneratedCodeCacheSettings GetGeneratedCodeCacheSettings(
content::BrowserContext* context) override;
#if defined(OS_LINUX) || defined(OS_ANDROID)
void GetAdditionalMappedFilesForChildProcess(
......
......@@ -58,7 +58,7 @@ import java.lang.reflect.Method;
public final class WebLayerImpl extends IWebLayer.Stub {
// TODO: should there be one tag for all this code?
private static final String TAG = "WebLayer";
private static final String PRIVATE_DATA_DIRECTORY_SUFFIX = "weblayer";
private static final String PRIVATE_DIRECTORY_SUFFIX = "weblayer";
// TODO: Configure this from the client.
private static final String COMMAND_LINE_FILE = "/data/local/tmp/weblayer-command-line";
......@@ -150,7 +150,7 @@ public final class WebLayerImpl extends IWebLayer.Stub {
R.onResourcesLoaded(resourcesPackageId);
ResourceBundle.setAvailablePakLocales(new String[] {}, ProductConfig.UNCOMPRESSED_LOCALES);
PathUtils.setPrivateDataDirectorySuffix(PRIVATE_DATA_DIRECTORY_SUFFIX);
PathUtils.setPrivateDataDirectorySuffix(PRIVATE_DIRECTORY_SUFFIX, PRIVATE_DIRECTORY_SUFFIX);
ChildProcessCreationParams.set(appContext.getPackageName(), false /* isExternalService */,
LibraryProcessType.PROCESS_WEBLAYER_CHILD, true /* bindToCaller */,
......
......@@ -37,6 +37,10 @@
#include "weblayer/browser/java/jni/ProfileImpl_jni.h"
#endif
#if defined(OS_POSIX)
#include "base/base_paths_posix.h"
#endif
#if defined(OS_ANDROID)
using base::android::AttachCurrentThread;
#endif
......@@ -100,7 +104,8 @@ bool IsNameValid(const std::string& name) {
class ProfileImpl::BrowserContextImpl : public content::BrowserContext {
public:
BrowserContextImpl(const base::FilePath& path) : path_(path) {
BrowserContextImpl(ProfileImpl* profile_impl, const base::FilePath& path)
: profile_impl_(profile_impl), path_(path) {
resource_context_ = std::make_unique<ResourceContextImpl>();
content::BrowserContext::Initialize(this, path_);
......@@ -177,6 +182,8 @@ class ProfileImpl::BrowserContextImpl : public content::BrowserContext {
return nullptr;
}
ProfileImpl* profile_impl() const { return profile_impl_; }
private:
// Creates a simple in-memory pref service.
// TODO(timvolodine): Investigate whether WebLayer needs persistent pref
......@@ -199,6 +206,7 @@ class ProfileImpl::BrowserContextImpl : public content::BrowserContext {
safe_browsing::RegisterProfilePrefs(pref_registry);
}
ProfileImpl* const profile_impl_;
base::FilePath path_;
std::unique_ptr<ResourceContextImpl> resource_context_;
DownloadManagerDelegateImpl download_delegate_;
......@@ -237,18 +245,36 @@ class ProfileImpl::DataClearer : public content::BrowsingDataRemover::Observer {
base::OnceCallback<void()> callback_;
};
ProfileImpl::ProfileImpl(const std::string& name) {
// static
base::FilePath ProfileImpl::GetCachePath(content::BrowserContext* context) {
DCHECK(context);
ProfileImpl* profile =
static_cast<BrowserContextImpl*>(context)->profile_impl();
#if defined(OS_POSIX)
base::FilePath path;
{
base::ScopedAllowBlocking allow_blocking;
CHECK(base::PathService::Get(base::DIR_CACHE, &path));
path = path.AppendASCII("profiles").AppendASCII(profile->name_.c_str());
if (!base::PathExists(path))
base::CreateDirectory(path);
}
return path;
#else
return profile->data_path_;
#endif
}
ProfileImpl::ProfileImpl(const std::string& name) : name_(name) {
if (!name.empty()) {
CHECK(IsNameValid(name));
{
base::ScopedAllowBlocking allow_blocking;
CHECK(base::PathService::Get(DIR_USER_DATA, &path));
path = path.AppendASCII("profiles").AppendASCII(name.c_str());
CHECK(base::PathService::Get(DIR_USER_DATA, &data_path_));
data_path_ = data_path_.AppendASCII("profiles").AppendASCII(name.c_str());
if (!base::PathExists(path))
base::CreateDirectory(path);
if (!base::PathExists(data_path_))
base::CreateDirectory(data_path_);
}
}
......@@ -256,7 +282,7 @@ ProfileImpl::ProfileImpl(const std::string& name) {
// OnRenderProcessHostCreated events.
web_cache::WebCacheManager::GetInstance();
browser_context_ = std::make_unique<BrowserContextImpl>(path);
browser_context_ = std::make_unique<BrowserContextImpl>(this, data_path_);
locale_change_subscription_ = i18n::RegisterLocaleChangeCallback(
base::Bind(&ProfileImpl::OnLocaleChanged, base::Unretained(this)));
......
......@@ -5,6 +5,7 @@
#ifndef WEBLAYER_BROWSER_PROFILE_IMPL_H_
#define WEBLAYER_BROWSER_PROFILE_IMPL_H_
#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "build/build_config.h"
......@@ -24,6 +25,13 @@ namespace weblayer {
class ProfileImpl : public Profile {
public:
// Return the cache directory path for this BrowserContext. On some
// platforms, file in cache directory may be deleted by the operating
// system. So it is suitable for storing data that can be recreated such
// as caches.
// |context| must not be null.
static base::FilePath GetCachePath(content::BrowserContext* context);
explicit ProfileImpl(const std::string& name);
~ProfileImpl() override;
......@@ -55,6 +63,9 @@ class ProfileImpl : public Profile {
// Callback when the system locale has been updated.
void OnLocaleChanged();
const std::string name_;
base::FilePath data_path_;
std::unique_ptr<BrowserContextImpl> browser_context_;
std::unique_ptr<i18n::LocaleChangeSubscription> locale_change_subscription_;
......
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