Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / browsing_data / browsing_data_appcache_helper.cc
blobb7c330e6400277df2caa9fcc99a345de286ecee0
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 : is_fetching_(false),
21 appcache_service_(BrowserContext::GetDefaultStoragePartition(
22 browser_context)->GetAppCacheService()) {
25 void BrowsingDataAppCacheHelper::StartFetching(const base::Closure& callback) {
26 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
27 DCHECK(!is_fetching_);
28 DCHECK(!callback.is_null());
29 is_fetching_ = true;
30 info_collection_ = new content::AppCacheInfoCollection;
31 completion_callback_ = callback;
32 BrowserThread::PostTask(
33 BrowserThread::IO, FROM_HERE,
34 base::Bind(&BrowsingDataAppCacheHelper::StartFetching, this, callback));
35 return;
38 DCHECK_CURRENTLY_ON(BrowserThread::IO);
39 appcache_service_->GetAllAppCacheInfo(
40 info_collection_.get(),
41 base::Bind(&BrowsingDataAppCacheHelper::OnFetchComplete, this));
44 void BrowsingDataAppCacheHelper::DeleteAppCacheGroup(
45 const GURL& manifest_url) {
46 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
47 BrowserThread::PostTask(
48 BrowserThread::IO, FROM_HERE,
49 base::Bind(&BrowsingDataAppCacheHelper::DeleteAppCacheGroup, this,
50 manifest_url));
51 return;
54 appcache_service_->DeleteAppCacheGroup(manifest_url,
55 net::CompletionCallback());
58 BrowsingDataAppCacheHelper::~BrowsingDataAppCacheHelper() {}
60 void BrowsingDataAppCacheHelper::OnFetchComplete(int rv) {
61 if (BrowserThread::CurrentlyOn(BrowserThread::IO)) {
62 // Filter out appcache info entries for non-websafe schemes. Extension state
63 // and DevTools, for example, are not considered browsing data.
64 typedef std::map<GURL, content::AppCacheInfoVector> InfoByOrigin;
65 InfoByOrigin& origin_map = info_collection_->infos_by_origin;
66 for (InfoByOrigin::iterator origin = origin_map.begin();
67 origin != origin_map.end();) {
68 InfoByOrigin::iterator current = origin;
69 ++origin;
70 if (!BrowsingDataHelper::HasWebScheme(current->first))
71 origin_map.erase(current);
74 BrowserThread::PostTask(
75 BrowserThread::UI, FROM_HERE,
76 base::Bind(&BrowsingDataAppCacheHelper::OnFetchComplete, this, rv));
77 return;
80 DCHECK_CURRENTLY_ON(BrowserThread::UI);
81 DCHECK(is_fetching_);
82 is_fetching_ = false;
83 completion_callback_.Run();
84 completion_callback_.Reset();
87 CannedBrowsingDataAppCacheHelper::CannedBrowsingDataAppCacheHelper(
88 BrowserContext* browser_context)
89 : BrowsingDataAppCacheHelper(browser_context) {
90 info_collection_ = new content::AppCacheInfoCollection;
93 void CannedBrowsingDataAppCacheHelper::AddAppCache(const GURL& manifest_url) {
94 if (!BrowsingDataHelper::HasWebScheme(manifest_url))
95 return; // Ignore non-websafe schemes.
97 OriginAppCacheInfoMap& origin_map = info_collection_->infos_by_origin;
98 content::AppCacheInfoVector& appcache_infos_ =
99 origin_map[manifest_url.GetOrigin()];
101 for (content::AppCacheInfoVector::iterator
102 appcache = appcache_infos_.begin(); appcache != appcache_infos_.end();
103 ++appcache) {
104 if (appcache->manifest_url == manifest_url)
105 return;
108 content::AppCacheInfo info;
109 info.manifest_url = manifest_url;
110 appcache_infos_.push_back(info);
113 void CannedBrowsingDataAppCacheHelper::Reset() {
114 info_collection_->infos_by_origin.clear();
117 bool CannedBrowsingDataAppCacheHelper::empty() const {
118 return info_collection_->infos_by_origin.empty();
121 size_t CannedBrowsingDataAppCacheHelper::GetAppCacheCount() const {
122 size_t count = 0;
123 const OriginAppCacheInfoMap& map = info_collection_->infos_by_origin;
124 for (OriginAppCacheInfoMap::const_iterator it = map.begin();
125 it != map.end();
126 ++it) {
127 count += it->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 base::Closure& completion_callback) {
139 completion_callback.Run();
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() {}