mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-27 12:15:33 +00:00
Bug 329698 r=joe@retrovirus.com,bryner Implement GetPageAnnotationNames
This commit is contained in:
parent
7f852eb898
commit
db969df09c
@ -38,9 +38,16 @@
|
||||
|
||||
#include "nsISupports.idl"
|
||||
|
||||
%{C++
|
||||
#include "nsTArray.h"
|
||||
#include "nsString.h"
|
||||
%}
|
||||
|
||||
interface nsIURI;
|
||||
interface nsIVariant;
|
||||
|
||||
[ptr] native CStringArray(nsTArray<nsCString>);
|
||||
|
||||
[scriptable, uuid(4ff680af-643e-4144-809f-dc53f71d7def)]
|
||||
interface nsIAnnotationObserver : nsISupports
|
||||
{
|
||||
@ -58,7 +65,7 @@ interface nsIAnnotationObserver : nsISupports
|
||||
void onAnnotationRemoved(in nsIURI aURI, in AUTF8String aName);
|
||||
};
|
||||
|
||||
[scriptable, uuid(05537263-9bb4-45c8-ae96-461817f53972)]
|
||||
[scriptable, uuid(6c170b7c-b4d4-4068-8c5b-53827b10557c)]
|
||||
interface nsIAnnotationService : nsISupports
|
||||
{
|
||||
/**
|
||||
@ -227,30 +234,17 @@ interface nsIAnnotationService : nsISupports
|
||||
* Get the names of all annotations for this URI.
|
||||
*
|
||||
* example JS:
|
||||
* var annotations = annotator.getAnnotations(myURI, {});
|
||||
* var annotations = annotator.getPageAnnotations(myURI, {});
|
||||
* You probably don't want to use this from C++, use
|
||||
* GetPageAnnotationsTArray instead.
|
||||
*/
|
||||
// IMPLEMENT ME
|
||||
//void getAnnotations(in AString aURI, out unsigned PRInt32 count,
|
||||
// [retval, array, size_is(count)] out wstring result);
|
||||
void getPageAnnotationNames(in nsIURI aURI, out PRUint32 count,
|
||||
[retval, array, size_is(count)] out nsIVariant result);
|
||||
|
||||
/**
|
||||
* Get the values of several annotations with arbitrary URI/name pairs.
|
||||
* There is some latency associated with each annotation query, so it is
|
||||
* a good idea to use this function if it is possible for you to batch
|
||||
* your requests together.
|
||||
*
|
||||
* This will return an array with the same number of values you requested.
|
||||
* If the requested URI/name pair does not exist, the corresponding result
|
||||
* element will be NULL.
|
||||
*
|
||||
* @param aURIList The list of URIs
|
||||
* TArray version of getPageAnnotationNames for ease of use in C++ code.
|
||||
*/
|
||||
/* IMPLEMENT ME?
|
||||
void getMultipleAnnotations([array, size_is(aCount)] in nsIURI aURIList,
|
||||
[array, size_is(aCount)] in wstring aNameList, in unsigned PRInt32 aCount,
|
||||
out unsigned PRInt32 aResultCount,
|
||||
[retval, array, size_is(aResultCount)] out wstring aResultList);
|
||||
*/
|
||||
[noscript] void GetPageAnnotationNamesTArray(in nsIURI aURI, in CStringArray aResult);
|
||||
|
||||
/**
|
||||
* Test for annotation existance.
|
||||
@ -268,7 +262,7 @@ interface nsIAnnotationService : nsISupports
|
||||
* We may want some other similar functions to get annotations with given
|
||||
* flags (once we have flags defined).
|
||||
*/
|
||||
//void removePageAnnotations(in nsIURI aURI);
|
||||
void removePageAnnotations(in nsIURI aURI);
|
||||
|
||||
|
||||
/**
|
||||
|
@ -47,6 +47,7 @@
|
||||
#include "nsIServiceManager.h"
|
||||
#include "nsIVariant.h"
|
||||
#include "nsString.h"
|
||||
#include "nsVariant.h"
|
||||
|
||||
const PRInt32 nsAnnotationService::kAnnoIndex_ID = 0;
|
||||
const PRInt32 nsAnnotationService::kAnnoIndex_Page = 1;
|
||||
@ -102,6 +103,9 @@ nsAnnotationService::Init()
|
||||
rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING("SELECT * FROM moz_anno WHERE page = ?1 AND name = ?2"),
|
||||
getter_AddRefs(mDBGetAnnotation));
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING("SELECT name FROM moz_anno WHERE page = ?1"),
|
||||
getter_AddRefs(mDBGetAnnotationNames));
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING("SELECT a.anno_id, a.page, a.name, a.mime_type, a.content, a.flags, a.expiration FROM moz_history h JOIN moz_anno a ON h.id = a.page WHERE h.url = ?1 AND a.name = ?2"),
|
||||
getter_AddRefs(mDBGetAnnotationFromURI));
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
@ -443,6 +447,79 @@ nsAnnotationService::GetPagesWithAnnotation(const nsACString& aName,
|
||||
}
|
||||
|
||||
|
||||
// nsAnnotationService::GetPageAnnotationNames
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsAnnotationService::GetPageAnnotationNames(nsIURI* aURI, PRUint32* aCount,
|
||||
nsIVariant*** _result)
|
||||
{
|
||||
*aCount = 0;
|
||||
*_result = nsnull;
|
||||
|
||||
nsTArray<nsCString> names;
|
||||
nsresult rv = GetPageAnnotationNamesTArray(aURI, &names);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
if (names.Length() == 0)
|
||||
return NS_OK;
|
||||
|
||||
*_result = NS_STATIC_CAST(nsIVariant**,
|
||||
nsMemory::Alloc(sizeof(nsIVariant*) * names.Length()));
|
||||
NS_ENSURE_TRUE(*_result, NS_ERROR_OUT_OF_MEMORY);
|
||||
|
||||
for (PRUint32 i = 0; i < names.Length(); i ++) {
|
||||
nsCOMPtr<nsIWritableVariant> var = new nsVariant;
|
||||
if (! var) {
|
||||
// need to release all the variants we've already created
|
||||
for (PRUint32 j = 0; j < i; j ++)
|
||||
NS_RELEASE((*_result)[j]);
|
||||
nsMemory::Free(*_result);
|
||||
*_result = nsnull;
|
||||
return rv;
|
||||
}
|
||||
var->SetAsAUTF8String(names[i]);
|
||||
NS_ADDREF((*_result)[i] = var);
|
||||
}
|
||||
*aCount = names.Length();
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
||||
// nsAnnotationService::GetPageAnnotationNamesTArray
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsAnnotationService::GetPageAnnotationNamesTArray(nsIURI* aURI,
|
||||
nsTArray<nsCString>* aResult)
|
||||
{
|
||||
aResult->Clear();
|
||||
|
||||
nsNavHistory* history = nsNavHistory::GetHistoryService();
|
||||
NS_ENSURE_TRUE(history, NS_ERROR_FAILURE);
|
||||
|
||||
PRInt64 uriID;
|
||||
nsresult rv = history->GetUrlIdFor(aURI, &uriID, PR_FALSE);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
if (uriID == 0) // Check if URI exists.
|
||||
return NS_OK;
|
||||
|
||||
mozStorageStatementScoper scoper(mDBGetAnnotationNames);
|
||||
rv = mDBGetAnnotationNames->BindInt64Parameter(0, uriID);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
PRBool hasResult;
|
||||
nsCAutoString name;
|
||||
while (NS_SUCCEEDED(mDBGetAnnotationNames->ExecuteStep(&hasResult)) &&
|
||||
hasResult) {
|
||||
rv = mDBGetAnnotationNames->GetUTF8String(0, name);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
if (! aResult->AppendElement(name))
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
||||
// nsAnnotationService::HasAnnotation
|
||||
|
||||
NS_IMETHODIMP
|
||||
@ -477,7 +554,7 @@ nsAnnotationService::RemoveAnnotation(nsIURI* aURI,
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
if (uriID == 0) // Check if URI exists.
|
||||
return NS_OK;
|
||||
|
||||
|
||||
mozStorageStatementScoper resetter(mDBRemoveAnnotation);
|
||||
|
||||
rv = mDBRemoveAnnotation->BindInt64Parameter(0, uriID);
|
||||
@ -490,7 +567,7 @@ nsAnnotationService::RemoveAnnotation(nsIURI* aURI,
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
resetter.Abandon();
|
||||
|
||||
|
||||
// Update observers
|
||||
for (PRInt32 i = 0; i < mObservers.Count(); i ++)
|
||||
mObservers[i]->OnAnnotationRemoved(aURI, aName);
|
||||
@ -499,6 +576,43 @@ nsAnnotationService::RemoveAnnotation(nsIURI* aURI,
|
||||
}
|
||||
|
||||
|
||||
// nsAnnotationSerivce::RemovePageAnnotations
|
||||
//
|
||||
// I don't believe this is a common operation, so am not using a precompiled
|
||||
// statement. If this ends up being used a lot, the statement should be
|
||||
// precompiled and added to the class.
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsAnnotationService::RemovePageAnnotations(nsIURI* aURI)
|
||||
{
|
||||
nsresult rv;
|
||||
nsNavHistory* history = nsNavHistory::GetHistoryService();
|
||||
NS_ENSURE_TRUE(history, NS_ERROR_UNEXPECTED);
|
||||
|
||||
PRInt64 uriID;
|
||||
rv = history->GetUrlIdFor(aURI, &uriID, PR_FALSE);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
if (uriID == 0)
|
||||
return NS_OK; // URI doesn't exist, nothing to delete
|
||||
|
||||
nsCOMPtr<mozIStorageStatement> statement;
|
||||
rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
|
||||
"DELETE FROM moz_anno WHERE page = ?1"),
|
||||
getter_AddRefs(statement));
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
rv = statement->BindInt64Parameter(0, uriID);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
rv = statement->Execute();
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
// Update observers
|
||||
for (PRInt32 i = 0; i < mObservers.Count(); i ++)
|
||||
mObservers[i]->OnAnnotationRemoved(aURI, EmptyCString());
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
||||
// nsAnnotationService::AddObserver
|
||||
|
||||
NS_IMETHODIMP
|
||||
|
@ -64,6 +64,7 @@ protected:
|
||||
|
||||
nsCOMPtr<mozIStorageStatement> mDBSetAnnotation;
|
||||
nsCOMPtr<mozIStorageStatement> mDBGetAnnotation;
|
||||
nsCOMPtr<mozIStorageStatement> mDBGetAnnotationNames;
|
||||
nsCOMPtr<mozIStorageStatement> mDBGetAnnotationFromURI;
|
||||
nsCOMPtr<mozIStorageStatement> mDBAddAnnotation;
|
||||
nsCOMPtr<mozIStorageStatement> mDBRemoveAnnotation;
|
||||
|
Loading…
Reference in New Issue
Block a user