From 4e2aa375b7d22c2f46a9dcb06c7041b9e8806bb9 Mon Sep 17 00:00:00 2001 From: aee Date: Thu, 19 Sep 2024 14:07:37 -0700 Subject: [PATCH] Rename protocolfilter setting, fix issue and add test. (#4128) b/205134049 --- cobalt/black_box_tests/black_box_tests.py | 1 + .../testdata/h5vcc_settings.html | 91 +++++++++++++++++++ .../tests/h5vcc_settings_test.py | 27 ++++++ cobalt/h5vcc/h5vcc_settings.cc | 27 ++---- cobalt/network/network_module.cc | 47 +++++++++- cobalt/network/network_module.h | 4 +- 6 files changed, 173 insertions(+), 24 deletions(-) create mode 100644 cobalt/black_box_tests/testdata/h5vcc_settings.html create mode 100644 cobalt/black_box_tests/tests/h5vcc_settings_test.py diff --git a/cobalt/black_box_tests/black_box_tests.py b/cobalt/black_box_tests/black_box_tests.py index e9f2f868a4a8..c2295f529513 100755 --- a/cobalt/black_box_tests/black_box_tests.py +++ b/cobalt/black_box_tests/black_box_tests.py @@ -69,6 +69,7 @@ 'cpu_usage_tracker_test', 'default_site_can_load', 'disable_eval_with_csp', + 'h5vcc_settings_test', 'h5vcc_storage_write_verify_test', # TODO(b/346882263): Disabled, it's suddenly flaky # 'h5vcc_watchdog_api_test', diff --git a/cobalt/black_box_tests/testdata/h5vcc_settings.html b/cobalt/black_box_tests/testdata/h5vcc_settings.html new file mode 100644 index 000000000000..65631171c54c --- /dev/null +++ b/cobalt/black_box_tests/testdata/h5vcc_settings.html @@ -0,0 +1,91 @@ + + + + + + + Cobalt h5vcc.settings + + + + + + diff --git a/cobalt/black_box_tests/tests/h5vcc_settings_test.py b/cobalt/black_box_tests/tests/h5vcc_settings_test.py new file mode 100644 index 000000000000..8c47d3b1458a --- /dev/null +++ b/cobalt/black_box_tests/tests/h5vcc_settings_test.py @@ -0,0 +1,27 @@ +# Copyright 2022 The Cobalt Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""Tests h5vcc.settings functionality.""" + +from cobalt.black_box_tests import black_box_tests +from cobalt.black_box_tests.threaded_web_server import ThreadedWebServer + + +class H5vccSettingsTest(black_box_tests.BlackBoxTestCase): + + def test_service_worker_fetch(self): + with ThreadedWebServer(binding_address=self.GetBindingAddress()) as server: + url = server.GetURL(file_name='testdata/h5vcc_settings.html') + with self.CreateCobaltRunner(url=url) as runner: + runner.WaitForJSTestsSetup() + self.assertTrue(runner.JSTestsSucceeded()) diff --git a/cobalt/h5vcc/h5vcc_settings.cc b/cobalt/h5vcc/h5vcc_settings.cc index 382febda7d99..165b86ca2426 100644 --- a/cobalt/h5vcc/h5vcc_settings.cc +++ b/cobalt/h5vcc/h5vcc_settings.cc @@ -118,30 +118,15 @@ bool H5vccSettings::Set(const std::string& name, SetValueType value) const { } } + // Disabled due to a bug with previous implementation. + if (name.compare("protocolfilter") == 0) { + return false; + } if (name.compare(network::kProtocolFilterKey) == 0 && value.IsType() && value.AsType().size() < 16384) { - std::string raw_json = value.AsType(); - base::Value old_config_json; - persistent_settings_->Get(network::kProtocolFilterKey, &old_config_json); - - if (raw_json.empty() && (!old_config_json.is_string() || - !old_config_json.GetString().empty())) { - persistent_settings_->Set(network::kProtocolFilterKey, base::Value()); - network_module_->SetProtocolFilterUpdatePending(); - return true; - } - - absl::optional old_config = - base::JSONReader::Read(old_config_json.GetString()); - absl::optional new_config = base::JSONReader::Read(raw_json); - if (!new_config) return false; - if (old_config && *old_config == *new_config) return false; - - persistent_settings_->Set(network::kProtocolFilterKey, - base::Value(raw_json)); - network_module_->SetProtocolFilterUpdatePending(); - return true; + return network_module_->SetHttpProtocolFilterPersistentSetting( + value.AsType()); } if (name.compare("cpu_usage_tracker_intervals") == 0 && diff --git a/cobalt/network/network_module.cc b/cobalt/network/network_module.cc index 8954d43c3fe1..5a41e7fa8044 100644 --- a/cobalt/network/network_module.cc +++ b/cobalt/network/network_module.cc @@ -19,6 +19,7 @@ #include "base/bind.h" #include "base/command_line.h" #include "base/json/json_reader.h" +#include "base/json/json_writer.h" #include "base/logging.h" #include "base/path_service.h" #include "base/strings/string_number_conversions.h" @@ -161,8 +162,52 @@ void NetworkModule::SetEnableHttp3FromPersistentSettings() { } } -void NetworkModule::SetProtocolFilterUpdatePending() { +bool NetworkModule::SetHttpProtocolFilterPersistentSetting( + const std::string& raw_json) { + base::Value old_config_json; + options_.persistent_settings->Get(network::kProtocolFilterKey, + &old_config_json); + + if (raw_json.empty()) { + if (old_config_json.is_none()) { + return false; + } + options_.persistent_settings->Set(network::kProtocolFilterKey, + base::Value()); + protocol_filter_update_pending_ = true; + return true; + } + + absl::optional old_config; + if (old_config_json.is_string()) { + old_config = base::JSONReader::Read(old_config_json.GetString()); + } + absl::optional raw_config = base::JSONReader::Read(raw_json); + if (!raw_config) return false; + base::Value::List new_config; + if (!raw_config->is_list()) return false; + for (auto& filter_value : raw_config->GetList()) { + if (!filter_value.is_dict()) continue; + const auto& dict = filter_value.GetDict(); + const base::Value* origin = dict.Find("origin"); + const base::Value* alt_svc = dict.Find("altSvc"); + if (!origin || !alt_svc) continue; + if (!origin->is_string() || !alt_svc->is_string()) continue; + base::Value::Dict dest_dict; + dest_dict.Set("origin", origin->GetString()); + dest_dict.Set("altSvc", alt_svc->GetString()); + new_config.Append(std::move(dest_dict)); + } + if (new_config.empty()) return false; + if (old_config && old_config->is_list() && + old_config->GetList() == new_config) + return false; + absl::optional json = base::WriteJson(new_config); + if (!json) return false; + options_.persistent_settings->Set(network::kProtocolFilterKey, + base::Value(*json)); protocol_filter_update_pending_ = true; + return true; } void NetworkModule::SetProtocolFilterFromPersistentSettings() { diff --git a/cobalt/network/network_module.h b/cobalt/network/network_module.h index 436d69c95958..9ddd9bb50447 100644 --- a/cobalt/network/network_module.h +++ b/cobalt/network/network_module.h @@ -61,7 +61,7 @@ constexpr int32_t kEnabledClientHintHeaders = (kCallTypeLoader | kCallTypeXHR); const char kQuicEnabledPersistentSettingsKey[] = "QUICEnabled"; const char kHttp2EnabledPersistentSettingsKey[] = "HTTP2Enabled"; const char kHttp3EnabledPersistentSettingsKey[] = "HTTP3Enabled"; -const char kProtocolFilterKey[] = "protocolfilter"; +const char kProtocolFilterKey[] = "httpProtocolFilter"; class NetworkSystem; // NetworkModule wraps various networking-related components such as @@ -134,7 +134,7 @@ class NetworkModule : public base::CurrentThread::DestructionObserver { void SetEnableQuicFromPersistentSettings(); void SetEnableHttp2FromPersistentSettings(); void SetEnableHttp3FromPersistentSettings(); - void SetProtocolFilterUpdatePending(); + bool SetHttpProtocolFilterPersistentSetting(const std::string&); void SetProtocolFilterFromPersistentSettings(); // Adds the Client Hint Headers to the provided URLFetcher if enabled.