From 32cc7daac6b232a8a199e3ecee4b474dd4e7f00f Mon Sep 17 00:00:00 2001 From: Dave Hylands Date: Tue, 14 Oct 2014 13:50:57 -0700 Subject: [PATCH] Bug 1072535 - Pt 1 - Have DownloadDone notify device storage when a new download is available. r=paolo --- .../jsdownloads/src/DownloadPlatform.cpp | 19 ++++++++++++++++++- .../test/unit/common_test_Download.js | 19 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/toolkit/components/jsdownloads/src/DownloadPlatform.cpp b/toolkit/components/jsdownloads/src/DownloadPlatform.cpp index 88c949636367..5f1ccd08f441 100644 --- a/toolkit/components/jsdownloads/src/DownloadPlatform.cpp +++ b/toolkit/components/jsdownloads/src/DownloadPlatform.cpp @@ -7,9 +7,12 @@ #include "nsString.h" #include "nsIURI.h" #include "nsIFile.h" +#include "nsIObserverService.h" +#include "nsISupportsPrimitives.h" #include "nsDirectoryServiceDefs.h" #include "mozilla/Preferences.h" +#include "mozilla/Services.h" #define PREF_BDM_ADDTORECENTDOCS "browser.download.manager.addToRecentDocs" @@ -69,7 +72,9 @@ static void gio_set_metadata_done(GObject *source_obj, GAsyncResult *res, gpoint nsresult DownloadPlatform::DownloadDone(nsIURI* aSource, nsIFile* aTarget, const nsACString& aContentType, bool aIsPrivate) { -#if defined(XP_WIN) || defined(XP_MACOSX) || defined(MOZ_WIDGET_ANDROID) || defined(MOZ_WIDGET_GTK) +#if defined(XP_WIN) || defined(XP_MACOSX) || defined(MOZ_WIDGET_ANDROID) \ + || defined(MOZ_WIDGET_GTK) || defined(MOZ_WIDGET_GONK) + nsAutoString path; if (aTarget && NS_SUCCEEDED(aTarget->GetPath(path))) { #if defined(XP_WIN) || defined(MOZ_WIDGET_GTK) || defined(MOZ_WIDGET_ANDROID) @@ -120,6 +125,18 @@ nsresult DownloadPlatform::DownloadDone(nsIURI* aSource, nsIFile* aTarget, observedObject, nullptr, TRUE); ::CFRelease(observedObject); #endif + if (mozilla::Preferences::GetBool("device.storage.enabled", true)) { + // Tell DeviceStorage that a new file may have been added. + nsCOMPtr obs = mozilla::services::GetObserverService(); + nsCOMPtr pathString + = do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID); + if (obs && pathString) { + if (NS_SUCCEEDED(pathString->SetData(path))) { + (void)obs->NotifyObservers(pathString, "download-watcher-notify", + MOZ_UTF16("modified")); + } + } + } } #ifdef XP_WIN diff --git a/toolkit/components/jsdownloads/test/unit/common_test_Download.js b/toolkit/components/jsdownloads/test/unit/common_test_Download.js index ee75367977f5..9cad569ed47b 100644 --- a/toolkit/components/jsdownloads/test/unit/common_test_Download.js +++ b/toolkit/components/jsdownloads/test/unit/common_test_Download.js @@ -1733,10 +1733,28 @@ add_task(function test_toSerializable_startTime() add_task(function test_platform_integration() { let downloadFiles = []; + let oldDeviceStorageEnabled = false; + try { + oldDeviceStorageEnabled = Services.prefs.getBoolPref("device.storage.enabled"); + } catch (e) { + // This happens if the pref doesn't exist. + } + let downloadWatcherNotified = false; + let observer = { + observe: function(subject, topic, data) { + do_check_eq(topic, "download-watcher-notify"); + do_check_eq(data, "modified"); + downloadWatcherNotified = true; + } + } + Services.obs.addObserver(observer, "download-watcher-notify", false); + Services.prefs.setBoolPref("device.storage.enabled", true); function cleanup() { for (let file of downloadFiles) { file.remove(true); } + Services.obs.removeObserver(observer, "download-watcher-notify"); + Services.prefs.setBoolPref("device.storage.enabled", oldDeviceStorageEnabled); } do_register_cleanup(cleanup); @@ -1770,6 +1788,7 @@ add_task(function test_platform_integration() // downloadDone should be called before the whenSucceeded promise is resolved. yield download.whenSucceeded().then(function () { do_check_true(DownloadIntegration.downloadDoneCalled); + do_check_true(downloadWatcherNotified); }); // Then, wait for the promise returned by "start" to be resolved.