fix: navigator.connection not working as intended (#38491)

* fix: navigator.connection not working as intended

* chore: make network quality methods private
This commit is contained in:
Shelley Vohr 2023-05-31 17:06:25 +02:00 committed by GitHub
parent 67f273a6d6
commit 57147d1b8d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 0 deletions

View file

@ -25,6 +25,8 @@
#include "components/proxy_config/proxy_config_dictionary.h" #include "components/proxy_config/proxy_config_dictionary.h"
#include "components/proxy_config/proxy_config_pref_names.h" #include "components/proxy_config/proxy_config_pref_names.h"
#include "content/public/browser/child_process_security_policy.h" #include "content/public/browser/child_process_security_policy.h"
#include "content/public/browser/network_quality_observer_factory.h"
#include "content/public/browser/network_service_instance.h"
#include "content/public/common/content_switches.h" #include "content/public/common/content_switches.h"
#include "electron/fuses.h" #include "electron/fuses.h"
#include "extensions/common/constants.h" #include "extensions/common/constants.h"
@ -125,6 +127,10 @@ void BrowserProcessImpl::PreCreateThreads() {
SystemNetworkContextManager::CreateInstance(local_state_.get()); SystemNetworkContextManager::CreateInstance(local_state_.get());
} }
void BrowserProcessImpl::PreMainMessageLoopRun() {
CreateNetworkQualityObserver();
}
void BrowserProcessImpl::PostMainMessageLoopRun() { void BrowserProcessImpl::PostMainMessageLoopRun() {
if (local_state_) if (local_state_)
local_state_->CommitPendingWrite(); local_state_->CommitPendingWrite();
@ -333,3 +339,18 @@ void BrowserProcessImpl::SetGeolocationManager(
std::unique_ptr<device::GeolocationManager> geolocation_manager) { std::unique_ptr<device::GeolocationManager> geolocation_manager) {
geolocation_manager_ = std::move(geolocation_manager); geolocation_manager_ = std::move(geolocation_manager);
} }
network::NetworkQualityTracker* BrowserProcessImpl::GetNetworkQualityTracker() {
if (!network_quality_tracker_) {
network_quality_tracker_ = std::make_unique<network::NetworkQualityTracker>(
base::BindRepeating(&content::GetNetworkService));
}
return network_quality_tracker_.get();
}
void BrowserProcessImpl::CreateNetworkQualityObserver() {
DCHECK(!network_quality_observer_);
network_quality_observer_ =
content::CreateNetworkQualityObserver(GetNetworkQualityTracker());
DCHECK(network_quality_observer_);
}

View file

@ -19,6 +19,7 @@
#include "components/prefs/pref_service.h" #include "components/prefs/pref_service.h"
#include "components/prefs/value_map_pref_store.h" #include "components/prefs/value_map_pref_store.h"
#include "printing/buildflags/buildflags.h" #include "printing/buildflags/buildflags.h"
#include "services/network/public/cpp/network_quality_tracker.h"
#include "services/network/public/cpp/shared_url_loader_factory.h" #include "services/network/public/cpp/shared_url_loader_factory.h"
#include "shell/browser/net/system_network_context_manager.h" #include "shell/browser/net/system_network_context_manager.h"
@ -45,6 +46,7 @@ class BrowserProcessImpl : public BrowserProcess {
BuildState* GetBuildState() override; BuildState* GetBuildState() override;
void PostEarlyInitialization(); void PostEarlyInitialization();
void PreCreateThreads(); void PreCreateThreads();
void PreMainMessageLoopRun();
void PostDestroyThreads() {} void PostDestroyThreads() {}
void PostMainMessageLoopRun(); void PostMainMessageLoopRun();
@ -113,6 +115,9 @@ class BrowserProcessImpl : public BrowserProcess {
std::unique_ptr<device::GeolocationManager> geolocation_manager) override; std::unique_ptr<device::GeolocationManager> geolocation_manager) override;
private: private:
void CreateNetworkQualityObserver();
network::NetworkQualityTracker* GetNetworkQualityTracker();
#if BUILDFLAG(ENABLE_PRINTING) #if BUILDFLAG(ENABLE_PRINTING)
std::unique_ptr<printing::PrintJobManager> print_job_manager_; std::unique_ptr<printing::PrintJobManager> print_job_manager_;
#endif #endif
@ -121,6 +126,11 @@ class BrowserProcessImpl : public BrowserProcess {
std::string locale_; std::string locale_;
std::string system_locale_; std::string system_locale_;
embedder_support::OriginTrialsSettingsStorage origin_trials_settings_storage_; embedder_support::OriginTrialsSettingsStorage origin_trials_settings_storage_;
std::unique_ptr<network::NetworkQualityTracker> network_quality_tracker_;
std::unique_ptr<
network::NetworkQualityTracker::RTTAndThroughputEstimatesObserver>
network_quality_observer_;
}; };
#endif // ELECTRON_SHELL_BROWSER_BROWSER_PROCESS_IMPL_H_ #endif // ELECTRON_SHELL_BROWSER_BROWSER_PROCESS_IMPL_H_

View file

@ -537,6 +537,8 @@ int ElectronBrowserMainParts::PreMainMessageLoopRun() {
// Notify observers that main thread message loop was initialized. // Notify observers that main thread message loop was initialized.
Browser::Get()->PreMainMessageLoopRun(); Browser::Get()->PreMainMessageLoopRun();
fake_browser_process_->PreMainMessageLoopRun();
return GetExitCode(); return GetExitCode();
} }

View file

@ -1970,6 +1970,30 @@ describe('chromium features', () => {
}); });
}); });
// https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation
describe('navigator.connection', () => {
it('returns the correct value', async () => {
const w = new BrowserWindow({ show: false });
w.webContents.session.enableNetworkEmulation({
latency: 500,
downloadThroughput: 6400,
uploadThroughput: 6400
});
await w.loadURL(`file://${fixturesPath}/pages/blank.html`);
const rtt = await w.webContents.executeJavaScript('navigator.connection.rtt');
expect(rtt).to.be.a('number');
const downlink = await w.webContents.executeJavaScript('navigator.connection.downlink');
expect(downlink).to.be.a('number');
const effectiveTypes = ['slow-2g', '2g', '3g', '4g'];
const effectiveType = await w.webContents.executeJavaScript('navigator.connection.effectiveType');
expect(effectiveTypes).to.include(effectiveType);
});
});
describe('navigator.userAgentData', () => { describe('navigator.userAgentData', () => {
// These tests are done on an http server because navigator.userAgentData // These tests are done on an http server because navigator.userAgentData
// requires a secure context. // requires a secure context.