feat: allow setting of global fallback user agent (#18016)

* feat: allow setting of global fallback user agent

* spec: add tests for app.set/getUserAgentFallback
This commit is contained in:
Samuel Attard 2019-05-01 16:34:42 -07:00 committed by GitHub
parent 649d7c0d9e
commit d4f5ebefe6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 63 additions and 5 deletions

View file

@ -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);
@ -1450,6 +1458,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);
}

View file

@ -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);

View file

@ -924,7 +924,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;
}
void AtomBrowserClient::RegisterNonNetworkNavigationURLLoaderFactories(

View file

@ -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,
@ -157,7 +160,6 @@ class AtomBrowserClient : public content::ContentBrowserClient,
std::vector<base::FilePath> GetNetworkContextsParentDirectory() override;
bool ShouldBypassCORB(int render_process_id) const override;
std::string GetProduct() const override;
std::string GetUserAgent() const override;
void RegisterNonNetworkNavigationURLLoaderFactories(
int frame_tree_node_id,
NonNetworkURLLoaderFactoryMap* factories) override;
@ -236,6 +238,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);
};

View file

@ -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"
@ -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();

View file

@ -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"
@ -245,7 +246,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;