blob: 54b08ac0582634d03321b4763cd761ce1db2dadb [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
7#include "chrome/browser/chrome_thread.h"
[email protected]d68a4fc62010-03-05 23:40:028#include "chrome/browser/net/chrome_url_request_context.h"
9#include "chrome/browser/profile.h"
[email protected]f58330c2010-03-29 07:21:1310#include "chrome/common/url_constants.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]d68a4fc62010-03-05 23:40:0222 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]f26795eb2010-02-26 23:45:3539}
40
41void BrowsingDataAppCacheHelper::CancelNotification() {
[email protected]d68a4fc62010-03-05 23:40:0242 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]f26795eb2010-02-26 23:45:3551}
52
[email protected]d68a4fc62010-03-05 23:40:0253void 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]f26795eb2010-02-26 23:45:3562}
63
[email protected]d68a4fc62010-03-05 23:40:0264void BrowsingDataAppCacheHelper::OnFetchComplete(int rv) {
65 if (ChromeThread::CurrentlyOn(ChromeThread::IO)) {
[email protected]f58330c2010-03-29 07:21:1366 // 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]d68a4fc62010-03-05 23:40:0278 appcache_info_callback_ = NULL;
79 ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, NewRunnableMethod(
80 this, &BrowsingDataAppCacheHelper::OnFetchComplete, rv));
81 return;
82 }
[email protected]f26795eb2010-02-26 23:45:3583
[email protected]f26795eb2010-02-26 23:45:3584 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]d68a4fc62010-03-05 23:40:0293ChromeAppCacheService* BrowsingDataAppCacheHelper::GetAppCacheService() {
[email protected]f26795eb2010-02-26 23:45:3594 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO));
[email protected]d68a4fc62010-03-05 23:40:0295 ChromeURLRequestContext* request_context =
96 reinterpret_cast<ChromeURLRequestContext*>(
97 request_context_getter_->GetURLRequestContext());
98 return request_context ? request_context->appcache_service()
99 : NULL;
[email protected]f26795eb2010-02-26 23:45:35100}
[email protected]33b61bc2010-06-10 12:55:00101
102CannedBrowsingDataAppCacheHelper::CannedBrowsingDataAppCacheHelper(
103 Profile* profile)
104 : BrowsingDataAppCacheHelper(profile) {
105 info_collection_ = new appcache::AppCacheInfoCollection;
106}
107
108void 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]9fb83e82010-07-02 18:24:55119void CannedBrowsingDataAppCacheHelper::Reset() {
120 info_collection_->infos_by_origin.clear();
121}
122
[email protected]33b61bc2010-06-10 12:55:00123void CannedBrowsingDataAppCacheHelper::StartFetching(
124 Callback0::Type* completion_callback) {
125 completion_callback->Run();
126}