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 "content/browser/appcache/appcache_storage.h"
8 #include "base/bind_helpers.h"
9 #include "base/stl_util.h"
10 #include "content/browser/appcache/appcache_response.h"
11 #include "content/browser/appcache/appcache_service_impl.h"
12 #include "storage/browser/quota/quota_client.h"
13 #include "storage/browser/quota/quota_manager_proxy.h"
18 const int64
AppCacheStorage::kUnitializedId
= -1;
20 AppCacheStorage::AppCacheStorage(AppCacheServiceImpl
* 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_
, 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 storage::QuotaClient::kAppcache
,
110 storage::kStorageTypeTemporary
,
111 new_usage
- old_usage
);
115 void AppCacheStorage::ClearUsageMapAndNotify() {
116 if (service()->quota_manager_proxy()) {
117 for (UsageMap::const_iterator iter
= usage_map_
.begin();
118 iter
!= usage_map_
.end(); ++iter
) {
119 service()->quota_manager_proxy()->NotifyStorageModified(
120 storage::QuotaClient::kAppcache
,
122 storage::kStorageTypeTemporary
,
129 void AppCacheStorage::NotifyStorageAccessed(const GURL
& origin
) {
130 if (service()->quota_manager_proxy() &&
131 usage_map_
.find(origin
) != usage_map_
.end())
132 service()->quota_manager_proxy()->NotifyStorageAccessed(
133 storage::QuotaClient::kAppcache
,
135 storage::kStorageTypeTemporary
);
138 } // namespace content