|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
2 * |
|
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/. */ |
|
6 |
|
7 |
|
8 #include "nspr.h" |
|
9 #include "nscore.h" |
|
10 #include "nsError.h" |
|
11 #include "nsIServiceManager.h" |
|
12 #include "nsNetCID.h" |
|
13 #include "nsICache.h" |
|
14 #include "nsICacheService.h" |
|
15 #include "nsICacheSession.h" |
|
16 #include "nsICacheEntryDescriptor.h" |
|
17 #include "nsICacheListener.h" |
|
18 #include "nsIDNSService.h" |
|
19 #include "nsXPCOM.h" |
|
20 #include "nsISupportsPrimitives.h" |
|
21 #include "nsSupportsPrimitives.h" |
|
22 #include "nsIEventQueueService.h" |
|
23 |
|
24 |
|
25 static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID); |
|
26 static NS_DEFINE_CID(kCacheServiceCID, NS_CACHESERVICE_CID); |
|
27 |
|
28 nsCOMPtr<nsIEventQueue> gEventQ; |
|
29 nsCOMPtr<nsICacheService> gCacheService; |
|
30 |
|
31 class AsyncCacheRequest |
|
32 { |
|
33 public: |
|
34 AsyncCacheRequest(const char * key); |
|
35 ~AsyncCacheRequest(); |
|
36 |
|
37 const char * mKey; |
|
38 }; |
|
39 |
|
40 |
|
41 nsresult |
|
42 MakeCacheSession(const char * clientID, nsICacheSession **session) |
|
43 { |
|
44 nsresult rv; |
|
45 |
|
46 if (!gCacheService) { |
|
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); |
|
52 goto error_exit; |
|
53 } |
|
54 } |
|
55 |
|
56 rv = gCacheService->CreateSession(clientID, |
|
57 nsICache::STORE_IN_MEMORY, |
|
58 nsICache::NOT_STREAM_BASED, |
|
59 session); |
|
60 if (NS_FAILED(rv)) |
|
61 printf("nsCacheService::CreateSession() failed : %x\n", rv); |
|
62 |
|
63 error_exit: |
|
64 return rv; |
|
65 } |
|
66 |
|
67 |
|
68 void |
|
69 TestMemoryObjectCache() |
|
70 { |
|
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; |
|
75 |
|
76 nsCOMPtr<nsICacheEntryDescriptor> descriptor; |
|
77 |
|
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, |
|
82 nsICache::BLOCKING, |
|
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); |
|
86 |
|
87 NS_NAMED_LITERAL_CSTRING(cacheKey, "http://www.mozilla.org/somekey"); |
|
88 |
|
89 // Test creating new entry |
|
90 printf("\nTest ACCESS_READ_WRITE:\n"); |
|
91 rv = session->OpenCacheEntry(cacheKey, |
|
92 nsICache::ACCESS_READ_WRITE, |
|
93 nsICache::BLOCKING, |
|
94 getter_AddRefs(descriptor)); |
|
95 if (NS_FAILED(rv)) { |
|
96 printf("OpenCacheEntry(ACCESS_READ_WRITE) failed: %x\n", rv); |
|
97 goto error_exit; |
|
98 } |
|
99 |
|
100 nsCOMPtr<nsISupportsCString> foo = |
|
101 do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID, &rv); |
|
102 |
|
103 foo->SetData(NS_LITERAL_CSTRING("hello world")); |
|
104 |
|
105 rv = descriptor->SetCacheElement(foo); |
|
106 rv = descriptor->SetDataSize(11); |
|
107 rv = descriptor->SetMetaDataElement("itemOne", "metaData works"); |
|
108 descriptor = nullptr; |
|
109 |
|
110 // Test refetching entry |
|
111 |
|
112 rv = session->OpenCacheEntry(cacheKey, |
|
113 nsICache::ACCESS_READ_WRITE, |
|
114 nsICache::BLOCKING, |
|
115 getter_AddRefs(descriptor)); |
|
116 if (NS_FAILED(rv)) { |
|
117 printf("OpenCacheEntry(ACCESS_READ_WRITE #2) failed: %x", rv); |
|
118 goto error_exit; |
|
119 } |
|
120 |
|
121 nsCOMPtr<nsISupportsCString> bar; |
|
122 descriptor->GetCacheElement(getter_AddRefs(bar)); |
|
123 if (foo.get() != bar.get()) { |
|
124 printf("cache elements not the same\n"); |
|
125 } else { |
|
126 printf("data matches...\n"); |
|
127 } |
|
128 |
|
129 char * metaData; |
|
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; |
|
134 |
|
135 // Test ACCESS_WRITE entry |
|
136 printf("\nTest ACCESS_WRITE:\n"); |
|
137 rv = session->OpenCacheEntry(cacheKey, |
|
138 nsICache::ACCESS_WRITE, |
|
139 nsICache::BLOCKING, |
|
140 getter_AddRefs(descriptor)); |
|
141 if (NS_FAILED(rv)) { |
|
142 printf("OpenCacheEntry(ACCESS_WRITE) failed: %x", rv); |
|
143 goto error_exit; |
|
144 } |
|
145 rv = descriptor->GetCacheElement(getter_AddRefs(bar)); |
|
146 if (bar) |
|
147 printf("FAILED: we didn't get new entry.\n"); |
|
148 if (NS_FAILED(rv)) |
|
149 printf("GetCacheElement failed : %x\n", rv); |
|
150 |
|
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); |
|
156 |
|
157 printf("\n"); |
|
158 error_exit: |
|
159 |
|
160 return; |
|
161 } |
|
162 |
|
163 |
|
164 int |
|
165 main(int argc, char* argv[]) |
|
166 { |
|
167 nsresult rv = NS_OK; |
|
168 |
|
169 // Start up XPCOM |
|
170 rv = NS_InitXPCOM2(nullptr, nullptr, nullptr); |
|
171 if (NS_FAILED(rv)) return rv; |
|
172 |
|
173 /** |
|
174 * Create event queue for this thread |
|
175 */ |
|
176 nsCOMPtr<nsIEventQueueService> eventQService = |
|
177 do_GetService(kEventQueueServiceCID, &rv); |
|
178 if (NS_FAILED(rv)) goto error_exit; |
|
179 |
|
180 eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, getter_AddRefs(gEventQ)); |
|
181 |
|
182 /** |
|
183 * Test Cache |
|
184 */ |
|
185 TestMemoryObjectCache(); |
|
186 |
|
187 |
|
188 error_exit: |
|
189 gEventQ = nullptr; |
|
190 eventQService = nullptr; |
|
191 |
|
192 NS_ShutdownXPCOM(nullptr); |
|
193 |
|
194 printf("XPCOM shut down.\n\n"); |
|
195 return rv; |
|
196 } |
|
197 |
|
198 |
|
199 |