2b5a8bdef63591d6aefda1235691811bd4bc71bf
[platform/framework/web/crosswalk.git] / src / chrome / browser / content_settings / local_shared_objects_container.cc
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_channel_id_helper.h"
9 #include "chrome/browser/browsing_data/browsing_data_cookie_helper.h"
10 #include "chrome/browser/browsing_data/browsing_data_database_helper.h"
11 #include "chrome/browser/browsing_data/browsing_data_file_system_helper.h"
12 #include "chrome/browser/browsing_data/browsing_data_indexed_db_helper.h"
13 #include "chrome/browser/browsing_data/browsing_data_local_storage_helper.h"
14 #include "chrome/browser/browsing_data/browsing_data_service_worker_helper.h"
15 #include "chrome/browser/browsing_data/cookies_tree_model.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "content/public/browser/storage_partition.h"
18 #include "content/public/common/url_constants.h"
19 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
20 #include "net/cookies/canonical_cookie.h"
21 #include "url/gurl.h"
22
23 namespace {
24
25 bool SameDomainOrHost(const GURL& gurl1, const GURL& gurl2) {
26   return net::registry_controlled_domains::SameDomainOrHost(
27       gurl1,
28       gurl2,
29       net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
30 }
31
32 }  // namespace
33
34 LocalSharedObjectsContainer::LocalSharedObjectsContainer(Profile* profile)
35     : appcaches_(new CannedBrowsingDataAppCacheHelper(profile)),
36       channel_ids_(new CannedBrowsingDataChannelIDHelper()),
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       service_workers_(new CannedBrowsingDataServiceWorkerHelper(
46           content::BrowserContext::GetDefaultStoragePartition(profile)->
47               GetServiceWorkerContext())),
48       session_storages_(new CannedBrowsingDataLocalStorageHelper(profile)) {
49 }
50
51 LocalSharedObjectsContainer::~LocalSharedObjectsContainer() {
52 }
53
54 void LocalSharedObjectsContainer::Reset() {
55   appcaches_->Reset();
56   channel_ids_->Reset();
57   cookies_->Reset();
58   databases_->Reset();
59   file_systems_->Reset();
60   indexed_dbs_->Reset();
61   local_storages_->Reset();
62   service_workers_->Reset();
63   session_storages_->Reset();
64 }
65
66 size_t LocalSharedObjectsContainer::GetObjectCount() const {
67   size_t count = 0;
68   count += appcaches()->GetAppCacheCount();
69   count += channel_ids()->GetChannelIDCount();
70   count += cookies()->GetCookieCount();
71   count += databases()->GetDatabaseCount();
72   count += file_systems()->GetFileSystemCount();
73   count += indexed_dbs()->GetIndexedDBCount();
74   count += local_storages()->GetLocalStorageCount();
75   count += service_workers()->GetServiceWorkerCount();
76   count += session_storages()->GetLocalStorageCount();
77   return count;
78 }
79
80 size_t LocalSharedObjectsContainer::GetObjectCountForDomain(
81     const GURL& origin) const {
82   size_t count = 0;
83
84   // Count all cookies that have the same domain as the provided |origin|. This
85   // means count all cookies that has been set by a host that is not considered
86   // to be a third party regarding the domain of the provided |origin|.
87   // E.g. if the origin is "http://foo.com" then all cookies with domain foo.com,
88   // a.foo.com, b.a.foo.com or *.foo.com will be counted.
89   typedef CannedBrowsingDataCookieHelper::OriginCookieListMap
90       OriginCookieListMap;
91   const OriginCookieListMap& origin_cookies_list_map =
92       cookies()->origin_cookie_list_map();
93   for (OriginCookieListMap::const_iterator it =
94           origin_cookies_list_map.begin();
95       it != origin_cookies_list_map.end();
96       ++it) {
97     const net::CookieList* cookie_list = it->second;
98     for (net::CookieList::const_iterator cookie = cookie_list->begin();
99          cookie != cookie_list->end();
100          ++cookie) {
101       // Strip leading '.'s.
102       std::string cookie_domain = cookie->Domain();
103       if (cookie_domain[0] == '.')
104         cookie_domain = cookie_domain.substr(1);
105       // The |domain_url| is only created in order to use the
106       // SameDomainOrHost method below. It does not matter which scheme is
107       // used as the scheme is ignored by the SameDomainOrHost method.
108       GURL domain_url(std::string(url::kHttpScheme) +
109                       url::kStandardSchemeSeparator + cookie_domain);
110       if (SameDomainOrHost(origin, domain_url))
111         ++count;
112     }
113   }
114
115   // Count local storages for the domain of the given |origin|.
116   const std::set<GURL> local_storage_info =
117       local_storages()->GetLocalStorageInfo();
118   for (std::set<GURL>::const_iterator it = local_storage_info.begin();
119        it != local_storage_info.end();
120        ++it) {
121     if (SameDomainOrHost(origin, *it))
122       ++count;
123   }
124
125   // Count session storages for the domain of the given |origin|.
126   const std::set<GURL> urls = session_storages()->GetLocalStorageInfo();
127   for (std::set<GURL>::const_iterator it = urls.begin();
128        it != urls.end();
129        ++it) {
130     if (SameDomainOrHost(origin, *it))
131       ++count;
132   }
133
134   // Count indexed dbs for the domain of the given |origin|.
135   typedef CannedBrowsingDataIndexedDBHelper::PendingIndexedDBInfo IndexedDBInfo;
136   const std::set<IndexedDBInfo>& indexed_db_info =
137       indexed_dbs()->GetIndexedDBInfo();
138   for (std::set<IndexedDBInfo>::const_iterator it =
139           indexed_db_info.begin();
140       it != indexed_db_info.end();
141       ++it) {
142     if (SameDomainOrHost(origin, it->origin))
143       ++count;
144   }
145
146   // Count service workers for the domain of the given |origin|.
147   typedef CannedBrowsingDataServiceWorkerHelper::PendingServiceWorkerUsageInfo
148       ServiceWorkerInfo;
149   const std::set<ServiceWorkerInfo>& service_worker_info =
150       service_workers()->GetServiceWorkerUsageInfo();
151   for (std::set<ServiceWorkerInfo>::const_iterator it =
152           service_worker_info.begin();
153       it != service_worker_info.end();
154       ++it) {
155     if (SameDomainOrHost(origin, it->origin))
156       ++count;
157   }
158
159   // Count filesystems for the domain of the given |origin|.
160   typedef BrowsingDataFileSystemHelper::FileSystemInfo FileSystemInfo;
161   typedef std::list<FileSystemInfo> FileSystemInfoList;
162   const FileSystemInfoList& file_system_info =
163       file_systems()->GetFileSystemInfo();
164   for (FileSystemInfoList::const_iterator it = file_system_info.begin();
165        it != file_system_info.end();
166        ++it) {
167     if (SameDomainOrHost(origin, it->origin))
168       ++count;
169   }
170
171   // Count databases for the domain of the given |origin|.
172   typedef CannedBrowsingDataDatabaseHelper::PendingDatabaseInfo DatabaseInfo;
173   const std::set<DatabaseInfo>& database_list =
174       databases()->GetPendingDatabaseInfo();
175   for (std::set<DatabaseInfo>::const_iterator it =
176           database_list.begin();
177       it != database_list.end();
178       ++it) {
179     if (SameDomainOrHost(origin, it->origin))
180       ++count;
181   }
182
183   // Count the AppCache manifest files for the domain of the given |origin|.
184   typedef BrowsingDataAppCacheHelper::OriginAppCacheInfoMap
185       OriginAppCacheInfoMap;
186   const OriginAppCacheInfoMap& map = appcaches()->GetOriginAppCacheInfoMap();
187   for (OriginAppCacheInfoMap::const_iterator it = map.begin();
188        it != map.end();
189        ++it) {
190     const content::AppCacheInfoVector& info_vector = it->second;
191     for (content::AppCacheInfoVector::const_iterator info =
192              info_vector.begin();
193          info != info_vector.end();
194          ++info) {
195        if (SameDomainOrHost(origin, info->manifest_url))
196          ++count;
197     }
198   }
199
200   return count;
201 }
202
203 scoped_ptr<CookiesTreeModel>
204 LocalSharedObjectsContainer::CreateCookiesTreeModel() const {
205   LocalDataContainer* container = new LocalDataContainer(
206       cookies(),
207       databases(),
208       local_storages(),
209       session_storages(),
210       appcaches(),
211       indexed_dbs(),
212       file_systems(),
213       NULL,
214       channel_ids(),
215       service_workers(),
216       NULL);
217
218   return make_scoped_ptr(new CookiesTreeModel(container, NULL, true));
219 }