blob: 512e6669cb011b5521499f6a501d75da210dd5dc [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]d68a4fc62010-03-05 23:40:027#include "chrome/browser/net/chrome_url_request_context.h"
[email protected]8ecad5e2010-12-02 21:18:338#include "chrome/browser/profiles/profile.h"
[email protected]f58330c2010-03-29 07:21:139#include "chrome/common/url_constants.h"
[email protected]1625ffd2011-03-01 17:51:5010#include "content/browser/browser_thread.h"
[email protected]d68a4fc62010-03-05 23:40:0211#include "webkit/appcache/appcache_database.h"
12#include "webkit/appcache/appcache_storage.h"
13
14using appcache::AppCacheDatabase;
[email protected]f26795eb2010-02-26 23:45:3515
16BrowsingDataAppCacheHelper::BrowsingDataAppCacheHelper(Profile* profile)
[email protected]63e26822011-07-16 19:07:3517 : is_fetching_(false),
18 appcache_service_(profile->GetAppCacheService()) {
[email protected]f26795eb2010-02-26 23:45:3519}
20
21void BrowsingDataAppCacheHelper::StartFetching(Callback0::Type* callback) {
[email protected]d04e7662010-10-10 22:24:4822 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
[email protected]d68a4fc62010-03-05 23:40:0223 DCHECK(!is_fetching_);
24 DCHECK(callback);
25 is_fetching_ = true;
26 info_collection_ = new appcache::AppCacheInfoCollection;
27 completion_callback_.reset(callback);
[email protected]d04e7662010-10-10 22:24:4828 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, NewRunnableMethod(
[email protected]d68a4fc62010-03-05 23:40:0229 this, &BrowsingDataAppCacheHelper::StartFetching, callback));
30 return;
31 }
32
[email protected]d04e7662010-10-10 22:24:4833 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]d68a4fc62010-03-05 23:40:0234 appcache_info_callback_ =
35 new net::CancelableCompletionCallback<BrowsingDataAppCacheHelper>(
36 this, &BrowsingDataAppCacheHelper::OnFetchComplete);
[email protected]63e26822011-07-16 19:07:3537 appcache_service_->GetAllAppCacheInfo(info_collection_,
38 appcache_info_callback_);
[email protected]f26795eb2010-02-26 23:45:3539}
40
41void BrowsingDataAppCacheHelper::CancelNotification() {
[email protected]d04e7662010-10-10 22:24:4842 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
[email protected]d68a4fc62010-03-05 23:40:0243 completion_callback_.reset();
[email protected]d04e7662010-10-10 22:24:4844 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, NewRunnableMethod(
[email protected]d68a4fc62010-03-05 23:40:0245 this, &BrowsingDataAppCacheHelper::CancelNotification));
46 return;
47 }
48
49 if (appcache_info_callback_)
50 appcache_info_callback_.release()->Cancel();
[email protected]f26795eb2010-02-26 23:45:3551}
52
[email protected]d68a4fc62010-03-05 23:40:0253void BrowsingDataAppCacheHelper::DeleteAppCacheGroup(
54 const GURL& manifest_url) {
[email protected]d04e7662010-10-10 22:24:4855 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
56 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, NewRunnableMethod(
[email protected]d68a4fc62010-03-05 23:40:0257 this, &BrowsingDataAppCacheHelper::DeleteAppCacheGroup,
58 manifest_url));
59 return;
60 }
[email protected]63e26822011-07-16 19:07:3561 appcache_service_->DeleteAppCacheGroup(manifest_url, NULL);
[email protected]f26795eb2010-02-26 23:45:3562}
63
[email protected]8e383412010-10-19 16:57:0364BrowsingDataAppCacheHelper::~BrowsingDataAppCacheHelper() {}
65
[email protected]d68a4fc62010-03-05 23:40:0266void BrowsingDataAppCacheHelper::OnFetchComplete(int rv) {
[email protected]d04e7662010-10-10 22:24:4867 if (BrowserThread::CurrentlyOn(BrowserThread::IO)) {
[email protected]f58330c2010-03-29 07:21:1368 // Filter out appache info entries for extensions. Extension state is not
69 // considered browsing data.
70 typedef std::map<GURL, appcache::AppCacheInfoVector> InfoByOrigin;
71 InfoByOrigin& origin_map = info_collection_->infos_by_origin;
72 for (InfoByOrigin::iterator origin = origin_map.begin();
73 origin != origin_map.end();) {
74 InfoByOrigin::iterator current = origin;
75 ++origin;
76 if (current->first.SchemeIs(chrome::kExtensionScheme))
77 origin_map.erase(current);
78 }
79
[email protected]d68a4fc62010-03-05 23:40:0280 appcache_info_callback_ = NULL;
[email protected]d04e7662010-10-10 22:24:4881 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, NewRunnableMethod(
[email protected]d68a4fc62010-03-05 23:40:0282 this, &BrowsingDataAppCacheHelper::OnFetchComplete, rv));
83 return;
84 }
[email protected]f26795eb2010-02-26 23:45:3585
[email protected]d04e7662010-10-10 22:24:4886 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]f26795eb2010-02-26 23:45:3587 DCHECK(is_fetching_);
88 is_fetching_ = false;
89 if (completion_callback_ != NULL) {
90 completion_callback_->Run();
91 completion_callback_.reset();
92 }
93}
94
[email protected]33b61bc2010-06-10 12:55:0095CannedBrowsingDataAppCacheHelper::CannedBrowsingDataAppCacheHelper(
96 Profile* profile)
[email protected]712a9a02011-03-15 12:27:3797 : BrowsingDataAppCacheHelper(profile),
98 profile_(profile) {
[email protected]33b61bc2010-06-10 12:55:0099 info_collection_ = new appcache::AppCacheInfoCollection;
100}
101
[email protected]712a9a02011-03-15 12:27:37102CannedBrowsingDataAppCacheHelper* CannedBrowsingDataAppCacheHelper::Clone() {
103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
104 CannedBrowsingDataAppCacheHelper* clone =
105 new CannedBrowsingDataAppCacheHelper(profile_);
106
107 clone->info_collection_->infos_by_origin = info_collection_->infos_by_origin;
108 return clone;
109}
110
[email protected]33b61bc2010-06-10 12:55:00111void CannedBrowsingDataAppCacheHelper::AddAppCache(const GURL& manifest_url) {
112 typedef std::map<GURL, appcache::AppCacheInfoVector> InfoByOrigin;
113 InfoByOrigin& origin_map = info_collection_->infos_by_origin;
[email protected]f2729702010-07-15 09:53:29114 appcache::AppCacheInfoVector& appcache_infos_ =
115 origin_map[manifest_url.GetOrigin()];
116
117 for (appcache::AppCacheInfoVector::iterator
118 appcache = appcache_infos_.begin(); appcache != appcache_infos_.end();
119 ++appcache) {
120 if (appcache->manifest_url == manifest_url)
121 return;
122 }
123
[email protected]ec5c1922010-07-28 03:14:37124 appcache::AppCacheInfo info;
125 info.manifest_url = manifest_url;
126 appcache_infos_.push_back(info);
[email protected]33b61bc2010-06-10 12:55:00127}
128
[email protected]9fb83e82010-07-02 18:24:55129void CannedBrowsingDataAppCacheHelper::Reset() {
130 info_collection_->infos_by_origin.clear();
131}
132
[email protected]dcd5b332010-08-11 08:55:18133bool CannedBrowsingDataAppCacheHelper::empty() const {
134 return info_collection_->infos_by_origin.empty();
135}
136
[email protected]33b61bc2010-06-10 12:55:00137void CannedBrowsingDataAppCacheHelper::StartFetching(
138 Callback0::Type* completion_callback) {
139 completion_callback->Run();
[email protected]42f9c702010-08-11 08:15:33140 delete completion_callback;
[email protected]33b61bc2010-06-10 12:55:00141}
[email protected]8e383412010-10-19 16:57:03142
143CannedBrowsingDataAppCacheHelper::~CannedBrowsingDataAppCacheHelper() {}