• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "chrome/browser/content_settings/local_shared_objects_container.h"
6 
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"
21 
22 namespace {
23 
SameDomainOrHost(const GURL & gurl1,const GURL & gurl2)24 bool SameDomainOrHost(const GURL& gurl1, const GURL& gurl2) {
25   return net::registry_controlled_domains::SameDomainOrHost(
26       gurl1,
27       gurl2,
28       net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
29 }
30 
31 }  // namespace
32 
LocalSharedObjectsContainer(Profile * profile)33 LocalSharedObjectsContainer::LocalSharedObjectsContainer(Profile* profile)
34     : appcaches_(new CannedBrowsingDataAppCacheHelper(profile)),
35       cookies_(new CannedBrowsingDataCookieHelper(
36           profile->GetRequestContext())),
37       databases_(new CannedBrowsingDataDatabaseHelper(profile)),
38       file_systems_(new CannedBrowsingDataFileSystemHelper(profile)),
39       indexed_dbs_(new CannedBrowsingDataIndexedDBHelper(
40           content::BrowserContext::GetDefaultStoragePartition(profile)->
41               GetIndexedDBContext())),
42       local_storages_(new CannedBrowsingDataLocalStorageHelper(profile)),
43       server_bound_certs_(new CannedBrowsingDataServerBoundCertHelper()),
44       session_storages_(new CannedBrowsingDataLocalStorageHelper(profile)) {
45 }
46 
~LocalSharedObjectsContainer()47 LocalSharedObjectsContainer::~LocalSharedObjectsContainer() {
48 }
49 
Reset()50 void LocalSharedObjectsContainer::Reset() {
51   appcaches_->Reset();
52   cookies_->Reset();
53   databases_->Reset();
54   file_systems_->Reset();
55   indexed_dbs_->Reset();
56   local_storages_->Reset();
57   server_bound_certs_->Reset();
58   session_storages_->Reset();
59 }
60 
GetObjectCount() const61 size_t LocalSharedObjectsContainer::GetObjectCount() const {
62   size_t count = 0;
63   count += appcaches()->GetAppCacheCount();
64   count += cookies()->GetCookieCount();
65   count += databases()->GetDatabaseCount();
66   count += file_systems()->GetFileSystemCount();
67   count += indexed_dbs()->GetIndexedDBCount();
68   count += local_storages()->GetLocalStorageCount();
69   count += server_bound_certs()->GetCertCount();
70   count += session_storages()->GetLocalStorageCount();
71   return count;
72 }
73 
GetObjectCountForDomain(const GURL & origin) const74 size_t LocalSharedObjectsContainer::GetObjectCountForDomain(
75     const GURL& origin) const {
76   size_t count = 0;
77 
78   // Count all cookies that have the same domain as the provided |origin|. This
79   // means count all cookies that has been set by a host that is not considered
80   // to be a third party regarding the domain of the provided |origin|.
81   // E.g. if the origin is "http://foo.com" then all cookies with domain foo.com,
82   // a.foo.com, b.a.foo.com or *.foo.com will be counted.
83   typedef CannedBrowsingDataCookieHelper::OriginCookieListMap
84       OriginCookieListMap;
85   const OriginCookieListMap& origin_cookies_list_map =
86       cookies()->origin_cookie_list_map();
87   for (OriginCookieListMap::const_iterator it =
88           origin_cookies_list_map.begin();
89       it != origin_cookies_list_map.end();
90       ++it) {
91     const net::CookieList* cookie_list = it->second;
92     for (net::CookieList::const_iterator cookie = cookie_list->begin();
93          cookie != cookie_list->end();
94          ++cookie) {
95       // Strip leading '.'s.
96       std::string cookie_domain = cookie->Domain();
97       if (cookie_domain[0] == '.')
98         cookie_domain = cookie_domain.substr(1);
99       // The |domain_url| is only created in order to use the
100       // SameDomainOrHost method below. It does not matter which scheme is
101       // used as the scheme is ignored by the SameDomainOrHost method.
102       GURL domain_url(std::string(url::kHttpScheme) +
103                       url::kStandardSchemeSeparator + cookie_domain);
104       if (SameDomainOrHost(origin, domain_url))
105         ++count;
106     }
107   }
108 
109   // Count local storages for the domain of the given |origin|.
110   const std::set<GURL> local_storage_info =
111       local_storages()->GetLocalStorageInfo();
112   for (std::set<GURL>::const_iterator it = local_storage_info.begin();
113        it != local_storage_info.end();
114        ++it) {
115     if (SameDomainOrHost(origin, *it))
116       ++count;
117   }
118 
119   // Count session storages for the domain of the given |origin|.
120   const std::set<GURL> urls = session_storages()->GetLocalStorageInfo();
121   for (std::set<GURL>::const_iterator it = urls.begin();
122        it != urls.end();
123        ++it) {
124     if (SameDomainOrHost(origin, *it))
125       ++count;
126   }
127 
128   // Count indexed dbs for the domain of the given |origin|.
129   typedef CannedBrowsingDataIndexedDBHelper::PendingIndexedDBInfo IndexedDBInfo;
130   const std::set<IndexedDBInfo>& indexed_db_info =
131       indexed_dbs()->GetIndexedDBInfo();
132   for (std::set<IndexedDBInfo>::const_iterator it =
133           indexed_db_info.begin();
134       it != indexed_db_info.end();
135       ++it) {
136     if (SameDomainOrHost(origin, it->origin))
137       ++count;
138   }
139 
140   // Count filesystems for the domain of the given |origin|.
141   typedef BrowsingDataFileSystemHelper::FileSystemInfo FileSystemInfo;
142   typedef std::list<FileSystemInfo> FileSystemInfoList;
143   const FileSystemInfoList& file_system_info =
144       file_systems()->GetFileSystemInfo();
145   for (FileSystemInfoList::const_iterator it = file_system_info.begin();
146        it != file_system_info.end();
147        ++it) {
148     if (SameDomainOrHost(origin, it->origin))
149       ++count;
150   }
151 
152   // Count databases for the domain of the given |origin|.
153   typedef CannedBrowsingDataDatabaseHelper::PendingDatabaseInfo DatabaseInfo;
154   const std::set<DatabaseInfo>& database_list =
155       databases()->GetPendingDatabaseInfo();
156   for (std::set<DatabaseInfo>::const_iterator it =
157           database_list.begin();
158       it != database_list.end();
159       ++it) {
160     if (SameDomainOrHost(origin, it->origin))
161       ++count;
162   }
163 
164   // Count the AppCache manifest files for the domain of the given |origin|.
165   typedef BrowsingDataAppCacheHelper::OriginAppCacheInfoMap
166       OriginAppCacheInfoMap;
167   const OriginAppCacheInfoMap& map = appcaches()->GetOriginAppCacheInfoMap();
168   for (OriginAppCacheInfoMap::const_iterator it = map.begin();
169        it != map.end();
170        ++it) {
171     const appcache::AppCacheInfoVector& info_vector = it->second;
172     for (appcache::AppCacheInfoVector::const_iterator info =
173              info_vector.begin();
174          info != info_vector.end();
175          ++info) {
176        if (SameDomainOrHost(origin, info->manifest_url))
177          ++count;
178     }
179   }
180 
181   return count;
182 }
183 
184 scoped_ptr<CookiesTreeModel>
CreateCookiesTreeModel() const185 LocalSharedObjectsContainer::CreateCookiesTreeModel() const {
186   LocalDataContainer* container = new LocalDataContainer(
187       cookies(),
188       databases(),
189       local_storages(),
190       session_storages(),
191       appcaches(),
192       indexed_dbs(),
193       file_systems(),
194       NULL,
195       server_bound_certs(),
196       NULL);
197 
198   return make_scoped_ptr(new CookiesTreeModel(container, NULL, true));
199 }
200