Skip to content

Commit

Permalink
Rename protocolfilter setting, fix issue and add test.
Browse files Browse the repository at this point in the history
b/205134049
  • Loading branch information
aee-google committed Sep 18, 2024
1 parent 64694cb commit bcb0bdb
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 49 deletions.
1 change: 1 addition & 0 deletions cobalt/black_box_tests/black_box_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
91 changes: 91 additions & 0 deletions cobalt/black_box_tests/testdata/h5vcc_settings.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<!DOCTYPE html>
<!--
Copyright 2024 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.
-->
<!--
This is a basic test of the h5vcc.settings.
-->

<html>
<head>
<title>Cobalt h5vcc.settings</title>
<script src='black_box_js_test_utils.js'></script>
</head>
<body>
<script>
const reset = () => window.h5vcc.settings.set('httpProtocolFilter', '');
const set = config => window.h5vcc.settings.set('httpProtocolFilter', JSON.stringify(config));
const get = () => window.h5vcc.settings.getPersistentSettingAsString('httpProtocolFilter');

const assertSetGetFixed = (config, fixedConfig) => {
assertTrue(set(config));
assertFalse(set(config));
const storedConfig = JSON.parse(get());
assertTrue(Array.isArray(fixedConfig) && Array.isArray(storedConfig));
fixedConfig.forEach((line, i) => {
const storedLine = storedConfig[i];
Object.keys(line).forEach(key => {
assertTrue(typeof(line[key]) === 'string');
assertEqual(line[key], storedLine[key]);
});
});
}
const assertSetGet = config => {
assertSetGetFixed(config, config);
};
const assertSetFailed = config => {
assertFalse(set(config));
};

setupFinished();

assertFalse(window.h5vcc.settings.set('protocolfilter', ''));
assertFalse(window.h5vcc.settings.set('protocolfilter', '""'));
assertFalse(window.h5vcc.settings.set('protocolfilter', '[]'));
assertFalse(window.h5vcc.settings.set('protocolfilter', '{}'));

reset();
assertEqual('', get());
[
{}, [], '', null, 1, true, [{}]
].forEach(assertSetFailed);

[
[
{origin: 'example.com:443', altSvc: 'h3'},
{origin: '*', altSvc: 'h3'},
],
[
{origin: 'a.example.com:443', altSvc: 'h3'},
{origin: 'b.example.com:443', altSvc: 'h2'},
{origin: 'c.example.com:443', altSvc: 'h3'},
{origin: 'd.example.com:443', altSvc: 'h2'},
{origin: '*example.com:443', altSvc: 'h3'},
{origin: '*', altSvc: 'h3'},
],
].forEach(assertSetGet);
assertSetGetFixed([
{origin: 'example.com:443', altSvc: 'h3'},
{origin: '*', mainSvc: 'h3'},
],
[
{origin: 'example.com:443', altSvc: 'h3'},
]);
assertTrue(reset());
assertFalse(reset());
onEndTest();
</script>
</body>
</html>
27 changes: 27 additions & 0 deletions cobalt/black_box_tests/tests/h5vcc_settings_test.py
Original file line number Diff line number Diff line change
@@ -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())
52 changes: 6 additions & 46 deletions cobalt/h5vcc/h5vcc_settings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,55 +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<std::string>() &&
value.AsType<std::string>().size() < 16384) {
std::string raw_json = value.AsType<std::string>();
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<base::Value> old_config =
base::JSONReader::Read(old_config_json.GetString());
absl::optional<base::Value> 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;
}

if (name.compare("cpu_usage_tracker_intervals") == 0 &&
value.IsType<std::string>() && value.AsType<std::string>().size() < 512) {
absl::optional<base::Value> config =
base::JSONReader::Read(value.AsType<std::string>());
browser::CpuUsageTracker::GetInstance()->UpdateIntervalsDefinition(
config.has_value() ? std::move(*config) : base::Value());
return true;
}
if (name.compare("cpu_usage_tracker_intervals_enabled") == 0 &&
value.IsType<int32>()) {
browser::CpuUsageTracker::GetInstance()->UpdateIntervalsEnabled(
value.AsType<int32>() != 0);
return true;
}
if (name.compare("cpu_usage_tracker_one_time_tracking") == 0 &&
value.IsType<int32>()) {
bool started = value.AsType<int32>() != 0;
if (started) {
browser::CpuUsageTracker::GetInstance()->StartOneTimeTracking();
} else {
browser::CpuUsageTracker::GetInstance()->StopAndCaptureOneTimeTracking();
}
return true;
return network_module_->SetHttpProtocolFilterPersistentSetting(
value.AsType<std::string>());
}

if (name.compare(kSkiaRasterizer) == 0 && value.IsType<int32>()) {
Expand Down
47 changes: 46 additions & 1 deletion cobalt/network/network_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<base::Value> old_config;
if (old_config_json.is_string()) {
old_config = base::JSONReader::Read(old_config_json.GetString());
}
absl::optional<base::Value> 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<std::string> 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() {
Expand Down
4 changes: 2 additions & 2 deletions cobalt/network/network_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit bcb0bdb

Please sign in to comment.