Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CodenameOne/src/com/codename1/ui/BrowserComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@
* @author Shai Almog
*/
public class BrowserComponent extends Container {
/**
* Browser property key to control whether links with {@code target="_blank"} or {@code target="_new"}
* should be followed in the current browser view. Defaults to {@code true}.
*/
public static final String BROWSER_PROPERTY_FOLLOW_TARGET_BLANK = "BrowserComponent.followTargetBlank";
/**
* String constant for web event listener {@link #addWebEventListener(java.lang.String, com.codename1.ui.events.ActionListener)}
*/
Expand Down
21 changes: 21 additions & 0 deletions Ports/JavaSE/src/com/codename1/impl/javase/cef/BrowserPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public abstract class BrowserPanel extends CN1JPanel {
//private CEFPeerComponentBuffer buffer_;
private boolean browserFocus_ = true;
private Runnable readyCallback;
private boolean followTargetBlank = true;
//private static AppHandler appHandler_;
//private BrowserComponent browserComponent;

Expand Down Expand Up @@ -425,6 +426,18 @@ protected void finalize() throws Throwable {
private static CefLifeSpanHandlerAdapter createLifespanHandler(BrowserPanel p) {
final WeakReference<BrowserPanel> selfRef = new WeakReference<BrowserPanel>(p);
return new CefLifeSpanHandlerAdapter() {
@Override
public boolean onBeforePopup(CefBrowser browser, CefFrame frame, String targetUrl, String targetFrameName) {
BrowserPanel self = selfRef.get();
if (self == null || !self.followTargetBlank) {
return false;
}
if (targetUrl != null) {
browser.loadURL(targetUrl);
}
return true;
}

@Override
public void onAfterCreated(CefBrowser browser) {
BrowserPanel self = selfRef.get();
Expand Down Expand Up @@ -465,6 +478,14 @@ public void onBeforeClose(CefBrowser browser) {
}
};
}

public void setFollowTargetBlank(boolean followTargetBlank) {
this.followTargetBlank = followTargetBlank;
}

public boolean isFollowTargetBlank() {
return followTargetBlank;
}

public void setBrowser(CefBrowser browser) {
if (browser_ == null) browser_ = browser;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,9 @@ public void setProperty(String key, Object value) {
if(key.equalsIgnoreCase("User-Agent")) {
//panel.getBrowser().getClient().
}
if (BrowserComponent.BROWSER_PROPERTY_FOLLOW_TARGET_BLANK.equals(key)) {
panel.setFollowTargetBlank(Boolean.TRUE.equals(value));
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@
import javafx.scene.paint.Color;

import javafx.scene.web.WebEngine;
import javafx.scene.web.PopupFeatures;
//import javafx.scene.web.WebErrorEvent;
import javafx.scene.web.WebEvent;
import javafx.scene.web.WebView;
import javafx.util.Callback;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
Expand Down Expand Up @@ -89,6 +91,7 @@ public class SEBrowserComponent extends PeerComponent implements IBrowserCompone
private AdjustmentListener adjustmentListener;
private BrowserComponent browserComp;
private boolean transparent;
private boolean followTargetBlank = true;

/**
* A bridge to inject java methods into the webview.
Expand Down Expand Up @@ -344,6 +347,17 @@ public void handle(WebEvent<String> t) {
}

});

self.web.getEngine().setCreatePopupHandler(new Callback<PopupFeatures, WebEngine>() {
@Override
public WebEngine call(PopupFeatures features) {
SEBrowserComponent self = weakSelf.get();
if (self == null || !self.followTargetBlank) {
return null;
}
return self.web.getEngine();
}
});

self.web.getEngine().getLoadWorker().exceptionProperty().addListener(new ChangeListener<Throwable>() {
@Override
Expand Down Expand Up @@ -841,6 +855,9 @@ public void setProperty(String key, Object value) {
if(key.equalsIgnoreCase("User-Agent")) {
//web.getEngine().setUserAgent((String)value);
}
if (BrowserComponent.BROWSER_PROPERTY_FOLLOW_TARGET_BLANK.equals(key)) {
followTargetBlank = Boolean.TRUE.equals(value);
}
}

@Override
Expand Down Expand Up @@ -968,4 +985,4 @@ public boolean supportsExecuteAndReturnString() {



}
}
33 changes: 33 additions & 0 deletions Ports/iOSPort/nativeSources/IOSNative.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "xmlvm.h"
#include "java_lang_String.h"
#import "CN1ES2compat.h"
#import <objc/runtime.h>

#ifndef NEW_CODENAME_ONE_VM
#include "xmlvm-util.h"
Expand Down Expand Up @@ -2482,6 +2483,18 @@ void com_codename1_impl_ios_IOSNative_retainPeer___long(CN1_THREAD_STATE_MULTI_A
#ifndef NO_UIWEBVIEW
UIWebView* com_codename1_impl_ios_IOSNative_createBrowserComponent = nil;
#endif
static void cn1_setBrowserFollowTargetBlank(id webView, BOOL follow) {
objc_setAssociatedObject(webView, @selector(cn1FollowTargetBlank), [NSNumber numberWithBool:follow], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

static BOOL cn1_shouldFollowTargetBlank(id webView) {
NSNumber *value = objc_getAssociatedObject(webView, @selector(cn1FollowTargetBlank));
if (value == nil) {
return YES;
}
return [value boolValue];
}

JAVA_LONG com_codename1_impl_ios_IOSNative_createBrowserComponent___java_lang_Object(CN1_THREAD_STATE_MULTI_ARG JAVA_OBJECT instanceObject, JAVA_OBJECT obj) {
#ifndef NO_UIWEBVIEW
dispatch_sync(dispatch_get_main_queue(), ^{
Expand All @@ -2493,6 +2506,7 @@ JAVA_LONG com_codename1_impl_ios_IOSNative_createBrowserComponent___java_lang_Ob
com_codename1_impl_ios_IOSNative_createBrowserComponent.delegate = del;
com_codename1_impl_ios_IOSNative_createBrowserComponent.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
[com_codename1_impl_ios_IOSNative_createBrowserComponent setAllowsInlineMediaPlayback:YES];
cn1_setBrowserFollowTargetBlank(com_codename1_impl_ios_IOSNative_createBrowserComponent, YES);
#ifndef CN1_USE_ARC
[com_codename1_impl_ios_IOSNative_createBrowserComponent retain];
#endif
Expand Down Expand Up @@ -2536,6 +2550,7 @@ JAVA_LONG com_codename1_impl_ios_IOSNative_createWKBrowserComponent___java_lang_
com_codename1_impl_ios_IOSNative_createWKBrowserComponent.backgroundColor = [UIColor clearColor];
com_codename1_impl_ios_IOSNative_createWKBrowserComponent.opaque = NO;
com_codename1_impl_ios_IOSNative_createWKBrowserComponent.autoresizesSubviews = YES;
cn1_setBrowserFollowTargetBlank(com_codename1_impl_ios_IOSNative_createWKBrowserComponent, YES);

if (getBooleanClientProperty(CN1_THREAD_GET_STATE_PASS_ARG obj, @"BrowserComponent.ios.debug")) {
com_codename1_impl_ios_IOSNative_createWKBrowserComponent.inspectable = YES;
Expand Down Expand Up @@ -2598,6 +2613,24 @@ void com_codename1_impl_ios_IOSNative_setBrowserUserAgent___long_java_lang_Strin
#endif
}

void com_codename1_impl_ios_IOSNative_setBrowserFollowTargetBlank___long_boolean(CN1_THREAD_STATE_MULTI_ARG JAVA_OBJECT instanceObject, JAVA_LONG peer, JAVA_BOOLEAN follow) {
dispatch_sync(dispatch_get_main_queue(), ^{
POOL_BEGIN();
if (isWKWebView(peer)) {
#ifdef supportsWKWebKit
WKWebView* w = (BRIDGE_CAST WKWebView*)((void *)peer);
cn1_setBrowserFollowTargetBlank(w, follow);
#endif
} else {
#ifndef NO_UIWEBVIEW
UIWebView* w = (BRIDGE_CAST UIWebView*)((void *)peer);
cn1_setBrowserFollowTargetBlank(w, follow);
#endif
}
POOL_END();
});
}


void com_codename1_impl_ios_IOSNative_setPinchToZoomEnabled___long_boolean(CN1_THREAD_STATE_MULTI_ARG JAVA_OBJECT instanceObject, JAVA_LONG peer, JAVA_BOOLEAN enabled) {
dispatch_sync(dispatch_get_main_queue(), ^{
Expand Down
16 changes: 15 additions & 1 deletion Ports/iOSPort/nativeSources/UIWebViewEventDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "com_codename1_ui_BrowserComponent.h"
#include "xmlvm.h"
#import "CodenameOne_GLViewController.h"
#import <objc/runtime.h>

#ifdef CN1_USE_JAVASCRIPTCORE
#import <JavaScriptCore/JSContext.h>
Expand Down Expand Up @@ -91,6 +92,14 @@ - (void)webViewDidFinishLoad:(UIWebView *)webView {
#endif

#ifdef ENABLE_WKWEBVIEW
- (BOOL)shouldFollowTargetBlank:(id)webView {
NSNumber *value = objc_getAssociatedObject(webView, @selector(cn1FollowTargetBlank));
if (value == nil) {
return YES;
}
return [value boolValue];
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
connections--;
if(connections < 1) {
Expand All @@ -110,7 +119,12 @@ - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigati
//NSLog(@"Firing navigation callback: %@", navigationAction.request.URL.absoluteString );
JAVA_BOOLEAN result = com_codename1_ui_BrowserComponent_fireBrowserNavigationCallbacks___java_lang_String_R_boolean(CN1_THREAD_GET_STATE_PASS_ARG c, xmlvm_create_java_string(CN1_THREAD_GET_STATE_PASS_ARG navigationAction.request.URL.absoluteString.UTF8String));
if(result) {
decisionHandler(WKNavigationActionPolicyAllow);
if (navigationAction.targetFrame == nil && [self shouldFollowTargetBlank:webView]) {
[webView loadRequest:navigationAction.request];
decisionHandler(WKNavigationActionPolicyCancel);
} else {
decisionHandler(WKNavigationActionPolicyAllow);
}
com_codename1_impl_ios_IOSImplementation_fireWebViewDidStartLoad___com_codename1_ui_BrowserComponent_java_lang_String(CN1_THREAD_GET_STATE_PASS_ARG c, xmlvm_create_java_string(CN1_THREAD_GET_STATE_PASS_ARG navigationAction.request.URL.absoluteString.UTF8String));
} else {
decisionHandler(WKNavigationActionPolicyCancel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6893,6 +6893,9 @@ public void setBrowserProperty(PeerComponent browserPeer, String key, Object val
nativeInstance.setBrowserUserAgent(datePickerResult, (String)value);
return;
}
if (BrowserComponent.BROWSER_PROPERTY_FOLLOW_TARGET_BLANK.equals(key)) {
nativeInstance.setBrowserFollowTargetBlank(get(browserPeer), Boolean.TRUE.equals(value));
}
}

/**
Expand Down Expand Up @@ -9536,4 +9539,3 @@ public void announceForAccessibility(final Component cmp, final String text) {
IOSNative.announceForAccessibility(text);
}
}

1 change: 1 addition & 0 deletions Ports/iOSPort/src/com/codename1/impl/ios/IOSNative.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ byte[] loadResource(String name, String type) {
native void setBrowserURL(long browserPeer, String url, String[] keys, String[] values);

native void setBrowserUserAgent(long browserPeer, String ua);
native void setBrowserFollowTargetBlank(long browserPeer, boolean follow);

native void browserBack(long browserPeer);
native void browserStop(long browserPeer);
Expand Down
Loading