1 // Copyright (c) 2012 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 "chrome/browser/content_settings/local_shared_objects_container.h"
7 #include "chrome/browser/browsing_data/browsing_data_appcache_helper.h"
8 #include "chrome/browser/browsing_data/browsing_data_cache_storage_helper.h"
9 #include "chrome/browser/browsing_data/browsing_data_channel_id_helper.h"
10 #include "chrome/browser/browsing_data/browsing_data_cookie_helper.h"
11 #include "chrome/browser/browsing_data/browsing_data_database_helper.h"
12 #include "chrome/browser/browsing_data/browsing_data_file_system_helper.h"
13 #include "chrome/browser/browsing_data/browsing_data_indexed_db_helper.h"
14 #include "chrome/browser/browsing_data/browsing_data_local_storage_helper.h"
15 #include "chrome/browser/browsing_data/browsing_data_service_worker_helper.h"
16 #include "chrome/browser/browsing_data/canonical_cookie_hash.h"
17 #include "chrome/browser/browsing_data/cookies_tree_model.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "content/public/browser/storage_partition.h"
20 #include "content/public/common/url_constants.h"
21 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
22 #include "net/cookies/canonical_cookie.h"
27 bool SameDomainOrHost(const GURL
& gurl1
, const GURL
& gurl2
) {
28 return net::registry_controlled_domains::SameDomainOrHost(
31 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES
);
36 LocalSharedObjectsContainer::LocalSharedObjectsContainer(Profile
* profile
)
37 : appcaches_(new CannedBrowsingDataAppCacheHelper(profile
)),
38 channel_ids_(new CannedBrowsingDataChannelIDHelper()),
40 new CannedBrowsingDataCookieHelper(profile
->GetRequestContext())),
41 databases_(new CannedBrowsingDataDatabaseHelper(profile
)),
42 file_systems_(new CannedBrowsingDataFileSystemHelper(profile
)),
43 indexed_dbs_(new CannedBrowsingDataIndexedDBHelper(
44 content::BrowserContext::GetDefaultStoragePartition(profile
)
45 ->GetIndexedDBContext())),
46 local_storages_(new CannedBrowsingDataLocalStorageHelper(profile
)),
47 service_workers_(new CannedBrowsingDataServiceWorkerHelper(
48 content::BrowserContext::GetDefaultStoragePartition(profile
)
49 ->GetServiceWorkerContext())),
50 cache_storages_(new CannedBrowsingDataCacheStorageHelper(
51 content::BrowserContext::GetDefaultStoragePartition(profile
)
52 ->GetCacheStorageContext())),
53 session_storages_(new CannedBrowsingDataLocalStorageHelper(profile
)) {}
55 LocalSharedObjectsContainer::~LocalSharedObjectsContainer() {
58 size_t LocalSharedObjectsContainer::GetObjectCount() const {
60 count
+= appcaches()->GetAppCacheCount();
61 count
+= channel_ids()->GetChannelIDCount();
62 count
+= cookies()->GetCookieCount();
63 count
+= databases()->GetDatabaseCount();
64 count
+= file_systems()->GetFileSystemCount();
65 count
+= indexed_dbs()->GetIndexedDBCount();
66 count
+= local_storages()->GetLocalStorageCount();
67 count
+= service_workers()->GetServiceWorkerCount();
68 count
+= cache_storages()->GetCacheStorageCount();
69 count
+= session_storages()->GetLocalStorageCount();
73 size_t LocalSharedObjectsContainer::GetObjectCountForDomain(
74 const GURL
& origin
) const {
77 // Count all cookies that have the same domain as the provided |origin|. This
78 // means count all cookies that has been set by a host that is not considered
79 // to be a third party regarding the domain of the provided |origin|.
80 // E.g. if the origin is "http://foo.com" then all cookies with domain foo.com,
81 // a.foo.com, b.a.foo.com or *.foo.com will be counted.
82 typedef CannedBrowsingDataCookieHelper::OriginCookieSetMap OriginCookieSetMap
;
83 const OriginCookieSetMap
& origin_cookies_set_map
=
84 cookies()->origin_cookie_set_map();
85 for (OriginCookieSetMap::const_iterator it
= origin_cookies_set_map
.begin();
86 it
!= origin_cookies_set_map
.end();
88 const canonical_cookie::CookieHashSet
* cookie_list
= it
->second
;
89 for (canonical_cookie::CookieHashSet::const_iterator cookie
=
91 cookie
!= cookie_list
->end();
93 // Strip leading '.'s.
94 std::string cookie_domain
= cookie
->Domain();
95 if (cookie_domain
[0] == '.')
96 cookie_domain
= cookie_domain
.substr(1);
97 // The |domain_url| is only created in order to use the
98 // SameDomainOrHost method below. It does not matter which scheme is
99 // used as the scheme is ignored by the SameDomainOrHost method.
100 GURL
domain_url(std::string(url::kHttpScheme
) +
101 url::kStandardSchemeSeparator
+ cookie_domain
);
102 if (SameDomainOrHost(origin
, domain_url
))
107 // Count local storages for the domain of the given |origin|.
108 const std::set
<GURL
> local_storage_info
=
109 local_storages()->GetLocalStorageInfo();
110 for (std::set
<GURL
>::const_iterator it
= local_storage_info
.begin();
111 it
!= local_storage_info
.end();
113 if (SameDomainOrHost(origin
, *it
))
117 // Count session storages for the domain of the given |origin|.
118 const std::set
<GURL
> urls
= session_storages()->GetLocalStorageInfo();
119 for (std::set
<GURL
>::const_iterator it
= urls
.begin();
122 if (SameDomainOrHost(origin
, *it
))
126 // Count indexed dbs for the domain of the given |origin|.
127 typedef CannedBrowsingDataIndexedDBHelper::PendingIndexedDBInfo IndexedDBInfo
;
128 const std::set
<IndexedDBInfo
>& indexed_db_info
=
129 indexed_dbs()->GetIndexedDBInfo();
130 for (std::set
<IndexedDBInfo
>::const_iterator it
=
131 indexed_db_info
.begin();
132 it
!= indexed_db_info
.end();
134 if (SameDomainOrHost(origin
, it
->origin
))
138 // Count service workers for the domain of the given |origin|.
139 typedef CannedBrowsingDataServiceWorkerHelper::PendingServiceWorkerUsageInfo
141 const std::set
<ServiceWorkerInfo
>& service_worker_info
=
142 service_workers()->GetServiceWorkerUsageInfo();
143 for (std::set
<ServiceWorkerInfo
>::const_iterator it
=
144 service_worker_info
.begin();
145 it
!= service_worker_info
.end();
147 if (SameDomainOrHost(origin
, it
->origin
))
151 // Count cache storages for the domain of the given |origin|.
152 typedef CannedBrowsingDataCacheStorageHelper::PendingCacheStorageUsageInfo
154 const std::set
<CacheStorageInfo
>& cache_storage_info
=
155 cache_storages()->GetCacheStorageUsageInfo();
156 for (const CacheStorageInfo
& it
: cache_storage_info
) {
157 if (SameDomainOrHost(origin
, it
.origin
))
161 // Count filesystems for the domain of the given |origin|.
162 typedef BrowsingDataFileSystemHelper::FileSystemInfo FileSystemInfo
;
163 typedef std::list
<FileSystemInfo
> FileSystemInfoList
;
164 const FileSystemInfoList
& file_system_info
=
165 file_systems()->GetFileSystemInfo();
166 for (FileSystemInfoList::const_iterator it
= file_system_info
.begin();
167 it
!= file_system_info
.end();
169 if (SameDomainOrHost(origin
, it
->origin
))
173 // Count databases for the domain of the given |origin|.
174 typedef CannedBrowsingDataDatabaseHelper::PendingDatabaseInfo DatabaseInfo
;
175 const std::set
<DatabaseInfo
>& database_list
=
176 databases()->GetPendingDatabaseInfo();
177 for (std::set
<DatabaseInfo
>::const_iterator it
=
178 database_list
.begin();
179 it
!= database_list
.end();
181 if (SameDomainOrHost(origin
, it
->origin
))
185 // Count the AppCache manifest files for the domain of the given |origin|.
186 typedef BrowsingDataAppCacheHelper::OriginAppCacheInfoMap
187 OriginAppCacheInfoMap
;
188 const OriginAppCacheInfoMap
& map
= appcaches()->GetOriginAppCacheInfoMap();
189 for (OriginAppCacheInfoMap::const_iterator it
= map
.begin();
192 const content::AppCacheInfoVector
& info_vector
= it
->second
;
193 for (content::AppCacheInfoVector::const_iterator info
=
195 info
!= info_vector
.end();
197 if (SameDomainOrHost(origin
, info
->manifest_url
))
205 void LocalSharedObjectsContainer::Reset() {
207 channel_ids_
->Reset();
210 file_systems_
->Reset();
211 indexed_dbs_
->Reset();
212 local_storages_
->Reset();
213 service_workers_
->Reset();
214 cache_storages_
->Reset();
215 session_storages_
->Reset();
218 scoped_ptr
<CookiesTreeModel
>
219 LocalSharedObjectsContainer::CreateCookiesTreeModel() const {
220 LocalDataContainer
* container
= new LocalDataContainer(
221 cookies(), databases(), local_storages(), session_storages(), appcaches(),
222 indexed_dbs(), file_systems(), NULL
, channel_ids(), service_workers(),
223 cache_storages(), NULL
);
225 return make_scoped_ptr(new CookiesTreeModel(container
, NULL
, true));