ExtensionSyncService: listen for relevant changes instead of being explicitly called...
[chromium-blink-merge.git] / chrome / browser / browsing_data / browsing_data_appcache_helper.cc
blob226ccaf5f38e8594b236e5b558f8ce69d186cdaf
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 "chrome/browser/browsing_data/browsing_data_helper.h"
10 #include "content/public/browser/browser_context.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/storage_partition.h"
13 #include "net/base/completion_callback.h"
15 using content::BrowserContext;
16 using content::BrowserThread;
18 BrowsingDataAppCacheHelper::BrowsingDataAppCacheHelper(
19 BrowserContext* browser_context)
20 : appcache_service_(
21 BrowserContext::GetDefaultStoragePartition(browser_context)
22 ->GetAppCacheService()) {}
24 void BrowsingDataAppCacheHelper::StartFetching(const FetchCallback& callback) {
25 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
26 DCHECK(!is_fetching_);
27 DCHECK(!callback.is_null());
28 is_fetching_ = true;
29 info_collection_ = new content::AppCacheInfoCollection;
30 completion_callback_ = callback;
31 BrowserThread::PostTask(
32 BrowserThread::IO, FROM_HERE,
33 base::Bind(&BrowsingDataAppCacheHelper::StartFetching, this, callback));
34 return;
37 DCHECK_CURRENTLY_ON(BrowserThread::IO);
38 appcache_service_->GetAllAppCacheInfo(
39 info_collection_.get(),
40 base::Bind(&BrowsingDataAppCacheHelper::OnFetchComplete, this));
43 void BrowsingDataAppCacheHelper::DeleteAppCacheGroup(
44 const GURL& manifest_url) {
45 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
46 BrowserThread::PostTask(
47 BrowserThread::IO, FROM_HERE,
48 base::Bind(&BrowsingDataAppCacheHelper::DeleteAppCacheGroup, this,
49 manifest_url));
50 return;
53 appcache_service_->DeleteAppCacheGroup(manifest_url,
54 net::CompletionCallback());
57 BrowsingDataAppCacheHelper::~BrowsingDataAppCacheHelper() {}
59 void BrowsingDataAppCacheHelper::OnFetchComplete(int rv) {
60 if (BrowserThread::CurrentlyOn(BrowserThread::IO)) {
61 // Filter out appcache info entries for non-websafe schemes. Extension state
62 // and DevTools, for example, are not considered browsing data.
63 typedef std::map<GURL, content::AppCacheInfoVector> InfoByOrigin;
64 InfoByOrigin& origin_map = info_collection_->infos_by_origin;
65 for (InfoByOrigin::iterator origin = origin_map.begin();
66 origin != origin_map.end();) {
67 InfoByOrigin::iterator current = origin;
68 ++origin;
69 if (!BrowsingDataHelper::HasWebScheme(current->first))
70 origin_map.erase(current);
73 BrowserThread::PostTask(
74 BrowserThread::UI, FROM_HERE,
75 base::Bind(&BrowsingDataAppCacheHelper::OnFetchComplete, this, rv));
76 return;
79 DCHECK_CURRENTLY_ON(BrowserThread::UI);
80 DCHECK(is_fetching_);
81 is_fetching_ = false;
82 completion_callback_.Run(info_collection_);
83 completion_callback_.Reset();
86 CannedBrowsingDataAppCacheHelper::CannedBrowsingDataAppCacheHelper(
87 BrowserContext* browser_context)
88 : BrowsingDataAppCacheHelper(browser_context) {
89 info_collection_ = new content::AppCacheInfoCollection;
92 void CannedBrowsingDataAppCacheHelper::AddAppCache(const GURL& manifest_url) {
93 if (!BrowsingDataHelper::HasWebScheme(manifest_url))
94 return; // Ignore non-websafe schemes.
96 OriginAppCacheInfoMap& origin_map = info_collection_->infos_by_origin;
97 content::AppCacheInfoVector& appcache_infos =
98 origin_map[manifest_url.GetOrigin()];
100 for (const auto& appcache : appcache_infos) {
101 if (appcache.manifest_url == manifest_url)
102 return;
105 content::AppCacheInfo info;
106 info.manifest_url = manifest_url;
107 appcache_infos.push_back(info);
110 void CannedBrowsingDataAppCacheHelper::Reset() {
111 info_collection_->infos_by_origin.clear();
114 bool CannedBrowsingDataAppCacheHelper::empty() const {
115 return info_collection_->infos_by_origin.empty();
118 size_t CannedBrowsingDataAppCacheHelper::GetAppCacheCount() const {
119 size_t count = 0;
120 const OriginAppCacheInfoMap& map = info_collection_->infos_by_origin;
121 for (const auto& pair : map)
122 count += pair.second.size();
123 return count;
126 const BrowsingDataAppCacheHelper::OriginAppCacheInfoMap&
127 CannedBrowsingDataAppCacheHelper::GetOriginAppCacheInfoMap() const {
128 return info_collection_->infos_by_origin;
131 void CannedBrowsingDataAppCacheHelper::StartFetching(
132 const FetchCallback& completion_callback) {
133 completion_callback.Run(info_collection_);
136 void CannedBrowsingDataAppCacheHelper::DeleteAppCacheGroup(
137 const GURL& manifest_url) {
138 info_collection_->infos_by_origin.erase(manifest_url.GetOrigin());
139 BrowsingDataAppCacheHelper::DeleteAppCacheGroup(manifest_url);
142 CannedBrowsingDataAppCacheHelper::~CannedBrowsingDataAppCacheHelper() {}