Skip to content

Commit

Permalink
fix: navigator.connection not working as intended (#38491)
Browse files Browse the repository at this point in the history
* fix: navigator.connection not working as intended

* chore: make network quality methods private
  • Loading branch information
codebytere committed May 31, 2023
1 parent 67f273a commit 57147d1
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
21 changes: 21 additions & 0 deletions shell/browser/browser_process_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include "components/proxy_config/proxy_config_dictionary.h"
#include "components/proxy_config/proxy_config_pref_names.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 "electron/fuses.h"
#include "extensions/common/constants.h"
Expand Down Expand Up @@ -125,6 +127,10 @@ void BrowserProcessImpl::PreCreateThreads() {
SystemNetworkContextManager::CreateInstance(local_state_.get());
}

void BrowserProcessImpl::PreMainMessageLoopRun() {
CreateNetworkQualityObserver();
}

void BrowserProcessImpl::PostMainMessageLoopRun() {
if (local_state_)
local_state_->CommitPendingWrite();
Expand Down Expand Up @@ -333,3 +339,18 @@ void BrowserProcessImpl::SetGeolocationManager(
std::unique_ptr<device::GeolocationManager> 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_);
}
10 changes: 10 additions & 0 deletions shell/browser/browser_process_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "components/prefs/pref_service.h"
#include "components/prefs/value_map_pref_store.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 "shell/browser/net/system_network_context_manager.h"

Expand All @@ -45,6 +46,7 @@ class BrowserProcessImpl : public BrowserProcess {
BuildState* GetBuildState() override;
void PostEarlyInitialization();
void PreCreateThreads();
void PreMainMessageLoopRun();
void PostDestroyThreads() {}
void PostMainMessageLoopRun();

Expand Down Expand Up @@ -113,6 +115,9 @@ class BrowserProcessImpl : public BrowserProcess {
std::unique_ptr<device::GeolocationManager> geolocation_manager) override;

private:
void CreateNetworkQualityObserver();
network::NetworkQualityTracker* GetNetworkQualityTracker();

#if BUILDFLAG(ENABLE_PRINTING)
std::unique_ptr<printing::PrintJobManager> print_job_manager_;
#endif
Expand All @@ -121,6 +126,11 @@ class BrowserProcessImpl : public BrowserProcess {
std::string locale_;
std::string system_locale_;
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_
2 changes: 2 additions & 0 deletions shell/browser/electron_browser_main_parts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,8 @@ int ElectronBrowserMainParts::PreMainMessageLoopRun() {
// Notify observers that main thread message loop was initialized.
Browser::Get()->PreMainMessageLoopRun();

fake_browser_process_->PreMainMessageLoopRun();

return GetExitCode();
}

Expand Down
24 changes: 24 additions & 0 deletions spec/chromium-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
// These tests are done on an http server because navigator.userAgentData
// requires a secure context.
Expand Down

0 comments on commit 57147d1

Please sign in to comment.