[email protected] | f26795eb | 2010-02-26 23:45:35 | [diff] [blame] | 1 | // 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] | f1b6de2 | 2010-03-06 12:13:47 | [diff] [blame] | 6 | |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 7 | #include "chrome/browser/net/chrome_url_request_context.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 8 | #include "chrome/browser/profiles/profile.h" |
[email protected] | f58330c | 2010-03-29 07:21:13 | [diff] [blame] | 9 | #include "chrome/common/url_constants.h" |
[email protected] | 1625ffd | 2011-03-01 17:51:50 | [diff] [blame] | 10 | #include "content/browser/browser_thread.h" |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 11 | #include "webkit/appcache/appcache_database.h" |
| 12 | #include "webkit/appcache/appcache_storage.h" |
| 13 | |
| 14 | using appcache::AppCacheDatabase; |
[email protected] | f26795eb | 2010-02-26 23:45:35 | [diff] [blame] | 15 | |
| 16 | BrowsingDataAppCacheHelper::BrowsingDataAppCacheHelper(Profile* profile) |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 17 | : request_context_getter_(profile->GetRequestContext()), |
| 18 | is_fetching_(false) { |
[email protected] | f26795eb | 2010-02-26 23:45:35 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | void BrowsingDataAppCacheHelper::StartFetching(Callback0::Type* callback) { |
[email protected] | d04e766 | 2010-10-10 22:24:48 | [diff] [blame] | 22 | if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 23 | DCHECK(!is_fetching_); |
| 24 | DCHECK(callback); |
| 25 | is_fetching_ = true; |
| 26 | info_collection_ = new appcache::AppCacheInfoCollection; |
| 27 | completion_callback_.reset(callback); |
[email protected] | d04e766 | 2010-10-10 22:24:48 | [diff] [blame] | 28 | BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, NewRunnableMethod( |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 29 | this, &BrowsingDataAppCacheHelper::StartFetching, callback)); |
| 30 | return; |
| 31 | } |
| 32 | |
[email protected] | d04e766 | 2010-10-10 22:24:48 | [diff] [blame] | 33 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 34 | appcache_info_callback_ = |
| 35 | new net::CancelableCompletionCallback<BrowsingDataAppCacheHelper>( |
| 36 | this, &BrowsingDataAppCacheHelper::OnFetchComplete); |
| 37 | GetAppCacheService()->GetAllAppCacheInfo(info_collection_, |
| 38 | appcache_info_callback_); |
[email protected] | f26795eb | 2010-02-26 23:45:35 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | void BrowsingDataAppCacheHelper::CancelNotification() { |
[email protected] | d04e766 | 2010-10-10 22:24:48 | [diff] [blame] | 42 | if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 43 | completion_callback_.reset(); |
[email protected] | d04e766 | 2010-10-10 22:24:48 | [diff] [blame] | 44 | BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, NewRunnableMethod( |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 45 | this, &BrowsingDataAppCacheHelper::CancelNotification)); |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | if (appcache_info_callback_) |
| 50 | appcache_info_callback_.release()->Cancel(); |
[email protected] | f26795eb | 2010-02-26 23:45:35 | [diff] [blame] | 51 | } |
| 52 | |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 53 | void BrowsingDataAppCacheHelper::DeleteAppCacheGroup( |
| 54 | const GURL& manifest_url) { |
[email protected] | d04e766 | 2010-10-10 22:24:48 | [diff] [blame] | 55 | if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 56 | BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, NewRunnableMethod( |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 57 | this, &BrowsingDataAppCacheHelper::DeleteAppCacheGroup, |
| 58 | manifest_url)); |
| 59 | return; |
| 60 | } |
| 61 | GetAppCacheService()->DeleteAppCacheGroup(manifest_url, NULL); |
[email protected] | f26795eb | 2010-02-26 23:45:35 | [diff] [blame] | 62 | } |
| 63 | |
[email protected] | 8e38341 | 2010-10-19 16:57:03 | [diff] [blame] | 64 | BrowsingDataAppCacheHelper::~BrowsingDataAppCacheHelper() {} |
| 65 | |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 66 | void BrowsingDataAppCacheHelper::OnFetchComplete(int rv) { |
[email protected] | d04e766 | 2010-10-10 22:24:48 | [diff] [blame] | 67 | if (BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
[email protected] | f58330c | 2010-03-29 07:21:13 | [diff] [blame] | 68 | // 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] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 80 | appcache_info_callback_ = NULL; |
[email protected] | d04e766 | 2010-10-10 22:24:48 | [diff] [blame] | 81 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, NewRunnableMethod( |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 82 | this, &BrowsingDataAppCacheHelper::OnFetchComplete, rv)); |
| 83 | return; |
| 84 | } |
[email protected] | f26795eb | 2010-02-26 23:45:35 | [diff] [blame] | 85 | |
[email protected] | d04e766 | 2010-10-10 22:24:48 | [diff] [blame] | 86 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | f26795eb | 2010-02-26 23:45:35 | [diff] [blame] | 87 | 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] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 95 | ChromeAppCacheService* BrowsingDataAppCacheHelper::GetAppCacheService() { |
[email protected] | d04e766 | 2010-10-10 22:24:48 | [diff] [blame] | 96 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 97 | ChromeURLRequestContext* request_context = |
| 98 | reinterpret_cast<ChromeURLRequestContext*>( |
| 99 | request_context_getter_->GetURLRequestContext()); |
| 100 | return request_context ? request_context->appcache_service() |
| 101 | : NULL; |
[email protected] | f26795eb | 2010-02-26 23:45:35 | [diff] [blame] | 102 | } |
[email protected] | 33b61bc | 2010-06-10 12:55:00 | [diff] [blame] | 103 | |
| 104 | CannedBrowsingDataAppCacheHelper::CannedBrowsingDataAppCacheHelper( |
| 105 | Profile* profile) |
[email protected] | 712a9a0 | 2011-03-15 12:27:37 | [diff] [blame^] | 106 | : BrowsingDataAppCacheHelper(profile), |
| 107 | profile_(profile) { |
[email protected] | 33b61bc | 2010-06-10 12:55:00 | [diff] [blame] | 108 | info_collection_ = new appcache::AppCacheInfoCollection; |
| 109 | } |
| 110 | |
[email protected] | 712a9a0 | 2011-03-15 12:27:37 | [diff] [blame^] | 111 | CannedBrowsingDataAppCacheHelper* 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] | 33b61bc | 2010-06-10 12:55:00 | [diff] [blame] | 120 | void 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] | f272970 | 2010-07-15 09:53:29 | [diff] [blame] | 123 | 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] | ec5c192 | 2010-07-28 03:14:37 | [diff] [blame] | 133 | appcache::AppCacheInfo info; |
| 134 | info.manifest_url = manifest_url; |
| 135 | appcache_infos_.push_back(info); |
[email protected] | 33b61bc | 2010-06-10 12:55:00 | [diff] [blame] | 136 | } |
| 137 | |
[email protected] | 9fb83e8 | 2010-07-02 18:24:55 | [diff] [blame] | 138 | void CannedBrowsingDataAppCacheHelper::Reset() { |
| 139 | info_collection_->infos_by_origin.clear(); |
| 140 | } |
| 141 | |
[email protected] | dcd5b33 | 2010-08-11 08:55:18 | [diff] [blame] | 142 | bool CannedBrowsingDataAppCacheHelper::empty() const { |
| 143 | return info_collection_->infos_by_origin.empty(); |
| 144 | } |
| 145 | |
[email protected] | 33b61bc | 2010-06-10 12:55:00 | [diff] [blame] | 146 | void CannedBrowsingDataAppCacheHelper::StartFetching( |
| 147 | Callback0::Type* completion_callback) { |
| 148 | completion_callback->Run(); |
[email protected] | 42f9c70 | 2010-08-11 08:15:33 | [diff] [blame] | 149 | delete completion_callback; |
[email protected] | 33b61bc | 2010-06-10 12:55:00 | [diff] [blame] | 150 | } |
[email protected] | 8e38341 | 2010-10-19 16:57:03 | [diff] [blame] | 151 | |
| 152 | CannedBrowsingDataAppCacheHelper::~CannedBrowsingDataAppCacheHelper() {} |