tab_specific_content_settings.cc revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
1// Copyright (c) 2010 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/tab_contents/tab_specific_content_settings.h"
6
7#include "base/utf_string_conversions.h"
8#include "chrome/browser/browsing_data_appcache_helper.h"
9#include "chrome/browser/browsing_data_database_helper.h"
10#include "chrome/browser/browsing_data_indexed_db_helper.h"
11#include "chrome/browser/browsing_data_local_storage_helper.h"
12#include "chrome/browser/cookies_tree_model.h"
13#include "net/base/cookie_monster.h"
14
15bool TabSpecificContentSettings::LocalSharedObjectsContainer::empty() const {
16  return cookies_->GetAllCookies().empty() &&
17      appcaches_->empty() &&
18      databases_->empty() &&
19      indexed_dbs_->empty() &&
20      local_storages_->empty() &&
21      session_storages_->empty();
22}
23
24bool TabSpecificContentSettings::IsContentBlocked(
25    ContentSettingsType content_type) const {
26  DCHECK(content_type != CONTENT_SETTINGS_TYPE_GEOLOCATION)
27      << "Geolocation settings handled by ContentSettingGeolocationImageModel";
28  DCHECK(content_type != CONTENT_SETTINGS_TYPE_NOTIFICATIONS)
29      << "Notifications settings handled by "
30      << "ContentSettingsNotificationsImageModel";
31
32  if (content_type == CONTENT_SETTINGS_TYPE_IMAGES ||
33      content_type == CONTENT_SETTINGS_TYPE_JAVASCRIPT ||
34      content_type == CONTENT_SETTINGS_TYPE_PLUGINS ||
35      content_type == CONTENT_SETTINGS_TYPE_COOKIES ||
36      content_type == CONTENT_SETTINGS_TYPE_POPUPS)
37    return content_blocked_[content_type];
38
39  NOTREACHED();
40  return false;
41}
42
43bool TabSpecificContentSettings::IsBlockageIndicated(
44    ContentSettingsType content_type) const {
45  return content_blockage_indicated_to_user_[content_type];
46}
47
48void TabSpecificContentSettings::SetBlockageHasBeenIndicated(
49    ContentSettingsType content_type) {
50  content_blockage_indicated_to_user_[content_type] = true;
51}
52
53bool TabSpecificContentSettings::IsContentAccessed(
54    ContentSettingsType content_type) const {
55  // This method currently only returns meaningful values for cookies.
56  if (content_type != CONTENT_SETTINGS_TYPE_COOKIES)
57    return false;
58
59  return content_accessed_[content_type];
60}
61
62const std::set<std::string>&
63    TabSpecificContentSettings::BlockedResourcesForType(
64        ContentSettingsType content_type) const {
65  if (blocked_resources_[content_type].get()) {
66    return *blocked_resources_[content_type];
67  } else {
68    static std::set<std::string> empty_set;
69    return empty_set;
70  }
71}
72
73void TabSpecificContentSettings::AddBlockedResource(
74    ContentSettingsType content_type,
75    const std::string& resource_identifier) {
76  if (!blocked_resources_[content_type].get())
77    blocked_resources_[content_type].reset(new std::set<std::string>());
78  blocked_resources_[content_type]->insert(resource_identifier);
79}
80
81void TabSpecificContentSettings::OnContentBlocked(
82    ContentSettingsType type,
83    const std::string& resource_identifier) {
84  DCHECK(type != CONTENT_SETTINGS_TYPE_GEOLOCATION)
85      << "Geolocation settings handled by OnGeolocationPermissionSet";
86  content_accessed_[type] = true;
87  if (!resource_identifier.empty())
88    AddBlockedResource(type, resource_identifier);
89  if (!content_blocked_[type]) {
90    content_blocked_[type] = true;
91    if (delegate_)
92      delegate_->OnContentSettingsAccessed(true);
93  }
94}
95
96void TabSpecificContentSettings::OnContentAccessed(ContentSettingsType type) {
97  DCHECK(type != CONTENT_SETTINGS_TYPE_GEOLOCATION)
98      << "Geolocation settings handled by OnGeolocationPermissionSet";
99  if (!content_accessed_[type]) {
100    content_accessed_[type] = true;
101    if (delegate_)
102      delegate_->OnContentSettingsAccessed(false);
103  }
104}
105
106void TabSpecificContentSettings::OnCookiesRead(
107    const GURL& url,
108    const net::CookieList& cookie_list,
109    bool blocked_by_policy) {
110  LocalSharedObjectsContainer& container = blocked_by_policy ?
111      blocked_local_shared_objects_ : allowed_local_shared_objects_;
112  typedef net::CookieList::const_iterator cookie_iterator;
113  for (cookie_iterator cookie = cookie_list.begin();
114       cookie != cookie_list.end(); ++cookie) {
115    container.cookies()->SetCookieWithDetails(url,
116                                              cookie->Name(),
117                                              cookie->Value(),
118                                              cookie->Domain(),
119                                              cookie->Path(),
120                                              cookie->ExpiryDate(),
121                                              cookie->IsSecure(),
122                                              cookie->IsHttpOnly());
123  }
124  if (blocked_by_policy)
125    OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES, std::string());
126  else
127    OnContentAccessed(CONTENT_SETTINGS_TYPE_COOKIES);
128}
129
130void TabSpecificContentSettings::OnCookieChanged(
131    const GURL& url,
132    const std::string& cookie_line,
133    const net::CookieOptions& options,
134    bool blocked_by_policy) {
135  if (blocked_by_policy) {
136    blocked_local_shared_objects_.cookies()->SetCookieWithOptions(
137        url, cookie_line, options);
138    OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES, std::string());
139  } else {
140    allowed_local_shared_objects_.cookies()->SetCookieWithOptions(
141        url, cookie_line, options);
142    OnContentAccessed(CONTENT_SETTINGS_TYPE_COOKIES);
143  }
144}
145
146void TabSpecificContentSettings::OnIndexedDBAccessed(
147    const GURL& url,
148    const string16& description,
149    bool blocked_by_policy) {
150  if (blocked_by_policy) {
151    blocked_local_shared_objects_.indexed_dbs()->AddIndexedDB(
152        url, description);
153    OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES, std::string());
154  }else {
155    allowed_local_shared_objects_.indexed_dbs()->AddIndexedDB(
156        url, description);
157    OnContentAccessed(CONTENT_SETTINGS_TYPE_COOKIES);
158  }
159}
160
161void TabSpecificContentSettings::OnLocalStorageAccessed(
162    const GURL& url,
163    DOMStorageType storage_type,
164    bool blocked_by_policy) {
165  LocalSharedObjectsContainer& container = blocked_by_policy ?
166      blocked_local_shared_objects_ : allowed_local_shared_objects_;
167  CannedBrowsingDataLocalStorageHelper* helper =
168      storage_type == DOM_STORAGE_LOCAL ?
169          container.local_storages() : container.session_storages();
170  helper->AddLocalStorage(url);
171
172  if (blocked_by_policy)
173    OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES, std::string());
174  else
175    OnContentAccessed(CONTENT_SETTINGS_TYPE_COOKIES);
176}
177
178void TabSpecificContentSettings::OnWebDatabaseAccessed(
179    const GURL& url,
180    const string16& name,
181    const string16& display_name,
182    unsigned long estimated_size,
183    bool blocked_by_policy) {
184  if (blocked_by_policy) {
185    blocked_local_shared_objects_.databases()->AddDatabase(
186        url, UTF16ToUTF8(name), UTF16ToUTF8(display_name));
187    OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES, std::string());
188  } else {
189    allowed_local_shared_objects_.databases()->AddDatabase(
190        url, UTF16ToUTF8(name), UTF16ToUTF8(display_name));
191    OnContentAccessed(CONTENT_SETTINGS_TYPE_COOKIES);
192  }
193}
194
195void TabSpecificContentSettings::OnAppCacheAccessed(
196    const GURL& manifest_url, bool blocked_by_policy) {
197  if (blocked_by_policy) {
198    blocked_local_shared_objects_.appcaches()->AddAppCache(manifest_url);
199    OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES, std::string());
200  } else {
201    allowed_local_shared_objects_.appcaches()->AddAppCache(manifest_url);
202    OnContentAccessed(CONTENT_SETTINGS_TYPE_COOKIES);
203  }
204}
205
206void TabSpecificContentSettings::OnGeolocationPermissionSet(
207    const GURL& requesting_origin,
208    bool allowed) {
209  geolocation_settings_state_.OnGeolocationPermissionSet(requesting_origin,
210                                                         allowed);
211  if (delegate_)
212    delegate_->OnContentSettingsAccessed(!allowed);
213}
214
215TabSpecificContentSettings::TabSpecificContentSettings(
216    Delegate* delegate, Profile* profile)
217    : allowed_local_shared_objects_(profile),
218      blocked_local_shared_objects_(profile),
219      geolocation_settings_state_(profile),
220      load_plugins_link_enabled_(true),
221      delegate_(NULL) {
222  ClearBlockedContentSettingsExceptForCookies();
223  ClearCookieSpecificContentSettings();
224  delegate_ = delegate;
225}
226
227void TabSpecificContentSettings::ClearBlockedContentSettingsExceptForCookies() {
228  for (size_t i = 0; i < arraysize(content_blocked_); ++i) {
229    if (i == CONTENT_SETTINGS_TYPE_COOKIES)
230      continue;
231    blocked_resources_[i].reset();
232    content_blocked_[i] = false;
233    content_accessed_[i] = false;
234    content_blockage_indicated_to_user_[i] = false;
235  }
236  load_plugins_link_enabled_ = true;
237  if (delegate_)
238    delegate_->OnContentSettingsAccessed(false);
239}
240
241void TabSpecificContentSettings::ClearCookieSpecificContentSettings() {
242  blocked_local_shared_objects_.Reset();
243  allowed_local_shared_objects_.Reset();
244  content_blocked_[CONTENT_SETTINGS_TYPE_COOKIES] = false;
245  content_accessed_[CONTENT_SETTINGS_TYPE_COOKIES] = false;
246  content_blockage_indicated_to_user_[CONTENT_SETTINGS_TYPE_COOKIES] = false;
247  if (delegate_)
248    delegate_->OnContentSettingsAccessed(false);
249}
250
251void TabSpecificContentSettings::SetPopupsBlocked(bool blocked) {
252  content_blocked_[CONTENT_SETTINGS_TYPE_POPUPS] = blocked;
253  content_blockage_indicated_to_user_[CONTENT_SETTINGS_TYPE_POPUPS] = false;
254  if (delegate_)
255    delegate_->OnContentSettingsAccessed(blocked);
256}
257
258void TabSpecificContentSettings::GeolocationDidNavigate(
259      const NavigationController::LoadCommittedDetails& details) {
260  geolocation_settings_state_.DidNavigate(details);
261}
262
263void TabSpecificContentSettings::ClearGeolocationContentSettings() {
264  geolocation_settings_state_.ClearStateMap();
265}
266
267CookiesTreeModel* TabSpecificContentSettings::GetAllowedCookiesTreeModel() {
268  return allowed_local_shared_objects_.GetCookiesTreeModel();
269}
270
271CookiesTreeModel* TabSpecificContentSettings::GetBlockedCookiesTreeModel() {
272  return blocked_local_shared_objects_.GetCookiesTreeModel();
273}
274
275TabSpecificContentSettings::LocalSharedObjectsContainer::
276    LocalSharedObjectsContainer(Profile* profile)
277    : cookies_(new net::CookieMonster(NULL, NULL)),
278      appcaches_(new CannedBrowsingDataAppCacheHelper(profile)),
279      databases_(new CannedBrowsingDataDatabaseHelper(profile)),
280      indexed_dbs_(new CannedBrowsingDataIndexedDBHelper(profile)),
281      local_storages_(new CannedBrowsingDataLocalStorageHelper(profile)),
282      session_storages_(new CannedBrowsingDataLocalStorageHelper(profile)) {
283}
284
285TabSpecificContentSettings::LocalSharedObjectsContainer::
286    ~LocalSharedObjectsContainer() {
287}
288
289void TabSpecificContentSettings::LocalSharedObjectsContainer::Reset() {
290  cookies_->DeleteAll(false);
291  appcaches_->Reset();
292  databases_->Reset();
293  indexed_dbs_->Reset();
294  local_storages_->Reset();
295  session_storages_->Reset();
296}
297
298CookiesTreeModel*
299TabSpecificContentSettings::LocalSharedObjectsContainer::GetCookiesTreeModel() {
300  return new CookiesTreeModel(cookies_,
301                              databases_,
302                              local_storages_,
303                              session_storages_,
304                              appcaches_,
305                              indexed_dbs_);
306}
307