[Bbs2ch-cvs 410] [392] [04_BRANCH] nsIWebBrowserPersist を使うようにした

Back to archive index

svnno****@sourc***** svnno****@sourc*****
2008年 2月 29日 (金) 23:25:08 JST


Revision: 392
          http://svn.sourceforge.jp/cgi-bin/viewcvs.cgi?root=bbs2ch&view=rev&rev=392
Author:   flyson
Date:     2008-02-29 23:25:08 +0900 (Fri, 29 Feb 2008)

Log Message:
-----------
[04_BRANCH] nsIWebBrowserPersist を使うようにした

Modified Paths:
--------------
    branches/bbs2chreader/04_BRANCH/chrome/content/bbs2chreader/lib/downloader.js


-------------- next part --------------
Modified: branches/bbs2chreader/04_BRANCH/chrome/content/bbs2chreader/lib/downloader.js
===================================================================
--- branches/bbs2chreader/04_BRANCH/chrome/content/bbs2chreader/lib/downloader.js	2008-02-29 14:23:48 UTC (rev 391)
+++ branches/bbs2chreader/04_BRANCH/chrome/content/bbs2chreader/lib/downloader.js	2008-02-29 14:25:08 UTC (rev 392)
@@ -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,107 +128,98 @@
 
 // ********** ********* メソッド ********** **********
 
-
 	/**
 	 * ダウンロードを開始する
 	 */
 	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 = Components.classes["@mozilla.org/network/io-service;1"]
-					.getService(Components.interfaces.nsIIOService);
-		var bbs2chService = Components.classes["@mozilla.org/bbs2ch-service;1"]
-					.getService(Components.interfaces.nsIBbs2chService);
+		var ioService = XPC.getService("@mozilla.org/network/io-service;1", "nsIIOService");
+		var bbs2chService = XPC.getService("@mozilla.org/bbs2ch-service;1", "nsIBbs2chService");
 
-			// nsIURI の作成
 		try{
-			var fromURI = ioService.newURI(this.urlSpec, null, null);
+			this._file = XPC.createInstance("@mozilla.org/file/local;1", "nsILocalFile");
+			this._file.initWithPath(this.filePath);
+			if(!this._file.parent.exists()){
+				this._file.parent.create(Ci.nsILocalFile.DIRECTORY_TYPE, 0755);
+			}
 		}catch(ex){
-			this.onError(this, this.ERROR_BAD_URL);
+			this.onError(this, this.ERROR_BAD_FILE_PATH);
 			return;
 		}
 
-		this._httpChannel = bbs2chService.getHttpChannel(fromURI);
-		this._httpChannel.requestMethod = "GET";
-		this._httpChannel.redirectionLimit = 0; // 302 等のリダイレクトを行わない
-		this._httpChannel.loadFlags = this._httpChannel.LOAD_BYPASS_CACHE;
-
+			// nsIURI の作成
 		try{
-			this._listener._context = this;
-			this._httpChannel.asyncOpen(this._listener, null);
-			this._loading = true;
+			var fromURI = ioService.newURI(this.urlSpec, null, null);
 		}catch(ex){
-			this.onError(this, this.ERROR_FAILURE);
+			this.onError(this, this.ERROR_BAD_URL);
 			return;
 		}
+
+		this._browserPersist = XPC.createInstance("@mozilla.org/embedding/browser/nsWebBrowserPersist;1", "nsIWebBrowserPersist");
+		this._browserPersist.persistFlags |= Ci.nsIWebBrowserPersist.PERSIST_FLAGS_BYPASS_CACHE
+					| Ci.nsIWebBrowserPersist.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
+
+		var httpChannel = bbs2chService.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){
-			this._bInputStream = Components.classes["@mozilla.org/binaryinputstream;1"]
-				.createInstance(Components.interfaces.nsIBinaryInputStream);
-			this._data = new Array();
-		},
-		onDataAvailable: function (aRequest, aContext, aInputStream, aOffset, aCount){
-			aRequest.QueryInterface(Components.interfaces.nsIHttpChannel);
-			var httpStatus = aRequest.responseStatus;
-				// 必要な情報がないなら終了
-			if(!(httpStatus==200 || httpStatus==206)) return;
-			if(aCount == 0) return;
+		onLocationChange : function(aWebProgress, aRequest, aLocation){},
+		onProgressChange : function (aWebProgress, aRequest,
+				aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress){
 
-			if(aRequest.contentLength > 0){
-				var context = this._context;
-				percentage = Math.floor((aOffset * 100.0) / aRequest.contentLength);
-				context.onProgressChange(context, percentage);
+			if(aMaxTotalProgress == -1){
+				this._context.onProgressChange(this._context, -1);
+				return;
 			}
 
-			this._bInputStream.setInputStream(aInputStream);
-			this._data.push(this._bInputStream.readBytes(aCount));
+			percentage = Math.floor(aCurTotalProgress / aMaxTotalProgress * 100);
+			this._context.onProgressChange(this._context, percentage);
 		},
-		onStopRequest: function(aRequest, aContext, 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;
+		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;
 
-			var context = this._context;
-			var data = this._data.join("");
-			this._data = null;
-
-			try{
-				aRequest.QueryInterface(Components.interfaces.nsIHttpChannel);
-				var httpStatus = aRequest.responseStatus;
-				if(httpStatus==200 || httpStatus==206){
-					bbs2chService = Components.classes["@mozilla.org/bbs2ch-service;1"]
-						.getService(Components.interfaces.nsIBbs2chService);
-					var writed = bbs2chService.writeFile(context.filePath, data, false);
-					if(!writed){
-						this.onError(context, context.ERROR_BAD_FILE_PATH);
-						return;
-					}
+			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);
 				}
-				context.onStop(context, httpStatus);
-			}catch(ex){
-				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;
 		}
 	},
 
@@ -239,12 +230,12 @@
 	 */
 	abort: function(aSilent){
 		try{
-			this._httpChannel.cancel(0);
-			this._httpChannel = null;
+			this._browserPersist.cancelSave();
+			this._browserPersist = null;
 		}catch(ex){}
-		
-		if(!aSilent) this.onError(this.ERROR_CANCEL);
-		
+
+		if(!aSilent) this.onError(this, this.ERROR_CANCEL);
+
 		this._loading = false;
 	},
 


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