From f56d1ea7b486828351f31682ea1fb71dd394f851 Mon Sep 17 00:00:00 2001 From: Frank Hale Date: Thu, 6 Nov 2014 14:29:41 -0500 Subject: [PATCH 1/5] Add support for setting http referrer - 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 | 16 +++++++--- atom/browser/api/atom_api_web_contents.h | 6 ++-- atom/browser/api/lib/browser-window.coffee | 17 +++++++++-- atom/browser/lib/guest-view-manager.coffee | 2 +- atom/browser/lib/guest-window-manager.coffee | 4 +-- atom/renderer/lib/web-view.coffee | 32 ++++++++++++++++++-- 6 files changed, 61 insertions(+), 16 deletions(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 5045efae2a..983d37d502 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -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 { diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index c29221008c..7f6a697190 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -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; diff --git a/atom/browser/api/lib/browser-window.coffee b/atom/browser/api/lib/browser-window.coffee index e855a93524..a25672e28e 100644 --- a/atom/browser/api/lib/browser-window.coffee +++ b/atom/browser/api/lib/browser-window.coffee @@ -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() diff --git a/atom/browser/lib/guest-view-manager.coffee b/atom/browser/lib/guest-view-manager.coffee index a5c5d021f4..5b6377847a 100644 --- a/atom/browser/lib/guest-view-manager.coffee +++ b/atom/browser/lib/guest-view-manager.coffee @@ -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 diff --git a/atom/browser/lib/guest-window-manager.coffee b/atom/browser/lib/guest-window-manager.coffee index 123c3898a9..abd2d1b872 100644 --- a/atom/browser/lib/guest-window-manager.coffee +++ b/atom/browser/lib/guest-window-manager.coffee @@ -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 diff --git a/atom/renderer/lib/web-view.coffee b/atom/renderer/lib/web-view.coffee index ee11683d9a..1c35c163ff 100644 --- a/atom/renderer/lib/web-view.coffee +++ b/atom/renderer/lib/web-view.coffee @@ -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 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 From ef255db069a52e8bfd53b3234a5217ab96e0c9a0 Mon Sep 17 00:00:00 2001 From: Frank Hale Date: Fri, 7 Nov 2014 21:54:36 -0500 Subject: [PATCH 2/5] Making Http Referrer addition better! - Code cleanup --- atom/browser/api/atom_api_web_contents.cc | 2 +- atom/browser/api/lib/browser-window.coffee | 16 +++------------- atom/browser/lib/guest-window-manager.coffee | 4 ++-- atom/renderer/lib/web-view.coffee | 19 +++++++++---------- 4 files changed, 15 insertions(+), 26 deletions(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 983d37d502..cf40de96e4 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -291,7 +291,7 @@ void WebContents::LoadURL(const GURL& url, const mate::Dictionary& options) { base::string16 http_referrer_; - if(options.Get("httpReferrer", &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; diff --git a/atom/browser/api/lib/browser-window.coffee b/atom/browser/api/lib/browser-window.coffee index a25672e28e..243ad1ac3c 100644 --- a/atom/browser/api/lib/browser-window.coffee +++ b/atom/browser/api/lib/browser-window.coffee @@ -11,8 +11,6 @@ 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() @@ -86,23 +84,15 @@ BrowserWindow.fromId = (id) -> BrowserWindow.windows.get id # Helpers. -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::loadUrl = (url, urlOptions={}) -> @webContents.loadUrl url, urlOptions BrowserWindow::send = -> @webContents.send.apply @webContents, arguments # Be compatible with old API. BrowserWindow::restart = -> @webContents.reload() BrowserWindow::getUrl = -> @webContents.getUrl() -BrowserWindow::reload = -> @webContents.reload(@urlOptions) -BrowserWindow::reloadIgnoringCache = -> @webContents.reloadIgnoringCache(@urlOptions) +BrowserWindow::reload = (urlOptions={}) -> @webContents.reload(urlOptions) +BrowserWindow::reloadIgnoringCache = (urlOptions={}) -> @webContents.reloadIgnoringCache(urlOptions) BrowserWindow::getPageTitle = -> @webContents.getTitle() BrowserWindow::isLoading = -> @webContents.isLoading() BrowserWindow::isWaitingForResponse = -> @webContents.isWaitingForResponse() diff --git a/atom/browser/lib/guest-window-manager.coffee b/atom/browser/lib/guest-window-manager.coffee index abd2d1b872..123c3898a9 100644 --- a/atom/browser/lib/guest-window-manager.coffee +++ b/atom/browser/lib/guest-window-manager.coffee @@ -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 diff --git a/atom/renderer/lib/web-view.coffee b/atom/renderer/lib/web-view.coffee index 1c35c163ff..96abb48657 100644 --- a/atom/renderer/lib/web-view.coffee +++ b/atom/renderer/lib/web-view.coffee @@ -199,10 +199,10 @@ class WebView # No setter. enumerable: true - @httpReferrer = @webviewNode.getAttribute 'httpReferrer' - Object.defineProperty @webviewNode, 'httpReferrer', + @httpReferrer = @webviewNode.getAttribute 'httpreferrer' + Object.defineProperty @webviewNode, 'httpreferrer', get: => @httpReferrer - set: (value) => @webviewNode.setAttribute 'httpReferrer', value + set: (value) => @webviewNode.setAttribute 'httpreferrer', value enumerable: true # The purpose of this mutation observer is to catch assignment to the src @@ -219,7 +219,7 @@ class WebView params = attributes: true, attributeOldValue: true, - attributeFilter: ['src', 'partition', 'httpReferrer'] + attributeFilter: ['src', 'partition', 'httpreferrer'] @srcAndPartitionObserver.observe @webviewNode, params # This observer monitors mutations to attributes of the and @@ -253,19 +253,18 @@ class WebView return unless @guestInstanceId guestViewInternal.setAllowTransparency @guestInstanceId, @allowtransparency - else if name is 'httpReferrer' + else if name is 'httpreferrer' oldValue ?= '' newValue ?= '' if newValue == '' and oldValue != '' - @webviewNode.setAttribute 'httpReferrer', 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. + # If the httpreferrer changes treat it as though the src changes and reload + # the page with the new httpreferrer. @parseSrcAttribute result throw result.error if result.error? @@ -388,7 +387,7 @@ class WebView return if @httpReferrer - @urlOptions = { "httpReferrer": @httpReferrer } + @urlOptions = { "httpreferrer": @httpReferrer } # Navigate to |this.src|. remote.getGuestWebContents(@guestInstanceId).loadUrl @src, @urlOptions From b92d5071fa8c3047cf2d15a0d9ba61a9c4de2d15 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Fri, 7 Nov 2014 15:05:55 +0800 Subject: [PATCH 3/5] views: Make auto-hide-menu-bar work when NumLock is on, fixes #796 --- atom/browser/native_window_views.cc | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/atom/browser/native_window_views.cc b/atom/browser/native_window_views.cc index dc1d684ea2..9a7e6d528b 100644 --- a/atom/browser/native_window_views.cc +++ b/atom/browser/native_window_views.cc @@ -113,9 +113,12 @@ bool IsAltKey(const content::NativeWebKeyboardEvent& event) { bool IsAltModifier(const content::NativeWebKeyboardEvent& event) { typedef content::NativeWebKeyboardEvent::Modifiers Modifiers; - return (event.modifiers == Modifiers::AltKey) || - (event.modifiers == (Modifiers::AltKey | Modifiers::IsLeft)) || - (event.modifiers == (Modifiers::AltKey | Modifiers::IsRight)); + int modifiers = event.modifiers; + modifiers &= ~Modifiers::NumLockOn; + modifiers &= ~Modifiers::CapsLockOn; + return (modifiers == Modifiers::AltKey) || + (modifiers == (Modifiers::AltKey | Modifiers::IsLeft)) || + (modifiers == (Modifiers::AltKey | Modifiers::IsRight)); } class NativeWindowClientView : public views::ClientView { From 716037544ae1ce08265f9bba9d09cd21bedbc43b Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Fri, 7 Nov 2014 15:20:16 +0800 Subject: [PATCH 4/5] views: Fix showing menu bar when pressing Alt for a long time --- atom/browser/native_window_views.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/atom/browser/native_window_views.cc b/atom/browser/native_window_views.cc index 9a7e6d528b..0a17b1787b 100644 --- a/atom/browser/native_window_views.cc +++ b/atom/browser/native_window_views.cc @@ -719,8 +719,7 @@ void NativeWindowViews::HandleKeyboardEvent( return; // Toggle the menu bar only when a single Alt is released. - if (event.type == blink::WebInputEvent::RawKeyDown && IsAltKey(event) && - IsAltModifier(event)) { + if (event.type == blink::WebInputEvent::RawKeyDown && IsAltKey(event)) { // When a single Alt is pressed: menu_bar_alt_pressed_ = true; } else if (event.type == blink::WebInputEvent::KeyUp && IsAltKey(event) && From 747698fe9b5589222e9beeea04bcb25c6d089b3d Mon Sep 17 00:00:00 2001 From: Frank Hale Date: Tue, 11 Nov 2014 13:33:15 -0500 Subject: [PATCH 5/5] Fix code style formatting - Fix code style formatting to be <= 80 lines - Add default parameter for urlOptions to loadUrl, reload and reloadIgnoringCache functions to be compatible with old API. --- atom/browser/api/atom_api_web_contents.cc | 5 +++-- atom/browser/api/lib/web-contents.coffee | 7 +++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index cf40de96e4..8bb6a35816 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -289,10 +289,11 @@ bool WebContents::IsAlive() const { void WebContents::LoadURL(const GURL& url, const mate::Dictionary& options) { content::NavigationController::LoadURLParams params(url); - base::string16 http_referrer_; + base::string16 http_referrer; if (options.Get("httpreferrer", &http_referrer_)) - params.referrer = content::Referrer(GURL(http_referrer_).GetAsReferrer(), blink::WebReferrerPolicyDefault); + 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; diff --git a/atom/browser/api/lib/web-contents.coffee b/atom/browser/api/lib/web-contents.coffee index e22d5e01c2..a0c8a00e7c 100644 --- a/atom/browser/api/lib/web-contents.coffee +++ b/atom/browser/api/lib/web-contents.coffee @@ -26,6 +26,13 @@ module.exports.wrap = (webContents) -> webContents.getId = -> "#{@getProcessId()}-#{@getRoutingId()}" webContents.equal = (other) -> @getId() is other.getId() + # Provide a default parameter for urlOptions to be compatible with the old + # API. + webContents::loadUrl = (url, urlOptions={}) -> loadUrl url, urlOptions + webContents::reload = (urlOptions={}) -> reload(urlOptions) + webContents::reloadIgnoringCache = (urlOptions={}) -> + reloadIgnoringCache(urlOptions) + # Translate |disposition| to string for 'new-window' event. webContents.on '-new-window', (args..., disposition) -> disposition =