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/browsing_data/local_data_container.h"
6
7#include "base/bind.h"
8#include "base/memory/linked_ptr.h"
9#include "chrome/browser/browsing_data/browsing_data_channel_id_helper.h"
10#include "chrome/browser/browsing_data/browsing_data_flash_lso_helper.h"
11#include "chrome/browser/browsing_data/cookies_tree_model.h"
12#include "chrome/browser/content_settings/cookie_settings.h"
13#include "net/cookies/canonical_cookie.h"
14
15///////////////////////////////////////////////////////////////////////////////
16// LocalDataContainer, public:
17
18LocalDataContainer::LocalDataContainer(
19    BrowsingDataCookieHelper* cookie_helper,
20    BrowsingDataDatabaseHelper* database_helper,
21    BrowsingDataLocalStorageHelper* local_storage_helper,
22    BrowsingDataLocalStorageHelper* session_storage_helper,
23    BrowsingDataAppCacheHelper* appcache_helper,
24    BrowsingDataIndexedDBHelper* indexed_db_helper,
25    BrowsingDataFileSystemHelper* file_system_helper,
26    BrowsingDataQuotaHelper* quota_helper,
27    BrowsingDataChannelIDHelper* channel_id_helper,
28    BrowsingDataServiceWorkerHelper* service_worker_helper,
29    BrowsingDataFlashLSOHelper* flash_lso_helper)
30    : appcache_helper_(appcache_helper),
31      cookie_helper_(cookie_helper),
32      database_helper_(database_helper),
33      local_storage_helper_(local_storage_helper),
34      session_storage_helper_(session_storage_helper),
35      indexed_db_helper_(indexed_db_helper),
36      file_system_helper_(file_system_helper),
37      quota_helper_(quota_helper),
38      channel_id_helper_(channel_id_helper),
39      service_worker_helper_(service_worker_helper),
40      flash_lso_helper_(flash_lso_helper),
41      model_(NULL),
42      weak_ptr_factory_(this) {
43}
44
45LocalDataContainer::~LocalDataContainer() {}
46
47void LocalDataContainer::Init(CookiesTreeModel* model) {
48  DCHECK(!model_);
49  model_ = model;
50
51  DCHECK(cookie_helper_.get());
52  cookie_helper_->StartFetching(
53      base::Bind(&LocalDataContainer::OnCookiesModelInfoLoaded,
54                 weak_ptr_factory_.GetWeakPtr()));
55
56  if (database_helper_.get()) {
57    database_helper_->StartFetching(
58        base::Bind(&LocalDataContainer::OnDatabaseModelInfoLoaded,
59                   weak_ptr_factory_.GetWeakPtr()));
60  }
61
62  if (local_storage_helper_.get()) {
63    local_storage_helper_->StartFetching(
64        base::Bind(&LocalDataContainer::OnLocalStorageModelInfoLoaded,
65                   weak_ptr_factory_.GetWeakPtr()));
66  }
67
68  if (session_storage_helper_.get()) {
69    session_storage_helper_->StartFetching(
70        base::Bind(&LocalDataContainer::OnSessionStorageModelInfoLoaded,
71                   weak_ptr_factory_.GetWeakPtr()));
72  }
73
74  // TODO(michaeln): When all of the UI implementations have been updated, make
75  // this a required parameter.
76  if (appcache_helper_.get()) {
77    appcache_helper_->StartFetching(
78        base::Bind(&LocalDataContainer::OnAppCacheModelInfoLoaded,
79                   weak_ptr_factory_.GetWeakPtr()));
80  }
81
82  if (indexed_db_helper_.get()) {
83    indexed_db_helper_->StartFetching(
84        base::Bind(&LocalDataContainer::OnIndexedDBModelInfoLoaded,
85                   weak_ptr_factory_.GetWeakPtr()));
86  }
87
88  if (file_system_helper_.get()) {
89    file_system_helper_->StartFetching(
90        base::Bind(&LocalDataContainer::OnFileSystemModelInfoLoaded,
91                   weak_ptr_factory_.GetWeakPtr()));
92  }
93
94  if (quota_helper_.get()) {
95    quota_helper_->StartFetching(
96        base::Bind(&LocalDataContainer::OnQuotaModelInfoLoaded,
97                   weak_ptr_factory_.GetWeakPtr()));
98  }
99
100  if (channel_id_helper_.get()) {
101    channel_id_helper_->StartFetching(
102        base::Bind(&LocalDataContainer::OnChannelIDModelInfoLoaded,
103                   weak_ptr_factory_.GetWeakPtr()));
104  }
105
106  if (service_worker_helper_.get()) {
107    service_worker_helper_->StartFetching(
108        base::Bind(&LocalDataContainer::OnServiceWorkerModelInfoLoaded,
109                   weak_ptr_factory_.GetWeakPtr()));
110  }
111
112  if (flash_lso_helper_.get()) {
113    flash_lso_helper_->StartFetching(
114        base::Bind(&LocalDataContainer::OnFlashLSOInfoLoaded,
115                   weak_ptr_factory_.GetWeakPtr()));
116  }
117}
118
119void LocalDataContainer::OnAppCacheModelInfoLoaded() {
120  using content::AppCacheInfo;
121  using content::AppCacheInfoCollection;
122  using content::AppCacheInfoVector;
123  typedef std::map<GURL, AppCacheInfoVector> InfoByOrigin;
124
125  scoped_refptr<AppCacheInfoCollection> appcache_info =
126      appcache_helper_->info_collection();
127  if (!appcache_info.get() || appcache_info->infos_by_origin.empty())
128    return;
129
130  for (InfoByOrigin::const_iterator origin =
131           appcache_info->infos_by_origin.begin();
132       origin != appcache_info->infos_by_origin.end(); ++origin) {
133    std::list<AppCacheInfo>& info_list = appcache_info_[origin->first];
134    info_list.insert(
135        info_list.begin(), origin->second.begin(), origin->second.end());
136  }
137
138  model_->PopulateAppCacheInfo(this);
139}
140
141void LocalDataContainer::OnCookiesModelInfoLoaded(
142    const net::CookieList& cookie_list) {
143  cookie_list_.insert(cookie_list_.begin(),
144                      cookie_list.begin(),
145                      cookie_list.end());
146  DCHECK(model_);
147  model_->PopulateCookieInfo(this);
148}
149
150void LocalDataContainer::OnDatabaseModelInfoLoaded(
151    const DatabaseInfoList& database_info) {
152  database_info_list_ = database_info;
153  DCHECK(model_);
154  model_->PopulateDatabaseInfo(this);
155}
156
157void LocalDataContainer::OnLocalStorageModelInfoLoaded(
158    const LocalStorageInfoList& local_storage_info) {
159  local_storage_info_list_ = local_storage_info;
160  DCHECK(model_);
161  model_->PopulateLocalStorageInfo(this);
162}
163
164void LocalDataContainer::OnSessionStorageModelInfoLoaded(
165    const LocalStorageInfoList& session_storage_info) {
166  session_storage_info_list_ = session_storage_info;
167  DCHECK(model_);
168  model_->PopulateSessionStorageInfo(this);
169}
170
171void LocalDataContainer::OnIndexedDBModelInfoLoaded(
172    const IndexedDBInfoList& indexed_db_info) {
173  indexed_db_info_list_ = indexed_db_info;
174  DCHECK(model_);
175  model_->PopulateIndexedDBInfo(this);
176}
177
178void LocalDataContainer::OnFileSystemModelInfoLoaded(
179    const FileSystemInfoList& file_system_info) {
180  file_system_info_list_ = file_system_info;
181  DCHECK(model_);
182  model_->PopulateFileSystemInfo(this);
183}
184
185void LocalDataContainer::OnQuotaModelInfoLoaded(
186    const QuotaInfoList& quota_info) {
187  quota_info_list_ = quota_info;
188  DCHECK(model_);
189  model_->PopulateQuotaInfo(this);
190}
191
192void LocalDataContainer::OnChannelIDModelInfoLoaded(
193    const ChannelIDList& channel_id_list) {
194  channel_id_list_ = channel_id_list;
195  DCHECK(model_);
196  model_->PopulateChannelIDInfo(this);
197}
198
199void LocalDataContainer::OnServiceWorkerModelInfoLoaded(
200    const ServiceWorkerUsageInfoList& service_worker_info) {
201  service_worker_info_list_ = service_worker_info;
202  DCHECK(model_);
203  model_->PopulateServiceWorkerUsageInfo(this);
204}
205
206void LocalDataContainer::OnFlashLSOInfoLoaded(
207    const FlashLSODomainList& domains) {
208  flash_lso_domain_list_ = domains;
209  DCHECK(model_);
210  model_->PopulateFlashLSOInfo(this);
211}
212