[M120 Migration][VD] TV customization of Disk Cache 51/310451/4
authorjiangyuwei <yuwei.jiang@samsung.com>
Mon, 29 Apr 2024 08:40:48 +0000 (16:40 +0800)
committerBot Blink <blinkbot@samsung.com>
Tue, 30 Apr 2024 07:28:50 +0000 (07:28 +0000)
1.Accept disk-cache-size from command line
2.SetNetworkCacheEnable API
3.Modify the disk cache storage path
4.Implement the disk cache when Network service is enabled
5.Change cache type from CACHE_BACKEND_SIMPLE to
CACHE_BACKEND_BLOCKFILE
6.Add check in NetworkContext::SetCacheMode to avoid crash

References:
 - https://review.tizen.org/gerrit/291930/
 - https://review.tizen.org/gerrit/293927/

Change-Id: I000498d698d6ba87784fb899b88f82bbba061146
Signed-off-by: jiangyuwei <yuwei.jiang@samsung.com>
components/network_session_configurator/browser/network_session_configurator.cc
content/browser/storage_partition_impl.cc
services/network/network_context.cc
services/network/network_context.h
services/network/public/mojom/network_context.mojom
tizen_src/ewk/efl_integration/common/content_switches_efl.cc
tizen_src/ewk/efl_integration/common/content_switches_efl.h
tizen_src/ewk/efl_integration/content_browser_client_efl.cc
tizen_src/ewk/efl_integration/content_browser_client_efl.h
tizen_src/ewk/efl_integration/eweb_context.cc
tizen_src/ewk/efl_integration/eweb_context.h

index 4ced7d762d0bfbf4f867dbab2679962ec6d5db1e..a560e5cd125043e374cdb559691f27107694a34d 100644 (file)
@@ -800,8 +800,9 @@ net::URLRequestContextBuilder::HttpCacheParams::Type ChooseCacheType() {
   // muddles the experiment data, but as this was written to be considered for
   // backport, having it behave differently than in stable would be a bigger
   // problem. TODO: Does this work in later macOS releases?
-#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || \
-    BUILDFLAG(IS_MAC)
+#if (BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || \
+     BUILDFLAG(IS_MAC)) &&                                                     \
+    !BUILDFLAG(IS_TIZEN_TV)
   return net::URLRequestContextBuilder::HttpCacheParams::DISK_SIMPLE;
 #else
   return net::URLRequestContextBuilder::HttpCacheParams::DISK_BLOCKFILE;
