tab_specific_content_settings.cc revision 513209b27ff55e2841eac0e4120199c23acce758
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::IsContentAccessed(
44    ContentSettingsType content_type) const {
45  // This method currently only returns meaningful values for cookies.
46  if (content_type != CONTENT_SETTINGS_TYPE_COOKIES)
47    return false;
48
49  return content_accessed_[content_type];
50}
51
52const std::set<std::string>&
53    TabSpecificContentSettings::BlockedResourcesForType(
54        ContentSettingsType content_type) const {
55  if (blocked_resources_[content_type].get()) {
56    return *blocked_resources_[content_type];
57  } else {
58    static std::set<std::string> empty_set;
59    return empty_set;
60  }
61}
62
63void TabSpecificContentSettings::AddBlockedResource(
64    ContentSettingsType content_type,
65    const std::string& resource_identifier) {
66  if (!blocked_resources_[content_type].get())
67    blocked_resources_[content_type].reset(new std::set<std::string>());
68  blocked_resources_[content_type]->insert(resource_identifier);
69}
70
71void TabSpecificContentSettings::OnContentBlocked(
72    ContentSettingsType type,
73    const std::string& resource_identifier) {
74  DCHECK(type != CONTENT_SETTINGS_TYPE_GEOLOCATION)
75      << "Geolocation settings handled by OnGeolocationPermissionSet";
76  content_accessed_[type] = true;
77  if (!resource_identifier.empty())
78    AddBlockedResource(type, resource_identifier);
79  if (!content_blocked_[type]) {
80    content_blocked_[type] = true;
81    if (delegate_)
82      delegate_->OnContentSettingsAccessed(true);
83  }
84}
85
86void TabSpecificContentSettings::OnContentAccessed(ContentSettingsType type) {
87  DCHECK(type != CONTENT_SETTINGS_TYPE_GEOLOCATION)
88      << "Geolocation settings handled by OnGeolocationPermissionSet";
89  if (!content_accessed_[type]) {
90    content_accessed_[type] = true;
91    if (delegate_)
92      delegate_->OnContentSettingsAccessed(false);
93  }
94}
95
96void TabSpecificContentSettings::OnCookieAccessed(
97    const GURL& url, const std::string& cookie_line, bool blocked_by_policy) {
98  net::CookieOptions options;
99  options.set_include_httponly();
100  if (blocked_by_policy) {
101    blocked_local_shared_objects_.cookies()->SetCookieWithOptions(
102        url, cookie_line, options);
103    OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES, std::string());
104  } else {
105    allowed_local_shared_objects_.cookies()->SetCookieWithOptions(
106        url, cookie_line, options);
107    OnContentAccessed(CONTENT_SETTINGS_TYPE_COOKIES);
108  }
109}
110
111void TabSpecificContentSettings::OnIndexedDBAccessed(
112    const GURL& url,
113    const string16& description,
114    bool blocked_by_policy) {
115  if (blocked_by_policy) {
116    blocked_local_shared_objects_.indexed_dbs()->AddIndexedDB(
117        url, description);
118    OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES, std::string());
119  }else {
120    allowed_local_shared_objects_.indexed_dbs()->AddIndexedDB(
121        url, description);
122    OnContentAccessed(CONTENT_SETTINGS_TYPE_COOKIES);
123  }
124}
125
126void TabSpecificContentSettings::OnLocalStorageAccessed(
127    const GURL& url,
128    DOMStorageType storage_type,
129    bool blocked_by_policy) {
130  LocalSharedObjectsContainer& container = blocked_by_policy ?
131      blocked_local_shared_objects_ : allowed_local_shared_objects_;
132  CannedBrowsingDataLocalStorageHelper* helper =
133      storage_type == DOM_STORAGE_LOCAL ?
134          container.local_storages() : container.session_storages();
135  helper->AddLocalStorage(url);
136
137  if (blocked_by_policy)
138    OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES, std::string());
139  else
140    OnContentAccessed(CONTENT_SETTINGS_TYPE_COOKIES);
141}
142
143void TabSpecificContentSettings::OnWebDatabaseAccessed(
144    const GURL& url,
145    const string16& name,
146    const string16& display_name,
147    unsigned long estimated_size,
148    bool blocked_by_policy) {
149  if (blocked_by_policy) {
150    blocked_local_shared_objects_.databases()->AddDatabase(
151        url, UTF16ToUTF8(name), UTF16ToUTF8(display_name));
152    OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES, std::string());
153  } else {
154    allowed_local_shared_objects_.databases()->AddDatabase(
155        url, UTF16ToUTF8(name), UTF16ToUTF8(display_name));
156    OnContentAccessed(CONTENT_SETTINGS_TYPE_COOKIES);
157  }
158}
159
160void TabSpecificContentSettings::OnAppCacheAccessed(
161    const GURL& manifest_url, bool blocked_by_policy) {
162  if (blocked_by_policy) {
163    blocked_local_shared_objects_.appcaches()->AddAppCache(manifest_url);
164    OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES, std::string());
165  } else {
166    allowed_local_shared_objects_.appcaches()->AddAppCache(manifest_url);
167    OnContentAccessed(CONTENT_SETTINGS_TYPE_COOKIES);
168  }
169}
170
171void TabSpecificContentSettings::OnGeolocationPermissionSet(
172    const GURL& requesting_origin,
173    bool allowed) {
174  geolocation_settings_state_.OnGeolocationPermissionSet(requesting_origin,
175                                                         allowed);
176  if (delegate_)
177    delegate_->OnContentSettingsAccessed(!allowed);
178}
179
180TabSpecificContentSettings::TabSpecificContentSettings(
181    Delegate* delegate, Profile* profile)
182    : allowed_local_shared_objects_(profile),
183      blocked_local_shared_objects_(profile),
184      geolocation_settings_state_(profile),
185      load_plugins_link_enabled_(true),
186      delegate_(NULL) {
187  ClearBlockedContentSettingsExceptForCookies();
188  ClearCookieSpecificContentSettings();
189  delegate_ = delegate;
190}
191
192void TabSpecificContentSettings::ClearBlockedContentSettingsExceptForCookies() {
193  for (size_t i = 0; i < arraysize(content_blocked_); ++i) {
194    if (i == CONTENT_SETTINGS_TYPE_COOKIES)
195      continue;
196    blocked_resources_[i].reset();
197    content_blocked_[i] = false;
198    content_accessed_[i] = false;
199  }
200  load_plugins_link_enabled_ = true;
201  if (delegate_)
202    delegate_->OnContentSettingsAccessed(false);
203}
204
205void TabSpecificContentSettings::ClearCookieSpecificContentSettings() {
206  blocked_local_shared_objects_.Reset();
207  allowed_local_shared_objects_.Reset();
208  content_blocked_[CONTENT_SETTINGS_TYPE_COOKIES] = false;
209  content_accessed_[CONTENT_SETTINGS_TYPE_COOKIES] = false;
210  if (delegate_)
211    delegate_->OnContentSettingsAccessed(false);
212}
213
214void TabSpecificContentSettings::SetPopupsBlocked(bool blocked) {
215  content_blocked_[CONTENT_SETTINGS_TYPE_POPUPS] = blocked;
216  if (delegate_)
217    delegate_->OnContentSettingsAccessed(blocked);
218}
219
220void TabSpecificContentSettings::GeolocationDidNavigate(
221      const NavigationController::LoadCommittedDetails& details) {
222  geolocation_settings_state_.DidNavigate(details);
223}
224
225void TabSpecificContentSettings::ClearGeolocationContentSettings() {
226  geolocation_settings_state_.ClearStateMap();
227}
228
229CookiesTreeModel* TabSpecificContentSettings::GetAllowedCookiesTreeModel() {
230  return allowed_local_shared_objects_.GetCookiesTreeModel();
231}
232
233CookiesTreeModel* TabSpecificContentSettings::GetBlockedCookiesTreeModel() {
234  return blocked_local_shared_objects_.GetCookiesTreeModel();
235}
236
237TabSpecificContentSettings::LocalSharedObjectsContainer::
238    LocalSharedObjectsContainer(Profile* profile)
239    : cookies_(new net::CookieMonster(NULL, NULL)),
240      appcaches_(new CannedBrowsingDataAppCacheHelper(profile)),
241      databases_(new CannedBrowsingDataDatabaseHelper(profile)),
242      indexed_dbs_(new CannedBrowsingDataIndexedDBHelper(profile)),
243      local_storages_(new CannedBrowsingDataLocalStorageHelper(profile)),
244      session_storages_(new CannedBrowsingDataLocalStorageHelper(profile)) {
245}
246
247TabSpecificContentSettings::LocalSharedObjectsContainer::
248    ~LocalSharedObjectsContainer() {
249}
250
251void TabSpecificContentSettings::LocalSharedObjectsContainer::Reset() {
252  cookies_->DeleteAll(false);
253  appcaches_->Reset();
254  databases_->Reset();
255  indexed_dbs_->Reset();
256  local_storages_->Reset();
257  session_storages_->Reset();
258}
259
260CookiesTreeModel*
261TabSpecificContentSettings::LocalSharedObjectsContainer::GetCookiesTreeModel() {
262  return new CookiesTreeModel(cookies_,
263                              databases_,
264                              local_storages_,
265                              session_storages_,
266                              appcaches_,
267                              indexed_dbs_);
268}
269