diff options
author | michaeln@chromium.org <michaeln@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-17 22:02:28 +0000 |
---|---|---|
committer | michaeln@chromium.org <michaeln@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-17 22:02:28 +0000 |
commit | fd2885ab79962663f474a39b13cfbfdb4827534e (patch) | |
tree | 1ce0e0967d005178a64cf0d4a7e11e16a76fd3d8 /webkit/appcache/appcache_disk_cache.cc | |
parent | 9348c1f762db498d399d07c0295e36d75ee7fa08 (diff) | |
download | chromium_src-fd2885ab79962663f474a39b13cfbfdb4827534e.zip chromium_src-fd2885ab79962663f474a39b13cfbfdb4827534e.tar.gz chromium_src-fd2885ab79962663f474a39b13cfbfdb4827534e.tar.bz2 |
AppCache: Migrate to the DiskCache's async interface and use the CacheType::APP_CACHE value.
BUG=38273
TEST=existing layout tests and unit tests apply
Review URL: http://codereview.chromium.org/886003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@41884 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/appcache/appcache_disk_cache.cc')
-rw-r--r-- | webkit/appcache/appcache_disk_cache.cc | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/webkit/appcache/appcache_disk_cache.cc b/webkit/appcache/appcache_disk_cache.cc new file mode 100644 index 0000000..b8b25bf --- /dev/null +++ b/webkit/appcache/appcache_disk_cache.cc @@ -0,0 +1,155 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "webkit/appcache/appcache_disk_cache.h" + +#include "base/file_path.h" +#include "base/logging.h" +#include "base/string_util.h" +#include "net/base/net_errors.h" + +namespace appcache { + +AppCacheDiskCache::AppCacheDiskCache() + : is_disabled_(false), init_callback_(NULL) { +} + +AppCacheDiskCache::~AppCacheDiskCache() { + if (create_backend_callback_) { + create_backend_callback_->Cancel(); + create_backend_callback_.release(); + OnCreateBackendComplete(net::ERR_ABORTED); + } +} + +int AppCacheDiskCache::InitWithDiskBackend( + const FilePath& disk_cache_directory, int disk_cache_size, bool force, + net::CompletionCallback* callback) { + return Init(net::APP_CACHE, disk_cache_directory, + disk_cache_size, force, callback); +} + +int AppCacheDiskCache::InitWithMemBackend( + int mem_cache_size, net::CompletionCallback* callback) { + return Init(net::MEMORY_CACHE, FilePath(), mem_cache_size, false, callback); +} + +void AppCacheDiskCache::Disable() { + if (is_disabled_) + return; + + is_disabled_ = true; + + if (create_backend_callback_) { + create_backend_callback_->Cancel(); + create_backend_callback_.release(); + OnCreateBackendComplete(net::ERR_ABORTED); + } +} + +int AppCacheDiskCache::CreateEntry(int64 key, disk_cache::Entry** entry, + net::CompletionCallback* callback) { + if (is_disabled_) + return net::ERR_ABORTED; + + if (is_initializing()) { + pending_calls_.push_back(PendingCall(CREATE, key, entry, callback)); + return net::ERR_IO_PENDING; + } + + if (!disk_cache_.get()) + return net::ERR_FAILED; + + return disk_cache_->CreateEntry(Int64ToString(key), entry, callback); +} + +int AppCacheDiskCache::OpenEntry(int64 key, disk_cache::Entry** entry, + net::CompletionCallback* callback) { + if (is_disabled_) + return net::ERR_ABORTED; + + if (is_initializing()) { + pending_calls_.push_back(PendingCall(OPEN, key, entry, callback)); + return net::ERR_IO_PENDING; + } + + if (!disk_cache_.get()) + return net::ERR_FAILED; + + return disk_cache_->OpenEntry(Int64ToString(key), entry, callback); +} + +int AppCacheDiskCache::DoomEntry(int64 key, + net::CompletionCallback* callback) { + if (is_disabled_) + return net::ERR_ABORTED; + + if (is_initializing()) { + pending_calls_.push_back(PendingCall(DOOM, key, NULL, callback)); + return net::ERR_IO_PENDING; + } + + if (!disk_cache_.get()) + return net::ERR_FAILED; + + return disk_cache_->DoomEntry(Int64ToString(key), callback); +} + +int AppCacheDiskCache::Init(net::CacheType cache_type, + const FilePath& cache_directory, + int cache_size, bool force, + net::CompletionCallback* callback) { + DCHECK(!is_initializing() && !disk_cache_.get()); + is_disabled_ = false; + create_backend_callback_ = new CreateBackendCallback( + this, &AppCacheDiskCache::OnCreateBackendComplete); + int rv = disk_cache::CreateCacheBackend( + cache_type, cache_directory, cache_size, force, + &(create_backend_callback_->backend_ptr_), create_backend_callback_); + if (rv == net::ERR_IO_PENDING) + init_callback_ = callback; + else + OnCreateBackendComplete(rv); + return rv; +} + +void AppCacheDiskCache::OnCreateBackendComplete(int rv) { + if (rv == net::OK) { + disk_cache_.reset(create_backend_callback_->backend_ptr_); + create_backend_callback_->backend_ptr_ = NULL; + } + create_backend_callback_ = NULL; + + // Invoke our clients callback function. + if (init_callback_) { + init_callback_->Run(rv); + init_callback_ = NULL; + } + + // Service pending calls that were queued up while we were initailizating. + for (PendingCalls::const_iterator iter = pending_calls_.begin(); + iter < pending_calls_.end(); ++iter) { + int rv = net::ERR_FAILED; + switch (iter->call_type) { + case CREATE: + rv = CreateEntry(iter->key, iter->entry, iter->callback); + break; + case OPEN: + rv = OpenEntry(iter->key, iter->entry, iter->callback); + break; + case DOOM: + rv = DoomEntry(iter->key, iter->callback); + break; + default: + NOTREACHED(); + break; + } + if (rv != net::ERR_IO_PENDING) + iter->callback->Run(rv); + } + pending_calls_.clear(); +} + +} // namespace appcache + |