1 // Copyright (c) 2009 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.
5 #include "webkit/browser/appcache/appcache_storage.h"
8 #include "base/bind_helpers.h"
9 #include "base/stl_util.h"
10 #include "webkit/browser/appcache/appcache_response.h"
11 #include "webkit/browser/appcache/appcache_service.h"
12 #include "webkit/browser/quota/quota_client.h"
13 #include "webkit/browser/quota/quota_manager.h"
18 const int64
AppCacheStorage::kUnitializedId
= -1;
20 AppCacheStorage::AppCacheStorage(AppCacheService
* service
)
21 : last_cache_id_(kUnitializedId
), last_group_id_(kUnitializedId
),
22 last_response_id_(kUnitializedId
), service_(service
) {
25 AppCacheStorage::~AppCacheStorage() {
26 STLDeleteValues(&pending_info_loads_
);
27 DCHECK(delegate_references_
.empty());
30 AppCacheStorage::DelegateReference::DelegateReference(
31 Delegate
* delegate
, AppCacheStorage
* storage
)
32 : delegate(delegate
), storage(storage
) {
33 storage
->delegate_references_
.insert(
34 DelegateReferenceMap::value_type(delegate
, this));
37 AppCacheStorage::DelegateReference::~DelegateReference() {
39 storage
->delegate_references_
.erase(delegate
);
42 AppCacheStorage::ResponseInfoLoadTask::ResponseInfoLoadTask(
43 const GURL
& manifest_url
,
46 AppCacheStorage
* storage
)
48 manifest_url_(manifest_url
),
50 response_id_(response_id
),
51 info_buffer_(new HttpResponseInfoIOBuffer
) {
52 storage_
->pending_info_loads_
.insert(
53 PendingResponseInfoLoads::value_type(response_id
, this));
56 AppCacheStorage::ResponseInfoLoadTask::~ResponseInfoLoadTask() {
59 void AppCacheStorage::ResponseInfoLoadTask::StartIfNeeded() {
63 storage_
->CreateResponseReader(manifest_url_
, group_id_
, response_id_
));
64 reader_
->ReadInfo(info_buffer_
.get(),
65 base::Bind(&ResponseInfoLoadTask::OnReadComplete
,
66 base::Unretained(this)));
69 void AppCacheStorage::ResponseInfoLoadTask::OnReadComplete(int result
) {
70 storage_
->pending_info_loads_
.erase(response_id_
);
71 scoped_refptr
<AppCacheResponseInfo
> info
;
73 info
= new AppCacheResponseInfo(storage_
->service(), manifest_url_
,
75 info_buffer_
->http_info
.release(),
76 info_buffer_
->response_data_size
);
78 FOR_EACH_DELEGATE(delegates_
, OnResponseInfoLoaded(info
.get(), response_id_
));
82 void AppCacheStorage::LoadResponseInfo(
83 const GURL
& manifest_url
, int64 group_id
, int64 id
, Delegate
* delegate
) {
84 AppCacheResponseInfo
* info
= working_set_
.GetResponseInfo(id
);
86 delegate
->OnResponseInfoLoaded(info
, id
);
89 ResponseInfoLoadTask
* info_load
=
90 GetOrCreateResponseInfoLoadTask(manifest_url
, group_id
, id
);
91 DCHECK(manifest_url
== info_load
->manifest_url());
92 DCHECK(group_id
== info_load
->group_id());
93 DCHECK(id
== info_load
->response_id());
94 info_load
->AddDelegate(GetOrCreateDelegateReference(delegate
));
95 info_load
->StartIfNeeded();
98 void AppCacheStorage::UpdateUsageMapAndNotify(
99 const GURL
& origin
, int64 new_usage
) {
100 DCHECK_GE(new_usage
, 0);
101 int64 old_usage
= usage_map_
[origin
];
103 usage_map_
[origin
] = new_usage
;
105 usage_map_
.erase(origin
);
106 if (new_usage
!= old_usage
&& service()->quota_manager_proxy()) {
107 service()->quota_manager_proxy()->NotifyStorageModified(
108 quota::QuotaClient::kAppcache
,
109 origin
, quota::kStorageTypeTemporary
,
110 new_usage
- old_usage
);
114 void AppCacheStorage::ClearUsageMapAndNotify() {
115 if (service()->quota_manager_proxy()) {
116 for (UsageMap::const_iterator iter
= usage_map_
.begin();
117 iter
!= usage_map_
.end(); ++iter
) {
118 service()->quota_manager_proxy()->NotifyStorageModified(
119 quota::QuotaClient::kAppcache
,
120 iter
->first
, quota::kStorageTypeTemporary
,
127 void AppCacheStorage::NotifyStorageAccessed(const GURL
& origin
) {
128 if (service()->quota_manager_proxy() &&
129 usage_map_
.find(origin
) != usage_map_
.end())
130 service()->quota_manager_proxy()->NotifyStorageAccessed(
131 quota::QuotaClient::kAppcache
,
132 origin
, quota::kStorageTypeTemporary
);
135 } // namespace appcache