[email protected] | 63e2682 | 2011-07-16 19:07:35 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | f26795eb | 2010-02-26 23:45:35 | [diff] [blame] | 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] | f1b6de2 | 2010-03-06 12:13:47 | [diff] [blame] | 6 | |
[email protected] | 006284f0 | 2011-10-19 22:06:23 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | 18d4f55 | 2011-12-02 02:48:31 | [diff] [blame] | 8 | #include "base/bind_helpers.h" |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 9 | #include "chrome/browser/net/chrome_url_request_context.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 10 | #include "chrome/browser/profiles/profile.h" |
[email protected] | f58330c | 2010-03-29 07:21:13 | [diff] [blame] | 11 | #include "chrome/common/url_constants.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 12 | #include "content/public/browser/browser_thread.h" |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 13 | #include "webkit/appcache/appcache_database.h" |
| 14 | #include "webkit/appcache/appcache_storage.h" |
| 15 | |
| 16 | using appcache::AppCacheDatabase; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 17 | using content::BrowserThread; |
[email protected] | f26795eb | 2010-02-26 23:45:35 | [diff] [blame] | 18 | |
| 19 | BrowsingDataAppCacheHelper::BrowsingDataAppCacheHelper(Profile* profile) |
[email protected] | 63e2682 | 2011-07-16 19:07:35 | [diff] [blame] | 20 | : is_fetching_(false), |
| 21 | appcache_service_(profile->GetAppCacheService()) { |
[email protected] | f26795eb | 2010-02-26 23:45:35 | [diff] [blame] | 22 | } |
| 23 | |
[email protected] | 4a6742a4 | 2011-10-18 23:42:51 | [diff] [blame] | 24 | void BrowsingDataAppCacheHelper::StartFetching(const base::Closure& callback) { |
[email protected] | d04e766 | 2010-10-10 22:24:48 | [diff] [blame] | 25 | if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 26 | DCHECK(!is_fetching_); |
[email protected] | 4a6742a4 | 2011-10-18 23:42:51 | [diff] [blame] | 27 | DCHECK_EQ(false, callback.is_null()); |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 28 | is_fetching_ = true; |
| 29 | info_collection_ = new appcache::AppCacheInfoCollection; |
[email protected] | 4a6742a4 | 2011-10-18 23:42:51 | [diff] [blame] | 30 | completion_callback_ = callback; |
[email protected] | 006284f0 | 2011-10-19 22:06:23 | [diff] [blame] | 31 | BrowserThread::PostTask( |
| 32 | BrowserThread::IO, FROM_HERE, |
| 33 | base::Bind(&BrowsingDataAppCacheHelper::StartFetching, this, callback)); |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 34 | return; |
| 35 | } |
| 36 | |
[email protected] | d04e766 | 2010-10-10 22:24:48 | [diff] [blame] | 37 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 18d4f55 | 2011-12-02 02:48:31 | [diff] [blame] | 38 | appcache_info_callback_.Reset( |
| 39 | base::Bind(&BrowsingDataAppCacheHelper::OnFetchComplete, |
| 40 | base::Unretained(this))); |
[email protected] | 63e2682 | 2011-07-16 19:07:35 | [diff] [blame] | 41 | appcache_service_->GetAllAppCacheInfo(info_collection_, |
[email protected] | 18d4f55 | 2011-12-02 02:48:31 | [diff] [blame] | 42 | appcache_info_callback_.callback()); |
[email protected] | f26795eb | 2010-02-26 23:45:35 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void BrowsingDataAppCacheHelper::CancelNotification() { |
[email protected] | d04e766 | 2010-10-10 22:24:48 | [diff] [blame] | 46 | if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
[email protected] | 4a6742a4 | 2011-10-18 23:42:51 | [diff] [blame] | 47 | completion_callback_.Reset(); |
[email protected] | 006284f0 | 2011-10-19 22:06:23 | [diff] [blame] | 48 | BrowserThread::PostTask( |
| 49 | BrowserThread::IO, FROM_HERE, |
| 50 | base::Bind(&BrowsingDataAppCacheHelper::CancelNotification, this)); |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 51 | return; |
| 52 | } |
| 53 | |
[email protected] | 18d4f55 | 2011-12-02 02:48:31 | [diff] [blame] | 54 | appcache_info_callback_.Cancel(); |
[email protected] | f26795eb | 2010-02-26 23:45:35 | [diff] [blame] | 55 | } |
| 56 | |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 57 | void BrowsingDataAppCacheHelper::DeleteAppCacheGroup( |
| 58 | const GURL& manifest_url) { |
[email protected] | d04e766 | 2010-10-10 22:24:48 | [diff] [blame] | 59 | if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
[email protected] | 006284f0 | 2011-10-19 22:06:23 | [diff] [blame] | 60 | BrowserThread::PostTask( |
| 61 | BrowserThread::IO, FROM_HERE, |
| 62 | base::Bind(&BrowsingDataAppCacheHelper::DeleteAppCacheGroup, this, |
| 63 | manifest_url)); |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 64 | return; |
| 65 | } |
[email protected] | 8b21196 | 2011-12-02 02:57:25 | [diff] [blame] | 66 | |
| 67 | appcache_service_->DeleteAppCacheGroup( |
| 68 | manifest_url, net::CompletionCallback()); |
[email protected] | f26795eb | 2010-02-26 23:45:35 | [diff] [blame] | 69 | } |
| 70 | |
[email protected] | 8e38341 | 2010-10-19 16:57:03 | [diff] [blame] | 71 | BrowsingDataAppCacheHelper::~BrowsingDataAppCacheHelper() {} |
| 72 | |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 73 | void BrowsingDataAppCacheHelper::OnFetchComplete(int rv) { |
[email protected] | d04e766 | 2010-10-10 22:24:48 | [diff] [blame] | 74 | if (BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
[email protected] | 8b21196 | 2011-12-02 02:57:25 | [diff] [blame] | 75 | // Filter out appcache info entries for extensions. Extension state is not |
[email protected] | f58330c | 2010-03-29 07:21:13 | [diff] [blame] | 76 | // 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] | 006284f0 | 2011-10-19 22:06:23 | [diff] [blame] | 87 | BrowserThread::PostTask( |
| 88 | BrowserThread::UI, FROM_HERE, |
| 89 | base::Bind(&BrowsingDataAppCacheHelper::OnFetchComplete, this, rv)); |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 90 | return; |
| 91 | } |
[email protected] | f26795eb | 2010-02-26 23:45:35 | [diff] [blame] | 92 | |
[email protected] | d04e766 | 2010-10-10 22:24:48 | [diff] [blame] | 93 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | f26795eb | 2010-02-26 23:45:35 | [diff] [blame] | 94 | DCHECK(is_fetching_); |
| 95 | is_fetching_ = false; |
[email protected] | 4a6742a4 | 2011-10-18 23:42:51 | [diff] [blame] | 96 | if (!completion_callback_.is_null()) { |
| 97 | completion_callback_.Run(); |
| 98 | completion_callback_.Reset(); |
[email protected] | f26795eb | 2010-02-26 23:45:35 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
[email protected] | 33b61bc | 2010-06-10 12:55:00 | [diff] [blame] | 102 | CannedBrowsingDataAppCacheHelper::CannedBrowsingDataAppCacheHelper( |
| 103 | Profile* profile) |
[email protected] | 712a9a0 | 2011-03-15 12:27:37 | [diff] [blame] | 104 | : BrowsingDataAppCacheHelper(profile), |
| 105 | profile_(profile) { |
[email protected] | 33b61bc | 2010-06-10 12:55:00 | [diff] [blame] | 106 | info_collection_ = new appcache::AppCacheInfoCollection; |
| 107 | } |
| 108 | |
[email protected] | 712a9a0 | 2011-03-15 12:27:37 | [diff] [blame] | 109 | CannedBrowsingDataAppCacheHelper* 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] | 33b61bc | 2010-06-10 12:55:00 | [diff] [blame] | 118 | void 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] | f272970 | 2010-07-15 09:53:29 | [diff] [blame] | 121 | 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] | ec5c192 | 2010-07-28 03:14:37 | [diff] [blame] | 131 | appcache::AppCacheInfo info; |
| 132 | info.manifest_url = manifest_url; |
| 133 | appcache_infos_.push_back(info); |
[email protected] | 33b61bc | 2010-06-10 12:55:00 | [diff] [blame] | 134 | } |
| 135 | |
[email protected] | 9fb83e8 | 2010-07-02 18:24:55 | [diff] [blame] | 136 | void CannedBrowsingDataAppCacheHelper::Reset() { |
| 137 | info_collection_->infos_by_origin.clear(); |
| 138 | } |
| 139 | |
[email protected] | dcd5b33 | 2010-08-11 08:55:18 | [diff] [blame] | 140 | bool CannedBrowsingDataAppCacheHelper::empty() const { |
| 141 | return info_collection_->infos_by_origin.empty(); |
| 142 | } |
| 143 | |
[email protected] | 33b61bc | 2010-06-10 12:55:00 | [diff] [blame] | 144 | void CannedBrowsingDataAppCacheHelper::StartFetching( |
[email protected] | 4a6742a4 | 2011-10-18 23:42:51 | [diff] [blame] | 145 | const base::Closure& completion_callback) { |
| 146 | completion_callback.Run(); |
[email protected] | 33b61bc | 2010-06-10 12:55:00 | [diff] [blame] | 147 | } |
[email protected] | 8e38341 | 2010-10-19 16:57:03 | [diff] [blame] | 148 | |
| 149 | CannedBrowsingDataAppCacheHelper::~CannedBrowsingDataAppCacheHelper() {} |