index 59801f30a40c551ef6634875aed8b317cc19d5de..e48de6053c853ce56ab81b65925301a27fc25bbd 100644 (file)
@@ -3338,8 +3338,29 @@ void StoragePartitionImpl::GetQuotaSettings(
 }
 
 void StoragePartitionImpl::InitNetworkContext() {
+  LOG(INFO) << "is_in_memory is " << is_in_memory();
   network::mojom::NetworkContextParamsPtr context_params =
       network::mojom::NetworkContextParams::New();
+  if (base::FeatureList::IsEnabled(
+          network::features::kCompressionDictionaryTransportBackend) &&
+      GetContentClient()->browser()->AllowCompressionDictionaryTransport(
+          browser_context_)) {
+    context_params->shared_dictionary_enabled = true;
+    if (!is_in_memory()) {
+      // Some callers may already initialize NetworkContextFilePaths, and we
+      // don't want to overwrite them.
+      if (!context_params->file_paths) {
+        context_params->file_paths =
+            network::mojom::NetworkContextFilePaths::New();
+      }
+      context_params->file_paths->shared_dictionary_directory =
+          partition_path_.Append(FILE_PATH_LITERAL("Shared Dictionary"));
+    }
+    if (context_params->shared_dictionary_cache_max_size == 0u) {
+      CalculateAndSetSharedDictionaryCacheMaxSize(
+          GetWeakPtr(), is_in_memory() ? base::FilePath() : partition_path_);
+    }
+  }
   cert_verifier::mojom::CertVerifierCreationParamsPtr
       cert_verifier_creation_params =
           cert_verifier::mojom::CertVerifierCreationParams::New();
@@ -3375,27 +3396,6 @@ void StoragePartitionImpl::InitNetworkContext() {
 
   cors_exempt_header_list_ = context_params->cors_exempt_header_list;
 
-  if (base::FeatureList::IsEnabled(
-          network::features::kCompressionDictionaryTransportBackend) &&
-      GetContentClient()->browser()->AllowCompressionDictionaryTransport(
-          browser_context_)) {
-    context_params->shared_dictionary_enabled = true;
-    if (!is_in_memory()) {
-      // Some callers may already initialize NetworkContextFilePaths, and we
-      // don't want to overwrite them.
-      if (!context_params->file_paths) {
-        context_params->file_paths =
-            network::mojom::NetworkContextFilePaths::New();
-      }
-      context_params->file_paths->shared_dictionary_directory =
-          partition_path_.Append(FILE_PATH_LITERAL("Shared Dictionary"));
-    }
-    if (context_params->shared_dictionary_cache_max_size == 0u) {
-      CalculateAndSetSharedDictionaryCacheMaxSize(
-          GetWeakPtr(), is_in_memory() ? base::FilePath() : partition_path_);
-    }
-  }
-
   if (cookie_deprecation_label_manager_) {
     context_params->cookie_deprecation_label =
         cookie_deprecation_label_manager_->GetValue();
index 3ebaf97c8510fbd5116e414d86e08603ff0b5cdd..6e7a19b444b757801fa9bd6d8c958d2ab862f686 100644 (file)
@@ -3127,6 +3127,16 @@ void NetworkContext::CreateTrustedUrlLoaderFactoryForNetworkService(
                          std::move(url_loader_factory_params));
 }
 
+void NetworkContext::SetCacheMode(bool enable) {
+  net::HttpCache* cache =
+      url_request_context_->http_transaction_factory()->GetCache();
+  if (!cache) {
+    LOG(ERROR) << "Fail to set cache mode.";
+    return;
+  }
+  cache->set_mode(enable ? net::HttpCache::NORMAL : net::HttpCache::DISABLE);
+}
+
 void NetworkContext::CreateCookieManager(
     mojom::NetworkContextParamsPtr params) {
   base::FilePath cookie_path;
index 1b03f8b06a0512976c7b20aa83905413271aeecd..d189f41847083978356d912141b4f3972ad5c493 100644 (file)
@@ -673,7 +673,8 @@ class COMPONENT_EXPORT(NETWORK_SERVICE) NetworkContext
   // be removed when AFP block list logic is migrated to subresource filter.
   bool AfpBlockListExperimentEnabled() const;
 
- void CreateCookieManager(mojom::NetworkContextParamsPtr params) override;
+  void CreateCookieManager(mojom::NetworkContextParamsPtr params) override;
+  void SetCacheMode(bool enable) override;
 
  private:
   class NetworkContextHttpAuthPreferences : public net::HttpAuthPreferences {
index a4a59eb47c0dda374cc80d0a33a3bb0596f7741b..d073132b2bb689cf83bed531923be6b76cc1ec47 100644 (file)
@@ -1683,4 +1683,7 @@ interface NetworkContext {
 
   // Create a new cookie manager with new parameters.
   CreateCookieManager(NetworkContextParams params);
+
+  // Disable/Enable diskcache
+  SetCacheMode(bool enable);
 };
index 0c4301397d0c97c849b216f2bc38d70e86869a3d..1d8769bba2523d201404e6a53f7f07b97641bf08 100644 (file)
@@ -23,6 +23,8 @@ const char kImplLibraryUpgrade[] = "impl-library-upgrade";
 const char kCORSEnabledURLSchemes[] = "cors-enabled-url-schemes";
 // JS plugin mime types
 const char kJSPluginMimeTypes[] = "jsplugin-mime-types";
+// Forces the maximum disk space to be used by the disk cache, in bytes.
+const char kSizeOfDiskCache[] = "disk-cache-size";
 // Time offset
 const char kTimeOffset[] = "time-offset";
 #endif
index 62703899fdbd5ded98ff3c99ff143ff763a5e78e..026e0c2ebb82a0cda81f5dbf3f8e5e7ae5ea9d1c 100644 (file)
@@ -28,6 +28,7 @@ CONTENT_EXPORT extern const char kImplLibraryUpgrade[];
 CONTENT_EXPORT extern const char kCORSEnabledURLSchemes[];
 // JS plugin mime types
 CONTENT_EXPORT extern const char kJSPluginMimeTypes[];
+CONTENT_EXPORT extern const char kSizeOfDiskCache[];
 // Enables time offset
 CONTENT_EXPORT extern const char kTimeOffset[];
 #endif
index ecd5017bfcbc6b0883f7a2b34737a222a5d7245a..d29add36495d759373cd2349ad9d0df46bfd9d5e 100644 (file)
@@ -5,6 +5,7 @@
 #include "content_browser_client_efl.h"
 
 #include "base/functional/callback.h"
+#include "base/path_service.h"
 #include "base/strings/string_number_conversions.h"
 #include "browser/navigation_throttle_efl.h"
 #include "browser/network_service/browser_url_loader_throttle_efl.h"
@@ -258,6 +259,40 @@ void AcceptLanguagesHelper::RemoveAcceptLangsChangedCallback(  // LCOV_EXCL_LINE
   }
 }
 
+void ContentBrowserClientEfl::ConfigureNetworkContextParams(
+    BrowserContext* context,
+    bool in_memory,
+    const base::FilePath& relative_partition_path,
+    network::mojom::NetworkContextParams* network_context_params,
+    cert_verifier::mojom::CertVerifierCreationParams*
+        cert_verifier_creation_params) {
+  base::FilePath cache_base_path;
+
+  if (!base::PathService::Get(base::DIR_CACHE, &cache_base_path)) {
+    LOG(ERROR) << "Could not retrieve path to the cache directory";
+    return;
+  }
+#if BUILDFLAG(IS_TIZEN_TV)
+  int cache_max_size = 0;
+  if (!base::StringToInt(
+          base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
+              switches::kSizeOfDiskCache),
+          &cache_max_size)) {
+    // Fallback when StringToInt failed as it could modify the output.
+    cache_max_size = 0;
+  }
+  network_context_params->http_cache_max_size = cache_max_size;
+  if (network_context_params->file_paths)
+    network_context_params->file_paths->http_cache_directory =
+        cache_base_path.Append("Cache");
+#else
+  if (network_context_params->file_paths)
+    network_context_params->file_paths->http_cache_directory = cache_base_path;
+#endif
+  network_context_params->user_agent = GetUserAgent();
+  network_context_params->accept_language = GetAcceptLangs(context);
+}
+
 void AcceptLanguagesHelper::NotifyAcceptLangsChanged() {
   std::vector<AcceptLangsChangedCallback> callbacks;
   callbacks.swap(accept_langs_changed_callbacks_);
index 7533547d23220dc2688d49aa05ae58b31da0365e..0f91362898cce6d7170c48b0b706fe255a0a3c3d 100644 (file)
@@ -162,6 +162,13 @@ class ContentBrowserClientEfl : public ContentBrowserClient {
       content::NavigationUIData* navigation_ui_data,
       int frame_tree_node_id) override;
   std::string GetAcceptLangs(BrowserContext* context) override;
+  void ConfigureNetworkContextParams(
+      BrowserContext* context,
+      bool in_memory,
+      const base::FilePath& relative_partition_path,
+      network::mojom::NetworkContextParams* network_context_params,
+      cert_verifier::mojom::CertVerifierCreationParams*
+          cert_verifier_creation_params) override;
 
   void EnableAppControl(bool enabled) {
     enabled_app_control = enabled;
index 48c3eeb7b4fca6d5f873a828cd2af3eca6c55660..2d15c3fc144c88fed7516a780836e7bb755a5d1a 100644 (file)
@@ -20,8 +20,8 @@
 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
 #include "components/password_manager/core/browser/password_form.h"
 #include "components/password_manager/core/browser/password_store.h"
-#include "content/browser/inspector/devtools_util_manager.h"
 #include "components/services/storage/public/cpp/buckets/bucket_locator.h"
+#include "content/browser/inspector/devtools_util_manager.h"
 #include "content/public/browser/browser_context.h"
 #include "content/public/browser/browser_task_traits.h"
 #include "content/public/browser/browser_thread.h"
@@ -34,6 +34,7 @@
 #include "net/proxy_resolution/proxy_config_service_fixed.h"
 #include "net/proxy_resolution/proxy_resolution_service.h"
 #include "net/url_request/url_request_context.h"
+#include "services/network/public/mojom/network_context.mojom.h"
 #include "storage/browser/database/database_quota_client.h"
 #include "storage/browser/file_system/file_system_quota_client.h"
 #include "storage/browser/quota/quota_manager.h"
@@ -89,6 +90,18 @@ const char* const kDynamicPreloading = "DynamicPreloading";
 typedef void (*DynamicPreloading)(void);
 #endif
 
+void SetNetworkCacheEnableOnIOThread(
+    bool enable,
+    content::StoragePartition* storage_partition) {
+  if (!storage_partition)
+    return;
+
+  auto network_context = storage_partition->GetNetworkContext();
+  if (!network_context)
+    return;
+
+  network_context->SetCacheMode(enable);
+}
 /**
  * @brief Helper class for obtaining WebStorage origins
  */
@@ -336,6 +349,7 @@ bool EWebContext::OverrideMimeForURL(const std::string& url_spec,
 
 EWebContext::EWebContext(bool incognito, const std::string& injectedBundlePath)
     : injected_bundle_path_(injectedBundlePath),
+      cache_enable_(false),
 #if BUILDFLAG(IS_TIZEN)
       injected_bundle_handle_(nullptr),
 #endif
@@ -453,45 +467,17 @@ Ewk_Cache_Model EWebContext::GetCacheModel() const {
 }
 
 void EWebContext::SetNetworkCacheEnable(bool enable) {  // LCOV_EXCL_LINE
-#if !defined(EWK_BRINGUP)  // FIXME: m85 bringup
-  net::URLRequestContextGetter* url_context =
-      BrowserContext::GetDefaultStoragePartition(browser_context())
-          ->GetURLRequestContext();
-  if (!url_context)
+  if (enable == cache_enable_)
     return;
 
-  net::HttpTransactionFactory* transaction_factory =
-      url_context->GetURLRequestContext()->http_transaction_factory();
-  if (!transaction_factory)
-    return;
-
-  net::HttpCache* http_cache = transaction_factory->GetCache();
-  if (!http_cache)
-    return;
+  cache_enable_ = enable;
 
-  if (enable)
-    http_cache->set_mode(net::HttpCache::NORMAL);
-  else
-    http_cache->set_mode(net::HttpCache::DISABLE);
-#endif
+  content::GetIOThreadTaskRunner({})->PostTask(
+      FROM_HERE,
+      base::BindOnce(&SetNetworkCacheEnableOnIOThread, cache_enable_,
+                     browser_context()->GetDefaultStoragePartition()));
 }  // LCOV_EXCL_LINE
 
-bool EWebContext::GetNetworkCacheEnable() const {  // LCOV_EXCL_LINE
-#if defined(EWK_BRINGUP)  // FIXME: m85 bringup
-  return false;           // LCOV_EXCL_LINE
-#else
-  net::HttpCache* http_cache =
-      BrowserContext::GetDefaultStoragePartition(browser_context())
-          ->GetURLRequestContext()
-          ->GetURLRequestContext()
-          ->http_transaction_factory()
-          ->GetCache();
-  if (!http_cache)
-    return false;
-
-  return (http_cache->mode() != net::HttpCache::DISABLE);
-#endif
-}
 /* LCOV_EXCL_START */
 void EWebContext::SetCertificatePath(const std::string& certificate_path) {
   browser_context_->SetCertificatePath(certificate_path);
index 545e5a77be88f3d09ea8ffcc14ea14fd292d5d66..cd261ea5be7723ccdc6238dee462dae22a37f025 100644 (file)
@@ -98,7 +98,7 @@ class EWebContext {
   void SetCacheModel(Ewk_Cache_Model);
   Ewk_Cache_Model GetCacheModel() const;
   void SetNetworkCacheEnable(bool enable);
-  bool GetNetworkCacheEnable() const;
+  bool GetNetworkCacheEnable() const { return cache_enable_; }
 
   void NotifyLowMemory();
 
@@ -252,6 +252,7 @@ class EWebContext {
   std::string proxy_username_;
   std::string proxy_password_;
   std::string injected_bundle_path_;
+  bool cache_enable_;
 
 #if BUILDFLAG(IS_TIZEN)
   void* injected_bundle_handle_;