1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
11 #include "nsIServiceManager.h"
14 #include "nsICacheService.h"
15 #include "nsICacheSession.h"
16 #include "nsICacheEntryDescriptor.h"
17 #include "nsICacheListener.h"
18 #include "nsIDNSService.h"
20 #include "nsISupportsPrimitives.h"
21 #include "nsSupportsPrimitives.h"
22 #include "nsIEventQueueService.h"
25 static NS_DEFINE_CID(kEventQueueServiceCID
, NS_EVENTQUEUESERVICE_CID
);
26 static NS_DEFINE_CID(kCacheServiceCID
, NS_CACHESERVICE_CID
);
28 nsCOMPtr
<nsIEventQueue
> gEventQ
;
29 nsCOMPtr
<nsICacheService
> gCacheService
;
31 class AsyncCacheRequest
34 AsyncCacheRequest(const char * key
);
42 MakeCacheSession(const char * clientID
, nsICacheSession
**session
)
47 // nsCOMPtr<nsICacheService> cacheService =
48 // do_GetService(kCacheServiceCID, &rv);
49 gCacheService
= do_GetService(kCacheServiceCID
, &rv
);
50 if (NS_FAILED(rv
) || !gCacheService
) {
51 printf("do_GetService(kCacheServiceCID) failed : %x\n", rv
);
56 rv
= gCacheService
->CreateSession(clientID
,
57 nsICache::STORE_IN_MEMORY
,
58 nsICache::NOT_STREAM_BASED
,
61 printf("nsCacheService::CreateSession() failed : %x\n", rv
);
69 TestMemoryObjectCache()
71 printf("\nTesting Memory Object Cache:\n");
72 nsCOMPtr
<nsICacheSession
> session
;
73 nsresult rv
= MakeCacheSession("testClientID", getter_AddRefs(session
));
74 if (NS_FAILED(rv
)) return;
76 nsCOMPtr
<nsICacheEntryDescriptor
> descriptor
;
78 // Test ACCESS_READ for nonexistent entry
79 printf("\nTest ACCESS_READ:\n");
80 rv
= session
->OpenCacheEntry(NS_LITERAL_CSTRING("nonexistent entry"),
81 nsICache::ACCESS_READ
,
83 getter_AddRefs(descriptor
));
84 if (rv
!= NS_ERROR_CACHE_KEY_NOT_FOUND
)
85 printf("OpenCacheEntry(ACCESS_READ) returned: %x for nonexistent entry\n", rv
);
87 NS_NAMED_LITERAL_CSTRING(cacheKey
, "http://www.mozilla.org/somekey");
89 // Test creating new entry
90 printf("\nTest ACCESS_READ_WRITE:\n");
91 rv
= session
->OpenCacheEntry(cacheKey
,
92 nsICache::ACCESS_READ_WRITE
,
94 getter_AddRefs(descriptor
));
96 printf("OpenCacheEntry(ACCESS_READ_WRITE) failed: %x\n", rv
);
100 nsCOMPtr
<nsISupportsCString
> foo
=
101 do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID
, &rv
);
103 foo
->SetData(NS_LITERAL_CSTRING("hello world"));
105 rv
= descriptor
->SetCacheElement(foo
);
106 rv
= descriptor
->SetDataSize(11);
107 rv
= descriptor
->SetMetaDataElement("itemOne", "metaData works");
108 descriptor
= nullptr;
110 // Test refetching entry
112 rv
= session
->OpenCacheEntry(cacheKey
,
113 nsICache::ACCESS_READ_WRITE
,
115 getter_AddRefs(descriptor
));
117 printf("OpenCacheEntry(ACCESS_READ_WRITE #2) failed: %x", rv
);
121 nsCOMPtr
<nsISupportsCString
> bar
;
122 descriptor
->GetCacheElement(getter_AddRefs(bar
));
123 if (foo
.get() != bar
.get()) {
124 printf("cache elements not the same\n");
126 printf("data matches...\n");
130 rv
= descriptor
->GetMetaDataElement("itemOne", &metaData
);
131 if (NS_SUCCEEDED(rv
)) printf("metaData = %s\n", metaData
);
132 else printf("GetMetaDataElement failed : rv = %x\n", rv
);
133 descriptor
= nullptr;
135 // Test ACCESS_WRITE entry
136 printf("\nTest ACCESS_WRITE:\n");
137 rv
= session
->OpenCacheEntry(cacheKey
,
138 nsICache::ACCESS_WRITE
,
140 getter_AddRefs(descriptor
));
142 printf("OpenCacheEntry(ACCESS_WRITE) failed: %x", rv
);
145 rv
= descriptor
->GetCacheElement(getter_AddRefs(bar
));
147 printf("FAILED: we didn't get new entry.\n");
149 printf("GetCacheElement failed : %x\n", rv
);
151 rv
= descriptor
->GetMetaDataElement("itemOne", &metaData
);
152 if (NS_SUCCEEDED(rv
))
153 if (metaData
) printf("metaData = %s\n", metaData
);
154 else printf("metaData = nullptr\n");
155 else printf("GetMetaDataElement failed : rv = %x\n", rv
);
165 main(int argc
, char* argv
[])
170 rv
= NS_InitXPCOM2(nullptr, nullptr, nullptr);
171 if (NS_FAILED(rv
)) return rv
;
174 * Create event queue for this thread
176 nsCOMPtr
<nsIEventQueueService
> eventQService
=
177 do_GetService(kEventQueueServiceCID
, &rv
);
178 if (NS_FAILED(rv
)) goto error_exit
;
180 eventQService
->GetThreadEventQueue(NS_CURRENT_THREAD
, getter_AddRefs(gEventQ
));
185 TestMemoryObjectCache();
190 eventQService
= nullptr;
192 NS_ShutdownXPCOM(nullptr);
194 printf("XPCOM shut down.\n\n");