blob: 6a7e8ee44e25edc4fef78ac9ffec3edbb8ff3f2d [file] [log] [blame]
[email protected]f26795eb2010-02-26 23:45:351// Copyright (c) 2010 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.
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]d68a4fc62010-03-05 23:40:0217 : request_context_getter_(profile->GetRequestContext()),
18 is_fetching_(false) {
[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);
37 GetAppCacheService()->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 }
61 GetAppCacheService()->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]d68a4fc62010-03-05 23:40:0295ChromeAppCacheService* BrowsingDataAppCacheHelper::GetAppCacheService() {
[email protected]d04e7662010-10-10 22:24:4896 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]d68a4fc62010-03-05 23:40:0297 ChromeURLRequestContext* request_context =
98 reinterpret_cast<ChromeURLRequestContext*>(
99 request_context_getter_->GetURLRequestContext());
100 return request_context ? request_context->appcache_service()
101 : NULL;
[email protected]f26795eb2010-02-26 23:45:35102}
[email protected]33b61bc2010-06-10 12:55:00103
104CannedBrowsingDataAppCacheHelper::CannedBrowsingDataAppCacheHelper(
105 Profile* profile)
[email protected]712a9a02011-03-15 12:27:37106 : BrowsingDataAppCacheHelper(profile),
107 profile_(profile) {
[email protected]33b61bc2010-06-10 12:55:00108 info_collection_ = new appcache::AppCacheInfoCollection;
109}
110
[email protected]712a9a02011-03-15 12:27:37111CannedBrowsingDataAppCacheHelper* CannedBrowsingDataAppCacheHelper::Clone() {
112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
113 CannedBrowsingDataAppCacheHelper* clone =
114 new CannedBrowsingDataAppCacheHelper(profile_);
115
116 clone->info_collection_->infos_by_origin = info_collection_->infos_by_origin;
117 return clone;
118}
119
[email protected]33b61bc2010-06-10 12:55:00120void CannedBrowsingDataAppCacheHelper::AddAppCache(const GURL& manifest_url) {
121 typedef std::map<GURL, appcache::AppCacheInfoVector> InfoByOrigin;
122 InfoByOrigin& origin_map = info_collection_->infos_by_origin;
[email protected]f2729702010-07-15 09:53:29123 appcache::AppCacheInfoVector& appcache_infos_ =
124 origin_map[manifest_url.GetOrigin()];
125
126 for (appcache::AppCacheInfoVector::iterator
127 appcache = appcache_infos_.begin(); appcache != appcache_infos_.end();
128 ++appcache) {
129 if (appcache->manifest_url == manifest_url)
130 return;
131 }
132
[email protected]ec5c1922010-07-28 03:14:37133 appcache::AppCacheInfo info;
134 info.manifest_url = manifest_url;
135 appcache_infos_.push_back(info);
[email protected]33b61bc2010-06-10 12:55:00136}
137
[email protected]9fb83e82010-07-02 18:24:55138void CannedBrowsingDataAppCacheHelper::Reset() {
139 info_collection_->infos_by_origin.clear();
140}
141
[email protected]dcd5b332010-08-11 08:55:18142bool CannedBrowsingDataAppCacheHelper::empty() const {
143 return info_collection_->infos_by_origin.empty();
144}
145
[email protected]33b61bc2010-06-10 12:55:00146void CannedBrowsingDataAppCacheHelper::StartFetching(
147 Callback0::Type* completion_callback) {
148 completion_callback->Run();
[email protected]42f9c702010-08-11 08:15:33149 delete completion_callback;
[email protected]33b61bc2010-06-10 12:55:00150}
[email protected]8e383412010-10-19 16:57:03151
152CannedBrowsingDataAppCacheHelper::~CannedBrowsingDataAppCacheHelper() {}