-
Notifications
You must be signed in to change notification settings - Fork 15k
/
electron_navigation_throttle.cc
68 lines (55 loc) · 1.95 KB
/
electron_navigation_throttle.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// 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 "shell/browser/electron_navigation_throttle.h"
#include "content/public/browser/navigation_handle.h"
#include "shell/browser/api/electron_api_web_contents.h"
namespace electron {
ElectronNavigationThrottle::ElectronNavigationThrottle(
content::NavigationHandle* navigation_handle)
: content::NavigationThrottle(navigation_handle) {}
ElectronNavigationThrottle::~ElectronNavigationThrottle() = default;
const char* ElectronNavigationThrottle::GetNameForLogging() {
return "ElectronNavigationThrottle";
}
content::NavigationThrottle::ThrottleCheckResult
ElectronNavigationThrottle::WillStartRequest() {
auto* handle = navigation_handle();
auto* contents = handle->GetWebContents();
if (!contents) {
NOTREACHED();
return PROCEED;
}
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::HandleScope scope(isolate);
auto api_contents = electron::api::WebContents::From(isolate, contents);
if (api_contents.IsEmpty()) {
// No need to emit any event if the WebContents is not available in JS.
return PROCEED;
}
if (handle->IsRendererInitiated() && handle->IsInMainFrame() &&
api_contents->EmitNavigationEvent("will-navigate", handle)) {
return CANCEL;
}
return PROCEED;
}
content::NavigationThrottle::ThrottleCheckResult
ElectronNavigationThrottle::WillRedirectRequest() {
auto* handle = navigation_handle();
auto* contents = handle->GetWebContents();
if (!contents) {
NOTREACHED();
return PROCEED;
}
auto api_contents =
electron::api::WebContents::From(v8::Isolate::GetCurrent(), contents);
if (api_contents.IsEmpty()) {
// No need to emit any event if the WebContents is not available in JS.
return PROCEED;
}
if (api_contents->EmitNavigationEvent("will-redirect", handle)) {
return CANCEL;
}
return PROCEED;
}
} // namespace electron