[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 | |
| 7 | #include "chrome/browser/chrome_thread.h" |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 8 | #include "chrome/browser/net/chrome_url_request_context.h" |
| 9 | #include "chrome/browser/profile.h" |
[email protected] | f58330c | 2010-03-29 07:21:13 | [diff] [blame] | 10 | #include "chrome/common/url_constants.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] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 22 | if (ChromeThread::CurrentlyOn(ChromeThread::UI)) { |
| 23 | DCHECK(!is_fetching_); |
| 24 | DCHECK(callback); |
| 25 | is_fetching_ = true; |
| 26 | info_collection_ = new appcache::AppCacheInfoCollection; |
| 27 | completion_callback_.reset(callback); |
| 28 | ChromeThread::PostTask(ChromeThread::IO, FROM_HERE, NewRunnableMethod( |
| 29 | this, &BrowsingDataAppCacheHelper::StartFetching, callback)); |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
| 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] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 42 | if (ChromeThread::CurrentlyOn(ChromeThread::UI)) { |
| 43 | completion_callback_.reset(); |
| 44 | ChromeThread::PostTask(ChromeThread::IO, FROM_HERE, NewRunnableMethod( |
| 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) { |
| 55 | if (ChromeThread::CurrentlyOn(ChromeThread::UI)) { |
| 56 | ChromeThread::PostTask(ChromeThread::IO, FROM_HERE, NewRunnableMethod( |
| 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] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 64 | void BrowsingDataAppCacheHelper::OnFetchComplete(int rv) { |
| 65 | if (ChromeThread::CurrentlyOn(ChromeThread::IO)) { |
[email protected] | f58330c | 2010-03-29 07:21:13 | [diff] [blame] | 66 | // Filter out appache info entries for extensions. Extension state is not |
| 67 | // considered browsing data. |
| 68 | typedef std::map<GURL, appcache::AppCacheInfoVector> InfoByOrigin; |
| 69 | InfoByOrigin& origin_map = info_collection_->infos_by_origin; |
| 70 | for (InfoByOrigin::iterator origin = origin_map.begin(); |
| 71 | origin != origin_map.end();) { |
| 72 | InfoByOrigin::iterator current = origin; |
| 73 | ++origin; |
| 74 | if (current->first.SchemeIs(chrome::kExtensionScheme)) |
| 75 | origin_map.erase(current); |
| 76 | } |
| 77 | |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 78 | appcache_info_callback_ = NULL; |
| 79 | ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, NewRunnableMethod( |
| 80 | this, &BrowsingDataAppCacheHelper::OnFetchComplete, rv)); |
| 81 | return; |
| 82 | } |
[email protected] | f26795eb | 2010-02-26 23:45:35 | [diff] [blame] | 83 | |
[email protected] | f26795eb | 2010-02-26 23:45:35 | [diff] [blame] | 84 | DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 85 | DCHECK(is_fetching_); |
| 86 | is_fetching_ = false; |
| 87 | if (completion_callback_ != NULL) { |
| 88 | completion_callback_->Run(); |
| 89 | completion_callback_.reset(); |
| 90 | } |
| 91 | } |
| 92 | |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 93 | ChromeAppCacheService* BrowsingDataAppCacheHelper::GetAppCacheService() { |
[email protected] | f26795eb | 2010-02-26 23:45:35 | [diff] [blame] | 94 | DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 95 | ChromeURLRequestContext* request_context = |
| 96 | reinterpret_cast<ChromeURLRequestContext*>( |
| 97 | request_context_getter_->GetURLRequestContext()); |
| 98 | return request_context ? request_context->appcache_service() |
| 99 | : NULL; |
[email protected] | f26795eb | 2010-02-26 23:45:35 | [diff] [blame] | 100 | } |
[email protected] | 33b61bc | 2010-06-10 12:55:00 | [diff] [blame] | 101 | |
| 102 | CannedBrowsingDataAppCacheHelper::CannedBrowsingDataAppCacheHelper( |
| 103 | Profile* profile) |
| 104 | : BrowsingDataAppCacheHelper(profile) { |
| 105 | info_collection_ = new appcache::AppCacheInfoCollection; |
| 106 | } |
| 107 | |
| 108 | void CannedBrowsingDataAppCacheHelper::AddAppCache(const GURL& manifest_url) { |
| 109 | typedef std::map<GURL, appcache::AppCacheInfoVector> InfoByOrigin; |
| 110 | InfoByOrigin& origin_map = info_collection_->infos_by_origin; |
| 111 | origin_map[manifest_url.GetOrigin()].push_back( |
| 112 | appcache::AppCacheInfo(manifest_url, |
| 113 | 0, |
| 114 | base::Time(), |
| 115 | base::Time(), |
| 116 | base::Time())); |
| 117 | } |
| 118 | |
[email protected] | 9fb83e8 | 2010-07-02 18:24:55 | [diff] [blame^] | 119 | void CannedBrowsingDataAppCacheHelper::Reset() { |
| 120 | info_collection_->infos_by_origin.clear(); |
| 121 | } |
| 122 | |
[email protected] | 33b61bc | 2010-06-10 12:55:00 | [diff] [blame] | 123 | void CannedBrowsingDataAppCacheHelper::StartFetching( |
| 124 | Callback0::Type* completion_callback) { |
| 125 | completion_callback->Run(); |
| 126 | } |