Skip to content

Commit

Permalink
Prevent conflicts between WebViews settings
Browse files Browse the repository at this point in the history
When we want to stop javaScript, switch to another WebPreferences instead
of modifying the current one.
Because WebPreferences can be shared among multiple WebView, as described by
https://developer.apple.com/documentation/webkit/webpreferences?language=objc,
modifying javaScriptEnabled for a single WebView could affect others.
  • Loading branch information
barijaona committed Apr 14, 2018
1 parent a1ab3fd commit fdce120
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/BrowserPane.m
Expand Up @@ -694,8 +694,8 @@ -(IBAction)handleReload:(id)sender
-(void)handleStopLoading:(id)sender
{
[self willChangeValueForKey:@"isLoading"];
[self.webPane.preferences setPlugInsEnabled:NO];
[self.webPane.preferences setJavaScriptEnabled:NO];
// stop Javascript and plugings
[self.webPane abortJavascriptAndPlugIns];
[self.webPane setFrameLoadDelegate:nil];
[self.webPane setUIDelegate:nil];
[self.webPane stopLoading:self];
Expand Down
2 changes: 1 addition & 1 deletion src/TabbedWebView.h
Expand Up @@ -24,7 +24,6 @@
@class AppController;

@interface TabbedWebView : WebView <WebPolicyDelegate> {
WebPreferences * defaultWebPrefs;
BOOL openLinksInNewBrowser;
BOOL isFeedRedirect;
BOOL isDownload;
Expand All @@ -36,6 +35,7 @@
-(void)setOpenLinksInNewBrowser:(BOOL)flag;
-(void)keyDown:(NSEvent *)theEvent;
-(void)printDocument:(id)sender;
-(void)abortJavascriptAndPlugIns;
@property (nonatomic, getter=isFeedRedirect, readonly) BOOL feedRedirect;
@property (nonatomic, getter=isDownload, readonly) BOOL download;
-(void)scrollToTop;
Expand Down
58 changes: 46 additions & 12 deletions src/TabbedWebView.m
Expand Up @@ -85,6 +85,37 @@ +(NSArray *)downloadableExtensions
return @[@"dmg", @"zip", @"gz", @"tgz", @"7z", @"rar", @"tar", @"bin", @"bz2", @"exe", @"sit", @"sitx"];
}

+(WebPreferences *)defaultWebPrefs
{
// Singleton
static WebPreferences * _webPrefs = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_webPrefs = [[WebPreferences alloc] initWithIdentifier:@"VNAStandardWebPrefs"];
_webPrefs.standardFontFamily = @"Arial";
_webPrefs.defaultFontSize = 12;
_webPrefs.privateBrowsingEnabled = NO;
});
return _webPrefs;
}

+(WebPreferences *)passiveWebPrefs
{
// Singleton
static WebPreferences * _webPrefs = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_webPrefs = [[WebPreferences alloc] initWithIdentifier:@"VNAPassiveWebPrefs"];
_webPrefs.standardFontFamily = @"Arial";
_webPrefs.defaultFontSize = 12;
_webPrefs.privateBrowsingEnabled = NO;
_webPrefs.javaScriptEnabled = NO;
_webPrefs.plugInsEnabled = NO;
});
return _webPrefs;
}


/* initWithFrame
* The designated instance initialiser.
*/
Expand Down Expand Up @@ -121,15 +152,10 @@ -(void)initTabbedWebView
[nc addObserver:self selector:@selector(handleUseWebPluginsChange:)
name:kMA_Notify_UseWebPluginsChange object:nil];

// Handle minimum font size, use of JavaScript, and use of plugins
defaultWebPrefs = self.preferences;
defaultWebPrefs.standardFontFamily = @"Arial";
defaultWebPrefs.defaultFontSize = 12;
[defaultWebPrefs setPrivateBrowsingEnabled:NO];
[defaultWebPrefs setJavaScriptEnabled:NO];
[defaultWebPrefs setPlugInsEnabled:NO];
// handle UserAgent
self.customUserAgent = [TabbedWebView userAgent];
// Handle minimum font size, use of JavaScript, and use of plugins
self.preferences = [TabbedWebView defaultWebPrefs];
[self loadMinimumFontSize];
[self loadUseJavaScript];
[self loadUseWebPlugins];
Expand Down Expand Up @@ -336,11 +362,11 @@ -(void)loadMinimumFontSize
{
Preferences * prefs = [Preferences standardPreferences];
if (!prefs.enableMinimumFontSize)
defaultWebPrefs.minimumFontSize = 1;
[TabbedWebView defaultWebPrefs].minimumFontSize = [TabbedWebView passiveWebPrefs].minimumFontSize = 1;
else
{
NSInteger size = prefs.minimumFontSize;
defaultWebPrefs.minimumFontSize = (int)size;
[TabbedWebView defaultWebPrefs].minimumFontSize = [TabbedWebView passiveWebPrefs].minimumFontSize = (int)size;
}
}

Expand Down Expand Up @@ -380,16 +406,24 @@ -(void)scrollToTop
-(void)loadUseJavaScript
{
Preferences * prefs = [Preferences standardPreferences];
defaultWebPrefs.javaScriptEnabled = prefs.useJavaScript;
[TabbedWebView defaultWebPrefs].javaScriptEnabled = prefs.useJavaScript;
}

/* loadUseWebPlugins
* Sets up the web preferences for using JavaScript.
* Sets up the web preferences for using WebPlugins.
*/
-(void)loadUseWebPlugins
{
Preferences * prefs = [Preferences standardPreferences];
defaultWebPrefs.plugInsEnabled = prefs.useWebPlugins;
[TabbedWebView defaultWebPrefs].plugInsEnabled = prefs.useWebPlugins;
}

/* abortJavascriptAndPlugIns
* Sets up the web preferences to stop JavaScript and WebPlugins
*/
-(void)abortJavascriptAndPlugIns
{
self.preferences = [TabbedWebView passiveWebPrefs];
}

/* keyDown
Expand Down

0 comments on commit fdce120

Please sign in to comment.