Fix bug 305010: change the ad blocking pref to use nsIStyleSheetService to load the file, rather than copying it into userContent.css, so that we can load and unload the file dynamically, and not clobber the user's userContent.css.

The first time you run a build with this change, it will move any existing userContent.css to userContent_unused.css. r=pinkerton
This commit is contained in:
smfr%smfr.org 2005-08-19 17:28:12 +00:00
Родитель 13160940a9
Коммит d59c0d22f8
4 изменённых файлов: 103 добавлений и 22 удалений

Просмотреть файл

@ -3,7 +3,7 @@
<plist version="1.0">
<dict>
<key>IBDocumentLocation</key>
<string>75 120 356 240 0 0 1600 1002 </string>
<string>35 27 356 240 0 0 1600 1002 </string>
<key>IBFramework Version</key>
<string>437.0</string>
<key>IBOldestOS</key>

Двоичные данные
camino/PreferencePanes/WebFeatures/English.lproj/WebFeatures.nib/keyedobjects.nib сгенерированный

Двоичный файл не отображается.

Просмотреть файл

@ -56,6 +56,9 @@
// but that requires linkage and extra search paths.
static NSString* XPCOMShutDownNotificationName = @"XPCOMShutDown";
// needs to match the string in PreferenceManager.mm
static NSString* const AdBlockingChangedNotificationName = @"AdBlockingChanged";
@interface OrgMozillaChimeraPreferenceWebFeatures(PRIVATE)
-(NSString*)profilePath;
@end
@ -116,11 +119,7 @@ static NSString* XPCOMShutDownNotificationName = @"XPCOMShutDown";
BOOL preventAnimation = [[self getStringPref:"image.animation_mode" withSuccess:&gotPref] isEqualToString:@"once"];
[mPreventAnimation setState:preventAnimation];
// check if userContent.css is in the profile. Yes, this will give false positives if the
// user has made their own, but that's their problem.
NSString* adBlockFile = [[[self profilePath] stringByAppendingPathComponent:@"chrome"]
stringByAppendingPathComponent:@"userContent.css"];
BOOL enableAdBlock = [[NSFileManager defaultManager] fileExistsAtPath:adBlockFile];
BOOL enableAdBlock = [self getBooleanPref:"camino.enable_ad_blocking" withSuccess:&gotPref];
[mEnableAdBlocking setState:enableAdBlock];
// store permission manager service and cache the enumerator.
@ -158,16 +157,8 @@ static NSString* XPCOMShutDownNotificationName = @"XPCOMShutDown";
//
- (IBAction)clickEnableAdBlocking:(id)sender
{
NSString* adBlockFileInProfile = [[[self profilePath] stringByAppendingPathComponent:@"chrome"]
stringByAppendingPathComponent:@"userContent.css"];
if ([sender state]) {
NSString* sourcePath = [[NSBundle mainBundle] pathForResource:@"ad_blocking" ofType:@"css"];
[[NSFileManager defaultManager] removeFileAtPath:adBlockFileInProfile handler:nil];
[[NSFileManager defaultManager] copyPath:sourcePath toPath:adBlockFileInProfile handler:nil];
}
else {
[[NSFileManager defaultManager] removeFileAtPath:adBlockFileInProfile handler:nil];
}
[self setPref:"camino.enable_ad_blocking" toBoolean:[sender state] == NSOnState];
[[NSNotificationCenter defaultCenter] postNotificationName:AdBlockingChangedNotificationName object:nil];
}
//
@ -178,10 +169,7 @@ static NSString* XPCOMShutDownNotificationName = @"XPCOMShutDown";
//
- (IBAction)clickEnablePopupBlocking:(id)sender
{
if ( [sender state] )
[self setPref:"dom.disable_open_during_load" toBoolean: YES];
else
[self setPref:"dom.disable_open_during_load" toBoolean: NO];
[self setPref:"dom.disable_open_during_load" toBoolean:[sender state] == NSOnState];
[mEditWhitelist setEnabled:[sender state]];
}
@ -192,7 +180,7 @@ static NSString* XPCOMShutDownNotificationName = @"XPCOMShutDown";
//
-(IBAction) clickEnableImageResizing:(id)sender
{
[self setPref:"browser.enable_automatic_image_resizing" toBoolean:[sender state] ? YES : NO];
[self setPref:"browser.enable_automatic_image_resizing" toBoolean:[sender state] == NSOnState];
}
//
@ -315,7 +303,7 @@ static NSString* XPCOMShutDownNotificationName = @"XPCOMShutDown";
if ( ![url rangeOfString:@"http://"].length && ![url rangeOfString:@"https://"].length )
url = [NSString stringWithFormat:@"http://%@", url];
const char* siteURL = [url cString];
const char* siteURL = [url UTF8String];
nsCOMPtr<nsIURI> newURI;
NS_NewURI(getter_AddRefs(newURI), siteURL);
if ( newURI ) {

Просмотреть файл

@ -55,6 +55,8 @@
#include "AppDirServiceProvider.h"
#include "nsAppDirectoryServiceDefs.h"
#include "nsIRegistry.h"
#include "nsIStyleSheetService.h"
#include "nsNetUtil.h"
#include "nsStaticComponents.h"
#ifndef _BUILD_STATIC_BIN
@ -62,6 +64,8 @@ nsStaticModuleInfo const *const kPStaticModules = nsnull;
PRUint32 const kStaticModuleCount = 0;
#endif
static NSString* const AdBlockingChangedNotificationName = @"AdBlockingChanged";
// This is an arbitrary version stamp that gets written to the prefs file.
// It can be used to detect when a new version of Camino is run that needs
// some prefs to be upgraded.
@ -88,6 +92,9 @@ static const PRInt32 kCurrentPrefsVersion = 1;
- (void)registerForProxyChanges;
- (BOOL)readSystemProxySettings;
- (BOOL)cleanupUserContentCSS;
- (void)refreshAdBlockingStyleSheet:(BOOL)inLoad;
@end
#pragma mark -
@ -191,6 +198,12 @@ static BOOL gMadePrefManager;
selector: @selector(xpcomTerminate:)
name: XPCOMShutDownNotificationName
object: nil];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(adBlockingPrefChanged:)
name: AdBlockingChangedNotificationName
object: nil];
}
- (void)savePrefsFile
@ -418,6 +431,18 @@ static BOOL gMadePrefManager;
[self setPref:"intl.accept_languages" toString:acceptLangHeader];
}
}
// load up the default stylesheet (is this the best place to do this?)
BOOL prefExists = NO;
BOOL enableAdBlocking = [self getBooleanPref:"camino.enable_ad_blocking" withSuccess:&prefExists];
if (!prefExists)
{
enableAdBlocking = [self cleanupUserContentCSS];
[self setPref:"camino.enable_ad_blocking" toBoolean:enableAdBlocking];
}
if (enableAdBlocking)
[self refreshAdBlockingStyleSheet:YES];
}
#pragma mark -
@ -573,6 +598,74 @@ static void SCProxiesChangedCallback(SCDynamicStoreRef store, CFArrayRef changed
#pragma mark -
- (void)adBlockingPrefChanged:(NSNotification*)inNotification
{
BOOL adBlockingEnabled = [self getBooleanPref:"camino.enable_ad_blocking" withSuccess:nil];
[self refreshAdBlockingStyleSheet:adBlockingEnabled];
}
// some versions of 0.9a copied ad_blocking.css into <profile>/chrome/userContent.css.
// now that we load ad_blocking.css dynamically, we have to move that file aside to
// avoid loading it.
// returns YES if there was a userContent.css in the chrome dir.
- (BOOL)cleanupUserContentCSS
{
NSString* profilePath = [self newProfilePath];
NSString* chromeDirPath = [profilePath stringByAppendingPathComponent:@"chrome"];
NSString* userContentCSSPath = [chromeDirPath stringByAppendingPathComponent:@"userContent.css"];
if ([[NSFileManager defaultManager] fileExistsAtPath:userContentCSSPath])
{
NSString* userContentBackPath = [chromeDirPath stringByAppendingPathComponent:@"userContent_unused.css"];
BOOL moveSucceeded = [[NSFileManager defaultManager] movePath:userContentCSSPath toPath:userContentBackPath handler:nil];
NSLog(@"Ad blocking now users a built-in CSS file; moving previous userContent.css file at\n %@\nto\n %@",
userContentCSSPath, userContentBackPath);
if (!moveSucceeded)
NSLog(@"Move failed; does %@ exist already?", userContentBackPath);
return YES;
}
return NO;
}
// this will reload the sheet if it's already registered, or unload it if the
// param is NO
- (void)refreshAdBlockingStyleSheet:(BOOL)inLoad
{
nsCOMPtr<nsIStyleSheetService> ssService = do_GetService("@mozilla.org/content/style-sheet-service;1");
if (!ssService)
return;
// the the uri of the sheet in our bundle
NSString* cssFilePath = [[NSBundle mainBundle] pathForResource:@"ad_blocking" ofType:@"css"];
if (![[NSFileManager defaultManager] isReadableFileAtPath:cssFilePath])
{
NSLog(@"ad_blocking.css file not found; ad blocking will be disabled");
return;
}
nsresult rv;
nsCOMPtr<nsILocalFile> cssFile;
rv = NS_NewNativeLocalFile(nsDependentCString([cssFilePath fileSystemRepresentation]), PR_TRUE, getter_AddRefs(cssFile));
if (NS_FAILED(rv))
return;
nsCOMPtr<nsIURI> cssFileURI;
rv = NS_NewFileURI(getter_AddRefs(cssFileURI), cssFile);
if (NS_FAILED(rv))
return;
PRBool alreadyRegistered = PR_FALSE;
rv = ssService->SheetRegistered(cssFileURI, nsIStyleSheetService::USER_SHEET, &alreadyRegistered);
if (alreadyRegistered)
ssService->UnregisterSheet(cssFileURI, nsIStyleSheetService::USER_SHEET);
if (inLoad)
ssService->LoadAndRegisterSheet(cssFileURI, nsIStyleSheetService::USER_SHEET);
}
#pragma mark -
- (NSString*)getStringPref: (const char*)prefName withSuccess:(BOOL*)outSuccess
{
NSString *prefValue = @"";