From a9f42915f450d73ea08dfd61c1c93888781ddac1 Mon Sep 17 00:00:00 2001 From: "brettw%gmail.com" Date: Mon, 21 Nov 2005 21:21:40 +0000 Subject: [PATCH] Bug 266174, serialize/deserialize, r=bryner --- .../components/places/src/nsNavBookmarks.cpp | 7 ++- .../components/places/src/nsNavHistory.cpp | 44 ++++++++++++- browser/components/places/src/nsNavHistory.h | 33 +++++++--- .../places/src/nsNavHistoryResult.cpp | 62 ++++++++++++++++++- 4 files changed, 132 insertions(+), 14 deletions(-) diff --git a/browser/components/places/src/nsNavBookmarks.cpp b/browser/components/places/src/nsNavBookmarks.cpp index e8b291753dbc..9cdc76a278d1 100644 --- a/browser/components/places/src/nsNavBookmarks.cpp +++ b/browser/components/places/src/nsNavBookmarks.cpp @@ -638,8 +638,8 @@ nsNavBookmarks::GetBookmarks(nsINavHistoryResult **aResult) { *aResult = nsnull; - nsNavHistory *history = History(); - nsRefPtr result(History()->NewHistoryResult()); + nsRefPtr result( + History()->NewHistoryResult(nsnull, 0, nsnull)); NS_ENSURE_TRUE(result, NS_ERROR_OUT_OF_MEMORY); nsresult rv = result->Init(); @@ -665,7 +665,8 @@ nsNavBookmarks::GetFolderChildren(PRInt64 aFolder, PRUint32 aOptions, { *aChildren = nsnull; - nsRefPtr result = History()->NewHistoryResult(); + nsRefPtr result( + History()->NewHistoryResult(nsnull, 0, nsnull)); NS_ENSURE_TRUE(result, NS_ERROR_OUT_OF_MEMORY); result->SetBookmarkOptions(aOptions); diff --git a/browser/components/places/src/nsNavHistory.cpp b/browser/components/places/src/nsNavHistory.cpp index ed26e2b562ee..a139f8427f1e 100644 --- a/browser/components/places/src/nsNavHistory.cpp +++ b/browser/components/places/src/nsNavHistory.cpp @@ -1062,7 +1062,8 @@ nsNavHistory::ExecuteQueries(const nsINavHistoryQuery** aQueries, PRUint32 aQuer PRInt32 numParameters = 0; nsCAutoString conditions; - for (PRUint32 i = 0; i < aQueryCount; i ++) { + PRUint32 i; + for (i = 0; i < aQueryCount; i ++) { nsCString queryClause; PRInt32 clauseParameters = 0; rv = QueryToSelectClause(NS_CONST_CAST(nsINavHistoryQuery*, aQueries[i]), @@ -1139,7 +1140,8 @@ nsNavHistory::ExecuteQueries(const nsINavHistoryQuery** aQueries, PRUint32 aQuer } // run query and put into result object - nsRefPtr result(new nsNavHistoryResult(this, mBundle)); + nsRefPtr result( + NewHistoryResult(aQueries, aQueryCount, aOptions)); if (! result) return NS_ERROR_OUT_OF_MEMORY; rv = result->Init(); @@ -3380,6 +3382,15 @@ NS_IMETHODIMP nsNavHistoryQuery::GetHasDomain(PRBool* _retval) return NS_OK; } +NS_IMETHODIMP nsNavHistoryQuery::Clone(nsINavHistoryQuery** _retval) +{ + *_retval = new nsNavHistoryQuery(*this); + if (! (*_retval)) + return NS_ERROR_OUT_OF_MEMORY; + NS_ADDREF(*_retval); + return NS_OK; +} + // nsNavHistoryQueryOptions NS_IMPL_ISUPPORTS2(nsNavHistoryQueryOptions, nsNavHistoryQueryOptions, nsINavHistoryQueryOptions) @@ -3422,3 +3433,32 @@ nsNavHistoryQueryOptions::SetExpandPlaces(PRBool aExpand) mExpandPlaces = aExpand; return NS_OK; } + +NS_IMETHODIMP +nsNavHistoryQueryOptions::Clone(nsINavHistoryQueryOptions** aResult) +{ + *aResult = nsnull; + nsNavHistoryQueryOptions* result = new nsNavHistoryQueryOptions(); + if (! result) + return NS_ERROR_OUT_OF_MEMORY; + + result->mSort = mSort; + result->mResultType = mResultType; + result->mGroupCount = mGroupCount; + if (mGroupCount) { + result->mGroupings = new PRInt32[mGroupCount]; + if (! result->mGroupings) { + delete result; + return NS_ERROR_OUT_OF_MEMORY; + } + for (PRUint32 i = 0; i < mGroupCount; i ++) + result->mGroupings[i] = mGroupings[i]; + } else { + result->mGroupCount = nsnull; + } + result->mExpandPlaces = mExpandPlaces; + + *aResult = result; + NS_ADDREF(*aResult); + return NS_OK; +} diff --git a/browser/components/places/src/nsNavHistory.h b/browser/components/places/src/nsNavHistory.h index df1c7fde75ea..0eaafc83d13a 100644 --- a/browser/components/places/src/nsNavHistory.h +++ b/browser/components/places/src/nsNavHistory.h @@ -85,6 +85,7 @@ class nsNavHistoryQuery : public nsINavHistoryQuery { public: nsNavHistoryQuery(); + // note: we use a copy constructor in Clone(), the default is good enough NS_DECL_ISUPPORTS NS_DECL_NSINAVHISTORYQUERY @@ -109,11 +110,16 @@ protected: // nsNavHistoryResultNode +#define NS_NAVHISTORYRESULTNODE_IID \ +{0x54b61d38, 0x57c1, 0x11da, {0x95, 0xb8, 0x00, 0x13, 0x21, 0xc9, 0xf6, 0x9e}} + class nsNavHistoryResultNode : public nsINavHistoryResultNode { public: nsNavHistoryResultNode(); + NS_DECLARE_STATIC_IID_ACCESSOR(NS_NAVHISTORYRESULTNODE_IID) + NS_DECL_ISUPPORTS NS_DECL_NSINAVHISTORYRESULTNODE @@ -176,7 +182,10 @@ class nsNavHistoryResult : public nsINavHistoryResult, { public: nsNavHistoryResult(nsNavHistory* aHistoryService, - nsIStringBundle* aHistoryBundle); + nsIStringBundle* aHistoryBundle, + const nsINavHistoryQuery** aQueries, + PRUint32 aQueryCount, + nsINavHistoryQueryOptions *aOptions); // Two-stage init, MUST BE CALLED BEFORE ANYTHING ELSE nsresult Init(); @@ -202,13 +211,14 @@ protected: nsCOMPtr mTree; // may be null if no tree has bound itself nsCOMPtr mSelection; // may be null - // This is a COM ptr that MUST BE AddRef'ed and Release'd MANUALLY. - // nsNavHistory has nsISupports as an ambiguous base class, so nsCOMPtr - // won't work. nsRefPtr mHistoryService; PRBool mCollapseDuplicates; + // what generated this result set + nsCOMArray mSourceQueries; + nsCOMPtr mSourceOptions; + // for locale-specific date formatting and string sorting nsCOMPtr mLocale; nsCOMPtr mCollation; @@ -296,7 +306,7 @@ public: mGroupCount(0), mGroupings(nsnull), mExpandPlaces(PR_FALSE) { } - NS_DECLARE_STATIC_IID_ACCESSOR(NS_INAVHISTORYQUERYOPTIONS_IID) + NS_DECLARE_STATIC_IID_ACCESSOR(NS_NAVHISTORYQUERYOPTIONS_IID) NS_DECL_ISUPPORTS NS_DECL_NSINAVHISTORYQUERYOPTIONS @@ -309,6 +319,8 @@ public: PRBool ExpandPlaces() const { return mExpandPlaces; } private: + nsNavHistoryQueryOptions(const nsNavHistoryQueryOptions& other) {} // no copy + ~nsNavHistoryQueryOptions() { delete[] mGroupings; } PRInt32 mSort; @@ -396,9 +408,14 @@ public: nsresult RowToResult(mozIStorageValueArray* aRow, PRBool aAsVisits, nsNavHistoryResultNode** aResult); - // Construct a new HistoryResult object. - nsNavHistoryResult* NewHistoryResult() - { return new nsNavHistoryResult(this, mBundle); } + // Construct a new HistoryResult object. You can give it null query/options. + nsNavHistoryResult* NewHistoryResult(const nsINavHistoryQuery** aQueries, + PRUint32 aQueryCount, + nsINavHistoryQueryOptions* aOptions) + { + return new nsNavHistoryResult(this, mBundle, aQueries, aQueryCount, + aOptions); + } private: ~nsNavHistory(); diff --git a/browser/components/places/src/nsNavHistoryResult.cpp b/browser/components/places/src/nsNavHistoryResult.cpp index be39c4e28a03..50c17301a529 100755 --- a/browser/components/places/src/nsNavHistoryResult.cpp +++ b/browser/components/places/src/nsNavHistoryResult.cpp @@ -173,13 +173,27 @@ NS_IMPL_ISUPPORTS2(nsNavHistoryResult, // nsNavHistoryResult::nsNavHistoryResult nsNavHistoryResult::nsNavHistoryResult(nsNavHistory* aHistoryService, - nsIStringBundle* aHistoryBundle) + nsIStringBundle* aHistoryBundle, + const nsINavHistoryQuery** aQueries, + PRUint32 aQueryCount, + nsINavHistoryQueryOptions* aOptions) : mBundle(aHistoryBundle), mHistoryService(aHistoryService), mCollapseDuplicates(PR_TRUE), mTimesIncludeDates(PR_TRUE), mCurrentSort(nsINavHistoryQueryOptions::SORT_BY_NONE), mBookmarkOptions(nsINavBookmarksService::ALL_CHILDREN) { + // Fill saved source queries with copies of the original (the caller might + // change their original objects, and we always want to reflect the source + // parameters). + for (PRUint32 i = 0; i < aQueryCount; i ++) { + nsINavHistoryQuery* query = NS_CONST_CAST(nsINavHistoryQuery*, aQueries[i]); + nsCOMPtr queryClone; + if (NS_SUCCEEDED(query->Clone(getter_AddRefs(queryClone)))) + mSourceQueries.AppendObject(queryClone); + } + if (aOptions) + aOptions->Clone(getter_AddRefs(mSourceOptions)); } // nsNavHistoryResult::~nsNavHistoryResult @@ -489,6 +503,52 @@ nsNavHistoryResult::NodeForTreeIndex(PRUint32 index, } +// nsNavHistoryResult::TreeIndexForNode + +NS_IMETHODIMP +nsNavHistoryResult::TreeIndexForNode(nsINavHistoryResultNode* aNode, + PRUint32* aResult) +{ + nsresult rv; + nsCOMPtr node = do_QueryInterface(aNode, &rv); + NS_ENSURE_SUCCESS(rv, rv); + + if (node->mVisibleIndex < 0) + *aResult = nsINavHistoryResult::INDEX_INVISIBLE; + else + *aResult = node->mVisibleIndex; + return NS_OK; +} + + +// nsNavHistoryResult::GetSourceQuery + +NS_IMETHODIMP +nsNavHistoryResult::GetSourceQuery(nsINavHistoryQuery*** aQueries, + PRUint32* aQueryCount, + nsINavHistoryQueryOptions** aOptions) +{ + *aQueries = nsnull; + *aQueryCount = 0; + if (mSourceQueries.Count() > 0) { + *aQueries = NS_STATIC_CAST(nsINavHistoryQuery**, + nsMemory::Alloc(sizeof(nsINavHistoryQuery*) * mSourceQueries.Count())); + if (! *aQueries) + return NS_ERROR_OUT_OF_MEMORY; + *aQueryCount = mSourceQueries.Count(); + + for (PRInt32 i = 0; i < mSourceQueries.Count(); i ++) { + (*aQueries)[i] = mSourceQueries[i]; + NS_ADDREF((*aQueries)[i]); + } + } + + *aOptions = mSourceOptions; + NS_IF_ADDREF(*aOptions); + return NS_OK; +} + + // nsNavHistoryResult::SetTreeSortingIndicator // // This is called to assign the correct arrow to the column header. It is