The nsIWebBrowserPersist service seems to be asynchronous as immediately
reading the file causes error as the saveURI method might not have done
its work. As a way out I repeatedly display alert showing "currentState"
until its value is PERSIST_STATE_FINISHED. After one or at most two
alerts it proceeds to the next code.
My question is, Is there any elegant way of forcing nsWebBrowserPersist
service to finish its job? Alternatively a method to read the message
source directly into a buffer will be appreciated.
Currently the code looks like this:
const nsIWBP = Components.interfaces.nsIWebBrowserPersist;
var persist =
Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].createInstance(nsIWBP);
persist.persistFlags = nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES
| nsIWBP.PERSIST_FLAGS_FROM_CACHE;
persist.saveURI(msgURI, null, null, null, null, localFile);
// force to complete
while(persist.currentState != persist.PERSIST_STATE_FINISHED)
alert(persist.currentState);
// further work
Regards,
Khalid