Bug 591289 - Save chosen download file name and other metadata in Places history [r=sdwilsh, ui-r=limi]

This commit is contained in:
Paolo Amadini 2011-08-15 18:08:48 -07:00
parent 00f450ae6c
commit f42744f84d
7 changed files with 181 additions and 47 deletions

View File

@ -58,7 +58,8 @@ NS_IMPL_ISUPPORTS1(nsDownloadHistory, nsIDownloadHistory)
NS_IMETHODIMP
nsDownloadHistory::AddDownload(nsIURI *aSource,
nsIURI *aReferrer,
PRTime aStartTime)
PRTime aStartTime,
nsIURI *aDestination)
{
NS_ENSURE_ARG_POINTER(aSource);

View File

@ -46,7 +46,7 @@ interface nsIURI;
* interface specifically for downloads in case embedders choose to track
* downloads differently from other types of history.
*/
[scriptable, uuid(202533cd-a8f1-4ee4-8d20-3a6a0d2c6c51)]
[scriptable, uuid(a7a3358c-9af2-41e3-adfe-3bf0b7ac2c38)]
interface nsIDownloadHistory : nsISupports {
/**
* Adds a download to history. This will also notify observers that the
@ -61,12 +61,16 @@ interface nsIDownloadHistory : nsISupports {
* @param aStartTime
* [optional] The time the download was started. If the start time
* is not given, the current time is used.
* @param aDestination
* [optional] The target where the download is to be saved on the local
* filesystem.
* @throws NS_ERROR_NOT_AVAILABLE
* In a situation where a history implementation is not available,
* where 'history implementation' refers to something like
* nsIGlobalHistory and friends.
*/
void addDownload(in nsIURI aSource, [optional] in nsIURI aReferrer,
[optional] in PRTime aStartTime);
[optional] in PRTime aStartTime,
[optional] in nsIURI aDestination);
};

View File

@ -2387,7 +2387,7 @@ nsDownload::OnProgressChange64(nsIWebProgress *aWebProgress,
nsCOMPtr<nsIDownloadHistory> dh =
do_GetService(NS_DOWNLOADHISTORY_CONTRACTID);
if (dh)
(void)dh->AddDownload(mSource, mReferrer, mStartTime);
(void)dh->AddDownload(mSource, mReferrer, mStartTime, mTarget);
}
// Fetch the entityID, but if we can't get it, don't panic (non-resumable)

View File

