Home | History | Annotate | Download | only in content_settings
      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 
     26 class CookiesTreeModel;
     27 class Profile;
     28 
     29 namespace content {
     30 class RenderViewHost;
     31 }
     32 
     33 namespace net {
     34 class 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.
     41 class 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   // Clears the MIDI settings.
    163   void ClearMIDIContentSettings();
    164 
    165   // Changes the |content_blocked_| entry for popups.
    166   void SetPopupsBlocked(bool blocked);
    167 
    168   // Changes the |content_blocked_| entry for downloads.
    169   void SetDownloadsBlocked(bool blocked);
    170 
    171   // Updates Geolocation settings on navigation.
    172   void GeolocationDidNavigate(
    173       const content::LoadCommittedDetails& details);
    174 
    175   // Updates MIDI settings on navigation.
    176   void MIDIDidNavigate(const content::LoadCommittedDetails& details);
    177 
    178   // Returns whether a particular kind of content has been blocked for this
    179   // page.
    180   bool IsContentBlocked(ContentSettingsType content_type) const;
    181 
    182   // Returns true if content blockage was indicated to the user.
    183   bool IsBlockageIndicated(ContentSettingsType content_type) const;
    184 
    185   void SetBlockageHasBeenIndicated(ContentSettingsType content_type);
    186 
    187   // Returns whether a particular kind of content has been allowed. Currently
    188   // only tracks cookies.
    189   bool IsContentAllowed(ContentSettingsType content_type) const;
    190 
    191   // Returns the state of the camera and microphone usage.
    192   MicrophoneCameraState GetMicrophoneCameraState() const;
    193 
    194   const std::set<std::string>& BlockedResourcesForType(
    195       ContentSettingsType content_type) const;
    196 
    197   // Returns the ContentSettingsUsagesState that controls the
    198   // geolocation API usage on this page.
    199   const ContentSettingsUsagesState& geolocation_usages_state() const {
    200     return geolocation_usages_state_;
    201   }
    202 
    203   // Returns the ContentSettingsUsageState that controls the MIDI usage on
    204   // this page.
    205   const ContentSettingsUsagesState& midi_usages_state() const {
    206     return midi_usages_state_;
    207   }
    208 
    209   // Call to indicate that there is a protocol handler pending user approval.
    210   void set_pending_protocol_handler(const ProtocolHandler& handler) {
    211     pending_protocol_handler_ = handler;
    212   }
    213 
    214   const ProtocolHandler& pending_protocol_handler() const {
    215     return pending_protocol_handler_;
    216   }
    217 
    218   void ClearPendingProtocolHandler() {
    219     pending_protocol_handler_ = ProtocolHandler::EmptyProtocolHandler();
    220   }
    221 
    222   // Sets the previous protocol handler which will be replaced by the
    223   // pending protocol handler.
    224   void set_previous_protocol_handler(const ProtocolHandler& handler) {
    225     previous_protocol_handler_ = handler;
    226   }
    227 
    228   const ProtocolHandler& previous_protocol_handler() const {
    229     return previous_protocol_handler_;
    230   }
    231 
    232   // Set whether the setting for the pending handler is DEFAULT (ignore),
    233   // ALLOW, or DENY.
    234   void set_pending_protocol_handler_setting(ContentSetting setting) {
    235     pending_protocol_handler_setting_ = setting;
    236   }
    237 
    238   ContentSetting pending_protocol_handler_setting() const {
    239     return pending_protocol_handler_setting_;
    240   }
    241 
    242 
    243   // Returns a pointer to the |LocalSharedObjectsContainer| that contains all
    244   // allowed local shared objects like cookies, local storage, ... .
    245   const LocalSharedObjectsContainer& allowed_local_shared_objects() const {
    246     return allowed_local_shared_objects_;
    247   }
    248 
    249   // Returns a pointer to the |LocalSharedObjectsContainer| that contains all
    250   // blocked local shared objects like cookies, local storage, ... .
    251   const LocalSharedObjectsContainer& blocked_local_shared_objects() const {
    252     return blocked_local_shared_objects_;
    253   }
    254 
    255   bool load_plugins_link_enabled() { return load_plugins_link_enabled_; }
    256   void set_load_plugins_link_enabled(bool enabled) {
    257     load_plugins_link_enabled_ = enabled;
    258   }
    259 
    260   // Called to indicate whether access to the Pepper broker was allowed or
    261   // blocked.
    262   void SetPepperBrokerAllowed(bool allowed);
    263 
    264   // content::WebContentsObserver overrides.
    265   virtual void RenderViewForInterstitialPageCreated(
    266       content::RenderViewHost* render_view_host) OVERRIDE;
    267   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
    268   virtual void DidNavigateMainFrame(
    269       const content::LoadCommittedDetails& details,
    270       const content::FrameNavigateParams& params) OVERRIDE;
    271   virtual void DidStartProvisionalLoadForFrame(
    272       int64 frame_id,
    273       int64 parent_frame_id,
    274       bool is_main_frame,
    275       const GURL& validated_url,
    276       bool is_error_page,
    277       bool is_iframe_srcdoc,
    278       content::RenderViewHost* render_view_host) OVERRIDE;
    279   virtual void AppCacheAccessed(const GURL& manifest_url,
    280                                 bool blocked_by_policy) OVERRIDE;
    281 
    282   // Message handlers. Public for testing.
    283   void OnContentBlocked(ContentSettingsType type,
    284                         const std::string& resource_identifier);
    285   void OnContentAllowed(ContentSettingsType type);
    286 
    287   // These methods are invoked on the UI thread by the static functions above.
    288   // Public for testing.
    289   void OnCookiesRead(const GURL& url,
    290                      const GURL& first_party_url,
    291                      const net::CookieList& cookie_list,
    292                      bool blocked_by_policy);
    293   void OnCookieChanged(const GURL& url,
    294                        const GURL& first_party_url,
    295                        const std::string& cookie_line,
    296                        const net::CookieOptions& options,
    297                        bool blocked_by_policy);
    298   void OnFileSystemAccessed(const GURL& url,
    299                             bool blocked_by_policy);
    300   void OnIndexedDBAccessed(const GURL& url,
    301                            const string16& description,
    302                            bool blocked_by_policy);
    303   void OnLocalStorageAccessed(const GURL& url,
    304                               bool local,
    305                               bool blocked_by_policy);
    306   void OnWebDatabaseAccessed(const GURL& url,
    307                              const string16& name,
    308                              const string16& display_name,
    309                              bool blocked_by_policy);
    310   void OnGeolocationPermissionSet(const GURL& requesting_frame,
    311                                   bool allowed);
    312 
    313   // These methods are called to update the status about the microphone and
    314   // camera stream access.
    315   void OnMicrophoneAccessed();
    316   void OnMicrophoneAccessBlocked();
    317   void OnCameraAccessed();
    318   void OnCameraAccessBlocked();
    319 
    320   // There methods are called to update the status about MIDI access.
    321   void OnMIDISysExAccessed(const GURL& reqesting_origin);
    322   void OnMIDISysExAccessBlocked(const GURL& requesting_origin);
    323 
    324   // Adds the given |SiteDataObserver|. The |observer| is notified when a
    325   // locale shared object, like for example a cookie, is accessed.
    326   void AddSiteDataObserver(SiteDataObserver* observer);
    327 
    328   // Removes the given |SiteDataObserver|.
    329   void RemoveSiteDataObserver(SiteDataObserver* observer);
    330 
    331  private:
    332   explicit TabSpecificContentSettings(content::WebContents* tab);
    333   friend class content::WebContentsUserData<TabSpecificContentSettings>;
    334 
    335   void AddBlockedResource(ContentSettingsType content_type,
    336                           const std::string& resource_identifier);
    337 
    338   // content::NotificationObserver implementation.
    339   virtual void Observe(int type,
    340                        const content::NotificationSource& source,
    341                        const content::NotificationDetails& details) OVERRIDE;
    342 
    343   // Notifies all registered |SiteDataObserver|s.
    344   void NotifySiteDataObservers();
    345 
    346   // All currently registered |SiteDataObserver|s.
    347   ObserverList<SiteDataObserver> observer_list_;
    348 
    349   // Stores which content setting types actually have blocked content.
    350   bool content_blocked_[CONTENT_SETTINGS_NUM_TYPES];
    351 
    352   // Stores if the blocked content was messaged to the user.
    353   bool content_blockage_indicated_to_user_[CONTENT_SETTINGS_NUM_TYPES];
    354 
    355   // Stores which content setting types actually were allowed.
    356   bool content_allowed_[CONTENT_SETTINGS_NUM_TYPES];
    357 
    358   // Stores the blocked resources for each content type.
    359   // Currently only used for plugins.
    360   scoped_ptr<std::set<std::string> >
    361       blocked_resources_[CONTENT_SETTINGS_NUM_TYPES];
    362 
    363   // The profile of the tab.
    364   Profile* profile_;
    365 
    366   // Stores the blocked/allowed cookies.
    367   LocalSharedObjectsContainer allowed_local_shared_objects_;
    368   LocalSharedObjectsContainer blocked_local_shared_objects_;
    369 
    370   // Manages information about Geolocation API usage in this page.
    371   ContentSettingsUsagesState geolocation_usages_state_;
    372 
    373   // Manages information about MIDI usages in this page.
    374   ContentSettingsUsagesState midi_usages_state_;
    375 
    376   // The pending protocol handler, if any. This can be set if
    377   // registerProtocolHandler was invoked without user gesture.
    378   // The |IsEmpty| method will be true if no protocol handler is
    379   // pending registration.
    380   ProtocolHandler pending_protocol_handler_;
    381 
    382   // The previous protocol handler to be replaced by
    383   // the pending_protocol_handler_, if there is one. Empty if
    384   // there is no handler which would be replaced.
    385   ProtocolHandler previous_protocol_handler_;
    386 
    387   // The setting on the pending protocol handler registration. Persisted in case
    388   // the user opens the bubble and makes changes multiple times.
    389   ContentSetting pending_protocol_handler_setting_;
    390 
    391   // Stores whether the user can load blocked plugins on this page.
    392   bool load_plugins_link_enabled_;
    393 
    394   content::NotificationRegistrar registrar_;
    395 
    396   DISALLOW_COPY_AND_ASSIGN(TabSpecificContentSettings);
    397 };
    398 
    399 #endif  // CHROME_BROWSER_CONTENT_SETTINGS_TAB_SPECIFIC_CONTENT_SETTINGS_H_
    400