1 // Constructs an application object
2 function WebDeveloperApplication(applicationPath
)
4 this.applicationPath
= applicationPath
;
5 this.executable
= this.getExecutable();
9 // Creates a source file
10 WebDeveloperApplication
.prototype.createSourceFile = function(temporaryDirectory
, uri
)
12 var sourceFile
= null;
14 // If the URI has a file scheme
15 if(uri
.scheme
== "file")
17 var fileProtocolHandler
= Components
.classes
["@mozilla.org/network/protocol;1?name=file"].createInstance(Components
.interfaces
.nsIFileProtocolHandler
);
19 sourceFile
= fileProtocolHandler
.getFileFromURLSpec(uri
.spec
);
22 // If the source file is not set
25 var fileExtension
= "html";
26 var fileName
= uri
.host
;
27 var url
= Components
.classes
["@mozilla.org/network/standard-url;1"].createInstance(Components
.interfaces
.nsIURL
);
29 sourceFile
= Components
.classes
["@mozilla.org/file/local;1"].createInstance(Components
.interfaces
.nsILocalFile
);
32 // If the URL has a file extension
35 fileExtension
= url
.fileExtension
;
38 temporaryDirectory
.append("webdeveloper_" + fileName
+ "_" + new Date().getTime() + "." + fileExtension
);
39 sourceFile
.initWithPath(temporaryDirectory
.path
);
45 // Returns an executable for the application
46 WebDeveloperApplication
.prototype.getExecutable = function()
48 var executable
= Components
.classes
["@mozilla.org/file/local;1"].createInstance(Components
.interfaces
.nsILocalFile
);
50 executable
.initWithPath(this.applicationPath
);
55 // Returns the post data
56 WebDeveloperApplication
.prototype.getPostData = function()
58 // Try to get the post data
61 var sessionHistory
= getWebNavigation().sessionHistory
;
62 var entry
= sessionHistory
.getEntryAtIndex(sessionHistory
.index
, false).QueryInterface(Components
.interfaces
.nsISHEntry
);
64 return entry
.postData
;
72 // Launch the application with the given file
73 WebDeveloperApplication
.prototype.launchWithFile = function()
75 var mimeService
= Components
.classes
["@mozilla.org/uriloader/external-helper-app-service;1"].getService(Components
.interfaces
.nsIMIMEService
);
76 var mimeInfo
= mimeService
.getFromTypeAndExtension(this.file
.path
, null);
78 mimeInfo
.alwaysAskBeforeHandling
= false;
79 mimeInfo
.preferredAction
= Components
.interfaces
.nsIMIMEInfo
.useHelperApp
;
80 mimeInfo
.preferredApplicationHandler
= this.executable
;
82 mimeInfo
.launchWithFile(this.file
);
85 // Launch the application with the source from the given URI
86 WebDeveloperApplication
.prototype.launchWithSource = function(uri
)
88 var stringBundle
= document
.getElementById("webdeveloper-string-bundle");
89 var temporaryDirectory
= Components
.classes
["@mozilla.org/file/directory_service;1"].getService(Components
.interfaces
.nsIProperties
).get("TmpD", Components
.interfaces
.nsIFile
);
91 // If the temporary directory exists, is a directory and is writable
92 if(temporaryDirectory
.exists() && temporaryDirectory
.isDirectory() && temporaryDirectory
.isWritable())
94 // If the executable exists
95 if(this.executable
.exists())
97 this.file
= this.createSourceFile(temporaryDirectory
, uri
);
99 if(uri
.scheme
== "file")
101 this.launchWithFile();
105 var webBrowserPersistInterface
= Components
.interfaces
.nsIWebBrowserPersist
;
106 var webBrowserPersist
= Components
.classes
["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].createInstance(webBrowserPersistInterface
);
108 webBrowserPersist
.persistFlags
= webBrowserPersistInterface
.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION
| webBrowserPersistInterface
.PERSIST_FLAGS_FROM_CACHE
| webBrowserPersistInterface
.PERSIST_FLAGS_REPLACE_EXISTING_FILES
;
109 webBrowserPersist
.progressListener
= this;
111 webBrowserPersist
.saveURI(uri
, null, uri
, this.getPostData(), null, this.file
);
116 webdeveloper_error(stringBundle
.getFormattedString("webdeveloper_launchApplicationFailed", [this.applicationPath
]));
121 webdeveloper_error(stringBundle
.getFormattedString("webdeveloper_tempDirectoryFailed", [temporaryDirectory
.path
]));
125 // Launch the application with the given URL
126 WebDeveloperApplication
.prototype.launchWithURL = function(url
)
128 // If the executable exists, is a file and is executable
129 if(this.executable
.exists() && this.executable
.isFile() && this.executable
.isExecutable())
131 var process
= Components
.classes
["@mozilla.org/process/util;1"].createInstance(Components
.interfaces
.nsIProcess
);
132 var processArguments
= new Array(url
);
134 process
.init(this.executable
);
135 process
.run(false, processArguments
, processArguments
.length
);
139 webdeveloper_error(document
.getElementById("webdeveloper-string-bundle").getFormattedString("webdeveloper_launchApplicationFailed", [this.applicationPath
]));
143 // Called when the progress state changes
144 WebDeveloperApplication
.prototype.onStateChange = function(webProgress
, request
, stateFlags
, status
)
146 // If the progress has stopped
147 if(stateFlags
& Components
.interfaces
.nsIWebProgressListener
.STATE_STOP
)
149 this.launchWithFile();
153 // Indicates the interfaces this object supports
154 WebDeveloperApplication
.prototype.QueryInterface = function(id
)
156 // If the query is for a supported interface
157 if(id
.equals(Components
.interfaces
.nsISupports
) || id
.equals(Components
.interfaces
.nsIWebProgressListener
))
162 throw Components
.results
.NS_NOINTERFACE
;
165 // Dummy methods requiring implementations
166 WebDeveloperApplication
.prototype.onLocationChange = function(webProgress
, request
, location
) {}
167 WebDeveloperApplication
.prototype.onProgressChange = function(webProgress
, request
, currentSelfProgress
, maximumSelfProgress
, currentTotalProgress
, maximumTotalProgress
) {}
168 WebDeveloperApplication
.prototype.onSecurityChange = function(webProgress
, request
, state
) {}
169 WebDeveloperApplication
.prototype.onStatusChange = function(webProgress
, request
, status
, message
) {}