[Bbs2ch-cvs 393] [375] nsIWebBrowserPersist を使うようにした

Back to archive index

svnno****@sourc***** svnno****@sourc*****
2008年 2月 17日 (日) 04:19:48 JST


Revision: 375
          http://svn.sourceforge.jp/cgi-bin/viewcvs.cgi?root=bbs2ch&view=rev&rev=375
Author:   flyson
Date:     2008-02-17 04:19:48 +0900 (Sun, 17 Feb 2008)

Log Message:
-----------
nsIWebBrowserPersist を使うようにした

Modified Paths:
--------------
    trunk/bbs2chreader/chrome/content/bbs2chreader/lib/downloader.js


-------------- next part --------------
Modified: trunk/bbs2chreader/chrome/content/bbs2chreader/lib/downloader.js
===================================================================
--- trunk/bbs2chreader/chrome/content/bbs2chreader/lib/downloader.js	2008-02-16 09:28:24 UTC (rev 374)
+++ trunk/bbs2chreader/chrome/content/bbs2chreader/lib/downloader.js	2008-02-16 19:19:48 UTC (rev 375)
@@ -15,7 +15,7 @@
  *
  * The Initial Developer of the Original Code is
  * flyson.
- * Portions created by the Initial Developer are Copyright (C) 2004
+ * Portions created by the Initial Developer are Copyright (C) 2008
  * the Initial Developer. All Rights Reserved.
  *
  * Contributor(s):
@@ -45,7 +45,7 @@
 function b2rDownloader(aURLSpec, aFilePath){
 	this._urlSpec = aURLSpec;
 	this._filePath = aFilePath;
-	this._httpChannel = null;
+	this._browserPersist = null;
 	this._loading = false;
 }
 
@@ -128,32 +128,24 @@
 
 // ********** ********* メソッド ********** **********
 