@ -29,6 +29,7 @@
* Ehsan Akhgari <ehsan.akhgari@gmail.com>
* Drew Willcoxon <adw@mozilla.com>
* Philipp von Weitershausen <philipp@weitershausen.de>
* Paolo Amadini <http://www.amadzone.org/>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
@ -170,6 +171,14 @@ static const PRInt64 USECS_PER_DAY = LL_INIT(20, 500654080);
// Sync guid annotation
#define SYNCGUID_ANNO NS_LITERAL_CSTRING("sync/guid")
// Download destination file URI annotation
#define DESTINATIONFILEURI_ANNO \
NS_LITERAL_CSTRING("downloads/destinationFileURI")
// Download destination file name annotation
#define DESTINATIONFILENAME_ANNO \
NS_LITERAL_CSTRING("downloads/destinationFileName")
// These macros are used when splitting history by date.
// These are the day containers and catch-all final container.
#define HISTORY_ADDITIONAL_DATE_CONT_NUM 3
@ -5216,7 +5225,7 @@ nsNavHistory::OnEndVacuum(PRBool aSucceeded)
NS_IMETHODIMP
nsNavHistory::AddDownload(nsIURI* aSource, nsIURI* aReferrer,
PRTime aStartTime)
PRTime aStartTime, nsIURI* aDestination)
{
NS_ASSERTION(NS_IsMainThread(), "This can only be called on the main thread");
NS_ENSURE_ARG(aSource);
@ -5226,8 +5235,62 @@ nsNavHistory::AddDownload(nsIURI* aSource, nsIURI* aReferrer,
return NS_OK;
PRInt64 visitID;
return AddVisit(aSource, aStartTime, aReferrer, TRANSITION_DOWNLOAD, PR_FALSE,
0, &visitID);
nsresult rv = AddVisit(aSource, aStartTime, aReferrer, TRANSITION_DOWNLOAD,
PR_FALSE, 0, &visitID);
NS_ENSURE_SUCCESS(rv, rv);
if (!aDestination) {
return NS_OK;
}
// Exit silently if the download destination is not a local file.
nsCOMPtr<nsIFileURL> destinationFileURL = do_QueryInterface(aDestination);
if (!destinationFileURL) {
return NS_OK;
}
nsCOMPtr<nsIFile> destinationFile;
rv = destinationFileURL->GetFile(getter_AddRefs(destinationFile));
NS_ENSURE_SUCCESS(rv, rv);
nsAutoString destinationFileName;
rv = destinationFile->GetLeafName(destinationFileName);
NS_ENSURE_SUCCESS(rv, rv);
nsCAutoString destinationURISpec;
rv = destinationFileURL->GetSpec(destinationURISpec);
NS_ENSURE_SUCCESS(rv, rv);
// Use annotations for storing the additional download metadata.
nsAnnotationService* annosvc = nsAnnotationService::GetAnnotationService();
NS_ENSURE_TRUE(annosvc, NS_ERROR_OUT_OF_MEMORY);
(void)annosvc->SetPageAnnotationString(
aSource,
DESTINATIONFILEURI_ANNO,
NS_ConvertUTF8toUTF16(destinationURISpec),
0,
nsIAnnotationService::EXPIRE_WITH_HISTORY
);
(void)annosvc->SetPageAnnotationString(
aSource,
DESTINATIONFILENAME_ANNO,
destinationFileName,
0,
nsIAnnotationService::EXPIRE_WITH_HISTORY
);
// In case we are downloading a file that does not correspond to a web
// page for which the title is present, we populate the otherwise empty
// history title with the name of the destination file, to allow it to be
// visible and searchable in history results.
nsAutoString title;
if (NS_SUCCEEDED(GetPageTitle(aSource, title)) && title.IsEmpty()) {
(void)SetPageTitle(aSource, destinationFileName);
}
return NS_OK;
}

View File

@ -65,6 +65,11 @@ XPCOMUtils.defineLazyGetter(this, "NetUtil", function() {
return NetUtil;
});
XPCOMUtils.defineLazyGetter(this, "FileUtils", function() {
Cu.import("resource://gre/modules/FileUtils.jsm");
return FileUtils;
});
XPCOMUtils.defineLazyGetter(this, "PlacesUtils", function() {
Cu.import("resource://gre/modules/PlacesUtils.jsm");
return PlacesUtils;

View File

@ -1,42 +1,10 @@
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et: */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Mozilla Corporation.
* Portions created by the Initial Developer are Copyright (C) 2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Shawn Wilsher <me@shawnwilsher.com> (Original Author)
* Marco Bonardo <mak77@bonardo.net>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
/**
* The first part of this file runs a series of tests for the synchronous
* behavior of the nsIDownloadHistory::AddDownload function.
*/
// Get services
var histsvc = Cc["@mozilla.org/browser/nav-history-service;1"].
@ -159,4 +127,93 @@ function run_test() {
}
os.removeObserver(observer, NS_LINK_VISITED_EVENT_TOPIC);
// Asynchronous part of the test.
test_dh_details();
}
/**
* The second part of this file tests that nsIDownloadHistory::AddDownload saves
* the additional download details if the optional destination URL is specified.
*/
function test_dh_details()
{
do_test_pending();
const SOURCE_URI = uri("http://example.com/test_download_history_details");
const DEST_FILE_NAME = "dest.txt";
// We must build a real, valid file URI for the destination.
let destFileUri = NetUtil.newURI(FileUtils.getFile("TmpD", [DEST_FILE_NAME]));
let titleSet = false;
let destinationFileUriSet = false;
let destinationFileNameSet = false;
function checkFinished()
{
if (titleSet && destinationFileUriSet && destinationFileNameSet) {
PlacesUtils.annotations.removeObserver(annoObserver);
PlacesUtils.history.removeObserver(historyObserver);
// Cleanup.
bh.removeAllPages();
do_test_finished();
}
};
let annoObserver = {
onPageAnnotationSet: function AO_onPageAnnotationSet(aPage, aName)
{
if (aPage.equals(SOURCE_URI)) {
let value = PlacesUtils.annotations.getPageAnnotation(aPage, aName);
switch (aName)
{
case "downloads/destinationFileURI":
destinationFileUriSet = true;
do_check_eq(value, destFileUri.spec);
break;
case "downloads/destinationFileName":
destinationFileNameSet = true;
do_check_eq(value, DEST_FILE_NAME);
break;
}
checkFinished();
}
},
onItemAnnotationSet: function() {},
onPageAnnotationRemoved: function() {},
onItemAnnotationRemoved: function() {}
}
let historyObserver = {
onBeginUpdateBatch: function() {},
onEndUpdateBatch: function() {},
onVisit: function() {},
onTitleChanged: function HO_onTitleChanged(aURI, aPageTitle)
{
if (aURI.equals(SOURCE_URI)) {
titleSet = true;
do_check_eq(aPageTitle, DEST_FILE_NAME);
checkFinished();
}
},
onBeforeDeleteURI: function() {},
onDeleteURI: function() {},
onClearHistory: function() {},
onPageChanged: function() {},
onDeleteVisits: function() {}
};
PlacesUtils.annotations.addObserver(annoObserver, false);
PlacesUtils.history.addObserver(historyObserver, false);
// Both null values and remote URIs should not cause errors.
dh.addDownload(SOURCE_URI, null, Date.now() * 1000);
dh.addDownload(SOURCE_URI, null, Date.now() * 1000, null);
dh.addDownload(SOURCE_URI, null, Date.now() * 1000, uri("http://localhost/"));
// Valid local file URIs should cause the download details to be saved.
dh.addDownload(SOURCE_URI, null, Date.now() * 1000, destFileUri);
}

View File

@ -1777,7 +1777,11 @@ NS_IMETHODIMP nsExternalAppHandler::OnStartRequest(nsIRequest *request, nsISuppo
nsCOMPtr<nsIURI> referrer;
if (aChannel)
NS_GetReferrerFromChannel(aChannel, getter_AddRefs(referrer));
dh->AddDownload(mSourceUrl, referrer, mTimeDownloadStarted);
nsCOMPtr<nsIURI> target;
NS_NewFileURI(getter_AddRefs(target), mFinalFileDestination);
dh->AddDownload(mSourceUrl, referrer, mTimeDownloadStarted, target);
}
return NS_OK;