Skip to content

Commit d4f5ebe

Browse files
feat: allow setting of global fallback user agent (electron#18016)
* feat: allow setting of global fallback user agent * spec: add tests for app.set/getUserAgentFallback
1 parent 649d7c0 commit d4f5ebe

File tree

9 files changed

+63
-5
lines changed

9 files changed

+63
-5
lines changed

atom/browser/api/atom_api_app.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,6 +1271,14 @@ void App::EnableSandbox(mate::Arguments* args) {
12711271
command_line->AppendSwitch(switches::kEnableSandbox);
12721272
}
12731273

1274+
void App::SetUserAgentFallback(const std::string& user_agent) {
1275+
AtomBrowserClient::Get()->SetUserAgent(user_agent);
1276+
}
1277+
1278+
std::string App::GetUserAgentFallback() {
1279+
return AtomBrowserClient::Get()->GetUserAgent();
1280+
}
1281+
12741282
#if defined(OS_MACOSX)
12751283
bool App::MoveToApplicationsFolder(mate::Arguments* args) {
12761284
return ui::cocoa::AtomBundleMover::Move(args);
@@ -1450,6 +1458,8 @@ void App::BuildPrototype(v8::Isolate* isolate,
14501458
#if defined(OS_MACOSX)
14511459
.SetProperty("dock", &App::GetDockAPI)
14521460
#endif
1461+
.SetProperty("userAgentFallback", &App::GetUserAgentFallback,
1462+
&App::SetUserAgentFallback)
14531463
.SetMethod("enableSandbox", &App::EnableSandbox);
14541464
}
14551465

atom/browser/api/atom_api_app.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ class App : public AtomBrowserClient::Delegate,
210210
v8::Local<v8::Promise> GetGPUInfo(v8::Isolate* isolate,
211211
const std::string& info_type);
212212
void EnableSandbox(mate::Arguments* args);
213+
void SetUserAgentFallback(const std::string& user_agent);
214+
std::string GetUserAgentFallback();
213215

214216
#if defined(OS_MACOSX)
215217
bool MoveToApplicationsFolder(mate::Arguments* args);

atom/browser/atom_browser_client.cc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,13 @@ std::string AtomBrowserClient::GetProduct() const {
924924
}
925925

926926
std::string AtomBrowserClient::GetUserAgent() const {
927-
return GetApplicationUserAgent();
927+
if (user_agent_override_.empty())
928+
return GetApplicationUserAgent();
929+
return user_agent_override_;
930+
}
931+
932+
void AtomBrowserClient::SetUserAgent(const std::string& user_agent) {
933+
user_agent_override_ = user_agent;
928934
}
929935

930936
void AtomBrowserClient::RegisterNonNetworkNavigationURLLoaderFactories(

atom/browser/atom_browser_client.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ class AtomBrowserClient : public content::ContentBrowserClient,
6464
// content::ContentBrowserClient:
6565
bool ShouldEnableStrictSiteIsolation() override;
6666

67+
std::string GetUserAgent() const override;
68+
void SetUserAgent(const std::string& user_agent);
69+
6770
protected:
6871
void RenderProcessWillLaunch(
6972
content::RenderProcessHost* host,
@@ -157,7 +160,6 @@ class AtomBrowserClient : public content::ContentBrowserClient,
157160
std::vector<base::FilePath> GetNetworkContextsParentDirectory() override;
158161
bool ShouldBypassCORB(int render_process_id) const override;
159162
std::string GetProduct() const override;
160-
std::string GetUserAgent() const override;
161163
void RegisterNonNetworkNavigationURLLoaderFactories(
162164
int frame_tree_node_id,
163165
NonNetworkURLLoaderFactoryMap* factories) override;
@@ -236,6 +238,8 @@ class AtomBrowserClient : public content::ContentBrowserClient,
236238
mutable base::Lock process_preferences_lock_;
237239
std::map<int, ProcessPreferences> process_preferences_;
238240

241+
std::string user_agent_override_ = "";
242+
239243
DISALLOW_COPY_AND_ASSIGN(AtomBrowserClient);
240244
};
241245

atom/browser/atom_browser_context.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <utility>
88

99
#include "atom/browser/atom_blob_reader.h"
10+
#include "atom/browser/atom_browser_client.h"
1011
#include "atom/browser/atom_browser_main_parts.h"
1112
#include "atom/browser/atom_download_manager_delegate.h"
1213
#include "atom/browser/atom_paths.h"
@@ -67,7 +68,7 @@ AtomBrowserContext::AtomBrowserContext(const std::string& partition,
6768
storage_policy_(new SpecialStoragePolicy),
6869
in_memory_(in_memory),
6970
weak_factory_(this) {
70-
user_agent_ = GetApplicationUserAgent();
71+
user_agent_ = AtomBrowserClient::Get()->GetUserAgent();
7172

7273
// Read options.
7374
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();

atom/browser/net/system_network_context_manager.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <string>
88
#include <utility>
99

10+
#include "atom/browser/atom_browser_client.h"
1011
#include "atom/browser/io_thread.h"
1112
#include "atom/common/application_info.h"
1213
#include "atom/common/options_switches.h"
@@ -245,7 +246,8 @@ SystemNetworkContextManager::CreateNetworkContextParams() {
245246

246247
network_context_params->context_name = std::string("system");
247248

248-
network_context_params->user_agent = atom::GetApplicationUserAgent();
249+
network_context_params->user_agent =
250+
atom::AtomBrowserClient::Get()->GetUserAgent();
249251

250252
network_context_params->http_cache_enabled = false;
251253

docs/api/app.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,6 +1354,15 @@ On macOS, setting this with any nonzero integer shows on the dock icon. On Linux
13541354
**Note:** Unity launcher requires the existence of a `.desktop` file to work,
13551355
for more information please read [Desktop Environment Integration][unity-requirement].
13561356

1357+
### `app.userAgentFallback`
1358+
1359+
A `String` which is the user agent string Electron will use as a global fallback.
1360+
1361+
This is the user agent that will be used when no user agent is set at the
1362+
`webContents` or `session` level. Useful for ensuring your entire
1363+
app has the same user agent. Set to a custom value as early as possible
1364+
in your apps initialization to ensure that your overridden value is used.
1365+
13571366
### `app.isPackaged`
13581367

13591368
A `Boolean` property that returns `true` if the app is packaged, `false` otherwise. For many apps, this property can be used to distinguish development and production environments.

spec-main/api-app-spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,30 @@ describe('default behavior', () => {
12961296
expect(result).to.equal(true)
12971297
})
12981298
})
1299+
1300+
describe('user agent fallback', () => {
1301+
let initialValue: string
1302+
1303+
before(() => {
1304+
initialValue = app.userAgentFallback!
1305+
})
1306+
1307+
it('should have a reasonable default', () => {
1308+
expect(initialValue).to.include(`Electron/${process.versions.electron}`)
1309+
expect(initialValue).to.include(`Chrome/${process.versions.chrome}`)
1310+
})
1311+
1312+
it('should be overridable', () => {
1313+
app.userAgentFallback = 'test-agent/123'
1314+
expect(app.userAgentFallback).to.equal('test-agent/123')
1315+
})
1316+
1317+
it('should be restorable', () => {
1318+
app.userAgentFallback = 'test-agent/123'
1319+
app.userAgentFallback = ''
1320+
expect(app.userAgentFallback).to.equal(initialValue)
1321+
})
1322+
})
12991323
})
13001324

13011325
async function runTestApp (name: string, ...args: any[]) {

spec-main/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,6 @@ app.whenReady().then(() => {
8080
process.exit(runner.failures)
8181
})
8282
}
83-
const runner = (isCI) ? mocha.run(cb) : mocha.forbidOnly().run(cb)
83+
const runner = (isCI) ? mocha.forbidOnly().run(cb) : mocha.run(cb)
8484
})
8585
})

0 commit comments

Comments
 (0)