Back to index

lightning-sunbird  0.9+nobinonly
nsExternalHelperAppService.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  * ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Mozilla browser.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications, Inc.
00019  * Portions created by the Initial Developer are Copyright (C) 1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Scott MacGregor <mscott@netscape.com>
00024  *   Christian Biesinger <cbiesinger@web.de>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #ifndef nsExternalHelperAppService_h__
00041 #define nsExternalHelperAppService_h__
00042 
00043 #ifdef MOZ_LOGGING
00044 #define FORCE_PR_LOG
00045 #endif
00046 #include "prlog.h"
00047 #include "prtime.h"
00048 
00049 #include "nsInt64.h"
00050 
00051 #include "nsIExternalHelperAppService.h"
00052 #include "nsIExternalProtocolService.h"
00053 #include "nsIWebProgressListener2.h"
00054 #include "nsIHelperAppLauncherDialog.h"
00055 
00056 #include "nsIMIMEInfo.h"
00057 #include "nsMIMEInfoImpl.h"
00058 #include "nsIMIMEService.h"
00059 #include "nsIStreamListener.h"
00060 #include "nsIFile.h"
00061 #include "nsIFileStreams.h"
00062 #include "nsIOutputStream.h"
00063 #include "nsString.h"
00064 #include "nsIInterfaceRequestor.h"
00065 #include "nsIInterfaceRequestorUtils.h"
00066 #include "nsILocalFile.h"
00067 #include "nsIChannel.h"
00068 #include "nsITimer.h"
00069 
00070 #include "nsIRDFDataSource.h"
00071 #include "nsIRDFResource.h"
00072 #include "nsCOMPtr.h"
00073 #include "nsIObserver.h"
00074 #include "nsCOMArray.h"
00075 #include "nsWeakReference.h"
00076 #include "nsIPrompt.h"
00077 #include "nsEventQueueUtils.h"
00078 
00079 class nsExternalAppHandler;
00080 class nsIMIMEInfo;
00081 class nsIRDFService;
00082 class nsITransfer;
00083 class nsIDOMWindowInternal;
00084 
00089 class nsExternalHelperAppService
00090 : public nsIExternalHelperAppService,
00091   public nsPIExternalAppLauncher,
00092   public nsIExternalProtocolService,
00093   public nsIMIMEService,
00094   public nsIObserver,
00095   public nsSupportsWeakReference
00096 {
00097 public:
00098   NS_DECL_ISUPPORTS
00099   NS_DECL_NSIEXTERNALHELPERAPPSERVICE
00100   NS_DECL_NSPIEXTERNALAPPLAUNCHER
00101   NS_DECL_NSIEXTERNALPROTOCOLSERVICE
00102   NS_DECL_NSIMIMESERVICE
00103   NS_DECL_NSIOBSERVER
00104 
00105   nsExternalHelperAppService();
00106   virtual ~nsExternalHelperAppService();
00113   NS_HIDDEN_(nsresult) InitDataSource();
00114 
00119   NS_HIDDEN_(nsresult) Init();
00120  
00127   NS_HIDDEN_(nsresult) GetMIMEInfoForMimeTypeFromDS(const nsACString& aContentType,
00128                                                     nsIMIMEInfo * aMIMEInfo);
00129   
00139   NS_HIDDEN_(nsresult) GetMIMEInfoForExtensionFromDS(const nsACString& aFileExtension,
00140                                                      nsIMIMEInfo * aMIMEInfo);
00141 
00148   NS_HIDDEN_(PRBool) GetTypeFromDS(const nsACString& aFileExtension,
00149                                    nsACString& aType);
00150 
00166   virtual already_AddRefed<nsIMIMEInfo> GetMIMEInfoFromOS(const nsACString& aMIMEType,
00167                                                           const nsACString& aFileExt,
00168                                                           PRBool     * aFound) = 0;
00169 
00184   virtual nsresult GetFileTokenForPath(const PRUnichar * platformAppPath,
00185                                        nsIFile ** aFile);
00186 
00191   NS_HIDDEN_(PRBool) MIMETypeIsInDataSource(const char * aContentType);
00192 
00193 protected:
00198   nsCOMPtr<nsIRDFDataSource> mOverRideDataSource;
00199 
00200   nsCOMPtr<nsIRDFResource> kNC_Description;
00201   nsCOMPtr<nsIRDFResource> kNC_Value;
00202   nsCOMPtr<nsIRDFResource> kNC_FileExtensions;
00203   nsCOMPtr<nsIRDFResource> kNC_Path;
00204   nsCOMPtr<nsIRDFResource> kNC_UseSystemDefault;
00205   nsCOMPtr<nsIRDFResource> kNC_SaveToDisk;
00206   nsCOMPtr<nsIRDFResource> kNC_AlwaysAsk;
00207   nsCOMPtr<nsIRDFResource> kNC_HandleInternal;
00208   nsCOMPtr<nsIRDFResource> kNC_PrettyName;
00209 
00213   PRBool mDataSourceInitialized;
00214 
00220   NS_HIDDEN_(nsresult) FillTopLevelProperties(nsIRDFResource * aContentTypeNodeResource, 
00221                                               nsIRDFService * aRDFService,
00222                                               nsIMIMEInfo * aMIMEInfo);
00226   NS_HIDDEN_(nsresult) FillContentHandlerProperties(const char * aContentType,
00227                                                     nsIRDFResource * aContentTypeNodeResource,
00228                                                     nsIRDFService * aRDFService,
00229                                                     nsIMIMEInfo * aMIMEInfo);
00230 
00236   NS_HIDDEN_(nsresult) FillLiteralValueFromTarget(nsIRDFResource * aSource,
00237                                                   nsIRDFResource * aProperty,
00238                                                   const PRUnichar ** aLiteralValue);
00239 
00248   NS_HIDDEN_(nsresult) GetMIMEInfoForMimeTypeFromExtras(const nsACString& aContentType,
00249                                                         nsIMIMEInfo * aMIMEInfo);
00258   NS_HIDDEN_(nsresult) GetMIMEInfoForExtensionFromExtras(const nsACString& aExtension,
00259                                                          nsIMIMEInfo * aMIMEInfo);
00260 
00267   NS_HIDDEN_(PRBool) GetTypeFromExtras(const nsACString& aExtension,
00268                                        nsACString& aMIMEType);
00269 
00275   virtual void FixFilePermissions(nsILocalFile* aFile);
00276 
00277 #ifdef PR_LOGGING
00278 
00283   static PRLogModuleInfo* mLog;
00284 
00285 #endif
00286   // friend, so that it can access the nspr log module and FixFilePermissions
00287   friend class nsExternalAppHandler;
00288 
00293   NS_HIDDEN_(nsresult) ExpungeTemporaryFiles();
00297   nsCOMArray<nsILocalFile> mTemporaryFilesList;
00298 
00302   virtual NS_HIDDEN_(nsresult) LoadUriInternal(nsIURI * aURL) = 0;
00303   NS_HIDDEN_(PRBool) isExternalLoadOK(nsIURI* aURI, nsIPrompt* aPrompt);
00304   NS_HIDDEN_(PRBool) promptForScheme(nsIURI* aURI, nsIPrompt* aPrompt, PRBool *aRemember);
00305 
00306   // friend event handler that accesses the external loading functions
00307   static void *PR_CALLBACK handleExternalLoadEvent(PLEvent *event);
00308 };
00309 
00315 #define DATA_BUFFER_SIZE (4096*2) 
00316 
00325 class nsExternalAppHandler : public nsIStreamListener,
00326                              public nsIHelperAppLauncher,
00327                              public nsITimerCallback
00328 {
00329 public:
00330   NS_DECL_ISUPPORTS
00331   NS_DECL_NSISTREAMLISTENER
00332   NS_DECL_NSIREQUESTOBSERVER
00333   NS_DECL_NSIHELPERAPPLAUNCHER
00334   NS_DECL_NSICANCELABLE
00335   NS_DECL_NSITIMERCALLBACK
00336 
00347   nsExternalAppHandler(nsIMIMEInfo * aMIMEInfo, const nsCSubstring& aFileExtension,
00348                        nsIInterfaceRequestor * aWindowContext,
00349                        const nsAString& aFilename,
00350                        PRUint32 aReason);
00351 
00352   ~nsExternalAppHandler();
00353 
00354 protected:
00355   nsCOMPtr<nsIFile> mTempFile;
00356   nsCOMPtr<nsIURI> mSourceUrl;
00357   nsString mTempFileExtension;
00361   nsCOMPtr<nsIMIMEInfo> mMimeInfo;
00362   nsCOMPtr<nsIOutputStream> mOutStream; 
00363   nsCOMPtr<nsIInterfaceRequestor> mWindowContext;
00364 
00369   nsCOMPtr<nsIDOMWindowInternal> mWindowToClose;
00370   nsCOMPtr<nsITimer> mTimer;
00371 
00377   nsString mSuggestedFileName;
00378 
00383   PRPackedBool mCanceled;
00384 
00390   PRPackedBool mShouldCloseWindow;
00391 
00396   PRPackedBool mReceivedDispositionInfo;
00397   PRPackedBool mStopRequestIssued; 
00398   PRPackedBool mProgressListenerInitialized;
00399 
00405   PRUint32 mReason;
00406 
00407   PRTime mTimeDownloadStarted;
00408   nsInt64 mContentLength;
00409   nsInt64 mProgress; 
00416   nsCOMPtr<nsIFile> mFinalFileDestination;
00417 
00418   char mDataBuffer[DATA_BUFFER_SIZE];
00419 
00424   nsresult SetUpTempFile(nsIChannel * aChannel);
00431   void RetargetLoadNotifications(nsIRequest *request); 
00438   nsresult CreateProgressListener();
00439   nsresult PromptForSaveToFile(nsILocalFile ** aNewFile,
00440                                const nsAFlatString &aDefaultFile,
00441                                const nsAFlatString &aDefaultFileExt);
00442 
00450   void ProcessAnyRefreshTags();
00451 
00456   nsresult MoveFile(nsIFile * aNewFileLocation);
00462   nsresult OpenWithApplication();
00463   
00468   nsresult ExecuteDesiredAction();
00472   PRBool GetNeverAskFlagFromPref(const char * prefName, const char * aContentType);
00473 
00477   nsresult InitializeDownload(nsITransfer*);
00478   
00484   void EnsureSuggestedFileName();
00485 
00486   typedef enum { kReadError, kWriteError, kLaunchError } ErrorType;
00490   void SendStatusChange(ErrorType type, nsresult aStatus, nsIRequest *aRequest, const nsAFlatString &path);
00491 
00497   nsresult MaybeCloseWindow();
00498 
00499   nsCOMPtr<nsIWebProgressListener2> mWebProgressListener;
00500   nsCOMPtr<nsIChannel> mOriginalChannel; 
00501   nsCOMPtr<nsIHelperAppLauncherDialog> mDialog;
00502 
00507   nsIRequest*  mRequest;
00508 };
00509 
00510 #endif // nsExternalHelperAppService_h__