tab_specific_content_settings.h revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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#ifndef CHROME_BROWSER_CONTENT_SETTINGS_TAB_SPECIFIC_CONTENT_SETTINGS_H_
6#define CHROME_BROWSER_CONTENT_SETTINGS_TAB_SPECIFIC_CONTENT_SETTINGS_H_
7
8#include <set>
9#include <string>
10
11#include "base/basictypes.h"
12#include "base/memory/ref_counted.h"
13#include "base/memory/scoped_ptr.h"
14#include "base/observer_list.h"
15#include "chrome/browser/content_settings/local_shared_objects_container.h"
16#include "chrome/browser/geolocation/geolocation_settings_state.h"
17#include "chrome/common/content_settings.h"
18#include "chrome/common/content_settings_types.h"
19#include "chrome/common/custom_handlers/protocol_handler.h"
20#include "content/public/browser/notification_observer.h"
21#include "content/public/browser/notification_registrar.h"
22#include "content/public/browser/web_contents_observer.h"
23#include "content/public/browser/web_contents_user_data.h"
24#include "net/cookies/canonical_cookie.h"
25
26class CookiesTreeModel;
27class Profile;
28
29namespace content {
30class RenderViewHost;
31}
32
33namespace net {
34class CookieOptions;
35}
36
37// This class manages state about permissions, content settings, cookies and
38// site data for a specific WebContents. It tracks which content was accessed
39// and which content was blocked. Based on this it provides information about
40// which types of content were accessed and blocked.
41class TabSpecificContentSettings
42    : public content::WebContentsObserver,
43      public content::NotificationObserver,
44      public content::WebContentsUserData<TabSpecificContentSettings> {
45 public:
46  // Classes that want to be notified about site data events must implement
47  // this abstract class and add themselves as observer to the
48  // |TabSpecificContentSettings|.
49  class SiteDataObserver {
50   public:
51    explicit SiteDataObserver(
52        TabSpecificContentSettings* tab_specific_content_settings);
53    virtual ~SiteDataObserver();
54
55    // Called whenever site data is accessed.
56    virtual void OnSiteDataAccessed() = 0;
57
58    TabSpecificContentSettings* tab_specific_content_settings() {
59      return tab_specific_content_settings_;
60    }
61
62    // Called when the TabSpecificContentSettings is destroyed; nulls out
63    // the local reference.
64    void ContentSettingsDestroyed();
65
66   private:
67    TabSpecificContentSettings* tab_specific_content_settings_;
68
69    DISALLOW_COPY_AND_ASSIGN(SiteDataObserver);
70  };
71
72  virtual ~TabSpecificContentSettings();
73
74  // Returns the object given a render view's id.
75  static TabSpecificContentSettings* Get(int render_process_id,
76                                         int render_view_id);
77
78  // Static methods called on the UI threads.
79  // Called when cookies for the given URL were read either from within the
80  // current page or while loading it. |blocked_by_policy| should be true, if
81  // reading cookies was blocked due to the user's content settings. In that
82  // case, this function should invoke OnContentBlocked.
83  static void CookiesRead(int render_process_id,
84                          int render_view_id,
85                          const GURL& url,
86                          const GURL& first_party_url,
87                          const net::CookieList& cookie_list,
88                          bool blocked_by_policy);
89
90  // Called when a specific cookie in the current page was changed.
91  // |blocked_by_policy| should be true, if the cookie was blocked due to the
92  // user's content settings. In that case, this function should invoke
93  // OnContentBlocked.
94  static void CookieChanged(int render_process_id,
95                            int render_view_id,
96                            const GURL& url,
97                            const GURL& first_party_url,
98                            const std::string& cookie_line,
99                            const net::CookieOptions& options,
100                            bool blocked_by_policy);
101
102  // Called when a specific Web database in the current page was accessed. If
103  // access was blocked due to the user's content settings,
104  // |blocked_by_policy| should be true, and this function should invoke
105  // OnContentBlocked.
106  static void WebDatabaseAccessed(int render_process_id,
107                                  int render_view_id,
108                                  const GURL& url,
109                                  const string16& name,
110                                  const string16& display_name,
111                                  bool blocked_by_policy);
112
113  // Called when a specific DOM storage area in the current page was
114  // accessed. If access was blocked due to the user's content settings,
115  // |blocked_by_policy| should be true, and this function should invoke
116  // OnContentBlocked.
117  static void DOMStorageAccessed(int render_process_id,
118                                 int render_view_id,
119                                 const GURL& url,
120                                 bool local,
121                                 bool blocked_by_policy);
122
123  // Called when a specific indexed db factory in the current page was
124  // accessed. If access was blocked due to the user's content settings,
125  // |blocked_by_policy| should be true, and this function should invoke
126  // OnContentBlocked.
127  static void IndexedDBAccessed(int render_process_id,
128                                int render_view_id,
129                                const GURL& url,
130                                const string16& description,
131                                bool blocked_by_policy);
132
133  // Called when a specific file system in the current page was accessed.
134  // If access was blocked due to the user's content settings,
135  // |blocked_by_policy| should be true, and this function should invoke
136  // OnContentBlocked.
137  static void FileSystemAccessed(int render_process_id,
138                                 int render_view_id,
139                                 const GURL& url,
140                                 bool blocked_by_policy);
141
142  // Resets the |content_blocked_| and |content_allowed_| arrays, except for
143  // CONTENT_SETTINGS_TYPE_COOKIES related information.
144  void ClearBlockedContentSettingsExceptForCookies();
145
146  // Resets all cookies related information.
147  void ClearCookieSpecificContentSettings();
148
149  // Clears the Geolocation settings.
150  void ClearGeolocationContentSettings();
151
152  // Changes the |content_blocked_| entry for popups.
153  void SetPopupsBlocked(bool blocked);
154
155  // Updates Geolocation settings on navigation.
156  void GeolocationDidNavigate(
157      const content::LoadCommittedDetails& details);
158
159  // Returns whether a particular kind of content has been blocked for this
160  // page.
161  bool IsContentBlocked(ContentSettingsType content_type) const;
162
163  // Returns true if content blockage was indicated to the user.
164  bool IsBlockageIndicated(ContentSettingsType content_type) const;
165
166  void SetBlockageHasBeenIndicated(ContentSettingsType content_type);
167
168  // Returns whether a particular kind of content has been allowed. Currently
169  // only tracks cookies.
170  bool IsContentAllowed(ContentSettingsType content_type) const;
171
172  const std::set<std::string>& BlockedResourcesForType(
173      ContentSettingsType content_type) const;
174
175  // Returns the GeolocationSettingsState that controls the
176  // geolocation API usage on this page.
177  const GeolocationSettingsState& geolocation_settings_state() const {
178    return geolocation_settings_state_;
179  }
180
181  // Call to indicate that there is a protocol handler pending user approval.
182  void set_pending_protocol_handler(const ProtocolHandler& handler) {
183    pending_protocol_handler_ = handler;
184  }
185
186  const ProtocolHandler& pending_protocol_handler() const {
187    return pending_protocol_handler_;
188  }
189
190  void ClearPendingProtocolHandler() {
191    pending_protocol_handler_ = ProtocolHandler::EmptyProtocolHandler();
192  }
193
194  // Sets the previous protocol handler which will be replaced by the
195  // pending protocol handler.
196  void set_previous_protocol_handler(const ProtocolHandler& handler) {
197    previous_protocol_handler_ = handler;
198  }
199
200  const ProtocolHandler& previous_protocol_handler() const {
201    return previous_protocol_handler_;
202  }
203
204  // Set whether the setting for the pending handler is DEFAULT (ignore),
205  // ALLOW, or DENY.
206  void set_pending_protocol_handler_setting(ContentSetting setting) {
207    pending_protocol_handler_setting_ = setting;
208  }
209
210  ContentSetting pending_protocol_handler_setting() const {
211    return pending_protocol_handler_setting_;
212  }
213
214
215  // Returns a pointer to the |LocalSharedObjectsContainer| that contains all
216  // allowed local shared objects like cookies, local storage, ... .
217  const LocalSharedObjectsContainer& allowed_local_shared_objects() const {
218    return allowed_local_shared_objects_;
219  }
220
221  // Returns a pointer to the |LocalSharedObjectsContainer| that contains all
222  // blocked local shared objects like cookies, local storage, ... .
223  const LocalSharedObjectsContainer& blocked_local_shared_objects() const {
224    return blocked_local_shared_objects_;
225  }
226
227  bool load_plugins_link_enabled() { return load_plugins_link_enabled_; }
228  void set_load_plugins_link_enabled(bool enabled) {
229    load_plugins_link_enabled_ = enabled;
230  }
231
232  // Called to indicate whether access to the Pepper broker was allowed or
233  // blocked.
234  void SetPepperBrokerAllowed(bool allowed);
235
236  // content::WebContentsObserver overrides.
237  virtual void RenderViewForInterstitialPageCreated(
238      content::RenderViewHost* render_view_host) OVERRIDE;
239  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
240  virtual void DidNavigateMainFrame(
241      const content::LoadCommittedDetails& details,
242      const content::FrameNavigateParams& params) OVERRIDE;
243  virtual void DidStartProvisionalLoadForFrame(
244      int64 frame_id,
245      int64 parent_frame_id,
246      bool is_main_frame,
247      const GURL& validated_url,
248      bool is_error_page,
249      bool is_iframe_srcdoc,
250      content::RenderViewHost* render_view_host) OVERRIDE;
251  virtual void AppCacheAccessed(const GURL& manifest_url,
252                                bool blocked_by_policy) OVERRIDE;
253
254  // Message handlers. Public for testing.
255  void OnContentBlocked(ContentSettingsType type,
256                        const std::string& resource_identifier);
257  void OnContentAllowed(ContentSettingsType type);
258
259  // These methods are invoked on the UI thread by the static functions above.
260  // Public for testing.
261  void OnCookiesRead(const GURL& url,
262                     const GURL& first_party_url,
263                     const net::CookieList& cookie_list,
264                     bool blocked_by_policy);
265  void OnCookieChanged(const GURL& url,
266                       const GURL& first_party_url,
267                       const std::string& cookie_line,
268                       const net::CookieOptions& options,
269                       bool blocked_by_policy);
270  void OnFileSystemAccessed(const GURL& url,
271                            bool blocked_by_policy);
272  void OnIndexedDBAccessed(const GURL& url,
273                           const string16& description,
274                           bool blocked_by_policy);
275  void OnLocalStorageAccessed(const GURL& url,
276                              bool local,
277                              bool blocked_by_policy);
278  void OnWebDatabaseAccessed(const GURL& url,
279                             const string16& name,
280                             const string16& display_name,
281                             bool blocked_by_policy);
282  void OnGeolocationPermissionSet(const GURL& requesting_frame,
283                                  bool allowed);
284
285  // This method is called when a media stream is allowed.
286  void OnMediaStreamAllowed();
287
288  // Adds the given |SiteDataObserver|. The |observer| is notified when a
289  // locale shared object, like for example a cookie, is accessed.
290  void AddSiteDataObserver(SiteDataObserver* observer);
291
292  // Removes the given |SiteDataObserver|.
293  void RemoveSiteDataObserver(SiteDataObserver* observer);
294
295 private:
296  explicit TabSpecificContentSettings(content::WebContents* tab);
297  friend class content::WebContentsUserData<TabSpecificContentSettings>;
298
299  void AddBlockedResource(ContentSettingsType content_type,
300                          const std::string& resource_identifier);
301
302  // content::NotificationObserver implementation.
303  virtual void Observe(int type,
304                       const content::NotificationSource& source,
305                       const content::NotificationDetails& details) OVERRIDE;
306
307  // Notifies all registered |SiteDataObserver|s.
308  void NotifySiteDataObservers();
309
310  // All currently registered |SiteDataObserver|s.
311  ObserverList<SiteDataObserver> observer_list_;
312
313  // Stores which content setting types actually have blocked content.
314  bool content_blocked_[CONTENT_SETTINGS_NUM_TYPES];
315
316  // Stores if the blocked content was messaged to the user.
317  bool content_blockage_indicated_to_user_[CONTENT_SETTINGS_NUM_TYPES];
318
319  // Stores which content setting types actually were allowed.
320  bool content_allowed_[CONTENT_SETTINGS_NUM_TYPES];
321
322  // Stores the blocked resources for each content type.
323  // Currently only used for plugins.
324  scoped_ptr<std::set<std::string> >
325      blocked_resources_[CONTENT_SETTINGS_NUM_TYPES];
326
327  // The profile of the tab.
328  Profile* profile_;
329
330  // Stores the blocked/allowed cookies.
331  LocalSharedObjectsContainer allowed_local_shared_objects_;
332  LocalSharedObjectsContainer blocked_local_shared_objects_;
333
334  // Manages information about Geolocation API usage in this page.
335  GeolocationSettingsState geolocation_settings_state_;
336
337  // The pending protocol handler, if any. This can be set if
338  // registerProtocolHandler was invoked without user gesture.
339  // The |IsEmpty| method will be true if no protocol handler is
340  // pending registration.
341  ProtocolHandler pending_protocol_handler_;
342
343  // The previous protocol handler to be replaced by
344  // the pending_protocol_handler_, if there is one. Empty if
345  // there is no handler which would be replaced.
346  ProtocolHandler previous_protocol_handler_;
347
348  // The setting on the pending protocol handler registration. Persisted in case
349  // the user opens the bubble and makes changes multiple times.
350  ContentSetting pending_protocol_handler_setting_;
351
352  // Stores whether the user can load blocked plugins on this page.
353  bool load_plugins_link_enabled_;
354
355  content::NotificationRegistrar registrar_;
356
357  DISALLOW_COPY_AND_ASSIGN(TabSpecificContentSettings);
358};
359
360#endif  // CHROME_BROWSER_CONTENT_SETTINGS_TAB_SPECIFIC_CONTENT_SETTINGS_H_
361