Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / content_settings / local_shared_objects_container.cc
blob310a106a751c87947eb44492980af99b0f12f4d5
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_cookie_helper.h"
9 #include "chrome/browser/browsing_data/browsing_data_database_helper.h"
10 #include "chrome/browser/browsing_data/browsing_data_file_system_helper.h"
11 #include "chrome/browser/browsing_data/browsing_data_indexed_db_helper.h"
12 #include "chrome/browser/browsing_data/browsing_data_local_storage_helper.h"
13 #include "chrome/browser/browsing_data/browsing_data_server_bound_cert_helper.h"
14 #include "chrome/browser/browsing_data/cookies_tree_model.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "content/public/browser/storage_partition.h"
17 #include "content/public/common/url_constants.h"
18 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
19 #include "net/cookies/canonical_cookie.h"
20 #include "url/gurl.h"
22 namespace {
24 // Helper wrapper for net::registry_controlled_domains::SameDomainOrHost
25 // which always excludes private registries.
26 bool SamePublicDomainOrHost(const GURL& gurl1, const GURL& gurl2) {
27 return net::registry_controlled_domains::SameDomainOrHost(
28 gurl1,
29 gurl2,
30 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
33 } // namespace
35 LocalSharedObjectsContainer::LocalSharedObjectsContainer(Profile* profile)
36 : appcaches_(new CannedBrowsingDataAppCacheHelper(profile)),
37 cookies_(new CannedBrowsingDataCookieHelper(
38 profile->GetRequestContext())),
39 databases_(new CannedBrowsingDataDatabaseHelper(profile)),
40 file_systems_(new CannedBrowsingDataFileSystemHelper(profile)),
41 indexed_dbs_(new CannedBrowsingDataIndexedDBHelper(
42 content::BrowserContext::GetDefaultStoragePartition(profile)->
43 GetIndexedDBContext())),
44 local_storages_(new CannedBrowsingDataLocalStorageHelper(profile)),
45 server_bound_certs_(new CannedBrowsingDataServerBoundCertHelper()),
46 session_storages_(new CannedBrowsingDataLocalStorageHelper(profile)) {
49 LocalSharedObjectsContainer::~LocalSharedObjectsContainer() {
52 void LocalSharedObjectsContainer::Reset() {
53 appcaches_->Reset();
54 cookies_->Reset();
55 databases_->Reset();
56 file_systems_->Reset();
57 indexed_dbs_->Reset();
58 local_storages_->Reset();
59 server_bound_certs_->Reset();
60 session_storages_->Reset();
63 size_t LocalSharedObjectsContainer::GetObjectCount() const {
64 size_t count = 0;
65 count += appcaches()->GetAppCacheCount();
66 count += cookies()->GetCookieCount();
67 count += databases()->GetDatabaseCount();
68 count += file_systems()->GetFileSystemCount();
69 count += indexed_dbs()->GetIndexedDBCount();
70 count += local_storages()->GetLocalStorageCount();
71 count += server_bound_certs()->GetCertCount();
72 count += session_storages()->GetLocalStorageCount();
73 return count;
76 size_t LocalSharedObjectsContainer::GetObjectCountForDomain(
77 const GURL& origin) const {
78 size_t count = 0;
80 // Count all cookies that have the same domain as the provided |origin|. This
81 // means count all cookies that has been set by a host that is not considered
82 // to be a third party regarding the domain of the provided |origin|.
83 // E.g. if the origin is "http://foo.com" then all cookies with domain foo.com,
84 // a.foo.com, b.a.foo.com or *.foo.com will be counted.
85 typedef CannedBrowsingDataCookieHelper::OriginCookieListMap
86 OriginCookieListMap;
87 const OriginCookieListMap& origin_cookies_list_map =
88 cookies()->origin_cookie_list_map();
89 for (OriginCookieListMap::const_iterator it =
90 origin_cookies_list_map.begin();
91 it != origin_cookies_list_map.end();
92 ++it) {
93 const net::CookieList* cookie_list = it->second;
94 for (net::CookieList::const_iterator cookie = cookie_list->begin();
95 cookie != cookie_list->end();
96 ++cookie) {
97 // Strip leading '.'s.
98 std::string cookie_domain = cookie->Domain();
99 if (cookie_domain[0] == '.')
100 cookie_domain = cookie_domain.substr(1);
101 // The |domain_url| is only created in order to use the
102 // SamePublicDomainOrHost method below. It does not matter which scheme is
103 // used as the scheme is ignored by the SamePublicDomainOrHost method.
104 GURL domain_url(std::string(content::kHttpScheme) +
105 content::kStandardSchemeSeparator + cookie_domain);
106 if (SamePublicDomainOrHost(origin, domain_url))
107 ++count;
111 // Count local storages for the domain of the given |origin|.
112 const std::set<GURL> local_storage_info =
113 local_storages()->GetLocalStorageInfo();
114 for (std::set<GURL>::const_iterator it = local_storage_info.begin();
115 it != local_storage_info.end();
116 ++it) {
117 if (SamePublicDomainOrHost(origin, *it))
118 ++count;
121 // Count session storages for the domain of the given |origin|.
122 const std::set<GURL> urls = session_storages()->GetLocalStorageInfo();
123 for (std::set<GURL>::const_iterator it = urls.begin();
124 it != urls.end();
125 ++it) {
126 if (SamePublicDomainOrHost(origin, *it))
127 ++count;
130 // Count indexed dbs for the domain of the given |origin|.
131 typedef CannedBrowsingDataIndexedDBHelper::PendingIndexedDBInfo IndexedDBInfo;
132 const std::set<IndexedDBInfo>& indexed_db_info =
133 indexed_dbs()->GetIndexedDBInfo();
134 for (std::set<IndexedDBInfo>::const_iterator it =
135 indexed_db_info.begin();
136 it != indexed_db_info.end();
137 ++it) {
138 if (SamePublicDomainOrHost(origin, it->origin))
139 ++count;
142 // Count filesystems for the domain of the given |origin|.
143 typedef BrowsingDataFileSystemHelper::FileSystemInfo FileSystemInfo;
144 typedef std::list<FileSystemInfo> FileSystemInfoList;
145 const FileSystemInfoList& file_system_info =
146 file_systems()->GetFileSystemInfo();
147 for (FileSystemInfoList::const_iterator it = file_system_info.begin();
148 it != file_system_info.end();
149 ++it) {
150 if (SamePublicDomainOrHost(origin, it->origin))
151 ++count;
154 // Count databases for the domain of the given |origin|.
155 typedef CannedBrowsingDataDatabaseHelper::PendingDatabaseInfo DatabaseInfo;
156 const std::set<DatabaseInfo>& database_list =
157 databases()->GetPendingDatabaseInfo();
158 for (std::set<DatabaseInfo>::const_iterator it =
159 database_list.begin();
160 it != database_list.end();
161 ++it) {
162 if (SamePublicDomainOrHost(origin, it->origin))
163 ++count;
166 // Count the AppCache manifest files for the domain of the given |origin|.
167 typedef BrowsingDataAppCacheHelper::OriginAppCacheInfoMap
168 OriginAppCacheInfoMap;
169 const OriginAppCacheInfoMap& map = appcaches()->GetOriginAppCacheInfoMap();
170 for (OriginAppCacheInfoMap::const_iterator it = map.begin();
171 it != map.end();
172 ++it) {
173 const appcache::AppCacheInfoVector& info_vector = it->second;
174 for (appcache::AppCacheInfoVector::const_iterator info =
175 info_vector.begin();
176 info != info_vector.end();
177 ++info) {
178 if (SamePublicDomainOrHost(origin, info->manifest_url))
179 ++count;
183 return count;
186 scoped_ptr<CookiesTreeModel>
187 LocalSharedObjectsContainer::CreateCookiesTreeModel() const {
188 LocalDataContainer* container = new LocalDataContainer(
189 cookies(),
190 databases(),
191 local_storages(),
192 session_storages(),
193 appcaches(),
194 indexed_dbs(),
195 file_systems(),
196 NULL,
197 server_bound_certs(),
198 NULL);
200 return make_scoped_ptr(new CookiesTreeModel(container, NULL, true));