Skip to content

Commit ef6b611

Browse files
committed
Fix to prevent background request to open new window
1 parent 8e723da commit ef6b611

File tree

6 files changed

+263
-247
lines changed

6 files changed

+263
-247
lines changed

src/app/hints.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use gdk::EventKey;
2525

2626
use super::App;
27+
use webview::Msg::SetClickedURL;
2728

2829
use titanium_common::Action::{
2930
self,
@@ -45,7 +46,8 @@ impl App {
4546
}
4647
}
4748

48-
pub fn click_hint_element(&mut self) {
49+
pub fn click_hint_element(&mut self, link: Option<String>) {
50+
self.components.webview.emit(SetClickedURL(link));
4951
self.activate_hint();
5052
self.hide_hints();
5153
}

src/app/server.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl App {
8282
pub fn message_recv(&mut self, message: InnerMessage) {
8383
match message {
8484
ActivateAction(action) => self.activate_action(action),
85-
ClickHintElement() => self.click_hint_element(),
85+
ClickHintElement(link) => self.click_hint_element(link),
8686
Credentials(ref username, ref password) => handle_error!(self.save_username_password(&username, &password)),
8787
EnterInsertMode() => self.go_in_insert_mode(),
8888
ScrollPercentage(percentage) => self.show_scroll(percentage),

src/webview/mod.rs

+18-10
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ macro_rules! handle_app_error {
3030

3131
mod settings;
3232

33-
use std::cell::Cell;
33+
use std::cell::{Cell, RefCell};
3434
use std::fs::{File, read_dir};
3535
use std::io::Read;
3636
use std::rc::Rc;
@@ -83,6 +83,7 @@ use settings::AppSettingsVariant;
8383
use stylesheet::get_stylesheet_and_whitelist;
8484

8585
pub struct Model {
86+
clicked_url: Rc<RefCell<Option<String>>>,
8687
config_dir: ConfigDir,
8788
context: WebContext,
8889
inspector_shown: Rc<Cell<bool>>,
@@ -115,6 +116,7 @@ pub enum Msg {
115116
PermissionRequest(PermissionRequest),
116117
SearchBackward(bool),
117118
SetOpenInNewWindow(bool),
119+
SetClickedURL(Option<String>),
118120
ShowInspector,
119121
WebViewSettingChanged(AppSettingsVariant),
120122
ZoomChange(i32),
@@ -133,6 +135,7 @@ impl Widget for WebView {
133135

134136
fn model(relm: &Relm<Self>, (config_dir, context): (ConfigDir, WebContext)) -> Model {
135137
Model {
138+
clicked_url: Rc::new(RefCell::new(None)),
136139
config_dir,
137140
context,
138141
inspector_shown: Rc::new(Cell::new(false)),
@@ -171,6 +174,7 @@ impl Widget for WebView {
171174
PermissionRequest(_) => (),
172175
SearchBackward(search_backwards) => self.model.search_backwards = search_backwards,
173176
SetOpenInNewWindow(open_in_new_window) => self.set_open_in_new_window(open_in_new_window),
177+
SetClickedURL(url) => *self.model.clicked_url.borrow_mut() = url,
174178
ShowInspector => self.show_inspector(),
175179
WebViewSettingChanged(setting) => self.setting_changed(setting),
176180
// To be listened by the user.
@@ -187,8 +191,8 @@ impl Widget for WebView {
187191
}) {
188192
close => Close,
189193
vexpand: true,
190-
decide_policy(_, policy_decision, policy_decision_type) with (open_in_new_window, relm) =>
191-
return WebView::decide_policy(&policy_decision, &policy_decision_type, &open_in_new_window, &relm),
194+
decide_policy(_, policy_decision, policy_decision_type) with (clicked_url, open_in_new_window, relm) =>
195+
return WebView::decide_policy(&policy_decision, &policy_decision_type, &clicked_url, &open_in_new_window, &relm),
192196
enter_fullscreen => (EnterFullScreen, false),
193197
leave_fullscreen => (LeaveFullScreen, false),
194198
permission_request(_, request) => (PermissionRequest(request.clone()), true),
@@ -233,10 +237,10 @@ impl WebView {
233237
}
234238

235239
fn decide_policy(policy_decision: &PolicyDecision, policy_decision_type: &PolicyDecisionType,
236-
open_in_new_window: &Rc<Cell<bool>>, relm: &Relm<WebView>) -> bool
240+
clicked_url: &Rc<RefCell<Option<String>>>, open_in_new_window: &Rc<Cell<bool>>, relm: &Relm<WebView>) -> bool
237241
{
238242
if *policy_decision_type == NavigationAction {
239-
Self::handle_navigation_action(policy_decision, open_in_new_window, relm)
243+
Self::handle_navigation_action(policy_decision, clicked_url, open_in_new_window, relm)
240244
}
241245
else if *policy_decision_type == Response {
242246
Self::handle_response(policy_decision)
@@ -272,7 +276,7 @@ impl WebView {
272276
}
273277

274278
/// Handle follow link in new window.
275-
fn handle_navigation_action(policy_decision: &PolicyDecision, open_in_new_window: &Rc<Cell<bool>>,
279+
fn handle_navigation_action(policy_decision: &PolicyDecision, clicked_url: &Rc<RefCell<Option<String>>>, open_in_new_window: &Rc<Cell<bool>>,
276280
relm: &Relm<WebView>) -> bool
277281
{
278282
let policy_decision = policy_decision.clone();
@@ -287,10 +291,14 @@ impl WebView {
287291
let url = policy_decision.request()
288292
.and_then(|request| request.uri());
289293
if let Some(url) = url {
290-
policy_decision.ignore();
291-
open_in_new_window.set(false);
292-
relm.stream().emit(NewWindow(url.to_string()));
293-
return true;
294+
// NOTE: only open the URL in a new window if it matches the hint URL that was
295+
// selected by the user.
296+
if Some(url.as_str()) == clicked_url.borrow().as_deref() {
297+
policy_decision.ignore();
298+
open_in_new_window.set(false);
299+
relm.stream().emit(NewWindow(url.to_string()));
300+
return true;
301+
}
294302
}
295303
}
296304
}

titanium-common/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ pub enum InnerMessage {
8484
/// Click on the link in the selection.
8585
ActivateSelection(),
8686
/// Response to EnterHintKey.
87-
ClickHintElement(),
87+
/// Contains the link (href) if it is a anchor element.
88+
ClickHintElement(Option<String>),
8889
/// Regex lookup next page link to click
8990
ClickNextPage(),
9091
/// Regex lookup prev page link to click

0 commit comments

Comments
 (0)