]> khamidou — GitHub mirrors - electron.git/commitdiff
Set default user agent for WebContents
authorCheng Zhao <zcbenz@gmail.com>
Tue, 14 Jul 2015 19:13:25 +0000 (12:13 -0700)
committerCheng Zhao <zcbenz@gmail.com>
Tue, 14 Jul 2015 19:13:25 +0000 (12:13 -0700)
atom/browser/api/atom_api_web_contents.cc
atom/browser/api/atom_api_web_contents.h

index 5490b505860a0941f46ade95321f4f2d4da40664..6311e1eaf9a45e8bb57a3fbba032518d6f2c2c70 100644 (file)
@@ -149,6 +149,7 @@ WebContents::WebContents(content::WebContents* web_contents)
     : content::WebContentsObserver(web_contents),
       type_(REMOTE) {
   AttachAsUserData(web_contents);
+  web_contents->SetUserAgentOverride(GetBrowserContext()->GetUserAgent());
 }
 
 WebContents::WebContents(const mate::Dictionary& options) {
@@ -175,6 +176,8 @@ WebContents::WebContents(const mate::Dictionary& options) {
   AttachAsUserData(web_contents);
   InitWithWebContents(web_contents);
 
+  web_contents->SetUserAgentOverride(GetBrowserContext()->GetUserAgent());
+
   if (is_guest) {
     guest_delegate_->Initialize(this);
 
@@ -630,9 +633,7 @@ void WebContents::InspectServiceWorker() {
 
 v8::Local<v8::Value> WebContents::Session(v8::Isolate* isolate) {
   if (session_.IsEmpty()) {
-    mate::Handle<api::Session> handle = Session::CreateFrom(
-        isolate,
-        static_cast<AtomBrowserContext*>(web_contents()->GetBrowserContext()));
+    auto handle = Session::CreateFrom(isolate, GetBrowserContext());
     session_.Reset(isolate, handle.ToV8());
   }
   return v8::Local<v8::Value>::New(isolate, session_);
@@ -807,6 +808,10 @@ bool WebContents::IsDestroyed() const {
   return !IsAlive();
 }
 
+AtomBrowserContext* WebContents::GetBrowserContext() const {
+  return static_cast<AtomBrowserContext*>(web_contents()->GetBrowserContext());
+}
+
 void WebContents::OnRendererMessage(const base::string16& channel,
                                     const base::ListValue& args) {
   // webContents.emit(channel, new Event(), args...);
index 085f82e3dd642c137da54cbf223e07d4332b77dc..bb6b22ac84e27e49deb3b10cf97f69f828128898 100644 (file)
@@ -27,6 +27,7 @@ class Dictionary;
 namespace atom {
 
 struct SetSizeParams;
+class AtomBrowserContext;
 class WebViewGuestDelegate;
 
 namespace api {
@@ -191,6 +192,8 @@ class WebContents : public mate::TrackableObject<WebContents>,
     REMOTE,  // Thin wrap around an existing WebContents.
   };
 
+  AtomBrowserContext* GetBrowserContext() const;
+
   // Called when received a message from renderer.
   void OnRendererMessage(const base::string16& channel,
                          const base::ListValue& args);