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