Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / browsing_data / browsing_data_appcache_helper.cc
blob88ce531c6f3123bc80a54884b3bfb89744b12ba8
1 // Copyright (c) 2012 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.
5 #include "chrome/browser/browsing_data/browsing_data_appcache_helper.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/location.h"
10 #include "chrome/browser/browsing_data/browsing_data_helper.h"
11 #include "content/public/browser/browser_context.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/storage_partition.h"
14 #include "net/base/completion_callback.h"
16 using content::BrowserContext;
17 using content::BrowserThread;
19 namespace {
21 void OnFetchComplete(
22 const BrowsingDataAppCacheHelper::FetchCallback& callback,
23 scoped_refptr<content::AppCacheInfoCollection> info_collection,
24 int /*rv*/) {
25 DCHECK_CURRENTLY_ON(BrowserThread::IO);
26 DCHECK(!callback.is_null());
28 // Filter out appcache info entries for non-websafe schemes. Extension state
29 // and DevTools, for example, are not considered browsing data.
30 typedef std::map<GURL, content::AppCacheInfoVector> InfoByOrigin;
31 InfoByOrigin& origin_map = info_collection->infos_by_origin;
32 for (InfoByOrigin::iterator it = origin_map.begin();
33 it != origin_map.end();) {
34 if (!BrowsingDataHelper::HasWebScheme(it->first))
35 origin_map.erase(it++);
36 else
37 ++it;
40 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
41 base::Bind(callback, info_collection));
44 } // namespace
46 BrowsingDataAppCacheHelper::BrowsingDataAppCacheHelper(
47 BrowserContext* browser_context)
48 : appcache_service_(
49 BrowserContext::GetDefaultStoragePartition(browser_context)
50 ->GetAppCacheService()) {}
52 void BrowsingDataAppCacheHelper::StartFetching(const FetchCallback& callback) {
53 DCHECK_CURRENTLY_ON(BrowserThread::UI);
54 DCHECK(!callback.is_null());
56 BrowserThread::PostTask(
57 BrowserThread::IO, FROM_HERE,
58 base::Bind(&BrowsingDataAppCacheHelper::StartFetchingOnIOThread, this,
59 callback));
62 void BrowsingDataAppCacheHelper::DeleteAppCacheGroup(const GURL& manifest_url) {
63 DCHECK_CURRENTLY_ON(BrowserThread::UI);
64 BrowserThread::PostTask(
65 BrowserThread::IO, FROM_HERE,
66 base::Bind(&BrowsingDataAppCacheHelper::DeleteAppCacheGroupOnIOThread,
67 this, manifest_url));
70 BrowsingDataAppCacheHelper::~BrowsingDataAppCacheHelper() {}
72 void BrowsingDataAppCacheHelper::StartFetchingOnIOThread(
73 const FetchCallback& callback) {
74 DCHECK_CURRENTLY_ON(BrowserThread::IO);
75 DCHECK(!callback.is_null());
77 scoped_refptr<content::AppCacheInfoCollection> info_collection =
78 new content::AppCacheInfoCollection();
80 appcache_service_->GetAllAppCacheInfo(
81 info_collection.get(),
82 base::Bind(&OnFetchComplete, callback, info_collection));
85 void BrowsingDataAppCacheHelper::DeleteAppCacheGroupOnIOThread(
86 const GURL& manifest_url) {
87 DCHECK_CURRENTLY_ON(BrowserThread::IO);
88 appcache_service_->DeleteAppCacheGroup(manifest_url,
89 net::CompletionCallback());
92 CannedBrowsingDataAppCacheHelper::CannedBrowsingDataAppCacheHelper(
93 BrowserContext* browser_context)
94 : BrowsingDataAppCacheHelper(browser_context) {
95 info_collection_ = new content::AppCacheInfoCollection;
98 void CannedBrowsingDataAppCacheHelper::AddAppCache(const GURL& manifest_url) {
99 if (!BrowsingDataHelper::HasWebScheme(manifest_url))
100 return; // Ignore non-websafe schemes.
102 OriginAppCacheInfoMap& origin_map = info_collection_->infos_by_origin;
103 content::AppCacheInfoVector& appcache_infos =
104 origin_map[manifest_url.GetOrigin()];
106 for (const auto& appcache : appcache_infos) {
107 if (appcache.manifest_url == manifest_url)
108 return;
111 content::AppCacheInfo info;
112 info.manifest_url = manifest_url;
113 appcache_infos.push_back(info);
116 void CannedBrowsingDataAppCacheHelper::Reset() {
117 info_collection_->infos_by_origin.clear();
120 bool CannedBrowsingDataAppCacheHelper::empty() const {
121 return info_collection_->infos_by_origin.empty();
124 size_t CannedBrowsingDataAppCacheHelper::GetAppCacheCount() const {
125 size_t count = 0;
126 const OriginAppCacheInfoMap& map = info_collection_->infos_by_origin;
127 for (const auto& pair : map)
128 count += pair.second.size();
129 return count;
132 const BrowsingDataAppCacheHelper::OriginAppCacheInfoMap&
133 CannedBrowsingDataAppCacheHelper::GetOriginAppCacheInfoMap() const {
134 return info_collection_->infos_by_origin;
137 void CannedBrowsingDataAppCacheHelper::StartFetching(
138 const FetchCallback& completion_callback) {
139 completion_callback.Run(info_collection_);
142 void CannedBrowsingDataAppCacheHelper::DeleteAppCacheGroup(
143 const GURL& manifest_url) {
144 info_collection_->infos_by_origin.erase(manifest_url.GetOrigin());
145 BrowsingDataAppCacheHelper::DeleteAppCacheGroup(manifest_url);
148 CannedBrowsingDataAppCacheHelper::~CannedBrowsingDataAppCacheHelper() {}