Skip to content

Commit

Permalink
Avoid querying persistent settings on every network request. (youtube…
Browse files Browse the repository at this point in the history
…#511)

b/188060079
  • Loading branch information
gbournou authored May 31, 2023
1 parent b811afb commit 971be16
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
4 changes: 4 additions & 0 deletions cobalt/h5vcc/h5vcc_settings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ bool H5vccSettings::Set(const std::string& name, int32 value) const {
persistent_settings_->SetPersistentSetting(
network::kClientHintHeadersEnabledPersistentSettingsKey,
std::make_unique<base::Value>(value != 0));
// Tell NetworkModule (if exists) to re-query persistent settings.
if (network_module_) {
network_module_->SetEnableClientHintHeadersFromPersistentSettings();
}
return true;
}
}
Expand Down
15 changes: 11 additions & 4 deletions cobalt/network/network_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ void NetworkModule::SetEnableQuic(bool enable_quic) {
base::Unretained(url_request_context_.get()), enable_quic));
}

void NetworkModule::SetEnableClientHintHeadersFromPersistentSettings() {
// Called on initialization and when the persistent setting is changed.
enable_client_hint_headers_.store(
options_.persistent_settings != nullptr &&
options_.persistent_settings->GetPersistentSettingAsBool(
kClientHintHeadersEnabledPersistentSettingsKey, false));
}

void NetworkModule::Initialize(const std::string& user_agent_string,
base::EventDispatcher* event_dispatcher) {
thread_.reset(new base::Thread("NetworkModule"));
Expand All @@ -111,6 +119,8 @@ void NetworkModule::Initialize(const std::string& user_agent_string,
http_user_agent_settings_.reset(new net::StaticHttpUserAgentSettings(
options_.preferred_language, user_agent_string));

SetEnableClientHintHeadersFromPersistentSettings();

#if defined(ENABLE_DEBUG_COMMAND_LINE_SWITCHES)
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();

Expand Down Expand Up @@ -202,10 +212,7 @@ void NetworkModule::OnCreate(base::WaitableEvent* creation_event) {
}

void NetworkModule::AddClientHintHeaders(net::URLFetcher& url_fetcher) const {
// Check if persistent setting is enabled before adding the headers.
if (options_.persistent_settings != nullptr &&
options_.persistent_settings->GetPersistentSettingAsBool(
kClientHintHeadersEnabledPersistentSettingsKey, false)) {
if (enable_client_hint_headers_.load()) {
for (const auto& header : client_hint_headers_) {
url_fetcher.AddExtraRequestHeader(header);
}
Expand Down
8 changes: 6 additions & 2 deletions cobalt/network/network_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "net/dial/dial_service.h"
#endif
#include "net/url_request/http_user_agent_settings.h"
#include "starboard/common/atomic.h"

namespace base {
class WaitableEvent;
Expand Down Expand Up @@ -116,8 +117,10 @@ class NetworkModule {

void SetEnableQuic(bool enable_quic);

// Adds the Client Hint Headers to the provided URLFetcher.
// It is conditional on kClientHintHeadersEnabledPersistentSettingsKey != 0.
// Checks persistent settings to determine if Client Hint Headers are enabled.
void SetEnableClientHintHeadersFromPersistentSettings();

// Adds the Client Hint Headers to the provided URLFetcher if enabled.
void AddClientHintHeaders(net::URLFetcher& url_fetcher) const;

private:
Expand All @@ -127,6 +130,7 @@ class NetworkModule {
std::unique_ptr<network_bridge::NetPoster> CreateNetPoster();

std::vector<std::string> client_hint_headers_;
starboard::atomic_bool enable_client_hint_headers_;
storage::StorageManager* storage_manager_;
std::unique_ptr<base::Thread> thread_;
std::unique_ptr<URLRequestContext> url_request_context_;
Expand Down

0 comments on commit 971be16

Please sign in to comment.