blob: 106d507e10b6e33b615a5e403e7ebce39082804d [file] [log] [blame]
[email protected]63e26822011-07-16 19:07:351// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]f26795eb2010-02-26 23:45:352// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/browsing_data_appcache_helper.h"
[email protected]f1b6de22010-03-06 12:13:476
[email protected]006284f02011-10-19 22:06:237#include "base/bind.h"
[email protected]18d4f552011-12-02 02:48:318#include "base/bind_helpers.h"
[email protected]d68a4fc62010-03-05 23:40:029#include "chrome/browser/net/chrome_url_request_context.h"
[email protected]8ecad5e2010-12-02 21:18:3310#include "chrome/browser/profiles/profile.h"
[email protected]f58330c2010-03-29 07:21:1311#include "chrome/common/url_constants.h"
[email protected]c38831a12011-10-28 12:44:4912#include "content/public/browser/browser_thread.h"
[email protected]d68a4fc62010-03-05 23:40:0213#include "webkit/appcache/appcache_database.h"
14#include "webkit/appcache/appcache_storage.h"
15
16using appcache::AppCacheDatabase;
[email protected]631bb742011-11-02 11:29:3917using content::BrowserThread;
[email protected]f26795eb2010-02-26 23:45:3518
19BrowsingDataAppCacheHelper::BrowsingDataAppCacheHelper(Profile* profile)
[email protected]63e26822011-07-16 19:07:3520 : is_fetching_(false),
21 appcache_service_(profile->GetAppCacheService()) {
[email protected]f26795eb2010-02-26 23:45:3522}
23
[email protected]4a6742a42011-10-18 23:42:5124void BrowsingDataAppCacheHelper::StartFetching(const base::Closure& callback) {
[email protected]d04e7662010-10-10 22:24:4825 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
[email protected]d68a4fc62010-03-05 23:40:0226 DCHECK(!is_fetching_);
[email protected]4a6742a42011-10-18 23:42:5127 DCHECK_EQ(false, callback.is_null());
[email protected]d68a4fc62010-03-05 23:40:0228 is_fetching_ = true;
29 info_collection_ = new appcache::AppCacheInfoCollection;
[email protected]4a6742a42011-10-18 23:42:5130 completion_callback_ = callback;
[email protected]006284f02011-10-19 22:06:2331 BrowserThread::PostTask(
32 BrowserThread::IO, FROM_HERE,
33 base::Bind(&BrowsingDataAppCacheHelper::StartFetching, this, callback));
[email protected]d68a4fc62010-03-05 23:40:0234 return;
35 }
36
[email protected]d04e7662010-10-10 22:24:4837 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]18d4f552011-12-02 02:48:3138 appcache_info_callback_.Reset(
39 base::Bind(&BrowsingDataAppCacheHelper::OnFetchComplete,
40 base::Unretained(this)));
[email protected]63e26822011-07-16 19:07:3541 appcache_service_->GetAllAppCacheInfo(info_collection_,
[email protected]18d4f552011-12-02 02:48:3142 appcache_info_callback_.callback());
[email protected]f26795eb2010-02-26 23:45:3543}
44
45void BrowsingDataAppCacheHelper::CancelNotification() {
[email protected]d04e7662010-10-10 22:24:4846 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
[email protected]4a6742a42011-10-18 23:42:5147 completion_callback_.Reset();
[email protected]006284f02011-10-19 22:06:2348 BrowserThread::PostTask(
49 BrowserThread::IO, FROM_HERE,
50 base::Bind(&BrowsingDataAppCacheHelper::CancelNotification, this));
[email protected]d68a4fc62010-03-05 23:40:0251 return;
52 }
53
[email protected]18d4f552011-12-02 02:48:3154 appcache_info_callback_.Cancel();
[email protected]f26795eb2010-02-26 23:45:3555}
56
[email protected]d68a4fc62010-03-05 23:40:0257void BrowsingDataAppCacheHelper::DeleteAppCacheGroup(
58 const GURL& manifest_url) {
[email protected]d04e7662010-10-10 22:24:4859 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
[email protected]006284f02011-10-19 22:06:2360 BrowserThread::PostTask(
61 BrowserThread::IO, FROM_HERE,
62 base::Bind(&BrowsingDataAppCacheHelper::DeleteAppCacheGroup, this,
63 manifest_url));
[email protected]d68a4fc62010-03-05 23:40:0264 return;
65 }
[email protected]8b211962011-12-02 02:57:2566
67 appcache_service_->DeleteAppCacheGroup(
68 manifest_url, net::CompletionCallback());
[email protected]f26795eb2010-02-26 23:45:3569}
70
[email protected]8e383412010-10-19 16:57:0371BrowsingDataAppCacheHelper::~BrowsingDataAppCacheHelper() {}
72
[email protected]d68a4fc62010-03-05 23:40:0273void BrowsingDataAppCacheHelper::OnFetchComplete(int rv) {
[email protected]d04e7662010-10-10 22:24:4874 if (BrowserThread::CurrentlyOn(BrowserThread::IO)) {
[email protected]8b211962011-12-02 02:57:2575 // Filter out appcache info entries for extensions. Extension state is not
[email protected]f58330c2010-03-29 07:21:1376 // considered browsing data.
77 typedef std::map<GURL, appcache::AppCacheInfoVector> InfoByOrigin;
78 InfoByOrigin& origin_map = info_collection_->infos_by_origin;
79 for (InfoByOrigin::iterator origin = origin_map.begin();
80 origin != origin_map.end();) {
81 InfoByOrigin::iterator current = origin;
82 ++origin;
83 if (current->first.SchemeIs(chrome::kExtensionScheme))
84 origin_map.erase(current);
85 }
86
[email protected]006284f02011-10-19 22:06:2387 BrowserThread::PostTask(
88 BrowserThread::UI, FROM_HERE,
89 base::Bind(&BrowsingDataAppCacheHelper::OnFetchComplete, this, rv));
[email protected]d68a4fc62010-03-05 23:40:0290 return;
91 }
[email protected]f26795eb2010-02-26 23:45:3592
[email protected]d04e7662010-10-10 22:24:4893 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]f26795eb2010-02-26 23:45:3594 DCHECK(is_fetching_);
95 is_fetching_ = false;
[email protected]4a6742a42011-10-18 23:42:5196 if (!completion_callback_.is_null()) {
97 completion_callback_.Run();
98 completion_callback_.Reset();
[email protected]f26795eb2010-02-26 23:45:3599 }
100}
101
[email protected]33b61bc2010-06-10 12:55:00102CannedBrowsingDataAppCacheHelper::CannedBrowsingDataAppCacheHelper(
103 Profile* profile)
[email protected]712a9a02011-03-15 12:27:37104 : BrowsingDataAppCacheHelper(profile),
105 profile_(profile) {
[email protected]33b61bc2010-06-10 12:55:00106 info_collection_ = new appcache::AppCacheInfoCollection;
107}
108
[email protected]712a9a02011-03-15 12:27:37109CannedBrowsingDataAppCacheHelper* CannedBrowsingDataAppCacheHelper::Clone() {
110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
111 CannedBrowsingDataAppCacheHelper* clone =
112 new CannedBrowsingDataAppCacheHelper(profile_);
113
114 clone->info_collection_->infos_by_origin = info_collection_->infos_by_origin;
115 return clone;
116}
117
[email protected]33b61bc2010-06-10 12:55:00118void CannedBrowsingDataAppCacheHelper::AddAppCache(const GURL& manifest_url) {
119 typedef std::map<GURL, appcache::AppCacheInfoVector> InfoByOrigin;
120 InfoByOrigin& origin_map = info_collection_->infos_by_origin;
[email protected]f2729702010-07-15 09:53:29121 appcache::AppCacheInfoVector& appcache_infos_ =
122 origin_map[manifest_url.GetOrigin()];
123
124 for (appcache::AppCacheInfoVector::iterator
125 appcache = appcache_infos_.begin(); appcache != appcache_infos_.end();
126 ++appcache) {
127 if (appcache->manifest_url == manifest_url)
128 return;
129 }
130
[email protected]ec5c1922010-07-28 03:14:37131 appcache::AppCacheInfo info;
132 info.manifest_url = manifest_url;
133 appcache_infos_.push_back(info);
[email protected]33b61bc2010-06-10 12:55:00134}
135
[email protected]9fb83e82010-07-02 18:24:55136void CannedBrowsingDataAppCacheHelper::Reset() {
137 info_collection_->infos_by_origin.clear();
138}
139
[email protected]dcd5b332010-08-11 08:55:18140bool CannedBrowsingDataAppCacheHelper::empty() const {
141 return info_collection_->infos_by_origin.empty();
142}
143
[email protected]33b61bc2010-06-10 12:55:00144void CannedBrowsingDataAppCacheHelper::StartFetching(
[email protected]4a6742a42011-10-18 23:42:51145 const base::Closure& completion_callback) {
146 completion_callback.Run();
[email protected]33b61bc2010-06-10 12:55:00147}
[email protected]8e383412010-10-19 16:57:03148
149CannedBrowsingDataAppCacheHelper::~CannedBrowsingDataAppCacheHelper() {}