summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjennb@chromium.org <jennb@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-08-26 23:10:09 +0000
committerjennb@chromium.org <jennb@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-08-26 23:10:09 +0000
commit49672df7e74a0633a0ee92e89b4daff904765638 (patch)
treede848215c76faf23ad3e99d456b8a056c8e61491
parentc21b90496d356e5d8e56231dc8c4d4962a67f117 (diff)
downloadchromium_src-49672df7e74a0633a0ee92e89b4daff904765638.zip
chromium_src-49672df7e74a0633a0ee92e89b4daff904765638.tar.gz
chromium_src-49672df7e74a0633a0ee92e89b4daff904765638.tar.bz2
Skeleton classes for appcache library framework. This is a work in progress. Committing early to allow other code to use these objects, but still need to consider refptrs and add unittests.
TEST=none (will add when classes are fleshed out more) BUG=none Review URL: http://codereview.chromium.org/174034 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@24554 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--webkit/appcache/appcache.cc51
-rw-r--r--webkit/appcache/appcache.h85
-rw-r--r--webkit/appcache/appcache_entry.h45
-rw-r--r--webkit/appcache/appcache_group.cc75
-rw-r--r--webkit/appcache/appcache_group.h63
-rw-r--r--webkit/appcache/appcache_host.cc25
-rw-r--r--webkit/appcache/appcache_host.h40
-rw-r--r--webkit/appcache/appcache_service.cc50
-rw-r--r--webkit/appcache/appcache_service.h60
-rw-r--r--webkit/webkit.gyp9
10 files changed, 503 insertions, 0 deletions
diff --git a/webkit/appcache/appcache.cc b/webkit/appcache/appcache.cc
new file mode 100644
index 0000000..d202308
--- /dev/null
+++ b/webkit/appcache/appcache.cc
@@ -0,0 +1,51 @@
+// Copyright (c) 2009 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.h"
+
+#include "base/logging.h"
+#include "webkit/appcache/appcache_group.h"
+#include "webkit/appcache/appcache_host.h"
+#include "webkit/appcache/appcache_service.h"
+
+namespace appcache {
+
+AppCache::~AppCache() {
+ DCHECK(associated_hosts_.empty());
+ DCHECK(!owning_group_);
+ service_->RemoveCache(this);
+}
+
+void AppCache::UnassociateHost(AppCacheHost* host) {
+ associated_hosts_.erase(host);
+
+ // Inform group if this cache is no longer in use.
+ if (associated_hosts_.empty()) {
+ if (!owning_group_ || owning_group_->RemoveCache(this)) {
+ owning_group_ = NULL;
+ delete this;
+ }
+ }
+}
+
+void AppCache::AddEntry(const GURL& url, const AppCacheEntry& entry) {
+ DCHECK(entries_.find(url) == entries_.end());
+ entries_.insert(EntryMap::value_type(url, entry));
+}
+
+void AppCache::AddOrModifyEntry(const GURL& url, const AppCacheEntry& entry) {
+ std::pair<EntryMap::iterator, bool> ret =
+ entries_.insert(EntryMap::value_type(url, entry));
+
+ // Entry already exists. Merge the types of the new and existing entries.
+ if (!ret.second)
+ ret.first->second.add_types(entry.types());
+}
+
+AppCacheEntry* AppCache::GetEntry(const GURL& url) {
+ EntryMap::iterator it = entries_.find(url);
+ return (it != entries_.end()) ? &(it->second) : NULL;
+}
+
+} // namespace appcache
diff --git a/webkit/appcache/appcache.h b/webkit/appcache/appcache.h
new file mode 100644
index 0000000..c56673d
--- /dev/null
+++ b/webkit/appcache/appcache.h
@@ -0,0 +1,85 @@
+// Copyright (c) 2009 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.
+
+#ifndef WEBKIT_APPCACHE_APPCACHE_H_
+#define WEBKIT_APPCACHE_APPCACHE_H_
+
+#include <map>
+#include <set>
+#include <string>
+#include <vector>
+
+#include "base/time.h"
+#include "googleurl/src/gurl.h"
+#include "webkit/appcache/appcache_entry.h"
+#include "webkit/appcache/manifest_parser.h"
+
+namespace appcache {
+
+class AppCacheGroup;
+class AppCacheHost;
+class AppCacheService;
+
+// Set of cached resources for an application.
+class AppCache {
+ public:
+ // TODO(jennb): need constructor to set cache_id and service
+ ~AppCache();
+
+ int64 cache_id() { return cache_id_; }
+
+ AppCacheGroup* owning_group() { return owning_group_; }
+ void set_owning_group(AppCacheGroup* group) { owning_group_ = group; }
+
+ void AssociateHost(AppCacheHost* host) {
+ associated_hosts_.insert(host);
+ }
+
+ // Cache may be deleted after host is unassociated.
+ void UnassociateHost(AppCacheHost* host);
+
+ bool is_complete() { return is_complete_; }
+ void set_complete(bool value) { is_complete_ = value; }
+
+ // Adds a new entry. Entry must not already be in cache.
+ void AddEntry(const GURL& url, const AppCacheEntry& entry);
+
+ // Adds a new entry or modifies an existing entry by merging the types
+ // of the new entry with the existing entry.
+ void AddOrModifyEntry(const GURL& url, const AppCacheEntry& entry);
+
+ // Do not store the returned object as it could be deleted anytime.
+ AppCacheEntry* GetEntry(const GURL& url);
+
+ typedef std::map<GURL, AppCacheEntry> EntryMap;
+ const EntryMap& entries() { return entries_; }
+
+ bool IsNewerThan(AppCache* cache) {
+ return update_time_ > cache->update_time_;
+ }
+
+ private:
+ int64 cache_id_;
+ AppCacheEntry* manifest_; // also in entry map
+ AppCacheGroup* owning_group_;
+ std::set<AppCacheHost*> associated_hosts_;
+
+ EntryMap entries_; // contains entries of all types
+
+ std::vector<FallbackNamespace> fallback_namespaces_;
+ std::vector<GURL> online_whitelist_namespaces_;
+ bool online_whitelist_all_;
+
+ bool is_complete_;
+
+ // when this cache was last updated
+ base::TimeTicks update_time_;
+
+ // to notify service when cache is deleted
+ AppCacheService* service_;
+};
+
+} // namespace appcache
+
+#endif // WEBKIT_APPCACHE_APPCACHE_H_
diff --git a/webkit/appcache/appcache_entry.h b/webkit/appcache/appcache_entry.h
new file mode 100644
index 0000000..5629cc2
--- /dev/null
+++ b/webkit/appcache/appcache_entry.h
@@ -0,0 +1,45 @@
+// Copyright (c) 2009 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.
+
+#ifndef WEBKIT_APPCACHE_APPCACHE_ENTRY_H_
+#define WEBKIT_APPCACHE_APPCACHE_ENTRY_H_
+
+#include "googleurl/src/gurl.h"
+
+namespace appcache {
+
+// A cached entry is identified by a URL and is classified into one
+// (or more) categories. URL is not stored here as this class is stored
+// with the URL as a map key or the user of this class already knows the URL.
+class AppCacheEntry {
+ public:
+
+ // An entry can be of more than one type so use a bitmask.
+ enum Type {
+ MASTER = 1 << 0,
+ MANIFEST = 1 << 1,
+ EXPLICIT = 1 << 2,
+ FOREIGN = 1 << 3,
+ FALLBACK = 1 << 4,
+ };
+
+ explicit AppCacheEntry(int type) : types_(type) {}
+
+ int types() const { return types_; }
+ void add_types(int added_types) { types_ |= added_types; }
+ bool IsMaster() const { return types_ & MASTER; }
+ bool IsManifest() const { return types_ & MANIFEST; }
+ bool IsExplicit() const { return types_ & EXPLICIT; }
+ bool IsForeign() const { return types_ & FOREIGN; }
+ bool IsFallback() const { return types_ & FALLBACK; }
+
+ private:
+ int types_;
+
+ // TODO(jennb): response storage key
+};
+
+} // namespace appcache
+
+#endif // WEBKIT_APPCACHE_APPCACHE_RESOURCE_H_
diff --git a/webkit/appcache/appcache_group.cc b/webkit/appcache/appcache_group.cc
new file mode 100644
index 0000000..36f4fb0
--- /dev/null
+++ b/webkit/appcache/appcache_group.cc
@@ -0,0 +1,75 @@
+// Copyright (c) 2009 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_group.h"
+
+#include <algorithm>
+
+#include "base/logging.h"
+#include "webkit/appcache/appcache.h"
+#include "webkit/appcache/appcache_service.h"
+
+namespace appcache {
+
+AppCacheGroup::AppCacheGroup(AppCacheService* service,
+ const GURL& manifest_url)
+ : manifest_url_(manifest_url),
+ update_status_(IDLE),
+ is_obsolete_(false),
+ newest_complete_cache_(NULL),
+ service_(service) {
+ service_->AddGroup(this);
+}
+
+AppCacheGroup::~AppCacheGroup() {
+ DCHECK(old_caches_.empty());
+
+ // Newest complete cache might never have been associated with a host
+ // and thus would not be cleaned up by the backend impl during shutdown.
+ if (newest_complete_cache_) {
+ delete newest_complete_cache_;
+ }
+
+ service_->RemoveGroup(this);
+}
+
+void AppCacheGroup::AddCache(AppCache* complete_cache) {
+ if (!newest_complete_cache_) {
+ newest_complete_cache_ = complete_cache;
+ return;
+ }
+
+ if (complete_cache->IsNewerThan(newest_complete_cache_)) {
+ old_caches_.push_back(newest_complete_cache_);
+ newest_complete_cache_ = complete_cache;
+ } else {
+ old_caches_.push_back(complete_cache);
+ }
+}
+
+bool AppCacheGroup::RemoveCache(AppCache* cache) {
+ if (cache == newest_complete_cache_) {
+
+ // Cannot remove newest cache if there are older caches as those may
+ // eventually be swapped to the newest cache.
+ if (!old_caches_.empty())
+ return false;
+
+ newest_complete_cache_ = NULL; // cache will be deleted by caller
+ } else {
+ // Unused old cache can always be removed.
+ std::vector<AppCache*>::iterator it =
+ std::find(old_caches_.begin(), old_caches_.end(), cache);
+ if (it != old_caches_.end())
+ old_caches_.erase(it); // cache will be deleted by caller
+ }
+
+ if (old_caches_.empty() && !newest_complete_cache_) {
+ delete this;
+ }
+
+ return true;
+}
+
+} // namespace appcache
diff --git a/webkit/appcache/appcache_group.h b/webkit/appcache/appcache_group.h
new file mode 100644
index 0000000..05f437d
--- /dev/null
+++ b/webkit/appcache/appcache_group.h
@@ -0,0 +1,63 @@
+// Copyright (c) 2009 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.
+
+#ifndef WEBKIT_APPCACHE_APPCACHE_GROUP_H_
+#define WEBKIT_APPCACHE_APPCACHE_GROUP_H_
+
+#include <vector>
+
+#include "googleurl/src/gurl.h"
+
+namespace appcache {
+
+class AppCache;
+class AppCacheService;
+
+// Collection of application caches identified by the same manifest URL.
+class AppCacheGroup {
+ public:
+
+ enum UpdateStatus {
+ IDLE,
+ CHECKING,
+ DOWNLOADING,
+ };
+
+ AppCacheGroup(AppCacheService* service, const GURL& manifest_url);
+ ~AppCacheGroup();
+
+ const GURL& manifest_url() { return manifest_url_; }
+
+ UpdateStatus update_status() { return update_status_; }
+ void set_update_status(UpdateStatus status) { update_status_ = status; }
+
+ bool is_obsolete() { return is_obsolete_; }
+ void set_obsolete(bool value) { is_obsolete_ = value; }
+
+ AppCache* newest_complete_cache() { return newest_complete_cache_; }
+
+ void AddCache(AppCache* complete_cache);
+
+ // Returns false if cache cannot be removed. The newest complete cache
+ // cannot be removed as long as the group is still in use.
+ bool RemoveCache(AppCache* cache);
+
+ private:
+ GURL manifest_url_;
+ UpdateStatus update_status_;
+ bool is_obsolete_;
+
+ // old complete app caches
+ std::vector<AppCache*> old_caches_;
+
+ // newest cache in this group to be complete, aka relevant cache
+ AppCache* newest_complete_cache_;
+
+ // to notify service when group is no longer needed
+ AppCacheService* service_;
+};
+
+} // namespace appcache
+
+#endif // WEBKIT_APPCACHE_APPCACHE_GROUP_H_
diff --git a/webkit/appcache/appcache_host.cc b/webkit/appcache/appcache_host.cc
new file mode 100644
index 0000000..7585f606
--- /dev/null
+++ b/webkit/appcache/appcache_host.cc
@@ -0,0 +1,25 @@
+// Copyright (c) 2009 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_host.h"
+
+#include "base/logging.h"
+#include "webkit/appcache/appcache.h"
+#include "webkit/appcache/appcache_interfaces.h"
+
+namespace appcache {
+
+AppCacheHost::AppCacheHost(int host_id, AppCacheFrontend* frontend)
+ : host_id_(host_id),
+ selected_cache_(NULL),
+ frontend_(frontend) {
+}
+
+AppCacheHost::~AppCacheHost() {
+ if (selected_cache_) {
+ selected_cache_->UnassociateHost(this);
+ }
+}
+
+} // namespace appcache
diff --git a/webkit/appcache/appcache_host.h b/webkit/appcache/appcache_host.h
new file mode 100644
index 0000000..439e042
--- /dev/null
+++ b/webkit/appcache/appcache_host.h
@@ -0,0 +1,40 @@
+// Copyright (c) 2009 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.
+
+#ifndef WEBKIT_APPCACHE_APPCACHE_HOST_H_
+#define WEBKIT_APPCACHE_APPCACHE_HOST_H_
+
+namespace appcache {
+
+class AppCache;
+class AppCacheFrontend;
+
+// Server-side representation of an application cache host.
+class AppCacheHost {
+ public:
+ AppCacheHost(int host_id, AppCacheFrontend* frontend);
+ ~AppCacheHost();
+
+ int host_id() { return host_id_; }
+ AppCacheFrontend* frontend() { return frontend_; }
+
+ AppCache* selected_cache() { return selected_cache_; }
+ void set_selected_cache(AppCache* cache) {
+ selected_cache_ = cache;
+ }
+
+ private:
+ // identifies the corresponding appcache host in the child process
+ int host_id_;
+
+ // application cache associated with this host, if any
+ AppCache* selected_cache_;
+
+ // frontend to deliver notifications about this host to child process
+ AppCacheFrontend* frontend_;
+};
+
+} // namespace appcache
+
+#endif // WEBKIT_APPCACHE_APPCACHE_HOST_H_
diff --git a/webkit/appcache/appcache_service.cc b/webkit/appcache/appcache_service.cc
new file mode 100644
index 0000000..0d3579b
--- /dev/null
+++ b/webkit/appcache/appcache_service.cc
@@ -0,0 +1,50 @@
+// Copyright (c) 2009 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_service.h"
+
+#include "base/logging.h"
+#include "webkit/appcache/appcache.h"
+#include "webkit/appcache/appcache_backend_impl.h"
+#include "webkit/appcache/appcache_group.h"
+
+namespace appcache {
+
+AppCacheService::~AppCacheService() {
+ DCHECK(backends_.empty());
+ DCHECK(caches_.empty());
+ DCHECK(groups_.empty());
+}
+
+void AppCacheService::RegisterBackendImpl(
+ AppCacheBackendImpl* backend_impl) {
+ backends_.insert(backend_impl);
+}
+
+void AppCacheService::UnregisterBackendImpl(
+ AppCacheBackendImpl* backend_impl) {
+ backends_.erase(backend_impl);
+}
+
+void AppCacheService::AddCache(AppCache* cache) {
+ int64 cache_id = cache->cache_id();
+ DCHECK(caches_.find(cache_id) == caches_.end());
+ caches_.insert(CacheMap::value_type(cache_id, cache));
+}
+
+void AppCacheService::RemoveCache(AppCache* cache) {
+ caches_.erase(cache->cache_id());
+}
+
+void AppCacheService::AddGroup(AppCacheGroup* group) {
+ const GURL& url = group->manifest_url();
+ DCHECK(groups_.find(url) == groups_.end());
+ groups_.insert(GroupMap::value_type(url, group));
+}
+
+void AppCacheService::RemoveGroup(AppCacheGroup* group) {
+ groups_.erase(group->manifest_url());
+}
+
+} // namespace appcache
diff --git a/webkit/appcache/appcache_service.h b/webkit/appcache/appcache_service.h
new file mode 100644
index 0000000..3eaa43d
--- /dev/null
+++ b/webkit/appcache/appcache_service.h
@@ -0,0 +1,60 @@
+// Copyright (c) 2009 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.
+
+#ifndef WEBKIT_APPCACHE_APPCACHE_SERVICE_H_
+#define WEBKIT_APPCACHE_APPCACHE_SERVICE_H_
+
+#include <map>
+#include <set>
+#include <vector>
+
+#include "base/hash_tables.h"
+#include "googleurl/src/gurl.h"
+
+namespace appcache {
+
+class AppCache;
+class AppCacheBackendImpl;
+class AppCacheGroup;
+
+// Class that manages the application cache service. Sends notifications
+// to many frontends. One instance per user-profile.
+class AppCacheService {
+ public:
+ virtual ~AppCacheService();
+
+ // TODO(jennb): API to set service settings, like file paths for storage
+
+ // track which processes are using this appcache service
+ void RegisterBackendImpl(AppCacheBackendImpl* backend_impl);
+ void UnregisterBackendImpl(AppCacheBackendImpl* backend_impl);
+
+ void AddCache(AppCache* cache);
+ void RemoveCache(AppCache* cache);
+ void AddGroup(AppCacheGroup* group);
+ void RemoveGroup(AppCacheGroup* group);
+
+ private:
+ // In-memory representation of stored appcache data. Represents a subset
+ // of the appcache database currently in use.
+ typedef base::hash_map<int64, AppCache*> CacheMap;
+ typedef std::map<GURL, AppCacheGroup*> GroupMap;
+ CacheMap caches_;
+ GroupMap groups_;
+
+ // Track current processes. One 'backend' per child process.
+ typedef std::set<AppCacheBackendImpl*> BackendSet;
+ BackendSet backends_;
+
+ // TODO(jennb): info about appcache storage
+ // AppCacheDatabase db_;
+ // DiskCache response_storage_;
+
+ // TODO(jennb): service settings: e.g. max size of app cache?
+ // TODO(jennb): service state: e.g. reached quota?
+};
+
+} // namespace appcache
+
+#endif // WEBKIT_APPCACHE_APPCACHE_SERVICE_H_
diff --git a/webkit/webkit.gyp b/webkit/webkit.gyp
index bf03e1d..3092d23 100644
--- a/webkit/webkit.gyp
+++ b/webkit/webkit.gyp
@@ -1236,12 +1236,21 @@
],
'sources': [
# This list contains all .h and .cc in appcache except for test code.
+ 'appcache/appcache.cc',
+ 'appcache/appcache.h',
'appcache/appcache_backend_impl.cc',
'appcache/appcache_backend_impl.h',
+ 'appcache/appcache_entry.h',
'appcache/appcache_frontend_impl.cc',
'appcache/appcache_frontend_impl.h',
+ 'appcache/appcache_group.cc',
+ 'appcache/appcache_group.h',
+ 'appcache/appcache_host.cc',
+ 'appcache/appcache_host.h',
'appcache/appcache_interfaces.cc',
'appcache/appcache_interfaces.h',
+ 'appcache/appcache_service.cc',
+ 'appcache/appcache_service.h',
'appcache/manifest_parser.cc',
'appcache/manifest_parser.h',
'appcache/web_application_cache_host_impl.cc',