LCOV - code coverage report
Current view: top level - toolkit/components/downloads - nsDownloadManager.cpp (source / functions) Hit Total Coverage
Test: output.info Lines: 0 127 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 31 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
       2             : /* This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
       5             : 
       6             : #include "nsIPrefService.h"
       7             : #include "nsIPropertyBag2.h"
       8             : #include "nsCExternalHandlerService.h"
       9             : #include "nsDirectoryServiceDefs.h"
      10             : #include "nsDownloadManager.h"
      11             : 
      12             : using namespace mozilla;
      13             : 
      14             : #define DOWNLOAD_MANAGER_BUNDLE "chrome://mozapps/locale/downloads/downloads.properties"
      15             : 
      16             : #define NS_SYSTEMINFO_CONTRACTID "@mozilla.org/system-info;1"
      17             : 
      18             : ////////////////////////////////////////////////////////////////////////////////
      19             : //// nsDownloadManager
      20             : 
      21           0 : NS_IMPL_ISUPPORTS(
      22             :   nsDownloadManager
      23             : , nsIDownloadManager
      24             : )
      25             : 
      26             : nsDownloadManager *nsDownloadManager::gDownloadManagerService = nullptr;
      27             : 
      28             : nsDownloadManager *
      29           0 : nsDownloadManager::GetSingleton()
      30             : {
      31           0 :   if (gDownloadManagerService) {
      32           0 :     NS_ADDREF(gDownloadManagerService);
      33           0 :     return gDownloadManagerService;
      34             :   }
      35             : 
      36           0 :   gDownloadManagerService = new nsDownloadManager();
      37           0 :   if (gDownloadManagerService) {
      38           0 :     NS_ADDREF(gDownloadManagerService);
      39           0 :     if (NS_FAILED(gDownloadManagerService->Init()))
      40           0 :       NS_RELEASE(gDownloadManagerService);
      41             :   }
      42             : 
      43           0 :   return gDownloadManagerService;
      44             : }
      45             : 
      46           0 : nsDownloadManager::~nsDownloadManager()
      47             : {
      48           0 :   gDownloadManagerService = nullptr;
      49           0 : }
      50             : 
      51             : nsresult
      52           0 : nsDownloadManager::Init()
      53             : {
      54             :   nsresult rv;
      55             : 
      56             :   nsCOMPtr<nsIStringBundleService> bundleService =
      57           0 :     mozilla::services::GetStringBundleService();
      58           0 :   if (!bundleService)
      59           0 :     return NS_ERROR_FAILURE;
      60             : 
      61           0 :   rv = bundleService->CreateBundle(DOWNLOAD_MANAGER_BUNDLE,
      62           0 :                                    getter_AddRefs(mBundle));
      63           0 :   NS_ENSURE_SUCCESS(rv, rv);
      64             : 
      65           0 :   return NS_OK;
      66             : }
      67             : 
      68             : ////////////////////////////////////////////////////////////////////////////////
      69             : //// nsIDownloadManager
      70             : 
      71             : NS_IMETHODIMP
      72           0 : nsDownloadManager::GetActivePrivateDownloadCount(int32_t* aResult)
      73             : {
      74           0 :   return NS_ERROR_UNEXPECTED;
      75             : }
      76             : 
      77             : NS_IMETHODIMP
      78           0 : nsDownloadManager::GetActiveDownloadCount(int32_t *aResult)
      79             : {
      80           0 :   return NS_ERROR_UNEXPECTED;
      81             : }
      82             : 
      83             : NS_IMETHODIMP
      84           0 : nsDownloadManager::GetActiveDownloads(nsISimpleEnumerator **aResult)
      85             : {
      86           0 :   return NS_ERROR_UNEXPECTED;
      87             : }
      88             : 
      89             : NS_IMETHODIMP
      90           0 : nsDownloadManager::GetActivePrivateDownloads(nsISimpleEnumerator **aResult)
      91             : {
      92           0 :   return NS_ERROR_UNEXPECTED;
      93             : }
      94             : 
      95             : /**
      96             :  * For platforms where helper apps use the downloads directory (i.e. mobile),
      97             :  * this should be kept in sync with nsExternalHelperAppService.cpp
      98             :  */
      99             : NS_IMETHODIMP
     100           0 : nsDownloadManager::GetDefaultDownloadsDirectory(nsIFile **aResult)
     101             : {
     102           0 :   nsCOMPtr<nsIFile> downloadDir;
     103             : 
     104             :   nsresult rv;
     105             :   nsCOMPtr<nsIProperties> dirService =
     106           0 :      do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv);
     107           0 :   NS_ENSURE_SUCCESS(rv, rv);
     108             : 
     109             :   // OSX 10.4:
     110             :   // Desktop
     111             :   // OSX 10.5:
     112             :   // User download directory
     113             :   // Vista:
     114             :   // Downloads
     115             :   // XP/2K:
     116             :   // My Documents/Downloads
     117             :   // Linux:
     118             :   // XDG user dir spec, with a fallback to Home/Downloads
     119             : 
     120           0 :   nsXPIDLString folderName;
     121           0 :   mBundle->GetStringFromName(u"downloadsFolder",
     122           0 :                              getter_Copies(folderName));
     123             : 
     124             : #if defined (XP_MACOSX)
     125             :   rv = dirService->Get(NS_OSX_DEFAULT_DOWNLOAD_DIR,
     126             :                        NS_GET_IID(nsIFile),
     127             :                        getter_AddRefs(downloadDir));
     128             :   NS_ENSURE_SUCCESS(rv, rv);
     129             : #elif defined(XP_WIN)
     130             :   rv = dirService->Get(NS_WIN_DEFAULT_DOWNLOAD_DIR,
     131             :                        NS_GET_IID(nsIFile),
     132             :                        getter_AddRefs(downloadDir));
     133             :   NS_ENSURE_SUCCESS(rv, rv);
     134             : 
     135             :   // Check the os version
     136             :   nsCOMPtr<nsIPropertyBag2> infoService =
     137             :      do_GetService(NS_SYSTEMINFO_CONTRACTID, &rv);
     138             :   NS_ENSURE_SUCCESS(rv, rv);
     139             : 
     140             :   int32_t version;
     141             :   NS_NAMED_LITERAL_STRING(osVersion, "version");
     142             :   rv = infoService->GetPropertyAsInt32(osVersion, &version);
     143             :   NS_ENSURE_SUCCESS(rv, rv);
     144             :   if (version < 6) { // XP/2K
     145             :     // First get "My Documents"
     146             :     rv = dirService->Get(NS_WIN_PERSONAL_DIR,
     147             :                          NS_GET_IID(nsIFile),
     148             :                          getter_AddRefs(downloadDir));
     149             :     NS_ENSURE_SUCCESS(rv, rv);
     150             : 
     151             :     rv = downloadDir->Append(folderName);
     152             :     NS_ENSURE_SUCCESS(rv, rv);
     153             : 
     154             :     // This could be the first time we are creating the downloads folder in My
     155             :     // Documents, so make sure it exists.
     156             :     bool exists;
     157             :     rv = downloadDir->Exists(&exists);
     158             :     NS_ENSURE_SUCCESS(rv, rv);
     159             :     if (!exists) {
     160             :       rv = downloadDir->Create(nsIFile::DIRECTORY_TYPE, 0755);
     161             :       NS_ENSURE_SUCCESS(rv, rv);
     162             :     }
     163             :   }
     164             : #elif defined(XP_UNIX)
     165             : #if defined(MOZ_WIDGET_ANDROID)
     166             :     // Android doesn't have a $HOME directory, and by default we only have
     167             :     // write access to /data/data/org.mozilla.{$APP} and /sdcard
     168             :     char* downloadDirPath = getenv("DOWNLOADS_DIRECTORY");
     169             :     if (downloadDirPath) {
     170             :       rv = NS_NewNativeLocalFile(nsDependentCString(downloadDirPath),
     171             :                                  true, getter_AddRefs(downloadDir));
     172             :       NS_ENSURE_SUCCESS(rv, rv);
     173             :     }
     174             :     else {
     175             :       rv = NS_ERROR_FAILURE;
     176             :     }
     177             : #else
     178           0 :   rv = dirService->Get(NS_UNIX_DEFAULT_DOWNLOAD_DIR,
     179             :                        NS_GET_IID(nsIFile),
     180           0 :                        getter_AddRefs(downloadDir));
     181             :   // fallback to Home/Downloads
     182           0 :   if (NS_FAILED(rv)) {
     183           0 :     rv = dirService->Get(NS_UNIX_HOME_DIR,
     184             :                          NS_GET_IID(nsIFile),
     185           0 :                          getter_AddRefs(downloadDir));
     186           0 :     NS_ENSURE_SUCCESS(rv, rv);
     187           0 :     rv = downloadDir->Append(folderName);
     188           0 :     NS_ENSURE_SUCCESS(rv, rv);
     189             :   }
     190             : #endif
     191             : #else
     192             :   rv = dirService->Get(NS_OS_HOME_DIR,
     193             :                        NS_GET_IID(nsIFile),
     194             :                        getter_AddRefs(downloadDir));
     195             :   NS_ENSURE_SUCCESS(rv, rv);
     196             :   rv = downloadDir->Append(folderName);
     197             :   NS_ENSURE_SUCCESS(rv, rv);
     198             : #endif
     199             : 
     200           0 :   downloadDir.forget(aResult);
     201             : 
     202           0 :   return NS_OK;
     203             : }
     204             : 
     205             : #define NS_BRANCH_DOWNLOAD     "browser.download."
     206             : #define NS_PREF_FOLDERLIST     "folderList"
     207             : #define NS_PREF_DIR            "dir"
     208             : 
     209             : NS_IMETHODIMP
     210           0 : nsDownloadManager::GetUserDownloadsDirectory(nsIFile **aResult)
     211             : {
     212             :   nsresult rv;
     213             :   nsCOMPtr<nsIProperties> dirService =
     214           0 :      do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv);
     215           0 :   NS_ENSURE_SUCCESS(rv, rv);
     216             : 
     217             :   nsCOMPtr<nsIPrefService> prefService =
     218           0 :      do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
     219           0 :   NS_ENSURE_SUCCESS(rv, rv);
     220             : 
     221           0 :   nsCOMPtr<nsIPrefBranch> prefBranch;
     222           0 :   rv = prefService->GetBranch(NS_BRANCH_DOWNLOAD,
     223           0 :                               getter_AddRefs(prefBranch));
     224           0 :   NS_ENSURE_SUCCESS(rv, rv);
     225             : 
     226             :   int32_t val;
     227           0 :   rv = prefBranch->GetIntPref(NS_PREF_FOLDERLIST,
     228           0 :                               &val);
     229           0 :   NS_ENSURE_SUCCESS(rv, rv);
     230             : 
     231           0 :   switch(val) {
     232             :     case 0: // Desktop
     233             :       {
     234           0 :         nsCOMPtr<nsIFile> downloadDir;
     235           0 :         rv = dirService->Get(NS_OS_DESKTOP_DIR,
     236             :                              NS_GET_IID(nsIFile),
     237           0 :                              getter_AddRefs(downloadDir));
     238           0 :         NS_ENSURE_SUCCESS(rv, rv);
     239           0 :         downloadDir.forget(aResult);
     240           0 :         return NS_OK;
     241             :       }
     242             :       break;
     243             :     case 1: // Downloads
     244           0 :       return GetDefaultDownloadsDirectory(aResult);
     245             :     case 2: // Custom
     246             :       {
     247           0 :         nsCOMPtr<nsIFile> customDirectory;
     248           0 :         prefBranch->GetComplexValue(NS_PREF_DIR,
     249             :                                     NS_GET_IID(nsIFile),
     250           0 :                                     getter_AddRefs(customDirectory));
     251           0 :         if (customDirectory) {
     252           0 :           bool exists = false;
     253           0 :           (void)customDirectory->Exists(&exists);
     254             : 
     255           0 :           if (!exists) {
     256           0 :             rv = customDirectory->Create(nsIFile::DIRECTORY_TYPE, 0755);
     257           0 :             if (NS_SUCCEEDED(rv)) {
     258           0 :               customDirectory.forget(aResult);
     259           0 :               return NS_OK;
     260             :             }
     261             : 
     262             :             // Create failed, so it still doesn't exist.  Fall out and get the
     263             :             // default downloads directory.
     264             :           }
     265             : 
     266           0 :           bool writable = false;
     267           0 :           bool directory = false;
     268           0 :           (void)customDirectory->IsWritable(&writable);
     269           0 :           (void)customDirectory->IsDirectory(&directory);
     270             : 
     271           0 :           if (exists && writable && directory) {
     272           0 :             customDirectory.forget(aResult);
     273           0 :             return NS_OK;
     274             :           }
     275             :         }
     276           0 :         rv = GetDefaultDownloadsDirectory(aResult);
     277           0 :         if (NS_SUCCEEDED(rv)) {
     278           0 :           (void)prefBranch->SetComplexValue(NS_PREF_DIR,
     279             :                                             NS_GET_IID(nsIFile),
     280           0 :                                             *aResult);
     281             :         }
     282           0 :         return rv;
     283             :       }
     284             :       break;
     285             :   }
     286           0 :   return NS_ERROR_INVALID_ARG;
     287             : }
     288             : 
     289             : NS_IMETHODIMP
     290           0 : nsDownloadManager::AddDownload(int16_t aDownloadType,
     291             :                                nsIURI *aSource,
     292             :                                nsIURI *aTarget,
     293             :                                const nsAString& aDisplayName,
     294             :                                nsIMIMEInfo *aMIMEInfo,
     295             :                                PRTime aStartTime,
     296             :                                nsIFile *aTempFile,
     297             :                                nsICancelable *aCancelable,
     298             :                                bool aIsPrivate,
     299             :                                nsIDownload **aDownload)
     300             : {
     301           0 :   return NS_ERROR_UNEXPECTED;
     302             : }
     303             : 
     304             : NS_IMETHODIMP
     305           0 : nsDownloadManager::GetDownload(uint32_t aID, nsIDownload **aDownloadItem)
     306             : {
     307           0 :   return NS_ERROR_UNEXPECTED;
     308             : }
     309             : 
     310             : NS_IMETHODIMP
     311           0 : nsDownloadManager::GetDownloadByGUID(const nsACString& aGUID,
     312             :                                      nsIDownloadManagerResult* aCallback)
     313             : {
     314           0 :   return NS_ERROR_UNEXPECTED;
     315             : }
     316             : 
     317             : NS_IMETHODIMP
     318           0 : nsDownloadManager::CancelDownload(uint32_t aID)
     319             : {
     320           0 :   return NS_ERROR_UNEXPECTED;
     321             : }
     322             : 
     323             : NS_IMETHODIMP
     324           0 : nsDownloadManager::RetryDownload(uint32_t aID)
     325             : {
     326           0 :   return NS_ERROR_UNEXPECTED;
     327             : }
     328             : 
     329             : NS_IMETHODIMP
     330           0 : nsDownloadManager::RemoveDownload(uint32_t aID)
     331             : {
     332           0 :   return NS_ERROR_UNEXPECTED;
     333             : }
     334             : 
     335             : NS_IMETHODIMP
     336           0 : nsDownloadManager::RemoveDownloadsByTimeframe(int64_t aStartTime,
     337             :                                               int64_t aEndTime)
     338             : {
     339           0 :   return NS_ERROR_UNEXPECTED;
     340             : }
     341             : 
     342             : NS_IMETHODIMP
     343           0 : nsDownloadManager::CleanUp()
     344             : {
     345           0 :   return NS_ERROR_UNEXPECTED;
     346             : }
     347             : 
     348             : NS_IMETHODIMP
     349           0 : nsDownloadManager::CleanUpPrivate()
     350             : {
     351           0 :   return NS_ERROR_UNEXPECTED;
     352             : }
     353             : 
     354             : NS_IMETHODIMP
     355           0 : nsDownloadManager::GetCanCleanUp(bool *aResult)
     356             : {
     357           0 :   return NS_ERROR_UNEXPECTED;
     358             : }
     359             : 
     360             : NS_IMETHODIMP
     361           0 : nsDownloadManager::GetCanCleanUpPrivate(bool *aResult)
     362             : {
     363           0 :   return NS_ERROR_UNEXPECTED;
     364             : }
     365             : 
     366             : NS_IMETHODIMP
     367           0 : nsDownloadManager::PauseDownload(uint32_t aID)
     368             : {
     369           0 :   return NS_ERROR_UNEXPECTED;
     370             : }
     371             : 
     372             : NS_IMETHODIMP
     373           0 : nsDownloadManager::ResumeDownload(uint32_t aID)
     374             : {
     375           0 :   return NS_ERROR_UNEXPECTED;
     376             : }
     377             : 
     378             : NS_IMETHODIMP
     379           0 : nsDownloadManager::GetDBConnection(mozIStorageConnection **aDBConn)
     380             : {
     381           0 :   return NS_ERROR_UNEXPECTED;
     382             : }
     383             : 
     384             : NS_IMETHODIMP
     385           0 : nsDownloadManager::GetPrivateDBConnection(mozIStorageConnection **aDBConn)
     386             : {
     387           0 :   return NS_ERROR_UNEXPECTED;
     388             : }
     389             : 
     390             : NS_IMETHODIMP
     391           0 : nsDownloadManager::AddListener(nsIDownloadProgressListener *aListener)
     392             : {
     393           0 :   return NS_ERROR_UNEXPECTED;
     394             : }
     395             : 
     396             : NS_IMETHODIMP
     397           0 : nsDownloadManager::AddPrivacyAwareListener(nsIDownloadProgressListener *aListener)
     398             : {
     399           0 :   return NS_ERROR_UNEXPECTED;
     400             : }
     401             : 
     402             : NS_IMETHODIMP
     403           0 : nsDownloadManager::RemoveListener(nsIDownloadProgressListener *aListener)
     404             : {
     405           0 :   return NS_ERROR_UNEXPECTED;
     406             : }

Generated by: LCOV version 1.13