blob: b30fbb44675fe8b13756c28f244d19225c193bd5 [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]d68a4fc62010-03-05 23:40:028#include "chrome/browser/net/chrome_url_request_context.h"
[email protected]8ecad5e2010-12-02 21:18:339#include "chrome/browser/profiles/profile.h"
[email protected]f58330c2010-03-29 07:21:1310#include "chrome/common/url_constants.h"
[email protected]c38831a12011-10-28 12:44:4911#include "content/public/browser/browser_thread.h"
[email protected]d68a4fc62010-03-05 23:40:0212#include "webkit/appcache/appcache_database.h"
13#include "webkit/appcache/appcache_storage.h"
14
15using appcache::AppCacheDatabase;
[email protected]631bb742011-11-02 11:29:3916using content::BrowserThread;
[email protected]f26795eb2010-02-26 23:45:3517
18BrowsingDataAppCacheHelper::BrowsingDataAppCacheHelper(Profile* profile)
[email protected]63e26822011-07-16 19:07:3519 : is_fetching_(false),
20 appcache_service_(profile->GetAppCacheService()) {
[email protected]f26795eb2010-02-26 23:45:3521}
22
[email protected]4a6742a42011-10-18 23:42:5123void BrowsingDataAppCacheHelper::StartFetching(const base::Closure& callback) {
[email protected]d04e7662010-10-10 22:24:4824 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
[email protected]d68a4fc62010-03-05 23:40:0225 DCHECK(!is_fetching_);
[email protected]4a6742a42011-10-18 23:42:5126 DCHECK_EQ(false, callback.is_null());
[email protected]d68a4fc62010-03-05 23:40:0227 is_fetching_ = true;
28 info_collection_ = new appcache::AppCacheInfoCollection;
[email protected]4a6742a42011-10-18 23:42:5129 completion_callback_ = callback;
[email protected]006284f02011-10-19 22:06:2330 BrowserThread::PostTask(
31 BrowserThread::IO, FROM_HERE,
32 base::Bind(&BrowsingDataAppCacheHelper::StartFetching, this, callback));
[email protected]d68a4fc62010-03-05 23:40:0233 return;
34 }
35
[email protected]d04e7662010-10-10 22:24:4836 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]d68a4fc62010-03-05 23:40:0237 appcache_info_callback_ =
[email protected]f1f3f0f82011-10-01 20:38:1038 new net::CancelableOldCompletionCallback<BrowsingDataAppCacheHelper>(
[email protected]d68a4fc62010-03-05 23:40:0239 this, &BrowsingDataAppCacheHelper::OnFetchComplete);
[email protected]63e26822011-07-16 19:07:3540 appcache_service_->GetAllAppCacheInfo(info_collection_,
41 appcache_info_callback_);
[email protected]f26795eb2010-02-26 23:45:3542}
43
44void BrowsingDataAppCacheHelper::CancelNotification() {
[email protected]d04e7662010-10-10 22:24:4845 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
[email protected]4a6742a42011-10-18 23:42:5146 completion_callback_.Reset();
[email protected]006284f02011-10-19 22:06:2347 BrowserThread::PostTask(
48 BrowserThread::IO, FROM_HERE,
49 base::Bind(&BrowsingDataAppCacheHelper::CancelNotification, this));
[email protected]d68a4fc62010-03-05 23:40:0250 return;
51 }
52
53 if (appcache_info_callback_)
54 appcache_info_callback_.release()->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]63e26822011-07-16 19:07:3566 appcache_service_->DeleteAppCacheGroup(manifest_url, NULL);
[email protected]f26795eb2010-02-26 23:45:3567}
68
[email protected]8e383412010-10-19 16:57:0369BrowsingDataAppCacheHelper::~BrowsingDataAppCacheHelper() {}
70
[email protected]d68a4fc62010-03-05 23:40:0271void BrowsingDataAppCacheHelper::OnFetchComplete(int rv) {
[email protected]d04e7662010-10-10 22:24:4872 if (BrowserThread::CurrentlyOn(BrowserThread::IO)) {
[email protected]f58330c2010-03-29 07:21:1373 // Filter out appache info entries for extensions. Extension state is not
74 // considered browsing data.
75 typedef std::map<GURL, appcache::AppCacheInfoVector> InfoByOrigin;
76 InfoByOrigin& origin_map = info_collection_->infos_by_origin;
77 for (InfoByOrigin::iterator origin = origin_map.begin();
78 origin != origin_map.end();) {
79 InfoByOrigin::iterator current = origin;
80 ++origin;
81 if (current->first.SchemeIs(chrome::kExtensionScheme))
82 origin_map.erase(current);
83 }
84
[email protected]d68a4fc62010-03-05 23:40:0285 appcache_info_callback_ = NULL;
[email protected]006284f02011-10-19 22:06:2386 BrowserThread::PostTask(
87 BrowserThread::UI, FROM_HERE,
88 base::Bind(&BrowsingDataAppCacheHelper::OnFetchComplete, this, rv));
[email protected]d68a4fc62010-03-05 23:40:0289 return;
90 }
[email protected]f26795eb2010-02-26 23:45:3591
[email protected]d04e7662010-10-10 22:24:4892 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]f26795eb2010-02-26 23:45:3593 DCHECK(is_fetching_);
94 is_fetching_ = false;
[email protected]4a6742a42011-10-18 23:42:5195 if (!completion_callback_.is_null()) {
96 completion_callback_.Run();
97 completion_callback_.Reset();
[email protected]f26795eb2010-02-26 23:45:3598 }
99}
100
[email protected]33b61bc2010-06-10 12:55:00101CannedBrowsingDataAppCacheHelper::CannedBrowsingDataAppCacheHelper(
102 Profile* profile)
[email protected]712a9a02011-03-15 12:27:37103 : BrowsingDataAppCacheHelper(profile),
104 profile_(profile) {
[email protected]33b61bc2010-06-10 12:55:00105 info_collection_ = new appcache::AppCacheInfoCollection;
106}
107
[email protected]712a9a02011-03-15 12:27:37108CannedBrowsingDataAppCacheHelper* CannedBrowsingDataAppCacheHelper::Clone() {
109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
110 CannedBrowsingDataAppCacheHelper* clone =
111 new CannedBrowsingDataAppCacheHelper(profile_);
112
113 clone->info_collection_->infos_by_origin = info_collection_->infos_by_origin;
114 return clone;
115}
116
[email protected]33b61bc2010-06-10 12:55:00117void CannedBrowsingDataAppCacheHelper::AddAppCache(const GURL& manifest_url) {
118 typedef std::map<GURL, appcache::AppCacheInfoVector> InfoByOrigin;
119 InfoByOrigin& origin_map = info_collection_->infos_by_origin;
[email protected]f2729702010-07-15 09:53:29120 appcache::AppCacheInfoVector& appcache_infos_ =
121 origin_map[manifest_url.GetOrigin()];
122
123 for (appcache::AppCacheInfoVector::iterator
124 appcache = appcache_infos_.begin(); appcache != appcache_infos_.end();
125 ++appcache) {
126 if (appcache->manifest_url == manifest_url)
127 return;
128 }
129
[email protected]ec5c1922010-07-28 03:14:37130 appcache::AppCacheInfo info;
131 info.manifest_url = manifest_url;
132 appcache_infos_.push_back(info);
[email protected]33b61bc2010-06-10 12:55:00133}
134
[email protected]9fb83e82010-07-02 18:24:55135void CannedBrowsingDataAppCacheHelper::Reset() {
136 info_collection_->infos_by_origin.clear();
137}
138
[email protected]dcd5b332010-08-11 08:55:18139bool CannedBrowsingDataAppCacheHelper::empty() const {
140 return info_collection_->infos_by_origin.empty();
141}
142
[email protected]33b61bc2010-06-10 12:55:00143void CannedBrowsingDataAppCacheHelper::StartFetching(
[email protected]4a6742a42011-10-18 23:42:51144 const base::Closure& completion_callback) {
145 completion_callback.Run();
[email protected]33b61bc2010-06-10 12:55:00146}
[email protected]8e383412010-10-19 16:57:03147
148CannedBrowsingDataAppCacheHelper::~CannedBrowsingDataAppCacheHelper() {}