Provide default user agent in BrowserContext

This commit is contained in:
Cheng Zhao 2015-07-14 11:40:07 -07:00
parent 56dfef8d0d
commit 940db1d1dd
4 changed files with 26 additions and 26 deletions

View file

@ -6,15 +6,12 @@
#include <set> #include <set>
#include "atom/browser/browser.h"
#include "atom/browser/api/atom_api_session.h" #include "atom/browser/api/atom_api_session.h"
#include "atom/browser/atom_browser_client.h" #include "atom/browser/atom_browser_client.h"
#include "atom/browser/atom_browser_context.h" #include "atom/browser/atom_browser_context.h"
#include "atom/browser/atom_browser_main_parts.h" #include "atom/browser/atom_browser_main_parts.h"
#include "atom/browser/native_window.h" #include "atom/browser/native_window.h"
#include "atom/browser/web_view_guest_delegate.h" #include "atom/browser/web_view_guest_delegate.h"
#include "atom/common/atom_version.h"
#include "atom/common/chrome_version.h"
#include "atom/common/api/api_messages.h" #include "atom/common/api/api_messages.h"
#include "atom/common/event_emitter_caller.h" #include "atom/common/event_emitter_caller.h"
#include "atom/common/native_mate_converters/gfx_converter.h" #include "atom/common/native_mate_converters/gfx_converter.h"
@ -23,7 +20,6 @@
#include "atom/common/native_mate_converters/string16_converter.h" #include "atom/common/native_mate_converters/string16_converter.h"
#include "atom/common/native_mate_converters/value_converter.h" #include "atom/common/native_mate_converters/value_converter.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "brightray/browser/inspectable_web_contents.h" #include "brightray/browser/inspectable_web_contents.h"
#include "chrome/browser/printing/print_view_manager_basic.h" #include "chrome/browser/printing/print_view_manager_basic.h"
@ -40,7 +36,6 @@
#include "content/public/browser/storage_partition.h" #include "content/public/browser/storage_partition.h"
#include "content/public/browser/site_instance.h" #include "content/public/browser/site_instance.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "content/public/common/user_agent.h"
#include "native_mate/callback.h" #include "native_mate/callback.h"
#include "native_mate/dictionary.h" #include "native_mate/dictionary.h"
#include "native_mate/object_template_builder.h" #include "native_mate/object_template_builder.h"
@ -57,14 +52,6 @@ struct PrintSettings {
bool print_background; bool print_background;
}; };
std::string RemoveWhitespace(const std::string& str) {
std::string trimmed;
if (base::RemoveChars(str, " ", &trimmed))
return trimmed;
else
return str;
}
void SetUserAgentInIO(scoped_refptr<net::URLRequestContextGetter> getter, void SetUserAgentInIO(scoped_refptr<net::URLRequestContextGetter> getter,
std::string user_agent) { std::string user_agent) {
getter->GetURLRequestContext()->set_http_user_agent_settings( getter->GetURLRequestContext()->set_http_user_agent_settings(
@ -187,7 +174,6 @@ WebContents::WebContents(const mate::Dictionary& options) {
Observe(web_contents); Observe(web_contents);
AttachAsUserData(web_contents); AttachAsUserData(web_contents);
InitWithWebContents(web_contents); InitWithWebContents(web_contents);
SetUserAgent(std::string());
if (is_guest) { if (is_guest) {
guest_delegate_->Initialize(this); guest_delegate_->Initialize(this);
@ -567,17 +553,6 @@ bool WebContents::IsCrashed() const {
} }
void WebContents::SetUserAgent(const std::string& user_agent) { void WebContents::SetUserAgent(const std::string& user_agent) {
if (user_agent.empty()) {
// Default User Agent.
Browser* browser = Browser::Get();
std::string product_name = base::StringPrintf(
"%s/%s Chrome/%s " ATOM_PRODUCT_NAME "/" ATOM_VERSION_STRING,
RemoveWhitespace(browser->GetName()).c_str(),
browser->GetVersion().c_str(),
CHROME_VERSION_STRING);
const_cast<std::string&>(user_agent) =
content::BuildUserAgentFromProduct(product_name);
}
web_contents()->SetUserAgentOverride(user_agent); web_contents()->SetUserAgentOverride(user_agent);
scoped_refptr<net::URLRequestContextGetter> getter = scoped_refptr<net::URLRequestContextGetter> getter =
web_contents()->GetBrowserContext()->GetRequestContext(); web_contents()->GetBrowserContext()->GetRequestContext();

View file

@ -6,16 +6,22 @@
#include "atom/browser/atom_browser_main_parts.h" #include "atom/browser/atom_browser_main_parts.h"
#include "atom/browser/atom_download_manager_delegate.h" #include "atom/browser/atom_download_manager_delegate.h"
#include "atom/browser/browser.h"
#include "atom/browser/net/atom_url_request_job_factory.h" #include "atom/browser/net/atom_url_request_job_factory.h"
#include "atom/browser/net/asar/asar_protocol_handler.h" #include "atom/browser/net/asar/asar_protocol_handler.h"
#include "atom/browser/net/http_protocol_handler.h" #include "atom/browser/net/http_protocol_handler.h"
#include "atom/browser/web_view_manager.h" #include "atom/browser/web_view_manager.h"
#include "atom/common/atom_version.h"
#include "atom/common/chrome_version.h"
#include "atom/common/options_switches.h" #include "atom/common/options_switches.h"
#include "base/command_line.h" #include "base/command_line.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/threading/sequenced_worker_pool.h" #include "base/threading/sequenced_worker_pool.h"
#include "base/threading/worker_pool.h" #include "base/threading/worker_pool.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "content/public/common/url_constants.h" #include "content/public/common/url_constants.h"
#include "content/public/common/user_agent.h"
#include "net/ftp/ftp_network_layer.h" #include "net/ftp/ftp_network_layer.h"
#include "net/url_request/data_protocol_handler.h" #include "net/url_request/data_protocol_handler.h"
#include "net/url_request/ftp_protocol_handler.h" #include "net/url_request/ftp_protocol_handler.h"
@ -37,6 +43,14 @@ class NoCacheBackend : public net::HttpCache::BackendFactory {
} }
}; };
std::string RemoveWhitespace(const std::string& str) {
std::string trimmed;
if (base::RemoveChars(str, " ", &trimmed))
return trimmed;
else
return str;
}
} // namespace } // namespace
AtomBrowserContext::AtomBrowserContext() AtomBrowserContext::AtomBrowserContext()
@ -46,6 +60,16 @@ AtomBrowserContext::AtomBrowserContext()
AtomBrowserContext::~AtomBrowserContext() { AtomBrowserContext::~AtomBrowserContext() {
} }
std::string AtomBrowserContext::GetUserAgent() {
Browser* browser = Browser::Get();
std::string user_agent = base::StringPrintf(
"%s/%s Chrome/%s " ATOM_PRODUCT_NAME "/" ATOM_VERSION_STRING,
RemoveWhitespace(browser->GetName()).c_str(),
browser->GetVersion().c_str(),
CHROME_VERSION_STRING);
return content::BuildUserAgentFromProduct(user_agent);
}
net::URLRequestJobFactory* AtomBrowserContext::CreateURLRequestJobFactory( net::URLRequestJobFactory* AtomBrowserContext::CreateURLRequestJobFactory(
content::ProtocolHandlerMap* handlers, content::ProtocolHandlerMap* handlers,
content::URLRequestInterceptorScopedVector* interceptors) { content::URLRequestInterceptorScopedVector* interceptors) {

View file

@ -19,6 +19,7 @@ class AtomBrowserContext : public brightray::BrowserContext {
virtual ~AtomBrowserContext(); virtual ~AtomBrowserContext();
// brightray::URLRequestContextGetter::Delegate: // brightray::URLRequestContextGetter::Delegate:
std::string GetUserAgent() override;
net::URLRequestJobFactory* CreateURLRequestJobFactory( net::URLRequestJobFactory* CreateURLRequestJobFactory(
content::ProtocolHandlerMap* handlers, content::ProtocolHandlerMap* handlers,
content::URLRequestInterceptorScopedVector* interceptors) override; content::URLRequestInterceptorScopedVector* interceptors) override;

2
vendor/brightray vendored

@ -1 +1 @@
Subproject commit 6a38d97aa8f4e1ab1842416f632a2a45adfbc738 Subproject commit 6328c6104131e623da87f479ea305b83169099b8