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