1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "webkit/browser/appcache/appcache_working_set.h" 6 7 #include "base/logging.h" 8 #include "webkit/browser/appcache/appcache.h" 9 #include "webkit/browser/appcache/appcache_group.h" 10 #include "webkit/browser/appcache/appcache_response.h" 11 12 namespace appcache { 13 14 AppCacheWorkingSet::AppCacheWorkingSet() : is_disabled_(false) {} 15 16 AppCacheWorkingSet::~AppCacheWorkingSet() { 17 DCHECK(caches_.empty()); 18 DCHECK(groups_.empty()); 19 DCHECK(groups_by_origin_.empty()); 20 } 21 22 void AppCacheWorkingSet::Disable() { 23 if (is_disabled_) 24 return; 25 is_disabled_ = true; 26 caches_.clear(); 27 groups_.clear(); 28 groups_by_origin_.clear(); 29 response_infos_.clear(); 30 } 31 32 void AppCacheWorkingSet::AddCache(AppCache* cache) { 33 if (is_disabled_) 34 return; 35 DCHECK(cache->cache_id() != kNoCacheId); 36 int64 cache_id = cache->cache_id(); 37 DCHECK(caches_.find(cache_id) == caches_.end()); 38 caches_.insert(CacheMap::value_type(cache_id, cache)); 39 } 40 41 void AppCacheWorkingSet::RemoveCache(AppCache* cache) { 42 caches_.erase(cache->cache_id()); 43 } 44 45 void AppCacheWorkingSet::AddGroup(AppCacheGroup* group) { 46 if (is_disabled_) 47 return; 48 const GURL& url = group->manifest_url(); 49 DCHECK(groups_.find(url) == groups_.end()); 50 groups_.insert(GroupMap::value_type(url, group)); 51 groups_by_origin_[url.GetOrigin()].insert(GroupMap::value_type(url, group)); 52 } 53 54 void AppCacheWorkingSet::RemoveGroup(AppCacheGroup* group) { 55 const GURL& url = group->manifest_url(); 56 groups_.erase(url); 57 58 GURL origin_url = url.GetOrigin(); 59 GroupMap* groups_in_origin = GetMutableGroupsInOrigin(origin_url); 60 if (groups_in_origin) { 61 groups_in_origin->erase(url); 62 if (groups_in_origin->empty()) 63 groups_by_origin_.erase(origin_url); 64 } 65 } 66 67 void AppCacheWorkingSet::AddResponseInfo(AppCacheResponseInfo* info) { 68 if (is_disabled_) 69 return; 70 DCHECK(info->response_id() != kNoResponseId); 71 int64 response_id = info->response_id(); 72 DCHECK(response_infos_.find(response_id) == response_infos_.end()); 73 response_infos_.insert(ResponseInfoMap::value_type(response_id, info)); 74 } 75 76 void AppCacheWorkingSet::RemoveResponseInfo(AppCacheResponseInfo* info) { 77 response_infos_.erase(info->response_id()); 78 } 79 80 } // namespace 81