]> khamidou — GitHub mirrors - electron.git/commitdiff
Add support for setting http referrer
authorFrank Hale <frankhale@gmail.com>
Thu, 6 Nov 2014 19:29:41 +0000 (14:29 -0500)
committerFrank Hale <frankhale@gmail.com>
Thu, 6 Nov 2014 19:29:41 +0000 (14:29 -0500)
- Add url option to specify the http referrer
- Add httpReferrer attribute to webview

NOTE: This is still not complete. Some love has to be done to
guest-view-manager.coffee and very likely the function calls called
createGuest and to the code that uses them.

atom/browser/api/atom_api_web_contents.cc
atom/browser/api/atom_api_web_contents.h
atom/browser/api/lib/browser-window.coffee
atom/browser/lib/guest-view-manager.coffee
atom/browser/lib/guest-window-manager.coffee
atom/renderer/lib/web-view.coffee

index 5045efae2a3760f6ea851e1790c562c923708c1e..983d37d502f253c4e825a226cca590c723e8d88c 100644 (file)
@@ -286,8 +286,14 @@ bool WebContents::IsAlive() const {
   return web_contents() != NULL;
 }
 
-void WebContents::LoadURL(const GURL& url) {
+void WebContents::LoadURL(const GURL& url, const mate::Dictionary& options) {
   content::NavigationController::LoadURLParams params(url);
+
+  base::string16 http_referrer_;
+
+  if(options.Get("httpReferrer", &http_referrer_))
+    params.referrer = content::Referrer(GURL(http_referrer_).GetAsReferrer(), blink::WebReferrerPolicyDefault);
+
   params.transition_type = content::PAGE_TRANSITION_TYPED;
   params.override_user_agent = content::NavigationController::UA_OVERRIDE_TRUE;
   web_contents()->GetController().LoadURLWithParams(params);
@@ -313,15 +319,15 @@ void WebContents::Stop() {
   web_contents()->Stop();
 }
 
-void WebContents::Reload() {
+void WebContents::Reload(const mate::Dictionary& options) {
   // Navigating to a URL would always restart the renderer process, we want this
   // because normal reloading will break our node integration.
   // This is done by AtomBrowserClient::ShouldSwapProcessesForNavigation.
-  LoadURL(GetURL());
+  LoadURL(GetURL(), options);
 }
 
-void WebContents::ReloadIgnoringCache() {
-  Reload();
+void WebContents::ReloadIgnoringCache(const mate::Dictionary& options) {
+  Reload(options);
 }
 
 bool WebContents::CanGoBack() const {
index c29221008c89cdc9e31254aec809362000b8e0e5..7f6a6971901dd23e9b0053a542f7f8ef75466431 100644 (file)
@@ -41,14 +41,14 @@ class WebContents : public mate::EventEmitter,
 
   void Destroy();
   bool IsAlive() const;
-  void LoadURL(const GURL& url);
+  void LoadURL(const GURL& url, const mate::Dictionary& options);
   GURL GetURL() const;
   base::string16 GetTitle() const;
   bool IsLoading() const;
   bool IsWaitingForResponse() const;
   void Stop();
-  void Reload();
-  void ReloadIgnoringCache();
+  void Reload(const mate::Dictionary& options);
+  void ReloadIgnoringCache(const mate::Dictionary& options);
   bool CanGoBack() const;
   bool CanGoForward() const;
   bool CanGoToOffset(int offset) const;
index e855a93524fcb25d11ea4b0e02e430b33a406c3c..a25672e28e1368a4c8c859af118a39fa4c6a7a5f 100644 (file)
@@ -11,6 +11,8 @@ BrowserWindow::__proto__ = EventEmitter.prototype
 BrowserWindow.windows = new IDWeakMap
 
 BrowserWindow::_init = ->
+  @urlOptions = {}
+
   # Simulate the application menu on platforms other than OS X.
   if process.platform isnt 'darwin'
     menu = app.getApplicationMenu()
@@ -84,14 +86,23 @@ BrowserWindow.fromId = (id) ->
   BrowserWindow.windows.get id
 
 # Helpers.
-BrowserWindow::loadUrl = -> @webContents.loadUrl.apply @webContents, arguments
+BrowserWindow::loadUrl = ->
+  args = [].slice.call arguments
+  unless args.length > 1
+    args.push @urlOptions
+
+  #TODO: This needs fixing!
+  @urlOptions = args[1]
+
+  @webContents.loadUrl.apply @webContents, args
+
 BrowserWindow::send = -> @webContents.send.apply @webContents, arguments
 
 # Be compatible with old API.
 BrowserWindow::restart = -> @webContents.reload()
 BrowserWindow::getUrl = -> @webContents.getUrl()
-BrowserWindow::reload = -> @webContents.reload()
-BrowserWindow::reloadIgnoringCache = -> @webContents.reloadIgnoringCache()
+BrowserWindow::reload = -> @webContents.reload(@urlOptions)
+BrowserWindow::reloadIgnoringCache = -> @webContents.reloadIgnoringCache(@urlOptions)
 BrowserWindow::getPageTitle = -> @webContents.getTitle()
 BrowserWindow::isLoading = -> @webContents.isLoading()
 BrowserWindow::isWaitingForResponse = -> @webContents.isWaitingForResponse()
index a5c5d021f401d26f4cf1545315bbf76cab0383c9..5b6377847a3748fae6a7a3889c4e6e0719474202 100644 (file)
@@ -47,7 +47,7 @@ createGuest = (embedder, params) ->
     max = width: params.maxwidth, height: params.maxheight
     @setAutoSize params.autosize, min, max
     if params.src
-      @loadUrl params.src
+      @loadUrl params.src, params.urlOptions
     if params.allowtransparency?
       @setAllowTransparency params.allowtransparency
 
index 123c3898a9d4232520d972bc90f42940aa87c85a..abd2d1b8729d10c8e31a7aa87cf6bf1c974f200c 100644 (file)
@@ -7,11 +7,11 @@ frameToGuest = {}
 createGuest = (embedder, url, frameName, options) ->
   guest = frameToGuest[frameName]
   if frameName and guest?
-    guest.loadUrl url
+    guest.loadUrl url {}
     return guest.id
 
   guest = new BrowserWindow(options)
-  guest.loadUrl url
+  guest.loadUrl url {}
 
   # When |embedder| is destroyed we should also destroy attached guest, and if
   # guest is closed by user then we should prevent |embedder| from double
index ee11683d9a529006c49297409b99c3f9793e510d..1c35c163ffe5b4557b9127c402841a238775e165 100644 (file)
@@ -94,6 +94,8 @@ class WebView
     # on* Event handlers.
     @on = {}
 
+    @urlOptions = {}
+
     @browserPluginNode = @createBrowserPluginNode()
     shadowRoot = @webviewNode.createShadowRoot()
     @partition = new Partition()
@@ -197,6 +199,12 @@ class WebView
       # No setter.
       enumerable: true
 
+    @httpReferrer = @webviewNode.getAttribute 'httpReferrer'
+    Object.defineProperty @webviewNode, 'httpReferrer',
+      get: => @httpReferrer
+      set: (value) => @webviewNode.setAttribute 'httpReferrer', value
+      enumerable: true
+
   # The purpose of this mutation observer is to catch assignment to the src
   # attribute without any changes to its value. This is useful in the case
   # where the webview guest has crashed and navigating to the same address
@@ -211,7 +219,7 @@ class WebView
     params =
       attributes: true,
       attributeOldValue: true,
-      attributeFilter: ['src', 'partition']
+      attributeFilter: ['src', 'partition', 'httpReferrer']
     @srcAndPartitionObserver.observe @webviewNode, params
 
   # This observer monitors mutations to attributes of the <webview> and
@@ -245,6 +253,22 @@ class WebView
       return unless @guestInstanceId
 
       guestViewInternal.setAllowTransparency @guestInstanceId, @allowtransparency
+    else if name is 'httpReferrer'
+      oldValue ?= ''
+      newValue ?= ''
+
+      if newValue == '' and oldValue != ''
+        @webviewNode.setAttribute 'httpReferrer', oldValue
+
+      @httpReferrer = newValue
+
+      result = {}
+      # I think the right thing to do if you change your referrer is to reload
+      # the src since I've bundled the referrer in with the parseSrcAttribute.
+      # I think it makes sense to do that.
+      @parseSrcAttribute result
+
+      throw result.error if result.error?
     else if name is 'src'
       # We treat null attribute (attribute removed) and the empty string as
       # one case.
@@ -363,8 +387,11 @@ class WebView
         @createGuest()
       return
 
+    if @httpReferrer
+      @urlOptions = { "httpReferrer": @httpReferrer }
+
     # Navigate to |this.src|.
-    remote.getGuestWebContents(@guestInstanceId).loadUrl @src
+    remote.getGuestWebContents(@guestInstanceId).loadUrl @src, @urlOptions
 
   parseAttributes: ->
     return unless @elementAttached
@@ -447,6 +474,7 @@ class WebView
     # set via this.onAttach().
     storagePartitionId: @partition.toAttribute()
     userAgentOverride: @userAgentOverride
+    urlOptions: @urlOptions
 
   attachWindow: (guestInstanceId, isNewWindow) ->
     @guestInstanceId = guestInstanceId