Skip to content

Commit

Permalink
feat: add will-redirect to allow people to prevent 30X redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
MarshallOfSound committed Jul 30, 2018
1 parent 2bba11c commit f483586
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
26 changes: 23 additions & 3 deletions atom/browser/api/atom_api_web_contents.cc
Expand Up @@ -877,7 +877,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();
Expand All @@ -898,8 +899,27 @@ 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 OnStopSoon(WebContents* web_contents) {
if (web_contents && !web_contents->IsDestroyed())
web_contents->Stop();
}

void WebContents::DidRedirectNavigation(
content::NavigationHandle* navigation_handle) {
if (EmitNavigationEvent("will-redirect", navigation_handle)) {
scoped_refptr<base::SingleThreadTaskRunner> task_runner(
base::ThreadTaskRunnerHandle::Get());
task_runner->PostTask(FROM_HERE, base::BindOnce(&OnStopSoon, this));
}
}

void WebContents::DidFinishNavigation(
Expand Down
4 changes: 4 additions & 0 deletions atom/browser/api/atom_api_web_contents.h
Expand Up @@ -357,6 +357,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;
Expand Down Expand Up @@ -431,6 +433,8 @@ class WebContents : public mate::TrackableObject<WebContents>,

void InitZoomController(content::WebContents* web_contents,
const mate::Dictionary& options);
bool EmitNavigationEvent(const std::string& event,
content::NavigationHandle* navigation_handle);

v8::Global<v8::Value> session_;
v8::Global<v8::Value> devtools_web_contents_;
Expand Down
21 changes: 21 additions & 0 deletions docs/api/web-contents.md
Expand Up @@ -170,6 +170,7 @@ Calling `event.preventDefault()` will prevent the navigation.

Returns:

* `event` Event
* `url` String
* `isInPlace` Boolean
* `isMainFrame` Boolean
Expand All @@ -179,6 +180,26 @@ Returns:
Emitted when any frame (including main) starts navigating. `isInplace` will be
`true` for in-page navigations.

#### Event: 'will-redirect'

Returns:

* `event` Event
* `url` String
* `isInPlace` Boolean
* `isMainFrame` Boolean
* `frameProcessId` Integer
* `frameRoutingId` Integer

Emitted when a server side redirect occurs during navigation. For example a 302
redirect.

This event will be emitted after `did-start-navigation` and always before the
`did-navigate` event for the same navigation.

Calling `event.preventDefault()` will prevent the navigation (not just the
redirect).

#### Event: 'did-navigate'

Returns:
Expand Down

0 comments on commit f483586

Please sign in to comment.