netwerk/cache2/OldWrappers.cpp

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

michael@0 1 // Stuff to link the old imp to the new api - will go away!
michael@0 2
michael@0 3 #include "CacheLog.h"
michael@0 4 #include "OldWrappers.h"
michael@0 5 #include "CacheStorage.h"
michael@0 6 #include "CacheStorageService.h"
michael@0 7 #include "LoadContextInfo.h"
michael@0 8
michael@0 9 #include "nsIURI.h"
michael@0 10 #include "nsICacheService.h"
michael@0 11 #include "nsICacheSession.h"
michael@0 12 #include "nsIApplicationCache.h"
michael@0 13 #include "nsIApplicationCacheService.h"
michael@0 14 #include "nsIStreamTransportService.h"
michael@0 15 #include "nsIFile.h"
michael@0 16 #include "nsICacheEntryDoomCallback.h"
michael@0 17 #include "nsICacheListener.h"
michael@0 18 #include "nsICacheStorageVisitor.h"
michael@0 19
michael@0 20 #include "nsServiceManagerUtils.h"
michael@0 21 #include "nsNetCID.h"
michael@0 22 #include "nsProxyRelease.h"
michael@0 23 #include "mozilla/Telemetry.h"
michael@0 24
michael@0 25 static NS_DEFINE_CID(kStreamTransportServiceCID,
michael@0 26 NS_STREAMTRANSPORTSERVICE_CID);
michael@0 27
michael@0 28 static uint32_t const CHECK_MULTITHREADED = nsICacheStorage::CHECK_MULTITHREADED;
michael@0 29
michael@0 30 namespace mozilla {
michael@0 31 namespace net {
michael@0 32
michael@0 33 namespace { // anon
michael@0 34
michael@0 35 // Fires the doom callback back on the main thread
michael@0 36 // after the cache I/O thread is looped.
michael@0 37
michael@0 38 class DoomCallbackSynchronizer : public nsRunnable
michael@0 39 {
michael@0 40 public:
michael@0 41 DoomCallbackSynchronizer(nsICacheEntryDoomCallback* cb) : mCB(cb)
michael@0 42 {
michael@0 43 MOZ_COUNT_CTOR(DoomCallbackSynchronizer);
michael@0 44 }
michael@0 45 nsresult Dispatch();
michael@0 46
michael@0 47 private:
michael@0 48 virtual ~DoomCallbackSynchronizer()
michael@0 49 {
michael@0 50 MOZ_COUNT_DTOR(DoomCallbackSynchronizer);
michael@0 51 }
michael@0 52
michael@0 53 NS_DECL_NSIRUNNABLE
michael@0 54 nsCOMPtr<nsICacheEntryDoomCallback> mCB;
michael@0 55 };
michael@0 56
michael@0 57 nsresult DoomCallbackSynchronizer::Dispatch()
michael@0 58 {
michael@0 59 nsresult rv;
michael@0 60
michael@0 61 nsCOMPtr<nsICacheService> serv =
michael@0 62 do_GetService(NS_CACHESERVICE_CONTRACTID, &rv);
michael@0 63 NS_ENSURE_SUCCESS(rv, rv);
michael@0 64
michael@0 65 nsCOMPtr<nsIEventTarget> eventTarget;
michael@0 66 rv = serv->GetCacheIOTarget(getter_AddRefs(eventTarget));
michael@0 67 NS_ENSURE_SUCCESS(rv, rv);
michael@0 68
michael@0 69 rv = eventTarget->Dispatch(this, NS_DISPATCH_NORMAL);
michael@0 70 NS_ENSURE_SUCCESS(rv, rv);
michael@0 71
michael@0 72 return NS_OK;
michael@0 73 }
michael@0 74
michael@0 75 NS_IMETHODIMP DoomCallbackSynchronizer::Run()
michael@0 76 {
michael@0 77 if (!NS_IsMainThread()) {
michael@0 78 NS_DispatchToMainThread(this);
michael@0 79 }
michael@0 80 else {
michael@0 81 if (mCB)
michael@0 82 mCB->OnCacheEntryDoomed(NS_OK);
michael@0 83 }
michael@0 84 return NS_OK;
michael@0 85 }
michael@0 86
michael@0 87 // Receives doom callback from the old API and forwards to the new API
michael@0 88
michael@0 89 class DoomCallbackWrapper : public nsICacheListener
michael@0 90 {
michael@0 91 NS_DECL_THREADSAFE_ISUPPORTS
michael@0 92 NS_DECL_NSICACHELISTENER
michael@0 93
michael@0 94 DoomCallbackWrapper(nsICacheEntryDoomCallback* cb) : mCB(cb)
michael@0 95 {
michael@0 96 MOZ_COUNT_CTOR(DoomCallbackWrapper);
michael@0 97 }
michael@0 98
michael@0 99 private:
michael@0 100 virtual ~DoomCallbackWrapper()
michael@0 101 {
michael@0 102 MOZ_COUNT_DTOR(DoomCallbackWrapper);
michael@0 103 }
michael@0 104
michael@0 105 nsCOMPtr<nsICacheEntryDoomCallback> mCB;
michael@0 106 };
michael@0 107
michael@0 108 NS_IMPL_ISUPPORTS(DoomCallbackWrapper, nsICacheListener);
michael@0 109
michael@0 110 NS_IMETHODIMP DoomCallbackWrapper::OnCacheEntryAvailable(nsICacheEntryDescriptor *descriptor,
michael@0 111 nsCacheAccessMode accessGranted,
michael@0 112 nsresult status)
michael@0 113 {
michael@0 114 return NS_OK;
michael@0 115 }
michael@0 116
michael@0 117 NS_IMETHODIMP DoomCallbackWrapper::OnCacheEntryDoomed(nsresult status)
michael@0 118 {
michael@0 119 if (!mCB)
michael@0 120 return NS_ERROR_NULL_POINTER;
michael@0 121
michael@0 122 mCB->OnCacheEntryDoomed(status);
michael@0 123 mCB = nullptr;
michael@0 124 return NS_OK;
michael@0 125 }
michael@0 126
michael@0 127 // Receives visit callbacks from the old API and forwards it to the new API
michael@0 128
michael@0 129 class VisitCallbackWrapper : public nsICacheVisitor
michael@0 130 {
michael@0 131 NS_DECL_THREADSAFE_ISUPPORTS
michael@0 132 NS_DECL_NSICACHEVISITOR
michael@0 133
michael@0 134 VisitCallbackWrapper(char* const deviceID,
michael@0 135 nsICacheStorageVisitor* cb,
michael@0 136 bool visitEntries)
michael@0 137 : mCB(cb)
michael@0 138 , mVisitEntries(visitEntries)
michael@0 139 , mDeviceID(deviceID)
michael@0 140 {
michael@0 141 MOZ_COUNT_CTOR(VisitCallbackWrapper);
michael@0 142 }
michael@0 143
michael@0 144 private:
michael@0 145 virtual ~VisitCallbackWrapper();
michael@0 146 nsCOMPtr<nsICacheStorageVisitor> mCB;
michael@0 147 bool mVisitEntries;
michael@0 148 char* const mDeviceID;
michael@0 149 };
michael@0 150
michael@0 151 NS_IMPL_ISUPPORTS(VisitCallbackWrapper, nsICacheVisitor)
michael@0 152
michael@0 153 VisitCallbackWrapper::~VisitCallbackWrapper()
michael@0 154 {
michael@0 155 if (mVisitEntries)
michael@0 156 mCB->OnCacheEntryVisitCompleted();
michael@0 157
michael@0 158 MOZ_COUNT_DTOR(VisitCallbackWrapper);
michael@0 159 }
michael@0 160
michael@0 161 NS_IMETHODIMP VisitCallbackWrapper::VisitDevice(const char * deviceID,
michael@0 162 nsICacheDeviceInfo *deviceInfo,
michael@0 163 bool *_retval)
michael@0 164 {
michael@0 165 if (!mCB)
michael@0 166 return NS_ERROR_NULL_POINTER;
michael@0 167
michael@0 168 *_retval = false;
michael@0 169 if (strcmp(deviceID, mDeviceID)) {
michael@0 170 // Not the device we want to visit
michael@0 171 return NS_OK;
michael@0 172 }
michael@0 173
michael@0 174 nsresult rv;
michael@0 175
michael@0 176 uint32_t entryCount;
michael@0 177 rv = deviceInfo->GetEntryCount(&entryCount);
michael@0 178 NS_ENSURE_SUCCESS(rv, rv);
michael@0 179
michael@0 180 uint32_t totalSize;
michael@0 181 rv = deviceInfo->GetTotalSize(&totalSize);
michael@0 182 NS_ENSURE_SUCCESS(rv, rv);
michael@0 183
michael@0 184 mCB->OnCacheStorageInfo(entryCount, totalSize);
michael@0 185 *_retval = mVisitEntries;
michael@0 186
michael@0 187 return NS_OK;
michael@0 188 }
michael@0 189
michael@0 190 NS_IMETHODIMP VisitCallbackWrapper::VisitEntry(const char * deviceID,
michael@0 191 nsICacheEntryInfo *entryInfo,
michael@0 192 bool *_retval)
michael@0 193 {
michael@0 194 MOZ_ASSERT(!strcmp(deviceID, mDeviceID));
michael@0 195
michael@0 196 nsRefPtr<_OldCacheEntryWrapper> wrapper = new _OldCacheEntryWrapper(entryInfo);
michael@0 197 nsresult rv = mCB->OnCacheEntryInfo(wrapper);
michael@0 198 *_retval = NS_SUCCEEDED(rv);
michael@0 199
michael@0 200 return NS_OK;
michael@0 201 }
michael@0 202
michael@0 203 } // anon
michael@0 204
michael@0 205
michael@0 206 // _OldGetDiskConsumption
michael@0 207
michael@0 208 //static
michael@0 209 nsresult _OldGetDiskConsumption::Get(nsICacheStorageConsumptionObserver* aCallback)
michael@0 210 {
michael@0 211 nsresult rv;
michael@0 212
michael@0 213 nsCOMPtr<nsICacheService> serv =
michael@0 214 do_GetService(NS_CACHESERVICE_CONTRACTID, &rv);
michael@0 215 NS_ENSURE_SUCCESS(rv, rv);
michael@0 216
michael@0 217 nsRefPtr<_OldGetDiskConsumption> cb = new _OldGetDiskConsumption(aCallback);
michael@0 218
michael@0 219 // _OldGetDiskConsumption stores the found size value, but until dispatched
michael@0 220 // to the main thread it doesn't call on the consupmtion observer. See bellow.
michael@0 221 rv = serv->VisitEntries(cb);
michael@0 222 NS_ENSURE_SUCCESS(rv, rv);
michael@0 223
michael@0 224 // We are called from CacheStorageService::AsyncGetDiskConsumption whose IDL
michael@0 225 // documentation claims the callback is always delievered asynchronously
michael@0 226 // back to the main thread. Despite we know the result synchronosusly when
michael@0 227 // querying the old cache, we need to stand the word and dispatch the result
michael@0 228 // to the main thread asynchronously. Hence the dispatch here.
michael@0 229 return NS_DispatchToMainThread(cb);
michael@0 230 }
michael@0 231
michael@0 232 NS_IMPL_ISUPPORTS_INHERITED(_OldGetDiskConsumption,
michael@0 233 nsRunnable,
michael@0 234 nsICacheVisitor)
michael@0 235
michael@0 236 _OldGetDiskConsumption::_OldGetDiskConsumption(
michael@0 237 nsICacheStorageConsumptionObserver* aCallback)
michael@0 238 : mCallback(aCallback)
michael@0 239 , mSize(0)
michael@0 240 {
michael@0 241 }
michael@0 242
michael@0 243 NS_IMETHODIMP
michael@0 244 _OldGetDiskConsumption::Run()
michael@0 245 {
michael@0 246 mCallback->OnNetworkCacheDiskConsumption(mSize);
michael@0 247 return NS_OK;
michael@0 248 }
michael@0 249
michael@0 250 NS_IMETHODIMP
michael@0 251 _OldGetDiskConsumption::VisitDevice(const char * deviceID,
michael@0 252 nsICacheDeviceInfo *deviceInfo,
michael@0 253 bool *_retval)
michael@0 254 {
michael@0 255 if (!strcmp(deviceID, "disk")) {
michael@0 256 uint32_t size;
michael@0 257 nsresult rv = deviceInfo->GetTotalSize(&size);
michael@0 258 if (NS_SUCCEEDED(rv))
michael@0 259 mSize = (int64_t)size;
michael@0 260 }
michael@0 261
michael@0 262 *_retval = false;
michael@0 263 return NS_OK;
michael@0 264 }
michael@0 265
michael@0 266 NS_IMETHODIMP
michael@0 267 _OldGetDiskConsumption::VisitEntry(const char * deviceID,
michael@0 268 nsICacheEntryInfo *entryInfo,
michael@0 269 bool *_retval)
michael@0 270 {
michael@0 271 MOZ_CRASH("Unexpected");
michael@0 272 return NS_OK;
michael@0 273 }
michael@0 274
michael@0 275
michael@0 276 // _OldCacheEntryWrapper
michael@0 277
michael@0 278 _OldCacheEntryWrapper::_OldCacheEntryWrapper(nsICacheEntryDescriptor* desc)
michael@0 279 : mOldDesc(desc), mOldInfo(desc)
michael@0 280 {
michael@0 281 MOZ_COUNT_CTOR(_OldCacheEntryWrapper);
michael@0 282 LOG(("Creating _OldCacheEntryWrapper %p for descriptor %p", this, desc));
michael@0 283 }
michael@0 284
michael@0 285 _OldCacheEntryWrapper::_OldCacheEntryWrapper(nsICacheEntryInfo* info)
michael@0 286 : mOldDesc(nullptr), mOldInfo(info)
michael@0 287 {
michael@0 288 MOZ_COUNT_CTOR(_OldCacheEntryWrapper);
michael@0 289 LOG(("Creating _OldCacheEntryWrapper %p for info %p", this, info));
michael@0 290 }
michael@0 291
michael@0 292 _OldCacheEntryWrapper::~_OldCacheEntryWrapper()
michael@0 293 {
michael@0 294 MOZ_COUNT_DTOR(_OldCacheEntryWrapper);
michael@0 295 LOG(("Destroying _OldCacheEntryWrapper %p for descriptor %p", this, mOldInfo.get()));
michael@0 296 }
michael@0 297
michael@0 298 NS_IMPL_ISUPPORTS(_OldCacheEntryWrapper, nsICacheEntry)
michael@0 299
michael@0 300 NS_IMETHODIMP _OldCacheEntryWrapper::AsyncDoom(nsICacheEntryDoomCallback* listener)
michael@0 301 {
michael@0 302 nsRefPtr<DoomCallbackWrapper> cb = listener
michael@0 303 ? new DoomCallbackWrapper(listener)
michael@0 304 : nullptr;
michael@0 305 return AsyncDoom(cb);
michael@0 306 }
michael@0 307
michael@0 308 NS_IMETHODIMP _OldCacheEntryWrapper::GetDataSize(int64_t *aSize)
michael@0 309 {
michael@0 310 uint32_t size;
michael@0 311 nsresult rv = GetDataSize(&size);
michael@0 312 if (NS_FAILED(rv))
michael@0 313 return rv;
michael@0 314
michael@0 315 *aSize = size;
michael@0 316 return NS_OK;
michael@0 317 }
michael@0 318
michael@0 319 NS_IMETHODIMP _OldCacheEntryWrapper::GetPersistent(bool *aPersistToDisk)
michael@0 320 {
michael@0 321 if (!mOldDesc) {
michael@0 322 return NS_ERROR_NULL_POINTER;
michael@0 323 }
michael@0 324
michael@0 325 nsresult rv;
michael@0 326
michael@0 327 nsCacheStoragePolicy policy;
michael@0 328 rv = mOldDesc->GetStoragePolicy(&policy);
michael@0 329 NS_ENSURE_SUCCESS(rv, rv);
michael@0 330
michael@0 331 *aPersistToDisk = policy != nsICache::STORE_IN_MEMORY;
michael@0 332
michael@0 333 return NS_OK;
michael@0 334 }
michael@0 335
michael@0 336 NS_IMETHODIMP _OldCacheEntryWrapper::Recreate(bool aMemoryOnly,
michael@0 337 nsICacheEntry** aResult)
michael@0 338 {
michael@0 339 NS_ENSURE_TRUE(mOldDesc, NS_ERROR_NOT_AVAILABLE);
michael@0 340
michael@0 341 nsCacheAccessMode mode;
michael@0 342 nsresult rv = mOldDesc->GetAccessGranted(&mode);
michael@0 343 NS_ENSURE_SUCCESS(rv, rv);
michael@0 344
michael@0 345 if (!(mode & nsICache::ACCESS_WRITE))
michael@0 346 return NS_ERROR_NOT_AVAILABLE;
michael@0 347
michael@0 348 LOG(("_OldCacheEntryWrapper::Recreate [this=%p]", this));
michael@0 349
michael@0 350 if (aMemoryOnly)
michael@0 351 mOldDesc->SetStoragePolicy(nsICache::STORE_IN_MEMORY);
michael@0 352
michael@0 353 nsCOMPtr<nsICacheEntry> self(this);
michael@0 354 self.forget(aResult);
michael@0 355 return NS_OK;
michael@0 356 }
michael@0 357
michael@0 358 NS_IMETHODIMP _OldCacheEntryWrapper::OpenInputStream(int64_t offset,
michael@0 359 nsIInputStream * *_retval)
michael@0 360 {
michael@0 361 if (offset > PR_UINT32_MAX)
michael@0 362 return NS_ERROR_INVALID_ARG;
michael@0 363
michael@0 364 return OpenInputStream(uint32_t(offset), _retval);
michael@0 365 }
michael@0 366 NS_IMETHODIMP _OldCacheEntryWrapper::OpenOutputStream(int64_t offset,
michael@0 367 nsIOutputStream * *_retval)
michael@0 368 {
michael@0 369 if (offset > PR_UINT32_MAX)
michael@0 370 return NS_ERROR_INVALID_ARG;
michael@0 371
michael@0 372 return OpenOutputStream(uint32_t(offset), _retval);
michael@0 373 }
michael@0 374
michael@0 375 NS_IMETHODIMP _OldCacheEntryWrapper::MaybeMarkValid()
michael@0 376 {
michael@0 377 LOG(("_OldCacheEntryWrapper::MaybeMarkValid [this=%p]", this));
michael@0 378
michael@0 379 NS_ENSURE_TRUE(mOldDesc, NS_ERROR_NULL_POINTER);
michael@0 380
michael@0 381 nsCacheAccessMode mode;
michael@0 382 nsresult rv = mOldDesc->GetAccessGranted(&mode);
michael@0 383 NS_ENSURE_SUCCESS(rv, rv);
michael@0 384
michael@0 385 if (mode & nsICache::ACCESS_WRITE) {
michael@0 386 LOG(("Marking cache entry valid [entry=%p, descr=%p]", this, mOldDesc));
michael@0 387 return mOldDesc->MarkValid();
michael@0 388 }
michael@0 389
michael@0 390 LOG(("Not marking read-only cache entry valid [entry=%p, descr=%p]", this, mOldDesc));
michael@0 391 return NS_OK;
michael@0 392 }
michael@0 393
michael@0 394 NS_IMETHODIMP _OldCacheEntryWrapper::HasWriteAccess(bool aWriteAllowed_unused, bool *aWriteAccess)
michael@0 395 {
michael@0 396 NS_ENSURE_TRUE(mOldDesc, NS_ERROR_NULL_POINTER);
michael@0 397 NS_ENSURE_ARG(aWriteAccess);
michael@0 398
michael@0 399 nsCacheAccessMode mode;
michael@0 400 nsresult rv = mOldDesc->GetAccessGranted(&mode);
michael@0 401 NS_ENSURE_SUCCESS(rv, rv);
michael@0 402
michael@0 403 *aWriteAccess = !!(mode & nsICache::ACCESS_WRITE);
michael@0 404
michael@0 405 LOG(("_OldCacheEntryWrapper::HasWriteAccess [this=%p, write-access=%d]", this, *aWriteAccess));
michael@0 406
michael@0 407 return NS_OK;
michael@0 408 }
michael@0 409
michael@0 410
michael@0 411 namespace { // anon
michael@0 412
michael@0 413 nsresult
michael@0 414 GetCacheSessionNameForStoragePolicy(
michael@0 415 nsCSubstring const &scheme,
michael@0 416 nsCacheStoragePolicy storagePolicy,
michael@0 417 bool isPrivate,
michael@0 418 uint32_t appId,
michael@0 419 bool inBrowser,
michael@0 420 nsACString& sessionName)
michael@0 421 {
michael@0 422 MOZ_ASSERT(!isPrivate || storagePolicy == nsICache::STORE_IN_MEMORY);
michael@0 423
michael@0 424 // HTTP
michael@0 425 if (scheme.Equals(NS_LITERAL_CSTRING("http")) ||
michael@0 426 scheme.Equals(NS_LITERAL_CSTRING("https"))) {
michael@0 427 switch (storagePolicy) {
michael@0 428 case nsICache::STORE_IN_MEMORY:
michael@0 429 if (isPrivate)
michael@0 430 sessionName.Assign(NS_LITERAL_CSTRING("HTTP-memory-only-PB"));
michael@0 431 else
michael@0 432 sessionName.Assign(NS_LITERAL_CSTRING("HTTP-memory-only"));
michael@0 433 break;
michael@0 434 case nsICache::STORE_OFFLINE:
michael@0 435 // XXX This is actually never used, only added to prevent
michael@0 436 // any compatibility damage.
michael@0 437 sessionName.Assign(NS_LITERAL_CSTRING("HTTP-offline"));
michael@0 438 break;
michael@0 439 default:
michael@0 440 sessionName.Assign(NS_LITERAL_CSTRING("HTTP"));
michael@0 441 break;
michael@0 442 }
michael@0 443 }
michael@0 444 // WYCIWYG
michael@0 445 else if (scheme.Equals(NS_LITERAL_CSTRING("wyciwyg"))) {
michael@0 446 if (isPrivate)
michael@0 447 sessionName.Assign(NS_LITERAL_CSTRING("wyciwyg-private"));
michael@0 448 else
michael@0 449 sessionName.Assign(NS_LITERAL_CSTRING("wyciwyg"));
michael@0 450 }
michael@0 451 // FTP
michael@0 452 else if (scheme.Equals(NS_LITERAL_CSTRING("ftp"))) {
michael@0 453 if (isPrivate)
michael@0 454 sessionName.Assign(NS_LITERAL_CSTRING("FTP-private"));
michael@0 455 else
michael@0 456 sessionName.Assign(NS_LITERAL_CSTRING("FTP"));
michael@0 457 }
michael@0 458 // all remaining URL scheme
michael@0 459 else {
michael@0 460 // Since with the new API a consumer cannot specify its own session name
michael@0 461 // and partitioning of the cache is handled stricly only by the cache
michael@0 462 // back-end internally, we will use a separate session name to pretend
michael@0 463 // functionality of the new API wrapping the Darin's cache for all other
michael@0 464 // URL schemes.
michael@0 465 // Deliberately omitting |anonymous| since other session types don't
michael@0 466 // recognize it too.
michael@0 467 sessionName.Assign(NS_LITERAL_CSTRING("other"));
michael@0 468 if (isPrivate)
michael@0 469 sessionName.Append(NS_LITERAL_CSTRING("-private"));
michael@0 470 }
michael@0 471
michael@0 472 if (appId != nsILoadContextInfo::NO_APP_ID || inBrowser) {
michael@0 473 sessionName.Append('~');
michael@0 474 sessionName.AppendInt(appId);
michael@0 475 sessionName.Append('~');
michael@0 476 sessionName.AppendInt(inBrowser);
michael@0 477 }
michael@0 478
michael@0 479 return NS_OK;
michael@0 480 }
michael@0 481
michael@0 482 nsresult
michael@0 483 GetCacheSession(nsCSubstring const &aScheme,
michael@0 484 bool aWriteToDisk,
michael@0 485 nsILoadContextInfo* aLoadInfo,
michael@0 486 nsIApplicationCache* aAppCache,
michael@0 487 nsICacheSession** _result)
michael@0 488 {
michael@0 489 nsresult rv;
michael@0 490
michael@0 491 nsCacheStoragePolicy storagePolicy;
michael@0 492 if (aAppCache)
michael@0 493 storagePolicy = nsICache::STORE_OFFLINE;
michael@0 494 else if (!aWriteToDisk || aLoadInfo->IsPrivate())
michael@0 495 storagePolicy = nsICache::STORE_IN_MEMORY;
michael@0 496 else
michael@0 497 storagePolicy = nsICache::STORE_ANYWHERE;
michael@0 498
michael@0 499 nsAutoCString clientId;
michael@0 500 if (aAppCache) {
michael@0 501 aAppCache->GetClientID(clientId);
michael@0 502 }
michael@0 503 else {
michael@0 504 rv = GetCacheSessionNameForStoragePolicy(
michael@0 505 aScheme,
michael@0 506 storagePolicy,
michael@0 507 aLoadInfo->IsPrivate(),
michael@0 508 aLoadInfo->AppId(),
michael@0 509 aLoadInfo->IsInBrowserElement(),
michael@0 510 clientId);
michael@0 511 NS_ENSURE_SUCCESS(rv, rv);
michael@0 512 }
michael@0 513
michael@0 514 LOG((" GetCacheSession for client=%s, policy=%d", clientId.get(), storagePolicy));
michael@0 515
michael@0 516 nsCOMPtr<nsICacheService> serv =
michael@0 517 do_GetService(NS_CACHESERVICE_CONTRACTID, &rv);
michael@0 518 NS_ENSURE_SUCCESS(rv, rv);
michael@0 519
michael@0 520 nsCOMPtr<nsICacheSession> session;
michael@0 521 rv = serv->CreateSession(clientId.get(),
michael@0 522 storagePolicy,
michael@0 523 nsICache::STREAM_BASED,
michael@0 524 getter_AddRefs(session));
michael@0 525 NS_ENSURE_SUCCESS(rv, rv);
michael@0 526
michael@0 527 rv = session->SetIsPrivate(aLoadInfo->IsPrivate());
michael@0 528 NS_ENSURE_SUCCESS(rv, rv);
michael@0 529
michael@0 530 rv = session->SetDoomEntriesIfExpired(false);
michael@0 531 NS_ENSURE_SUCCESS(rv, rv);
michael@0 532
michael@0 533 if (aAppCache) {
michael@0 534 nsCOMPtr<nsIFile> profileDirectory;
michael@0 535 aAppCache->GetProfileDirectory(getter_AddRefs(profileDirectory));
michael@0 536 if (profileDirectory)
michael@0 537 rv = session->SetProfileDirectory(profileDirectory);
michael@0 538 NS_ENSURE_SUCCESS(rv, rv);
michael@0 539 }
michael@0 540
michael@0 541 session.forget(_result);
michael@0 542 return NS_OK;
michael@0 543 }
michael@0 544
michael@0 545 } // anon
michael@0 546
michael@0 547
michael@0 548 NS_IMPL_ISUPPORTS_INHERITED(_OldCacheLoad, nsRunnable, nsICacheListener)
michael@0 549
michael@0 550 _OldCacheLoad::_OldCacheLoad(nsCSubstring const& aScheme,
michael@0 551 nsCSubstring const& aCacheKey,
michael@0 552 nsICacheEntryOpenCallback* aCallback,
michael@0 553 nsIApplicationCache* aAppCache,
michael@0 554 nsILoadContextInfo* aLoadInfo,
michael@0 555 bool aWriteToDisk,
michael@0 556 uint32_t aFlags)
michael@0 557 : mScheme(aScheme)
michael@0 558 , mCacheKey(aCacheKey)
michael@0 559 , mCallback(aCallback)
michael@0 560 , mLoadInfo(GetLoadContextInfo(aLoadInfo))
michael@0 561 , mFlags(aFlags)
michael@0 562 , mWriteToDisk(aWriteToDisk)
michael@0 563 , mNew(true)
michael@0 564 , mOpening(true)
michael@0 565 , mSync(false)
michael@0 566 , mStatus(NS_ERROR_UNEXPECTED)
michael@0 567 , mRunCount(0)
michael@0 568 , mAppCache(aAppCache)
michael@0 569 {
michael@0 570 MOZ_COUNT_CTOR(_OldCacheLoad);
michael@0 571 }
michael@0 572
michael@0 573 _OldCacheLoad::~_OldCacheLoad()
michael@0 574 {
michael@0 575 ProxyReleaseMainThread(mAppCache);
michael@0 576 MOZ_COUNT_DTOR(_OldCacheLoad);
michael@0 577 }
michael@0 578
michael@0 579 nsresult _OldCacheLoad::Start()
michael@0 580 {
michael@0 581 LOG(("_OldCacheLoad::Start [this=%p, key=%s]", this, mCacheKey.get()));
michael@0 582
michael@0 583 mLoadStart = mozilla::TimeStamp::Now();
michael@0 584
michael@0 585 nsresult rv;
michael@0 586
michael@0 587 // Consumers that can invoke this code as first and off the main thread
michael@0 588 // are responsible for initiating these two services on the main thread.
michael@0 589 // Currently this is only nsWyciwygChannel.
michael@0 590
michael@0 591 // XXX: Start the cache service; otherwise DispatchToCacheIOThread will
michael@0 592 // fail.
michael@0 593 nsCOMPtr<nsICacheService> service =
michael@0 594 do_GetService(NS_CACHESERVICE_CONTRACTID, &rv);
michael@0 595
michael@0 596 // Ensure the stream transport service gets initialized on the main thread
michael@0 597 if (NS_SUCCEEDED(rv) && NS_IsMainThread()) {
michael@0 598 nsCOMPtr<nsIStreamTransportService> sts =
michael@0 599 do_GetService(kStreamTransportServiceCID, &rv);
michael@0 600 }
michael@0 601
michael@0 602 if (NS_SUCCEEDED(rv)) {
michael@0 603 rv = service->GetCacheIOTarget(getter_AddRefs(mCacheThread));
michael@0 604 }
michael@0 605
michael@0 606 if (NS_SUCCEEDED(rv)) {
michael@0 607 bool onCacheTarget;
michael@0 608 rv = mCacheThread->IsOnCurrentThread(&onCacheTarget);
michael@0 609 if (NS_SUCCEEDED(rv) && onCacheTarget) {
michael@0 610 mSync = true;
michael@0 611 }
michael@0 612 }
michael@0 613
michael@0 614 if (NS_SUCCEEDED(rv)) {
michael@0 615 if (mSync) {
michael@0 616 rv = Run();
michael@0 617 }
michael@0 618 else {
michael@0 619 rv = mCacheThread->Dispatch(this, NS_DISPATCH_NORMAL);
michael@0 620 }
michael@0 621 }
michael@0 622
michael@0 623 return rv;
michael@0 624 }
michael@0 625
michael@0 626 NS_IMETHODIMP
michael@0 627 _OldCacheLoad::Run()
michael@0 628 {
michael@0 629 LOG(("_OldCacheLoad::Run [this=%p, key=%s, cb=%p]", this, mCacheKey.get(), mCallback.get()));
michael@0 630
michael@0 631 nsresult rv;
michael@0 632
michael@0 633 if (mOpening) {
michael@0 634 mOpening = false;
michael@0 635 nsCOMPtr<nsICacheSession> session;
michael@0 636 rv = GetCacheSession(mScheme, mWriteToDisk, mLoadInfo, mAppCache,
michael@0 637 getter_AddRefs(session));
michael@0 638 if (NS_SUCCEEDED(rv)) {
michael@0 639 // AsyncOpenCacheEntry isn't really async when its called on the
michael@0 640 // cache service thread.
michael@0 641
michael@0 642 nsCacheAccessMode cacheAccess;
michael@0 643 if (mFlags & nsICacheStorage::OPEN_TRUNCATE)
michael@0 644 cacheAccess = nsICache::ACCESS_WRITE;
michael@0 645 else if ((mFlags & nsICacheStorage::OPEN_READONLY) || mAppCache)
michael@0 646 cacheAccess = nsICache::ACCESS_READ;
michael@0 647 else
michael@0 648 cacheAccess = nsICache::ACCESS_READ_WRITE;
michael@0 649
michael@0 650 LOG((" session->AsyncOpenCacheEntry with access=%d", cacheAccess));
michael@0 651
michael@0 652 bool bypassBusy = mFlags & nsICacheStorage::OPEN_BYPASS_IF_BUSY;
michael@0 653
michael@0 654 if (mSync && cacheAccess == nsICache::ACCESS_WRITE) {
michael@0 655 nsCOMPtr<nsICacheEntryDescriptor> entry;
michael@0 656 rv = session->OpenCacheEntry(mCacheKey, cacheAccess, bypassBusy,
michael@0 657 getter_AddRefs(entry));
michael@0 658
michael@0 659 nsCacheAccessMode grantedAccess = 0;
michael@0 660 if (NS_SUCCEEDED(rv)) {
michael@0 661 entry->GetAccessGranted(&grantedAccess);
michael@0 662 }
michael@0 663
michael@0 664 return OnCacheEntryAvailable(entry, grantedAccess, rv);
michael@0 665 }
michael@0 666
michael@0 667 rv = session->AsyncOpenCacheEntry(mCacheKey, cacheAccess, this, bypassBusy);
michael@0 668 if (NS_SUCCEEDED(rv))
michael@0 669 return NS_OK;
michael@0 670 }
michael@0 671
michael@0 672 // Opening failed, propagate the error to the consumer
michael@0 673 LOG((" Opening cache entry failed with rv=0x%08x", rv));
michael@0 674 mStatus = rv;
michael@0 675 mNew = false;
michael@0 676 NS_DispatchToMainThread(this);
michael@0 677 } else {
michael@0 678 if (!mCallback) {
michael@0 679 LOG((" duplicate call, bypassed"));
michael@0 680 return NS_OK;
michael@0 681 }
michael@0 682
michael@0 683 if (NS_SUCCEEDED(mStatus)) {
michael@0 684 if (mFlags & nsICacheStorage::OPEN_TRUNCATE) {
michael@0 685 mozilla::Telemetry::AccumulateTimeDelta(
michael@0 686 mozilla::Telemetry::NETWORK_CACHE_V1_TRUNCATE_TIME_MS,
michael@0 687 mLoadStart);
michael@0 688 }
michael@0 689 else if (mNew) {
michael@0 690 mozilla::Telemetry::AccumulateTimeDelta(
michael@0 691 mozilla::Telemetry::NETWORK_CACHE_V1_MISS_TIME_MS,
michael@0 692 mLoadStart);
michael@0 693 }
michael@0 694 else {
michael@0 695 mozilla::Telemetry::AccumulateTimeDelta(
michael@0 696 mozilla::Telemetry::NETWORK_CACHE_V1_HIT_TIME_MS,
michael@0 697 mLoadStart);
michael@0 698 }
michael@0 699 }
michael@0 700
michael@0 701 if (!(mFlags & CHECK_MULTITHREADED))
michael@0 702 Check();
michael@0 703
michael@0 704 // break cycles
michael@0 705 nsCOMPtr<nsICacheEntryOpenCallback> cb = mCallback.forget();
michael@0 706 mCacheThread = nullptr;
michael@0 707 nsCOMPtr<nsICacheEntry> entry = mCacheEntry.forget();
michael@0 708
michael@0 709 rv = cb->OnCacheEntryAvailable(entry, mNew, mAppCache, mStatus);
michael@0 710
michael@0 711 if (NS_FAILED(rv) && entry) {
michael@0 712 LOG((" cb->OnCacheEntryAvailable failed with rv=0x%08x", rv));
michael@0 713 if (mNew)
michael@0 714 entry->AsyncDoom(nullptr);
michael@0 715 else
michael@0 716 entry->Close();
michael@0 717 }
michael@0 718 }
michael@0 719
michael@0 720 return rv;
michael@0 721 }
michael@0 722
michael@0 723 NS_IMETHODIMP
michael@0 724 _OldCacheLoad::OnCacheEntryAvailable(nsICacheEntryDescriptor *entry,
michael@0 725 nsCacheAccessMode access,
michael@0 726 nsresult status)
michael@0 727 {
michael@0 728 LOG(("_OldCacheLoad::OnCacheEntryAvailable [this=%p, ent=%p, cb=%p, appcache=%p, access=%x]",
michael@0 729 this, entry, mCallback.get(), mAppCache.get(), access));
michael@0 730
michael@0 731 // XXX Bug 759805: Sometimes we will call this method directly from
michael@0 732 // HttpCacheQuery::Run when AsyncOpenCacheEntry fails, but
michael@0 733 // AsyncOpenCacheEntry will also call this method. As a workaround, we just
michael@0 734 // ensure we only execute this code once.
michael@0 735 NS_ENSURE_TRUE(mRunCount == 0, NS_ERROR_UNEXPECTED);
michael@0 736 ++mRunCount;
michael@0 737
michael@0 738 mCacheEntry = entry ? new _OldCacheEntryWrapper(entry) : nullptr;
michael@0 739 mStatus = status;
michael@0 740 mNew = access == nsICache::ACCESS_WRITE;
michael@0 741
michael@0 742 if (mFlags & CHECK_MULTITHREADED)
michael@0 743 Check();
michael@0 744
michael@0 745 if (mSync)
michael@0 746 return Run();
michael@0 747
michael@0 748 return NS_DispatchToMainThread(this);
michael@0 749 }
michael@0 750
michael@0 751 void
michael@0 752 _OldCacheLoad::Check()
michael@0 753 {
michael@0 754 if (!mCacheEntry)
michael@0 755 return;
michael@0 756
michael@0 757 if (mNew)
michael@0 758 return;
michael@0 759
michael@0 760 uint32_t result;
michael@0 761 nsresult rv = mCallback->OnCacheEntryCheck(mCacheEntry, mAppCache, &result);
michael@0 762 LOG((" OnCacheEntryCheck result ent=%p, cb=%p, appcache=%p, rv=0x%08x, result=%d",
michael@0 763 mCacheEntry.get(), mCallback.get(), mAppCache.get(), rv, result));
michael@0 764
michael@0 765 if (NS_FAILED(rv)) {
michael@0 766 NS_WARNING("cache check failed");
michael@0 767 }
michael@0 768
michael@0 769 if (NS_FAILED(rv) || result == nsICacheEntryOpenCallback::ENTRY_NOT_WANTED) {
michael@0 770 mCacheEntry->Close();
michael@0 771 mCacheEntry = nullptr;
michael@0 772 mStatus = NS_ERROR_CACHE_KEY_NOT_FOUND;
michael@0 773 }
michael@0 774 }
michael@0 775
michael@0 776 NS_IMETHODIMP
michael@0 777 _OldCacheLoad::OnCacheEntryDoomed(nsresult)
michael@0 778 {
michael@0 779 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 780 }
michael@0 781
michael@0 782 // nsICacheStorage old cache wrapper
michael@0 783
michael@0 784 NS_IMPL_ISUPPORTS(_OldStorage, nsICacheStorage)
michael@0 785
michael@0 786 _OldStorage::_OldStorage(nsILoadContextInfo* aInfo,
michael@0 787 bool aAllowDisk,
michael@0 788 bool aLookupAppCache,
michael@0 789 bool aOfflineStorage,
michael@0 790 nsIApplicationCache* aAppCache)
michael@0 791 : mLoadInfo(GetLoadContextInfo(aInfo))
michael@0 792 , mAppCache(aAppCache)
michael@0 793 , mWriteToDisk(aAllowDisk)
michael@0 794 , mLookupAppCache(aLookupAppCache)
michael@0 795 , mOfflineStorage(aOfflineStorage)
michael@0 796 {
michael@0 797 MOZ_COUNT_CTOR(_OldStorage);
michael@0 798 }
michael@0 799
michael@0 800 _OldStorage::~_OldStorage()
michael@0 801 {
michael@0 802 MOZ_COUNT_DTOR(_OldStorage);
michael@0 803 }
michael@0 804
michael@0 805 NS_IMETHODIMP _OldStorage::AsyncOpenURI(nsIURI *aURI,
michael@0 806 const nsACString & aIdExtension,
michael@0 807 uint32_t aFlags,
michael@0 808 nsICacheEntryOpenCallback *aCallback)
michael@0 809 {
michael@0 810 NS_ENSURE_ARG(aURI);
michael@0 811 NS_ENSURE_ARG(aCallback);
michael@0 812
michael@0 813 #ifdef MOZ_LOGGING
michael@0 814 nsAutoCString uriSpec;
michael@0 815 aURI->GetAsciiSpec(uriSpec);
michael@0 816 LOG(("_OldStorage::AsyncOpenURI [this=%p, uri=%s, ide=%s, flags=%x]",
michael@0 817 this, uriSpec.get(), aIdExtension.BeginReading(), aFlags));
michael@0 818 #endif
michael@0 819
michael@0 820 nsresult rv;
michael@0 821
michael@0 822 nsAutoCString cacheKey, scheme;
michael@0 823 rv = AssembleCacheKey(aURI, aIdExtension, cacheKey, scheme);
michael@0 824 NS_ENSURE_SUCCESS(rv, rv);
michael@0 825
michael@0 826 if (!mAppCache && (mLookupAppCache || mOfflineStorage)) {
michael@0 827 rv = ChooseApplicationCache(cacheKey, getter_AddRefs(mAppCache));
michael@0 828 NS_ENSURE_SUCCESS(rv, rv);
michael@0 829
michael@0 830 if (mAppCache) {
michael@0 831 // From a chosen appcache open only as readonly
michael@0 832 aFlags &= ~nsICacheStorage::OPEN_TRUNCATE;
michael@0 833 }
michael@0 834 }
michael@0 835
michael@0 836 nsRefPtr<_OldCacheLoad> cacheLoad =
michael@0 837 new _OldCacheLoad(scheme, cacheKey, aCallback, mAppCache,
michael@0 838 mLoadInfo, mWriteToDisk, aFlags);
michael@0 839
michael@0 840 rv = cacheLoad->Start();
michael@0 841 NS_ENSURE_SUCCESS(rv, rv);
michael@0 842
michael@0 843 return NS_OK;
michael@0 844 }
michael@0 845
michael@0 846 NS_IMETHODIMP _OldStorage::AsyncDoomURI(nsIURI *aURI, const nsACString & aIdExtension,
michael@0 847 nsICacheEntryDoomCallback* aCallback)
michael@0 848 {
michael@0 849 LOG(("_OldStorage::AsyncDoomURI"));
michael@0 850
michael@0 851 nsresult rv;
michael@0 852
michael@0 853 nsAutoCString cacheKey, scheme;
michael@0 854 rv = AssembleCacheKey(aURI, aIdExtension, cacheKey, scheme);
michael@0 855 NS_ENSURE_SUCCESS(rv, rv);
michael@0 856
michael@0 857 nsCOMPtr<nsICacheSession> session;
michael@0 858 rv = GetCacheSession(scheme, mWriteToDisk, mLoadInfo, mAppCache,
michael@0 859 getter_AddRefs(session));
michael@0 860 NS_ENSURE_SUCCESS(rv, rv);
michael@0 861
michael@0 862 nsRefPtr<DoomCallbackWrapper> cb = aCallback
michael@0 863 ? new DoomCallbackWrapper(aCallback)
michael@0 864 : nullptr;
michael@0 865 rv = session->DoomEntry(cacheKey, cb);
michael@0 866 NS_ENSURE_SUCCESS(rv, rv);
michael@0 867
michael@0 868 return NS_OK;
michael@0 869 }
michael@0 870
michael@0 871 NS_IMETHODIMP _OldStorage::AsyncEvictStorage(nsICacheEntryDoomCallback* aCallback)
michael@0 872 {
michael@0 873 LOG(("_OldStorage::AsyncEvictStorage"));
michael@0 874
michael@0 875 nsresult rv;
michael@0 876
michael@0 877 if (!mAppCache && mOfflineStorage) {
michael@0 878 // Special casing for pure offline storage
michael@0 879 if (mLoadInfo->AppId() == nsILoadContextInfo::NO_APP_ID &&
michael@0 880 !mLoadInfo->IsInBrowserElement()) {
michael@0 881
michael@0 882 // Clear everything.
michael@0 883 nsCOMPtr<nsICacheService> serv =
michael@0 884 do_GetService(NS_CACHESERVICE_CONTRACTID, &rv);
michael@0 885 NS_ENSURE_SUCCESS(rv, rv);
michael@0 886
michael@0 887 rv = serv->EvictEntries(nsICache::STORE_OFFLINE);
michael@0 888 NS_ENSURE_SUCCESS(rv, rv);
michael@0 889 }
michael@0 890 else {
michael@0 891 // Clear app or inbrowser staff.
michael@0 892 nsCOMPtr<nsIApplicationCacheService> appCacheService =
michael@0 893 do_GetService(NS_APPLICATIONCACHESERVICE_CONTRACTID, &rv);
michael@0 894 NS_ENSURE_SUCCESS(rv, rv);
michael@0 895
michael@0 896 rv = appCacheService->DiscardByAppId(mLoadInfo->AppId(),
michael@0 897 mLoadInfo->IsInBrowserElement());
michael@0 898 NS_ENSURE_SUCCESS(rv, rv);
michael@0 899 }
michael@0 900 }
michael@0 901 else {
michael@0 902 if (mAppCache) {
michael@0 903 nsCOMPtr<nsICacheSession> session;
michael@0 904 rv = GetCacheSession(EmptyCString(),
michael@0 905 mWriteToDisk, mLoadInfo, mAppCache,
michael@0 906 getter_AddRefs(session));
michael@0 907 NS_ENSURE_SUCCESS(rv, rv);
michael@0 908
michael@0 909 rv = session->EvictEntries();
michael@0 910 NS_ENSURE_SUCCESS(rv, rv);
michael@0 911 }
michael@0 912 else {
michael@0 913 // Oh, I'll be so happy when session names are gone...
michael@0 914 nsCOMPtr<nsICacheSession> session;
michael@0 915 rv = GetCacheSession(NS_LITERAL_CSTRING("http"),
michael@0 916 mWriteToDisk, mLoadInfo, mAppCache,
michael@0 917 getter_AddRefs(session));
michael@0 918 NS_ENSURE_SUCCESS(rv, rv);
michael@0 919
michael@0 920 rv = session->EvictEntries();
michael@0 921 NS_ENSURE_SUCCESS(rv, rv);
michael@0 922
michael@0 923 rv = GetCacheSession(NS_LITERAL_CSTRING("wyciwyg"),
michael@0 924 mWriteToDisk, mLoadInfo, mAppCache,
michael@0 925 getter_AddRefs(session));
michael@0 926 NS_ENSURE_SUCCESS(rv, rv);
michael@0 927
michael@0 928 rv = session->EvictEntries();
michael@0 929 NS_ENSURE_SUCCESS(rv, rv);
michael@0 930
michael@0 931 // This clears any data from scheme other then http, wyciwyg or ftp
michael@0 932 rv = GetCacheSession(EmptyCString(),
michael@0 933 mWriteToDisk, mLoadInfo, mAppCache,
michael@0 934 getter_AddRefs(session));
michael@0 935 NS_ENSURE_SUCCESS(rv, rv);
michael@0 936
michael@0 937 rv = session->EvictEntries();
michael@0 938 NS_ENSURE_SUCCESS(rv, rv);
michael@0 939 }
michael@0 940 }
michael@0 941
michael@0 942 if (aCallback) {
michael@0 943 nsRefPtr<DoomCallbackSynchronizer> sync =
michael@0 944 new DoomCallbackSynchronizer(aCallback);
michael@0 945 rv = sync->Dispatch();
michael@0 946 NS_ENSURE_SUCCESS(rv, rv);
michael@0 947 }
michael@0 948
michael@0 949 return NS_OK;
michael@0 950 }
michael@0 951
michael@0 952 NS_IMETHODIMP _OldStorage::AsyncVisitStorage(nsICacheStorageVisitor* aVisitor,
michael@0 953 bool aVisitEntries)
michael@0 954 {
michael@0 955 LOG(("_OldStorage::AsyncVisitStorage"));
michael@0 956
michael@0 957 NS_ENSURE_ARG(aVisitor);
michael@0 958
michael@0 959 if (mLoadInfo->IsAnonymous()) {
michael@0 960 // There is no concept of 'anonymous' storage in the old cache
michael@0 961 // since anon cache entries are stored in 'non-anon' storage
michael@0 962 // with a special prefix.
michael@0 963 // Just fake we have 0 items with 0 consumption. This at least
michael@0 964 // prevents displaying double size in the advanced section of
michael@0 965 // the Options dialog.
michael@0 966 aVisitor->OnCacheStorageInfo(0, 0);
michael@0 967 if (aVisitEntries)
michael@0 968 aVisitor->OnCacheEntryVisitCompleted();
michael@0 969 return NS_OK;
michael@0 970 }
michael@0 971
michael@0 972 nsresult rv;
michael@0 973
michael@0 974 nsCOMPtr<nsICacheService> serv =
michael@0 975 do_GetService(NS_CACHESERVICE_CONTRACTID, &rv);
michael@0 976 NS_ENSURE_SUCCESS(rv, rv);
michael@0 977
michael@0 978 char* deviceID;
michael@0 979 if (mAppCache || mOfflineStorage) {
michael@0 980 deviceID = const_cast<char*>("offline");
michael@0 981 } else if (!mWriteToDisk || mLoadInfo->IsPrivate()) {
michael@0 982 deviceID = const_cast<char*>("memory");
michael@0 983 } else {
michael@0 984 deviceID = const_cast<char*>("disk");
michael@0 985 }
michael@0 986
michael@0 987 nsRefPtr<VisitCallbackWrapper> cb = new VisitCallbackWrapper(
michael@0 988 deviceID, aVisitor, aVisitEntries);
michael@0 989 rv = serv->VisitEntries(cb);
michael@0 990 NS_ENSURE_SUCCESS(rv, rv);
michael@0 991
michael@0 992 return NS_OK;
michael@0 993 }
michael@0 994
michael@0 995 // Internal
michael@0 996
michael@0 997 nsresult _OldStorage::AssembleCacheKey(nsIURI *aURI,
michael@0 998 nsACString const & aIdExtension,
michael@0 999 nsACString & aCacheKey,
michael@0 1000 nsACString & aScheme)
michael@0 1001 {
michael@0 1002 // Copied from nsHttpChannel::AssembleCacheKey
michael@0 1003
michael@0 1004 aCacheKey.Truncate();
michael@0 1005
michael@0 1006 nsresult rv;
michael@0 1007
michael@0 1008 rv = aURI->GetScheme(aScheme);
michael@0 1009 NS_ENSURE_SUCCESS(rv, rv);
michael@0 1010
michael@0 1011 nsAutoCString uriSpec;
michael@0 1012 if (aScheme.Equals(NS_LITERAL_CSTRING("http")) ||
michael@0 1013 aScheme.Equals(NS_LITERAL_CSTRING("https"))) {
michael@0 1014 if (mLoadInfo->IsAnonymous()) {
michael@0 1015 aCacheKey.AssignLiteral("anon&");
michael@0 1016 }
michael@0 1017
michael@0 1018 if (!aIdExtension.IsEmpty()) {
michael@0 1019 aCacheKey.AppendPrintf("id=%s&", aIdExtension.BeginReading());
michael@0 1020 }
michael@0 1021
michael@0 1022 nsCOMPtr<nsIURI> noRefURI;
michael@0 1023 rv = aURI->CloneIgnoringRef(getter_AddRefs(noRefURI));
michael@0 1024 NS_ENSURE_SUCCESS(rv, rv);
michael@0 1025
michael@0 1026 rv = noRefURI->GetAsciiSpec(uriSpec);
michael@0 1027 NS_ENSURE_SUCCESS(rv, rv);
michael@0 1028
michael@0 1029 if (!aCacheKey.IsEmpty()) {
michael@0 1030 aCacheKey.AppendLiteral("uri=");
michael@0 1031 }
michael@0 1032 }
michael@0 1033 else if (aScheme.Equals(NS_LITERAL_CSTRING("wyciwyg"))) {
michael@0 1034 rv = aURI->GetSpec(uriSpec);
michael@0 1035 NS_ENSURE_SUCCESS(rv, rv);
michael@0 1036 }
michael@0 1037 else {
michael@0 1038 rv = aURI->GetAsciiSpec(uriSpec);
michael@0 1039 NS_ENSURE_SUCCESS(rv, rv);
michael@0 1040 }
michael@0 1041
michael@0 1042 aCacheKey.Append(uriSpec);
michael@0 1043
michael@0 1044 return NS_OK;
michael@0 1045 }
michael@0 1046
michael@0 1047 nsresult _OldStorage::ChooseApplicationCache(nsCSubstring const &cacheKey,
michael@0 1048 nsIApplicationCache** aCache)
michael@0 1049 {
michael@0 1050 nsresult rv;
michael@0 1051
michael@0 1052 nsCOMPtr<nsIApplicationCacheService> appCacheService =
michael@0 1053 do_GetService(NS_APPLICATIONCACHESERVICE_CONTRACTID, &rv);
michael@0 1054 NS_ENSURE_SUCCESS(rv, rv);
michael@0 1055
michael@0 1056 rv = appCacheService->ChooseApplicationCache(cacheKey, mLoadInfo, aCache);
michael@0 1057 NS_ENSURE_SUCCESS(rv, rv);
michael@0 1058
michael@0 1059 return NS_OK;
michael@0 1060 }
michael@0 1061
michael@0 1062 } // net
michael@0 1063 } // mozilla

mercurial