Merge pull request #2220 from atom/provide-user-agent
Provide default user agent in BrowserContext
This commit is contained in:
commit
4b06c0645c
7 changed files with 71 additions and 30 deletions
|
@ -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(
|
||||||
|
@ -162,6 +149,7 @@ WebContents::WebContents(content::WebContents* web_contents)
|
||||||
: content::WebContentsObserver(web_contents),
|
: content::WebContentsObserver(web_contents),
|
||||||
type_(REMOTE) {
|
type_(REMOTE) {
|
||||||
AttachAsUserData(web_contents);
|
AttachAsUserData(web_contents);
|
||||||
|
web_contents->SetUserAgentOverride(GetBrowserContext()->GetUserAgent());
|
||||||
}
|
}
|
||||||
|
|
||||||
WebContents::WebContents(const mate::Dictionary& options) {
|
WebContents::WebContents(const mate::Dictionary& options) {
|
||||||
|
@ -187,7 +175,8 @@ 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());
|
|
||||||
|
web_contents->SetUserAgentOverride(GetBrowserContext()->GetUserAgent());
|
||||||
|
|
||||||
if (is_guest) {
|
if (is_guest) {
|
||||||
guest_delegate_->Initialize(this);
|
guest_delegate_->Initialize(this);
|
||||||
|
@ -567,17 +556,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();
|
||||||
|
@ -655,9 +633,7 @@ void WebContents::InspectServiceWorker() {
|
||||||
|
|
||||||
v8::Local<v8::Value> WebContents::Session(v8::Isolate* isolate) {
|
v8::Local<v8::Value> WebContents::Session(v8::Isolate* isolate) {
|
||||||
if (session_.IsEmpty()) {
|
if (session_.IsEmpty()) {
|
||||||
mate::Handle<api::Session> handle = Session::CreateFrom(
|
auto handle = Session::CreateFrom(isolate, GetBrowserContext());
|
||||||
isolate,
|
|
||||||
static_cast<AtomBrowserContext*>(web_contents()->GetBrowserContext()));
|
|
||||||
session_.Reset(isolate, handle.ToV8());
|
session_.Reset(isolate, handle.ToV8());
|
||||||
}
|
}
|
||||||
return v8::Local<v8::Value>::New(isolate, session_);
|
return v8::Local<v8::Value>::New(isolate, session_);
|
||||||
|
@ -832,6 +808,10 @@ bool WebContents::IsDestroyed() const {
|
||||||
return !IsAlive();
|
return !IsAlive();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
AtomBrowserContext* WebContents::GetBrowserContext() const {
|
||||||
|
return static_cast<AtomBrowserContext*>(web_contents()->GetBrowserContext());
|
||||||
|
}
|
||||||
|
|
||||||
void WebContents::OnRendererMessage(const base::string16& channel,
|
void WebContents::OnRendererMessage(const base::string16& channel,
|
||||||
const base::ListValue& args) {
|
const base::ListValue& args) {
|
||||||
// webContents.emit(channel, new Event(), args...);
|
// webContents.emit(channel, new Event(), args...);
|
||||||
|
|
|
@ -27,6 +27,7 @@ class Dictionary;
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
struct SetSizeParams;
|
struct SetSizeParams;
|
||||||
|
class AtomBrowserContext;
|
||||||
class WebViewGuestDelegate;
|
class WebViewGuestDelegate;
|
||||||
|
|
||||||
namespace api {
|
namespace api {
|
||||||
|
@ -191,6 +192,8 @@ class WebContents : public mate::TrackableObject<WebContents>,
|
||||||
REMOTE, // Thin wrap around an existing WebContents.
|
REMOTE, // Thin wrap around an existing WebContents.
|
||||||
};
|
};
|
||||||
|
|
||||||
|
AtomBrowserContext* GetBrowserContext() const;
|
||||||
|
|
||||||
// Called when received a message from renderer.
|
// Called when received a message from renderer.
|
||||||
void OnRendererMessage(const base::string16& channel,
|
void OnRendererMessage(const base::string16& channel,
|
||||||
const base::ListValue& args);
|
const base::ListValue& args);
|
||||||
|
|
|
@ -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,23 @@ AtomBrowserContext::AtomBrowserContext()
|
||||||
AtomBrowserContext::~AtomBrowserContext() {
|
AtomBrowserContext::~AtomBrowserContext() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string AtomBrowserContext::GetUserAgent() {
|
||||||
|
Browser* browser = Browser::Get();
|
||||||
|
std::string name = RemoveWhitespace(browser->GetName());
|
||||||
|
std::string user_agent;
|
||||||
|
if (name == ATOM_PRODUCT_NAME) {
|
||||||
|
user_agent = "Chrome/" CHROME_VERSION_STRING " "
|
||||||
|
ATOM_PRODUCT_NAME "/" ATOM_VERSION_STRING;
|
||||||
|
} else {
|
||||||
|
user_agent = base::StringPrintf(
|
||||||
|
"%s/%s Chrome/%s " ATOM_PRODUCT_NAME "/" ATOM_VERSION_STRING,
|
||||||
|
name.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) {
|
||||||
|
|
|
@ -5,6 +5,8 @@
|
||||||
#ifndef ATOM_BROWSER_ATOM_BROWSER_CONTEXT_H_
|
#ifndef ATOM_BROWSER_ATOM_BROWSER_CONTEXT_H_
|
||||||
#define ATOM_BROWSER_ATOM_BROWSER_CONTEXT_H_
|
#define ATOM_BROWSER_ATOM_BROWSER_CONTEXT_H_
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include "brightray/browser/browser_context.h"
|
#include "brightray/browser/browser_context.h"
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
@ -19,6 +21,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,6 +2,7 @@ assert = require 'assert'
|
||||||
http = require 'http'
|
http = require 'http'
|
||||||
https = require 'https'
|
https = require 'https'
|
||||||
path = require 'path'
|
path = require 'path'
|
||||||
|
ws = require 'ws'
|
||||||
|
|
||||||
describe 'chromium feature', ->
|
describe 'chromium feature', ->
|
||||||
fixtures = path.resolve __dirname, 'fixtures'
|
fixtures = path.resolve __dirname, 'fixtures'
|
||||||
|
@ -88,3 +89,25 @@ describe 'chromium feature', ->
|
||||||
navigator.webkitPersistentStorage.requestQuota 1024 * 1024, (grantedBytes) ->
|
navigator.webkitPersistentStorage.requestQuota 1024 * 1024, (grantedBytes) ->
|
||||||
assert.equal grantedBytes, 1048576
|
assert.equal grantedBytes, 1048576
|
||||||
done()
|
done()
|
||||||
|
|
||||||
|
describe 'websockets', ->
|
||||||
|
wss = null
|
||||||
|
server = null
|
||||||
|
WebSocketServer = ws.Server
|
||||||
|
|
||||||
|
afterEach ->
|
||||||
|
wss.close()
|
||||||
|
server.close()
|
||||||
|
|
||||||
|
it 'has user agent', (done) ->
|
||||||
|
server = http.createServer()
|
||||||
|
server.listen 0, '127.0.0.1', ->
|
||||||
|
port = server.address().port
|
||||||
|
wss = new WebSocketServer(server: server)
|
||||||
|
wss.on 'error', done
|
||||||
|
wss.on 'connection', (ws) ->
|
||||||
|
if ws.upgradeReq.headers['user-agent']
|
||||||
|
done()
|
||||||
|
else
|
||||||
|
done('user agent is empty')
|
||||||
|
websocket = new WebSocket("ws://127.0.0.1:#{port}")
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
"q": "0.9.7",
|
"q": "0.9.7",
|
||||||
"runas": "2.x",
|
"runas": "2.x",
|
||||||
"temp": "0.8.1",
|
"temp": "0.8.1",
|
||||||
"walkdir": "0.0.7"
|
"walkdir": "0.0.7",
|
||||||
|
"ws": "0.7.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
2
vendor/brightray
vendored
2
vendor/brightray
vendored
|
@ -1 +1 @@
|
||||||
Subproject commit 6a38d97aa8f4e1ab1842416f632a2a45adfbc738
|
Subproject commit 6328c6104131e623da87f479ea305b83169099b8
|
Loading…
Reference in a new issue