Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow setting of global fallback user agent (backport for 6-0-x) #18473

Merged
merged 1 commit into from
May 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions atom/browser/api/atom_api_app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1271,6 +1271,14 @@ void App::EnableSandbox(mate::Arguments* args) {
command_line->AppendSwitch(switches::kEnableSandbox);
}

void App::SetUserAgentFallback(const std::string& user_agent) {
AtomBrowserClient::Get()->SetUserAgent(user_agent);
}

std::string App::GetUserAgentFallback() {
return AtomBrowserClient::Get()->GetUserAgent();
}

#if defined(OS_MACOSX)
bool App::MoveToApplicationsFolder(mate::Arguments* args) {
return ui::cocoa::AtomBundleMover::Move(args);
Expand Down Expand Up @@ -1431,6 +1439,8 @@ void App::BuildPrototype(v8::Isolate* isolate,
#if defined(OS_MACOSX)
.SetProperty("dock", &App::GetDockAPI)
#endif
.SetProperty("userAgentFallback", &App::GetUserAgentFallback,
&App::SetUserAgentFallback)
.SetMethod("enableSandbox", &App::EnableSandbox);
}

Expand Down
2 changes: 2 additions & 0 deletions atom/browser/api/atom_api_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ class App : public AtomBrowserClient::Delegate,
v8::Local<v8::Promise> GetGPUInfo(v8::Isolate* isolate,
const std::string& info_type);
void EnableSandbox(mate::Arguments* args);
void SetUserAgentFallback(const std::string& user_agent);
std::string GetUserAgentFallback();

#if defined(OS_MACOSX)
bool MoveToApplicationsFolder(mate::Arguments* args);
Expand Down
8 changes: 7 additions & 1 deletion atom/browser/atom_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,13 @@ std::string AtomBrowserClient::GetProduct() const {
}

std::string AtomBrowserClient::GetUserAgent() const {
return GetApplicationUserAgent();
if (user_agent_override_.empty())
return GetApplicationUserAgent();
return user_agent_override_;
}

void AtomBrowserClient::SetUserAgent(const std::string& user_agent) {
user_agent_override_ = user_agent;
}

std::string AtomBrowserClient::GetApplicationLocale() {
Expand Down
6 changes: 5 additions & 1 deletion atom/browser/atom_browser_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ class AtomBrowserClient : public content::ContentBrowserClient,
// content::ContentBrowserClient:
bool ShouldEnableStrictSiteIsolation() override;

std::string GetUserAgent() const override;
void SetUserAgent(const std::string& user_agent);

protected:
void RenderProcessWillLaunch(
content::RenderProcessHost* host,
Expand Down Expand Up @@ -156,7 +159,6 @@ class AtomBrowserClient : public content::ContentBrowserClient,
network::mojom::NetworkService* network_service) override;
bool ShouldBypassCORB(int render_process_id) const override;
std::string GetProduct() const override;
std::string GetUserAgent() const override;

// content::RenderProcessHostObserver:
void RenderProcessHostDestroyed(content::RenderProcessHost* host) override;
Expand Down Expand Up @@ -230,6 +232,8 @@ class AtomBrowserClient : public content::ContentBrowserClient,
mutable base::Lock process_preferences_lock_;
std::map<int, ProcessPreferences> process_preferences_;

std::string user_agent_override_ = "";

DISALLOW_COPY_AND_ASSIGN(AtomBrowserClient);
};

Expand Down
3 changes: 2 additions & 1 deletion atom/browser/atom_browser_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <utility>

#include "atom/browser/atom_blob_reader.h"
#include "atom/browser/atom_browser_client.h"
#include "atom/browser/atom_browser_main_parts.h"
#include "atom/browser/atom_download_manager_delegate.h"
#include "atom/browser/atom_paths.h"
Expand Down Expand Up @@ -67,7 +68,7 @@ AtomBrowserContext::AtomBrowserContext(const std::string& partition,
storage_policy_(new SpecialStoragePolicy),
in_memory_(in_memory),
weak_factory_(this) {
user_agent_ = GetApplicationUserAgent();
user_agent_ = AtomBrowserClient::Get()->GetUserAgent();

// Read options.
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
Expand Down
4 changes: 3 additions & 1 deletion atom/browser/net/system_network_context_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <string>
#include <utility>

#include "atom/browser/atom_browser_client.h"
#include "atom/browser/io_thread.h"
#include "atom/common/application_info.h"
#include "atom/common/options_switches.h"
Expand Down Expand Up @@ -246,7 +247,8 @@ SystemNetworkContextManager::CreateNetworkContextParams() {

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

network_context_params->user_agent = atom::GetApplicationUserAgent();
network_context_params->user_agent =
atom::AtomBrowserClient::Get()->GetUserAgent();

network_context_params->http_cache_enabled = false;

Expand Down
9 changes: 9 additions & 0 deletions docs/api/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 +1361,15 @@ This API must be called after the `ready` event is emitted.

**Note:** Rendering accessibility tree can significantly affect the performance of your app. It should not be enabled by default.

### `app.userAgentFallback`

A `String` which is the user agent string Electron will use as a global fallback.

This is the user agent that will be used when no user agent is set at the
`webContents` or `session` level. Useful for ensuring your entire
app has the same user agent. Set to a custom value as early as possible
in your apps initialization to ensure that your overridden value is used.

### `app.isPackaged`

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.
Expand Down
24 changes: 24 additions & 0 deletions spec-main/api-app-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1333,6 +1333,30 @@ describe('default behavior', () => {
expect(result).to.equal(true)
})
})

describe('user agent fallback', () => {
let initialValue: string

before(() => {
initialValue = app.userAgentFallback!
})

it('should have a reasonable default', () => {
expect(initialValue).to.include(`Electron/${process.versions.electron}`)
expect(initialValue).to.include(`Chrome/${process.versions.chrome}`)
})

it('should be overridable', () => {
app.userAgentFallback = 'test-agent/123'
expect(app.userAgentFallback).to.equal('test-agent/123')
})

it('should be restorable', () => {
app.userAgentFallback = 'test-agent/123'
app.userAgentFallback = ''
expect(app.userAgentFallback).to.equal(initialValue)
})
})
})

async function runTestApp (name: string, ...args: any[]) {
Expand Down
2 changes: 1 addition & 1 deletion spec-main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,6 @@ app.whenReady().then(() => {
process.exit(runner.failures)
})
}
const runner = (isCI) ? mocha.run(cb) : mocha.forbidOnly().run(cb)
const runner = (isCI) ? mocha.run(cb) : mocha.forbidOnly().run(cb)
})
})