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_flash_lso_helper.h"
10#include "chrome/browser/browsing_data/browsing_data_server_bound_cert_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    BrowsingDataServerBoundCertHelper* server_bound_cert_helper,
28    BrowsingDataFlashLSOHelper* flash_lso_helper)
29    : appcache_helper_(appcache_helper),
30      cookie_helper_(cookie_helper),
31      database_helper_(database_helper),
32      local_storage_helper_(local_storage_helper),
33      session_storage_helper_(session_storage_helper),
34      indexed_db_helper_(indexed_db_helper),
35      file_system_helper_(file_system_helper),
36      quota_helper_(quota_helper),
37      server_bound_cert_helper_(server_bound_cert_helper),
38      flash_lso_helper_(flash_lso_helper),
39      model_(NULL),
40      weak_ptr_factory_(this) {}
41
42LocalDataContainer::~LocalDataContainer() {}
43
44void LocalDataContainer::Init(CookiesTreeModel* model) {
45  DCHECK(!model_);
46  model_ = model;
47
48  DCHECK(cookie_helper_.get());
49  cookie_helper_->StartFetching(
50      base::Bind(&LocalDataContainer::OnCookiesModelInfoLoaded,
51                 weak_ptr_factory_.GetWeakPtr()));
52
53  if (database_helper_.get()) {
54    database_helper_->StartFetching(
55        base::Bind(&LocalDataContainer::OnDatabaseModelInfoLoaded,
56                   weak_ptr_factory_.GetWeakPtr()));
57  }
58
59  if (local_storage_helper_.get()) {
60    local_storage_helper_->StartFetching(
61        base::Bind(&LocalDataContainer::OnLocalStorageModelInfoLoaded,
62                   weak_ptr_factory_.GetWeakPtr()));
63  }
64
65  if (session_storage_helper_.get()) {
66    session_storage_helper_->StartFetching(
67        base::Bind(&LocalDataContainer::OnSessionStorageModelInfoLoaded,
68                   weak_ptr_factory_.GetWeakPtr()));
69  }
70
71  // TODO(michaeln): When all of the UI implementations have been updated, make
72  // this a required parameter.
73  if (appcache_helper_.get()) {
74    appcache_helper_->StartFetching(
75        base::Bind(&LocalDataContainer::OnAppCacheModelInfoLoaded,
76                   weak_ptr_factory_.GetWeakPtr()));
77  }
78
79  if (indexed_db_helper_.get()) {
80    indexed_db_helper_->StartFetching(
81        base::Bind(&LocalDataContainer::OnIndexedDBModelInfoLoaded,
82                   weak_ptr_factory_.GetWeakPtr()));
83  }
84
85  if (file_system_helper_.get()) {
86    file_system_helper_->StartFetching(
87        base::Bind(&LocalDataContainer::OnFileSystemModelInfoLoaded,
88                   weak_ptr_factory_.GetWeakPtr()));
89  }
90
91  if (quota_helper_.get()) {
92    quota_helper_->StartFetching(
93        base::Bind(&LocalDataContainer::OnQuotaModelInfoLoaded,
94                   weak_ptr_factory_.GetWeakPtr()));
95  }
96
97  if (server_bound_cert_helper_.get()) {
98    server_bound_cert_helper_->StartFetching(
99        base::Bind(&LocalDataContainer::OnServerBoundCertModelInfoLoaded,
100                   weak_ptr_factory_.GetWeakPtr()));
101  }
102
103  if (flash_lso_helper_.get()) {
104    flash_lso_helper_->StartFetching(
105        base::Bind(&LocalDataContainer::OnFlashLSOInfoLoaded,
106                   weak_ptr_factory_.GetWeakPtr()));
107  }
108}
109
110void LocalDataContainer::OnAppCacheModelInfoLoaded() {
111  using appcache::AppCacheInfo;
112  using appcache::AppCacheInfoCollection;
113  using appcache::AppCacheInfoVector;
114  typedef std::map<GURL, AppCacheInfoVector> InfoByOrigin;
115
116  scoped_refptr<AppCacheInfoCollection> appcache_info =
117      appcache_helper_->info_collection();
118  if (!appcache_info.get() || appcache_info->infos_by_origin.empty())
119    return;
120
121  for (InfoByOrigin::const_iterator origin =
122           appcache_info->infos_by_origin.begin();
123       origin != appcache_info->infos_by_origin.end(); ++origin) {
124    std::list<AppCacheInfo>& info_list = appcache_info_[origin->first];
125    info_list.insert(
126        info_list.begin(), origin->second.begin(), origin->second.end());
127  }
128
129  model_->PopulateAppCacheInfo(this);
130}
131
132void LocalDataContainer::OnCookiesModelInfoLoaded(
133    const net::CookieList& cookie_list) {
134  cookie_list_.insert(cookie_list_.begin(),
135                      cookie_list.begin(),
136                      cookie_list.end());
137  DCHECK(model_);
138  model_->PopulateCookieInfo(this);
139}
140
141void LocalDataContainer::OnDatabaseModelInfoLoaded(
142    const DatabaseInfoList& database_info) {
143  database_info_list_ = database_info;
144  DCHECK(model_);
145  model_->PopulateDatabaseInfo(this);
146}
147
148void LocalDataContainer::OnLocalStorageModelInfoLoaded(
149    const LocalStorageInfoList& local_storage_info) {
150  local_storage_info_list_ = local_storage_info;
151  DCHECK(model_);
152  model_->PopulateLocalStorageInfo(this);
153}
154
155void LocalDataContainer::OnSessionStorageModelInfoLoaded(
156    const LocalStorageInfoList& session_storage_info) {
157  session_storage_info_list_ = session_storage_info;
158  DCHECK(model_);
159  model_->PopulateSessionStorageInfo(this);
160}
161
162void LocalDataContainer::OnIndexedDBModelInfoLoaded(
163    const IndexedDBInfoList& indexed_db_info) {
164  indexed_db_info_list_ = indexed_db_info;
165  DCHECK(model_);
166  model_->PopulateIndexedDBInfo(this);
167}
168
169void LocalDataContainer::OnFileSystemModelInfoLoaded(
170    const FileSystemInfoList& file_system_info) {
171  file_system_info_list_ = file_system_info;
172  DCHECK(model_);
173  model_->PopulateFileSystemInfo(this);
174}
175
176void LocalDataContainer::OnQuotaModelInfoLoaded(
177    const QuotaInfoList& quota_info) {
178  quota_info_list_ = quota_info;
179  DCHECK(model_);
180  model_->PopulateQuotaInfo(this);
181}
182
183void LocalDataContainer::OnServerBoundCertModelInfoLoaded(
184    const ServerBoundCertList& cert_list) {
185  server_bound_cert_list_ = cert_list;
186  DCHECK(model_);
187  model_->PopulateServerBoundCertInfo(this);
188}
189
190void LocalDataContainer::OnFlashLSOInfoLoaded(
191    const FlashLSODomainList& domains) {
192  flash_lso_domain_list_ = domains;
193  DCHECK(model_);
194  model_->PopulateFlashLSOInfo(this);
195}
196