netscape.security.PrivilegeManager.enablePrivilege( "UniversalXPConnect" ); function Proxy() { this.dirname = ""; this.plugin_name = ""; this.pluginsDir = null; }; // install the new plugin Proxy.prototype.installPlugin = function( name ) { log('Proxy.prototype.installPlugin'); netscape.security.PrivilegeManager.enablePrivilege( "UniversalXPConnect" ); var file = Cc["@mozilla.org/file/local;1"].createInstance( Ci.nsILocalFile ); var oldPlugin = Cc["@mozilla.org/file/local;1"].createInstance( Ci.nsILocalFile ); try { file.initWithPath( name ); file.normalize( ); if( file.exists() && file.leafName == this.plugin_name ) { try { oldPlugin.initWithFile(this.getPluginsDir()); oldPlugin.append(file.leafName); if(oldPlugin.exists()) { try { oldPlugin.remove(true); } catch( err ) { return err.name + ": " + err.message; } } file.copyTo( this.getPluginsDir( ), file.leafName ); this.installOtherFiles( file.parent.path ); return true; } catch( error ) { if( error.name == 'NS_ERROR_FILE_ACCESS_DENIED' ) { log( "6" ); try { return "Access to folder " + this.getPluginsDir( ).path + " is denied. Consider to use an user accesible plugins folder instead (for example: ~/.mozilla/plugins/ )"; } catch( err ) { return err.name + ": " + err.message; } } else { return error.message; } } } else { return file.path + " does not exists"; } } catch( e ) { return e.name + ": " + e.message; } }; // return the default plugins dir Proxy.prototype.getDefaultPluginsDir = function( ) { netscape.security.PrivilegeManager.enablePrivilege( "UniversalXPConnect" ); var props = Cc["@mozilla.org/file/directory_service;1"] .getService( Ci.nsIProperties ) .get("APlugns", Components.interfaces.nsIFile ); return props; }; // return the user-defined plugins dir Proxy.prototype.getPluginsDir = function( ) { netscape.security.PrivilegeManager.enablePrivilege( "UniversalXPConnect" ); var file = Cc["@mozilla.org/file/local;1"].createInstance( Ci.nsILocalFile ); try { file.initWithPath( this.parent.utils.getTargetDir() ); return file; } catch( error ) { log( error ); return this.getDefaultPluginsDir( ); } } // Set the FlashSwitcher owner class Proxy.prototype.setOwner = function( owner ){ this.parent = owner; }; Proxy.prototype.saveOtherFiles = function( dist_dir ){}; Proxy.prototype.installOtherFiles = function( parent_path ){}; Proxy.prototype.removeOtherFiles = function( ) {}; // Return the all available plugins installed Proxy.prototype.getAvailablePlugins = function( ) { var dir = this.getRepoDir( ).clone( ); dir.append( this.dirname ); var entries = dir.directoryEntries; var arr = []; var label; while( entries.hasMoreElements( ) ) { var entry = entries.getNext( ); entry.QueryInterface( Components.interfaces.nsIFile ); entry.append( this.plugin_name ); if( entry.exists( ) ) { arr.push( entry ); } } return arr; }; /** * Attempt to get the repository directory * first check for user defined directory * otherwise use the default repository * directory */ Proxy.prototype.getRepoDir = function( ) { netscape.security.PrivilegeManager.enablePrivilege( "UniversalXPConnect" ); var file = Cc["@mozilla.org/file/local;1"].createInstance( Ci.nsILocalFile ); var user_dir = this.parent.utils.getUserRepoDir( ); try { file.initWithPath( user_dir ); if(!file.exists() || file.isFile()){ return undefined; } } catch( error ) { log( error.name + ", " + error.message ); return this.parent.utils.getDefaultDir( ); } return file; } /** * Remove the plug-in file from the * system * */ Proxy.prototype.removePlugin = function( ) { log('Proxy.prototype.removePlugin'); var file = this.getRunningPluginFilePath( ); if( file != undefined ) { if( !file.isWritable() ){ return file.path + " is not writeable. please check permissions"; } if( file.exists( ) && file.isFile( ) && file.isWritable( ) ) { try { file.remove( false ); this.removeOtherFiles( ); return true; } catch( e ) { return e.message; } } } return "Cannot find any Flash player installed"; } Proxy.prototype.getRunningPluginFilePath = function( ) { log('Proxy.prototype.getRunningPluginFilePath'); var current = this.getRunningPluginFileName( ); log('current: ' + current ); var file = Cc["@mozilla.org/file/local;1"].createInstance( Ci.nsILocalFile ); if( current != undefined ) { try { file.initWithPath( current ); return file; } catch( error ) { log( error ); } } else { return undefined; } } /** * Return the filename of the currently installed * flahs player plugin * */ Proxy.prototype.getRunningPluginFileName = function( ) { log('Proxy.prototype.getRunningPluginFileName'); var phs = Cc["@mozilla.org/plugin/host;1"].getService( Ci.nsIPluginHost ); var r = phs.getPluginTags({ }); for( var a = 0; a < r.length; a++ ) { if( r[a].name.indexOf("Shockwave Flash") == 0 ) { return r[a].filename; } } return undefined; } Proxy.prototype.saveCurrent = function( new_dir_name ) { var repo_path = this.getRepoDir( ).clone( ); var current_plugin = this.getRunningPluginFilePath( ); var error_string = ""; if( current_plugin != undefined ) { if( current_plugin.isReadable( ) ) { if( repo_path.isWritable() ) { try { repo_path.append( this.dirname ); repo_path.append( new_dir_name ); repo_path.create( Ci.nsIFile.DIRECTORY_TYPE, 0777); current_plugin.copyTo( repo_path, current_plugin.leafName ); this.saveOtherFiles( repo_path ); error_string = current_plugin.leafName + " copied to " + repo_path.path; } catch( error ) { if( error.name == 'NS_ERROR_FILE_ALREADY_EXISTS') { error_string = "Destination file or path already exists!"; } else { error_string = error.name + ": " + error.message; } } } else { error_string = "Repository folder is not writable, please check folder permissions"; } } else { error_string = current_plugin.path + " is not readable"; } } else { error_string = "Invalid plugin!"; } return error_string; } /* --------------------------- LINUX Implementation ----------------------------*/ function LinuxProxy( ){ this.dirname = "linux"; this.plugin_name = "libflashplayer.so"; }; LinuxProxy.prototype = new Proxy( ); /* --------------------------- MAC Implementation ----------------------------*/ function MacProxy( ){ this.dirname = "mac"; this.plugin_name = "Flash Player.plugin"; }; MacProxy.prototype = new Proxy( ); MacProxy.prototype.getDefaultPluginsDir = function( ) { netscape.security.PrivilegeManager.enablePrivilege( "UniversalXPConnect" ); var props = Cc["@mozilla.org/file/directory_service;1"] .getService( Ci.nsIProperties ) .get("LoclIntrntPlgn", Components.interfaces.nsIFile ); return props; }; MacProxy.prototype.getRunningPluginFilePath = function( ) { log('MacProxy.prototype.getRunningPluginFilePath'); var current = this.getRunningPluginFileName( ); log('current: ' + current ); var file = Cc["@mozilla.org/file/local;1"].createInstance( Ci.nsILocalFile ); if( current != undefined ) { try { file.initWithPath( this.getPluginsDir().path ); file.append( current ); log( 'real path: ' + file.path ); return file; } catch( error ) { log( error ); } } else { return undefined; } } MacProxy.prototype.removePlugin = function( ) { log('MacProxy.prototype.removePlugin'); var file = this.getRunningPluginFilePath( ); if( file != undefined ) { if( !file.isWritable() ){ return file.path + " is not writeable. please check permissions"; } var secondFile = Cc["@mozilla.org/file/local;1"].createInstance( Ci.nsILocalFile ); secondFile.initWithPath( file.parent.path ); secondFile.append( "flashplayer.xpt" ); if( secondFile.exists( ) && secondFile.isWritable( ) ) { try { secondFile.remove( false ); } catch( e ) { log( e ); } } var parentPath; if( file.exists( ) && file.isDirectory( ) && file.isWritable( ) ) { try { file.remove( 'PR_TRUE' ); this.removeOtherFiles( ); return true; } catch( e ) { log( e ); return e.message; } } } return "Cannot find any Flash player installed"; } MacProxy.prototype.removeOtherFiles = function( ) { var parentPath = this.getPluginsDir().path; var secondFile = Cc["@mozilla.org/file/local;1"].createInstance( Ci.nsILocalFile ); if( parentPath != undefined ) { try { secondFile.initWithPath( parentPath ); secondFile.append( "flashplayer.xpt" ); if( secondFile.exists( ) ) { if( secondFile.isWritable( ) ) { secondFile.remove( false ); } } } catch( e ) { log( e ); return e.message; } return true; } return false; } /** * When save the current installed plugin into the user * repository directory, copy also the flashplayer.xpt * file if exists */ MacProxy.prototype.saveOtherFiles = function( dist_dir ) { var parentPath = this.getPluginsDir().path; var secondFile = Cc["@mozilla.org/file/local;1"].createInstance( Ci.nsILocalFile ); if( parentPath != undefined ) { try { secondFile.initWithPath( parentPath ); secondFile.append( "flashplayer.xpt" ); if( secondFile.exists( ) ) { secondFile.copyTo( dist_dir, secondFile.leafName ); } } catch( e ) { log( e ); return e.message; } return true; } return false; }; MacProxy.prototype.installOtherFiles = function( parent_path ) { var dest_dir = this.getPluginsDir( ); var secondFile = Cc["@mozilla.org/file/local;1"].createInstance( Ci.nsILocalFile ); if( parent_path != undefined ) { try { secondFile.initWithPath( parent_path ); secondFile.append( "flashplayer.xpt" ); if( secondFile.exists( ) ) { secondFile.copyTo( dest_dir, secondFile.leafName ); } } catch( e ) { log( e ); return e.message; } return true; } return false; } /* --------------------------- Windows Implementation ----------------------------*/ function WinProxy( ){ this.dirname = "win"; this.plugin_name = "NPSWF32.dll"; }; WinProxy.prototype = new Proxy( ); // return the Windows plugins dir ( Windows/System32/Marcomed ) WinProxy.prototype.getDefaultPluginsDir = function( ) { netscape.security.PrivilegeManager.enablePrivilege( "UniversalXPConnect" ); var props; props = Cc["@mozilla.org/file/directory_service;1"].getService( Ci.nsIProperties ).get("WinD", Components.interfaces.nsIFile ); try { if( props.exists( ) ) { props.append( "system32" ); props.append( "Macromed" ); props.append( "Flash" ); log( "The default is: " + props.path ); if( props.exists( ) ) { return props; } } } catch( e ) { props = Cc["@mozilla.org/file/directory_service;1"].getService( Ci.nsIProperties ).get("APlugns", Components.interfaces.nsIFile ); } return props; }; WinProxy.prototype.removeOtherFiles = function( ) { var parentPath = this.getPluginsDir().path; var secondFile = Cc["@mozilla.org/file/local;1"].createInstance( Ci.nsILocalFile ); if( parentPath != undefined ) { try { secondFile.initWithPath( parentPath ); secondFile.append( "flashplayer.xpt" ); if( secondFile.exists( ) ) { if( secondFile.isWritable( ) ) { secondFile.remove( false ); } } } catch( e ) { log( e ); return e.message; } return true; } return false; } WinProxy.prototype.saveOtherFiles = function( dist_dir ) { var parentPath = this.getPluginsDir().path; var secondFile = Cc["@mozilla.org/file/local;1"].createInstance( Ci.nsILocalFile ); if( parentPath != undefined ) { try { secondFile.initWithPath( parentPath ); secondFile.append( "flashplayer.xpt" ); if( secondFile.exists( ) ) { secondFile.copyTo( dist_dir, secondFile.leafName ); } } catch( e ) { log( e ); return e.message; } return true; } return false; }; WinProxy.prototype.installOtherFiles = function( parent_path ) { var dest_dir = this.getPluginsDir( ); var secondFile = Cc["@mozilla.org/file/local;1"].createInstance( Ci.nsILocalFile ); if( parent_path != undefined ) { try { secondFile.initWithPath( parent_path ); secondFile.append( "flashplayer.xpt" ); if( secondFile.exists( ) ) { secondFile.copyTo( dest_dir, secondFile.leafName ); } } catch( e ) { log( e ); return e.message; } return true; } return false; }