-
 	/**
 	 * ダウンロードを開始する
 	 */
 	download: function(){
 		this._loading = false;
-		if(this._httpChannel){
-			this._httpChannel.cancel(0);
-			this._httpChannel = null;
+		if(this._browserPersist){
+			this._browserPersist.cancelSave();
+			this._browserPersist = null;
 		}
 
-		var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
-		var b2rService = Cc["@bbs2ch.sourceforge.jp/b2r-global-service;1"].getService(Ci.b2rIGlobalService);
+		var ioService = XPC.getService("@mozilla.org/network/io-service;1", "nsIIOService");
+		var b2rService = XPC.getService("@bbs2ch.sourceforge.jp/b2r-global-service;1", "b2rIGlobalService");
 
 		try{
-			this._file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
+			this._file = XPC.createInstance("@mozilla.org/file/local;1", "nsILocalFile");
 			this._file.initWithPath(this.filePath);
-
-			this._tempFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
-			this._tempFile.initWithPath(this._file.path + ".tmp");
-			this._tempFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
-			if(!this._tempFile.clone().exists()){
-				this._tempFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
-			}
 		}catch(ex){
-			this.onError(context, context.ERROR_BAD_FILE_PATH);
+			this.onError(this, this.ERROR_BAD_FILE_PATH);
 			return;
 		}
 
@@ -161,117 +153,70 @@
 		try{
 			var fromURI = ioService.newURI(this.urlSpec, null, null);
 		}catch(ex){
-			if(this._tempFile.clone().exists()){
-				this._tempFile.remove(false);
-			}
 			this.onError(this, this.ERROR_BAD_URL);
 			return;
 		}
 
-		this._httpChannel = b2rService.getHttpChannel(fromURI);
-		this._httpChannel.requestMethod = "GET";
-		this._httpChannel.redirectionLimit = 0; // 302 等のリダイレクトを行わない
-		this._httpChannel.loadFlags = Ci.nsIHttpChannel.LOAD_BYPASS_CACHE;
+		this._browserPersist = XPC.createInstance("@mozilla.org/embedding/browser/nsWebBrowserPersist;1", "nsIWebBrowserPersist");
+		this._browserPersist.persistFlags |= Ci.nsIWebBrowserPersist.PERSIST_FLAGS_BYPASS_CACHE;
 
-		try{
-			this.wrappedJSObject = this;
-			this._httpChannel.asyncOpen(this._listener, this);
-			this._loading = true;
-		}catch(ex){
-			if(this._tempFile.clone().exists()){
-				this._tempFile.remove(false);
-			}
-			this.onError(this, this.ERROR_FAILURE);
-			return;
-		}
+		var httpChannel = b2rService.getHttpChannel(fromURI);
+		httpChannel.requestMethod = "GET";
+		httpChannel.redirectionLimit = 0; // 302 等のリダイレクトを行わない
+		httpChannel.loadFlags |= Ci.nsIHttpChannel.LOAD_BYPASS_CACHE;
+		httpChannel.notificationCallbacks = this._browserPersist;
+
+		this._listener._context = this;
+		this._browserPersist.progressListener = this._listener;
+		this._browserPersist.saveChannel(httpChannel, this._file);
 	},
 
 	_listener: {
-		onStartRequest: function(aRequest, aContext){
-			var context = aContext.wrappedJSObject;
-
-				// 0x02=PR_WRONLY; 0x08=PR_CREATE_FILE;
-				// 0x10=PR_APPEND; 0x20=PR_TRUNCATE;
-			var fileOutputStream = Cc["@mozilla.org/network/file-output-stream;1"]
-						.createInstance(Ci.nsIFileOutputStream);
-			fileOutputStream.init(context._tempFile, 0x02|0x08|0x20, 0666, 0);
-
-			this._outputStream = Cc["@mozilla.org/network/buffered-output-stream;1"]
-						.createInstance(Ci.nsIBufferedOutputStream);
-			this._outputStream.init(fileOutputStream, 1024*16);
-		},
-		onDataAvailable: function (aRequest, aContext, aInputStream, aOffset, aCount){
-			var context = aContext.wrappedJSObject;
-
-			aRequest.QueryInterface(Components.interfaces.nsIHttpChannel);
-			var httpStatus = aRequest.responseStatus;
-				// 必要な情報がないなら終了
-			if(!(httpStatus==200 || httpStatus==206)) return;
-			if(aCount == 0) return;
-
-			if(aRequest.contentLength > 0){
-				percentage = Math.floor((aOffset * 100.0) / aRequest.contentLength);
-				context.onProgressChange(context, percentage);
+		onLocationChange : function(aWebProgress, aRequest, aLocation){},
+		onProgressChange : function (aWebProgress, aRequest,
+				aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress){
+				
+			if(aMaxTotalProgress == -1){
+				this._context.onProgressChange(this._context, -1);
+				return;
 			}
 
-			this._outputStream.writeFrom(aInputStream, aCount);
+			percentage = Math.floor(aCurTotalProgress / aMaxTotalProgress * 100);
+			this._context.onProgressChange(this._context, percentage);
 		},
-		onStopRequest: function(aRequest, aContext, aStatus){
-			var context = aContext.wrappedJSObject;
-			context._loading = false;
-
-			if(!this._outputStream) return;
-			this._outputStream.close();
-			this._outputStream = null;
-
-				// nsNetError.h
-			const NS_ERROR_MODULE_NETWORK = 2152398848;
-			const NS_ERROR_NET_TIMEOUT         = NS_ERROR_MODULE_NETWORK + 14;
-			const NS_ERROR_UNKNOWN_HOST        = NS_ERROR_MODULE_NETWORK + 30;
+		onSecurityChange: function(aWebProgress, aRequest, aState){},
+		onStatusChange : function(aWebProgress, aRequest, aStatus, aMessage){},
+		onStateChange : function(aWebProgress, aRequest, aStateFlags, aStatus){
+ 				// nsNetError.h
+ 			const NS_ERROR_MODULE_NETWORK      = 2152398848;
+ 			const NS_ERROR_NET_TIMEOUT         = NS_ERROR_MODULE_NETWORK + 14;
+ 			const NS_ERROR_UNKNOWN_HOST        = NS_ERROR_MODULE_NETWORK + 30;
+			const NS_ERROR_REDIRECT_LOOP       = NS_ERROR_MODULE_NETWORK + 31;
 			const NS_ERROR_DOCUMENT_NOT_CACHED = NS_ERROR_MODULE_NETWORK + 70;
 
-			try{
-				aRequest.QueryInterface(Components.interfaces.nsIHttpChannel);
-				var httpStatus = aRequest.responseStatus;
-				if(httpStatus==200 || httpStatus==206){
-					try{
-						if(context._file.exists()){
-							context._file.remove(false);
-						}
-						context._tempFile.moveTo(context._file.parent, context._file.leafName);
-					}catch(ex){
-						if(context._tempFile.clone().exists()){
-							context._tempFile.remove(false);
-						}
-						context.onError(context, context.ERROR_BAD_FILE_PATH);
-					}
+			
+			if(aStateFlags & Ci.nsIWebProgressListener.STATE_START){
+				this._context._loading = true;
+				this._context.onStart(this._context);
+			}else if(aStateFlags & Ci.nsIWebProgressListener.STATE_STOP){
+				this._context._loading = false;
+				if(aStatus==0 || aStatus==NS_ERROR_REDIRECT_LOOP){
+					aRequest.QueryInterface(Ci.nsIHttpChannel);
+					this._context.onStop(this._context, aRequest.responseStatus);
+				}else{
+						// XXX 詳細なエラーを出す
+					this._context.onError(this._context, this._context.ERROR_FAILURE);
 				}
-				if(httpStatus==404){
-					if(context._tempFile.clone().exists()){
-						context._tempFile.remove(false);
-					}
-					context.onError(context, context.ERROR_FAILURE);
-				}
-				context.onStop(context, httpStatus);
-			}catch(ex){
-				if(context._tempFile.clone().exists()){
-					context._tempFile.remove(false);
-				}
-				switch(aRequest.status){
-					case NS_ERROR_NET_TIMEOUT:
-						context.onError(context, context.ERROR_NET_TIMEOUT);
-						break;
-					case NS_ERROR_UNKNOWN_HOST:
-						context.onError(context, context.ERROR_UNKNOWN_HOST);
-						break;
-					case NS_ERROR_DOCUMENT_NOT_CACHED:
-						context.onError(context, context.ERROR_NOT_CACHED);
-						break;
-					default:
-						context.onError(context, context.ERROR_FAILURE);
-						break;
-				}
 			}
+		},
+
+		QueryInterface : function(aIID){
+			if(aIID.equals(Components.interfaces.nsIWebProgressListener) ||
+					aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
+					aIID.equals(Components.interfaces.nsISupports)){
+				return this;
+			}
+			throw Components.results.NS_NOINTERFACE;
 		}
 	},
 
@@ -282,15 +227,8 @@
 	 */
 	abort: function(aSilent){
 		try{
-			this._httpChannel.cancel(0);
-			this._httpChannel = null;
-			if(this._listener._outputStream){
-				this._listener._outputStream.close();
-				this._listener._outputStream = null;
-			}
-			if(this._tempFile.clone().exists()){
-				this._tempFile.remove(false);
-			}
+			this._browserPersist.cancelSave();
+			this._browserPersist = null;
 		}catch(ex){}
 
 		if(!aSilent) this.onError(this, this.ERROR_CANCEL);


bbs2ch-cvs メーリングリストの案内
Back to archive index