feat: add will-redirect (#13866)

* feat: add will-redirect to allow people to prevent 30X redirects

* spec: add tests for the will-redirect event

* refactor: implement will-redirect using NavigationThrottle instead of PostTask

This avoids a potential race condition and immediately cancels the
navigation

* docs: add docs for did-redirect-navigation

* refactor: move AtomNavigationThrottle out of net folder

* refactor: update header guard for atom_navigation_throttle.h

* refactor: fix chromium style errors in the GN build

* refactor: update throttle impl to NOTREACHED and std::make_unqique
This commit is contained in:
Samuel Attard 2018-09-16 01:42:43 +10:00 committed by GitHub
parent 6ad8583a8b
commit 7065093869
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 222 additions and 3 deletions

View file

@ -16,6 +16,7 @@
#include "atom/browser/atom_browser_context.h"
#include "atom/browser/atom_browser_main_parts.h"
#include "atom/browser/atom_javascript_dialog_manager.h"
#include "atom/browser/atom_navigation_throttle.h"
#include "atom/browser/child_web_contents_tracker.h"
#include "atom/browser/lib/bluetooth_chooser.h"
#include "atom/browser/native_window.h"
@ -845,7 +846,8 @@ void WebContents::DidStopLoading() {
Emit("did-stop-loading");
}
void WebContents::DidStartNavigation(
bool WebContents::EmitNavigationEvent(
const std::string& event,
content::NavigationHandle* navigation_handle) {
bool is_main_frame = navigation_handle->IsInMainFrame();
int frame_tree_node_id = navigation_handle->GetFrameTreeNodeId();
@ -866,8 +868,18 @@ void WebContents::DidStartNavigation(
}
bool is_same_document = navigation_handle->IsSameDocument();
auto url = navigation_handle->GetURL();
Emit("did-start-navigation", url, is_same_document, is_main_frame,
frame_process_id, frame_routing_id);
return Emit(event, url, is_same_document, is_main_frame, frame_process_id,
frame_routing_id);
}
void WebContents::DidStartNavigation(
content::NavigationHandle* navigation_handle) {
EmitNavigationEvent("did-start-navigation", navigation_handle);
}
void WebContents::DidRedirectNavigation(
content::NavigationHandle* navigation_handle) {
EmitNavigationEvent("did-redirect-navigation", navigation_handle);
}
void WebContents::DidFinishNavigation(

View file

@ -266,6 +266,9 @@ class WebContents : public mate::TrackableObject<WebContents>,
observers_.RemoveObserver(obs);
}
bool EmitNavigationEvent(const std::string& event,
content::NavigationHandle* navigation_handle);
protected:
WebContents(v8::Isolate* isolate,
content::WebContents* web_contents,
@ -366,6 +369,8 @@ class WebContents : public mate::TrackableObject<WebContents>,
void DidStopLoading() override;
void DidStartNavigation(
content::NavigationHandle* navigation_handle) override;
void DidRedirectNavigation(
content::NavigationHandle* navigation_handle) override;
void DidFinishNavigation(
content::NavigationHandle* navigation_handle) override;
bool OnMessageReceived(const IPC::Message& message) override;

View file

@ -16,6 +16,7 @@
#include "atom/browser/api/atom_api_web_contents.h"
#include "atom/browser/atom_browser_context.h"
#include "atom/browser/atom_browser_main_parts.h"
#include "atom/browser/atom_navigation_throttle.h"
#include "atom/browser/atom_quota_permission_context.h"
#include "atom/browser/atom_resource_dispatcher_host_delegate.h"
#include "atom/browser/atom_speech_recognition_manager_delegate.h"
@ -613,4 +614,12 @@ bool AtomBrowserClient::HandleExternalProtocol(
return true;
}
std::vector<std::unique_ptr<content::NavigationThrottle>>
AtomBrowserClient::CreateThrottlesForNavigation(
content::NavigationHandle* handle) {
std::vector<std::unique_ptr<content::NavigationThrottle>> throttles;
throttles.push_back(std::make_unique<AtomNavigationThrottle>(handle));
return throttles;
}
} // namespace atom

View file

@ -47,6 +47,9 @@ class AtomBrowserClient : public brightray::BrowserClient,
static void SetCustomServiceWorkerSchemes(
const std::vector<std::string>& schemes);
std::vector<std::unique_ptr<content::NavigationThrottle>>
CreateThrottlesForNavigation(content::NavigationHandle* handle) override;
protected:
// content::ContentBrowserClient:
void RenderProcessWillLaunch(

View file

@ -0,0 +1,44 @@
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/atom_navigation_throttle.h"
#include "atom/browser/api/atom_api_web_contents.h"
#include "content/public/browser/navigation_handle.h"
namespace atom {
AtomNavigationThrottle::AtomNavigationThrottle(
content::NavigationHandle* navigation_handle)
: content::NavigationThrottle(navigation_handle) {}
AtomNavigationThrottle::~AtomNavigationThrottle() {}
const char* AtomNavigationThrottle::GetNameForLogging() {
return "AtomNavigationThrottle";
}
content::NavigationThrottle::ThrottleCheckResult
AtomNavigationThrottle::WillRedirectRequest() {
auto* handle = navigation_handle();
auto* contents = handle->GetWebContents();
if (!contents) {
NOTREACHED();
return PROCEED;
}
auto api_contents =
atom::api::WebContents::CreateFrom(v8::Isolate::GetCurrent(), contents);
if (api_contents.IsEmpty()) {
NOTREACHED();
return PROCEED;
}
if (api_contents->EmitNavigationEvent("will-redirect", handle)) {
return CANCEL;
}
return PROCEED;
}
} // namespace atom

View file

@ -0,0 +1,27 @@
// Copyright (c) 2018 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_BROWSER_ATOM_NAVIGATION_THROTTLE_H_
#define ATOM_BROWSER_ATOM_NAVIGATION_THROTTLE_H_
#include "content/public/browser/navigation_throttle.h"
namespace atom {
class AtomNavigationThrottle : public content::NavigationThrottle {
public:
explicit AtomNavigationThrottle(content::NavigationHandle* handle);
~AtomNavigationThrottle() override;
AtomNavigationThrottle::ThrottleCheckResult WillRedirectRequest() override;
const char* GetNameForLogging() override;
private:
DISALLOW_COPY_AND_ASSIGN(AtomNavigationThrottle);
};
} // namespace atom
#endif // ATOM_BROWSER_ATOM_NAVIGATION_THROTTLE_H_