Set default user agent for WebContents

This commit is contained in:
Cheng Zhao 2015-07-14 12:13:25 -07:00
parent 1615c97ce8
commit 053594eae8
2 changed files with 11 additions and 3 deletions

View file

@ -149,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) {
@ -175,6 +176,8 @@ WebContents::WebContents(const mate::Dictionary& options) {
AttachAsUserData(web_contents); AttachAsUserData(web_contents);
InitWithWebContents(web_contents); InitWithWebContents(web_contents);
web_contents->SetUserAgentOverride(GetBrowserContext()->GetUserAgent());
if (is_guest) { if (is_guest) {
guest_delegate_->Initialize(this); guest_delegate_->Initialize(this);
@ -630,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_);
@ -807,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...);

View file

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