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