2001-02-28 05:28:50 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
2004-04-18 22:01:16 +00:00
|
|
|
* ***** 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 TestCacheService.cpp, released
|
|
|
|
* February 7, 2001.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2001
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Gordon Sheridan, 7-February-2001
|
|
|
|
*
|
|
|
|
* 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 ***** */
|
2001-02-28 05:28:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
#include "nspr.h"
|
|
|
|
#include "nscore.h"
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "nsICache.h"
|
|
|
|
#include "nsICacheService.h"
|
|
|
|
#include "nsICacheSession.h"
|
|
|
|
#include "nsICacheEntryDescriptor.h"
|
2001-03-07 09:31:45 +00:00
|
|
|
#include "nsICacheListener.h"
|
2001-02-28 05:28:50 +00:00
|
|
|
#include "nsIDNSService.h"
|
2002-09-03 23:36:13 +00:00
|
|
|
#include "nsXPCOM.h"
|
2001-02-28 05:28:50 +00:00
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
#include "nsSupportsPrimitives.h"
|
2001-03-07 09:31:45 +00:00
|
|
|
#include "nsIEventQueueService.h"
|
2001-02-28 05:28:50 +00:00
|
|
|
|
|
|
|
|
2001-03-07 09:31:45 +00:00
|
|
|
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
|
|
|
|
static NS_DEFINE_CID(kCacheServiceCID, NS_CACHESERVICE_CID);
|
2001-02-28 05:28:50 +00:00
|
|
|
|
2001-03-07 09:31:45 +00:00
|
|
|
nsCOMPtr<nsIEventQueue> gEventQ;
|
|
|
|
nsCOMPtr<nsICacheService> gCacheService;
|
2001-02-28 05:28:50 +00:00
|
|
|
|
2001-03-07 09:31:45 +00:00
|
|
|
class AsyncCacheRequest
|
2001-02-28 05:28:50 +00:00
|
|
|
{
|
2001-03-07 09:31:45 +00:00
|
|
|
public:
|
|
|
|
AsyncCacheRequest(const char * key);
|
|
|
|
~AsyncCacheRequest();
|
2001-02-28 05:28:50 +00:00
|
|
|
|
2001-03-07 09:31:45 +00:00
|
|
|
const char * mKey;
|
|
|
|
};
|
2001-02-28 05:28:50 +00:00
|
|
|
|
|
|
|
|
2001-03-07 09:31:45 +00:00
|
|
|
nsresult
|
|
|
|
MakeCacheSession(const char * clientID, nsICacheSession **session)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (!gCacheService) {
|
2001-07-25 07:54:28 +00:00
|
|
|
// nsCOMPtr<nsICacheService> cacheService =
|
|
|
|
// do_GetService(kCacheServiceCID, &rv);
|
2001-03-07 09:31:45 +00:00
|
|
|
gCacheService = do_GetService(kCacheServiceCID, &rv);
|
|
|
|
if (NS_FAILED(rv) || !gCacheService) {
|
|
|
|
printf("do_GetService(kCacheServiceCID) failed : %x\n", rv);
|
|
|
|
goto error_exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = gCacheService->CreateSession(clientID,
|
2001-02-28 05:28:50 +00:00
|
|
|
nsICache::STORE_IN_MEMORY,
|
|
|
|
nsICache::NOT_STREAM_BASED,
|
2001-03-07 09:31:45 +00:00
|
|
|
session);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
printf("nsCacheService::CreateSession() failed : %x\n", rv);
|
|
|
|
|
|
|
|
error_exit:
|
|
|
|
return rv;
|
|
|
|
}
|
2001-02-28 05:28:50 +00:00
|
|
|
|
2001-03-07 09:31:45 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
TestMemoryObjectCache()
|
|
|
|
{
|
|
|
|
printf("\nTesting Memory Object Cache:\n");
|
|
|
|
nsCOMPtr<nsICacheSession> session;
|
|
|
|
nsresult rv = MakeCacheSession("testClientID", getter_AddRefs(session));
|
|
|
|
if (NS_FAILED(rv)) return;
|
2001-02-28 05:28:50 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsICacheEntryDescriptor> descriptor;
|
2001-03-07 09:31:45 +00:00
|
|
|
|
|
|
|
// Test ACCESS_READ for non-existent entry
|
|
|
|
printf("\nTest ACCESS_READ:\n");
|
2005-01-13 03:25:28 +00:00
|
|
|
rv = session->OpenCacheEntry(NS_LITERAL_CSTRING("non-existent entry"),
|
2001-03-07 09:31:45 +00:00
|
|
|
nsICache::ACCESS_READ,
|
2001-05-09 04:26:19 +00:00
|
|
|
nsICache::BLOCKING,
|
2001-03-07 09:31:45 +00:00
|
|
|
getter_AddRefs(descriptor));
|
|
|
|
if (rv != NS_ERROR_CACHE_KEY_NOT_FOUND)
|
|
|
|
printf("OpenCacheEntry(ACCESS_READ) returned: %x for non-existent entry\n", rv);
|
|
|
|
|
2005-01-13 03:25:28 +00:00
|
|
|
NS_NAMED_LITERAL_CSTRING(cacheKey, "http://www.mozilla.org/somekey");
|
|
|
|
|
2001-03-07 09:31:45 +00:00
|
|
|
// Test creating new entry
|
|
|
|
printf("\nTest ACCESS_READ_WRITE:\n");
|
2005-01-13 03:25:28 +00:00
|
|
|
rv = session->OpenCacheEntry(cacheKey,
|
2001-02-28 05:28:50 +00:00
|
|
|
nsICache::ACCESS_READ_WRITE,
|
2001-05-09 05:05:20 +00:00
|
|
|
nsICache::BLOCKING,
|
2001-02-28 05:28:50 +00:00
|
|
|
getter_AddRefs(descriptor));
|
|
|
|
if (NS_FAILED(rv)) {
|
2001-03-07 09:31:45 +00:00
|
|
|
printf("OpenCacheEntry(ACCESS_READ_WRITE) failed: %x\n", rv);
|
|
|
|
goto error_exit;
|
2001-02-28 05:28:50 +00:00
|
|
|
}
|
2002-07-04 14:29:25 +00:00
|
|
|
|
2002-08-06 00:53:19 +00:00
|
|
|
nsCOMPtr<nsISupportsCString> foo =
|
|
|
|
do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID, &rv);
|
2001-03-07 09:31:45 +00:00
|
|
|
|
2002-08-26 21:20:34 +00:00
|
|
|
foo->SetData(NS_LITERAL_CSTRING("hello world"));
|
2001-02-28 05:28:50 +00:00
|
|
|
|
2001-03-07 09:31:45 +00:00
|
|
|
rv = descriptor->SetCacheElement(foo);
|
|
|
|
rv = descriptor->SetDataSize(11);
|
|
|
|
rv = descriptor->SetMetaDataElement("itemOne", "metaData works");
|
2001-02-28 05:28:50 +00:00
|
|
|
descriptor = nsnull;
|
|
|
|
|
2001-03-07 09:31:45 +00:00
|
|
|
// Test refetching entry
|
2002-07-04 14:29:25 +00:00
|
|
|
|
2005-01-13 03:25:28 +00:00
|
|
|
rv = session->OpenCacheEntry(cacheKey,
|
2001-02-28 05:28:50 +00:00
|
|
|
nsICache::ACCESS_READ_WRITE,
|
2001-05-09 05:05:20 +00:00
|
|
|
nsICache::BLOCKING,
|
2001-02-28 05:28:50 +00:00
|
|
|
getter_AddRefs(descriptor));
|
2001-03-07 09:31:45 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
printf("OpenCacheEntry(ACCESS_READ_WRITE #2) failed: %x", rv);
|
|
|
|
goto error_exit;
|
|
|
|
}
|
2001-02-28 05:28:50 +00:00
|
|
|
|
2002-08-06 00:53:19 +00:00
|
|
|
nsCOMPtr<nsISupportsCString> bar;
|
2001-02-28 05:28:50 +00:00
|
|
|
descriptor->GetCacheElement(getter_AddRefs(bar));
|
|
|
|
if (foo.get() != bar.get()) {
|
|
|
|
printf("cache elements not the same\n");
|
|
|
|
} else {
|
2001-03-07 09:31:45 +00:00
|
|
|
printf("data matches...\n");
|
2001-02-28 05:28:50 +00:00
|
|
|
}
|
2002-07-04 14:29:25 +00:00
|
|
|
|
2001-03-07 09:31:45 +00:00
|
|
|
char * metaData;
|
|
|
|
rv = descriptor->GetMetaDataElement("itemOne", &metaData);
|
|
|
|
if (NS_SUCCEEDED(rv)) printf("metaData = %s\n", metaData);
|
|
|
|
else printf("GetMetaDataElement failed : rv = %x\n", rv);
|
|
|
|
descriptor = nsnull;
|
|
|
|
|
|
|
|
// Test ACCESS_WRITE entry
|
|
|
|
printf("\nTest ACCESS_WRITE:\n");
|
2005-01-13 03:25:28 +00:00
|
|
|
rv = session->OpenCacheEntry(cacheKey,
|
2001-03-07 09:31:45 +00:00
|
|
|
nsICache::ACCESS_WRITE,
|
2001-05-09 05:05:20 +00:00
|
|
|
nsICache::BLOCKING,
|
2001-03-07 09:31:45 +00:00
|
|
|
getter_AddRefs(descriptor));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
printf("OpenCacheEntry(ACCESS_WRITE) failed: %x", rv);
|
|
|
|
goto error_exit;
|
|
|
|
}
|
|
|
|
rv = descriptor->GetCacheElement(getter_AddRefs(bar));
|
|
|
|
if (bar)
|
|
|
|
printf("FAILED: we didn't get new entry.\n");
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
printf("GetCacheElement failed : %x\n", rv);
|
|
|
|
|
|
|
|
rv = descriptor->GetMetaDataElement("itemOne", &metaData);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
if (metaData) printf("metaData = %s\n", metaData);
|
|
|
|
else printf("metaData = nsnull\n");
|
|
|
|
else printf("GetMetaDataElement failed : rv = %x\n", rv);
|
|
|
|
|
|
|
|
printf("\n");
|
2001-02-28 05:28:50 +00:00
|
|
|
error_exit:
|
|
|
|
|
2001-03-07 09:31:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
// Start up XPCOM
|
2001-10-16 03:35:52 +00:00
|
|
|
rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
|
2001-03-07 09:31:45 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create event queue for this thread
|
|
|
|
*/
|
2001-07-25 07:54:28 +00:00
|
|
|
nsCOMPtr<nsIEventQueueService> eventQService =
|
|
|
|
do_GetService(kEventQueueServiceCID, &rv);
|
2001-03-07 09:31:45 +00:00
|
|
|
if (NS_FAILED(rv)) goto error_exit;
|
|
|
|
|
|
|
|
eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, getter_AddRefs(gEventQ));
|
2002-07-04 14:29:25 +00:00
|
|
|
|
2001-03-07 09:31:45 +00:00
|
|
|
/**
|
|
|
|
* Test Cache
|
|
|
|
*/
|
|
|
|
TestMemoryObjectCache();
|
|
|
|
|
|
|
|
|
|
|
|
error_exit:
|
|
|
|
gEventQ = nsnull;
|
|
|
|
eventQService = nsnull;
|
|
|
|
|
2001-02-28 05:28:50 +00:00
|
|
|
NS_ShutdownXPCOM(nsnull);
|
|
|
|
|
2001-03-07 09:31:45 +00:00
|
|
|
printf("XPCOM shut down.\n\n");
|
2001-02-28 05:28:50 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2002-07-04 14:29:25 +00:00
|
|
|
|
2001-02-28 05:28:50 +00:00
|
|
|
|