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   // Returns the object given a render frame's id.
     90   static TabSpecificContentSettings* GetForFrame(int render_process_id,
     91                                                  int render_view_id);
     92 
     93   // Static methods called on the UI threads.
     94   // Called when cookies for the given URL were read either from within the
     95   // current page or while loading it. |blocked_by_policy| should be true, if
     96   // reading cookies was blocked due to the user's content settings. In that
     97   // case, this function should invoke OnContentBlocked.
     98   // |is_for_blocking_resource| indicates whether the cookies read were for a
     99   // blocking resource (eg script, css). It is only temporarily added for
    100   // diagnostic purposes, per bug 353678. Will be removed again once data
    101   // collection is finished.
    102   static void CookiesRead(int render_process_id,
    103                           int render_frame_id,
    104                           const GURL& url,
    105                           const GURL& first_party_url,
    106                           const net::CookieList& cookie_list,
    107                           bool blocked_by_policy,
    108                           bool is_for_blocking_resource);
    109 
    110   // Called when a specific cookie in the current page was changed.
    111   // |blocked_by_policy| should be true, if the cookie was blocked due to the
    112   // user's content settings. In that case, this function should invoke
    113   // OnContentBlocked.
    114   static void CookieChanged(int render_process_id,
    115                             int render_frame_id,
    116                             const GURL& url,
    117                             const GURL& first_party_url,
    118                             const std::string& cookie_line,
    119                             const net::CookieOptions& options,
    120                             bool blocked_by_policy);
    121 
    122   // Called when a specific Web database in the current page was accessed. If
    123   // access was blocked due to the user's content settings,
    124   // |blocked_by_policy| should be true, and this function should invoke
    125   // OnContentBlocked.
    126   static void WebDatabaseAccessed(int render_process_id,
    127                                   int render_frame_id,
    128                                   const GURL& url,
    129                                   const base::string16& name,
    130                                   const base::string16& display_name,
    131                                   bool blocked_by_policy);
    132 
    133   // Called when a specific DOM storage area 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 DOMStorageAccessed(int render_process_id,
    138                                  int render_frame_id,
    139                                  const GURL& url,
    140                                  bool local,
    141                                  bool blocked_by_policy);
    142 
    143   // Called when a specific indexed db factory in the current page was
    144   // accessed. 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 IndexedDBAccessed(int render_process_id,
    148                                 int render_frame_id,
    149                                 const GURL& url,
    150                                 const base::string16& description,
    151                                 bool blocked_by_policy);
    152 
    153   // Called when a specific file system in the current page was accessed.
    154   // If access was blocked due to the user's content settings,
    155   // |blocked_by_policy| should be true, and this function should invoke
    156   // OnContentBlocked.
    157   static void FileSystemAccessed(int render_process_id,
    158                                  int render_frame_id,
    159                                  const GURL& url,
    160                                  bool blocked_by_policy);
    161 
    162   // Resets the |content_blocked_| and |content_allowed_| arrays, except for
    163   // CONTENT_SETTINGS_TYPE_COOKIES related information.
    164   void ClearBlockedContentSettingsExceptForCookies();
    165 
    166   // Resets all cookies related information.
    167   void ClearCookieSpecificContentSettings();
    168 
    169   // Clears the Geolocation settings.
    170   void ClearGeolocationContentSettings();
    171 
    172   // Clears the MIDI settings.
    173   void ClearMidiContentSettings();
    174 
    175   // Changes the |content_blocked_| entry for popups.
    176   void SetPopupsBlocked(bool blocked);
    177 
    178   // Changes the |content_blocked_| entry for downloads.
    179   void SetDownloadsBlocked(bool blocked);
    180 
    181   // Updates Geolocation settings on navigation.
    182   void GeolocationDidNavigate(
    183       const content::LoadCommittedDetails& details);
    184 
    185   // Updates MIDI settings on navigation.
    186   void MidiDidNavigate(const content::LoadCommittedDetails& details);
    187 
    188   // Returns whether a particular kind of content has been blocked for this
    189   // page.
    190   bool IsContentBlocked(ContentSettingsType content_type) const;
    191 
    192   // Returns true if content blockage was indicated to the user.
    193   bool IsBlockageIndicated(ContentSettingsType content_type) const;
    194 
    195   void SetBlockageHasBeenIndicated(ContentSettingsType content_type);
    196 
    197   // Returns whether a particular kind of content has been allowed. Currently
    198   // only tracks cookies.
    199   bool IsContentAllowed(ContentSettingsType content_type) const;
    200 
    201   const GURL& media_stream_access_origin() const {
    202     return media_stream_access_origin_;
    203   }
    204 
    205   const std::string& media_stream_requested_audio_device() const {
    206     return media_stream_requested_audio_device_;
    207   }
    208 
    209   const std::string& media_stream_requested_video_device() const {
    210     return media_stream_requested_video_device_;
    211   }
    212 
    213   // Returns the state of the camera and microphone usage.
    214   MicrophoneCameraState GetMicrophoneCameraState() const;
    215 
    216   // Returns the ContentSettingsUsagesState that controls the
    217   // geolocation API usage on this page.
    218   const ContentSettingsUsagesState& geolocation_usages_state() const {
    219     return geolocation_usages_state_;
    220   }
    221 
    222   // Returns the ContentSettingsUsageState that controls the MIDI usage on
    223   // this page.
    224   const ContentSettingsUsagesState& midi_usages_state() const {
    225     return midi_usages_state_;
    226   }
    227 
    228   // Call to indicate that there is a protocol handler pending user approval.
    229   void set_pending_protocol_handler(const ProtocolHandler& handler) {
    230     pending_protocol_handler_ = handler;
    231   }
    232 
    233   const ProtocolHandler& pending_protocol_handler() const {
    234     return pending_protocol_handler_;
    235   }
    236 
    237   void ClearPendingProtocolHandler() {
    238     pending_protocol_handler_ = ProtocolHandler::EmptyProtocolHandler();
    239   }
    240 
    241   // Sets the previous protocol handler which will be replaced by the
    242   // pending protocol handler.
    243   void set_previous_protocol_handler(const ProtocolHandler& handler) {
    244     previous_protocol_handler_ = handler;
    245   }
    246 
    247   const ProtocolHandler& previous_protocol_handler() const {
    248     return previous_protocol_handler_;
    249   }
    250 
    251   // Set whether the setting for the pending handler is DEFAULT (ignore),
    252   // ALLOW, or DENY.
    253   void set_pending_protocol_handler_setting(ContentSetting setting) {
    254     pending_protocol_handler_setting_ = setting;
    255   }
    256 
    257   ContentSetting pending_protocol_handler_setting() const {
    258     return pending_protocol_handler_setting_;
    259   }
    260 
    261 
    262   // Returns a pointer to the |LocalSharedObjectsContainer| that contains all
    263   // allowed local shared objects like cookies, local storage, ... .
    264   const LocalSharedObjectsContainer& allowed_local_shared_objects() const {
    265     return allowed_local_shared_objects_;
    266   }
    267 
    268   // Returns a pointer to the |LocalSharedObjectsContainer| that contains all
    269   // blocked local shared objects like cookies, local storage, ... .
    270   const LocalSharedObjectsContainer& blocked_local_shared_objects() const {
    271     return blocked_local_shared_objects_;
    272   }
    273 
    274   bool load_plugins_link_enabled() { return load_plugins_link_enabled_; }
    275   void set_load_plugins_link_enabled(bool enabled) {
    276     load_plugins_link_enabled_ = enabled;
    277   }
    278 
    279   // Called to indicate whether access to the Pepper broker was allowed or
    280   // blocked.
    281   void SetPepperBrokerAllowed(bool allowed);
    282 
    283   // content::WebContentsObserver overrides.
    284   virtual void RenderFrameForInterstitialPageCreated(
    285       content::RenderFrameHost* render_frame_host) OVERRIDE;
    286   virtual bool OnMessageReceived(
    287       const IPC::Message& message,
    288       content::RenderFrameHost* render_frame_host) OVERRIDE;
    289   virtual void DidNavigateMainFrame(
    290       const content::LoadCommittedDetails& details,
    291       const content::FrameNavigateParams& params) OVERRIDE;
    292   virtual void DidStartProvisionalLoadForFrame(
    293       int64 frame_id,
    294       int64 parent_frame_id,
    295       bool is_main_frame,
    296       const GURL& validated_url,
    297       bool is_error_page,
    298       bool is_iframe_srcdoc,
    299       content::RenderViewHost* render_view_host) OVERRIDE;
    300   virtual void AppCacheAccessed(const GURL& manifest_url,
    301                                 bool blocked_by_policy) OVERRIDE;
    302 
    303   // Message handlers. Public for testing.
    304   void OnContentBlocked(ContentSettingsType type);
    305   void OnContentAllowed(ContentSettingsType type);
    306 
    307   // These methods are invoked on the UI thread by the static functions above.
    308   // Public for testing.
    309   void OnCookiesRead(const GURL& url,
    310                      const GURL& first_party_url,
    311                      const net::CookieList& cookie_list,
    312                      bool blocked_by_policy);
    313   void OnCookieChanged(const GURL& url,
    314                        const GURL& first_party_url,
    315                        const std::string& cookie_line,
    316                        const net::CookieOptions& options,
    317                        bool blocked_by_policy);
    318   void OnFileSystemAccessed(const GURL& url,
    319                             bool blocked_by_policy);
    320   void OnIndexedDBAccessed(const GURL& url,
    321                            const base::string16& description,
    322                            bool blocked_by_policy);
    323   void OnLocalStorageAccessed(const GURL& url,
    324                               bool local,
    325                               bool blocked_by_policy);
    326   void OnWebDatabaseAccessed(const GURL& url,
    327                              const base::string16& name,
    328                              const base::string16& display_name,
    329                              bool blocked_by_policy);
    330   void OnGeolocationPermissionSet(const GURL& requesting_frame,
    331                                   bool allowed);
    332 #if defined(OS_ANDROID)
    333   void OnProtectedMediaIdentifierPermissionSet(const GURL& requesting_frame,
    334                                                bool allowed);
    335 #endif
    336 
    337   // This method is called to update the status about the microphone and
    338   // camera stream access. |request_permissions| contains a list of requested
    339   // media stream types and the permission for each type.
    340   void OnMediaStreamPermissionSet(
    341       const GURL& request_origin,
    342       const MediaStreamDevicesController::MediaStreamTypeSettingsMap&
    343           request_permissions);
    344 
    345   // There methods are called to update the status about MIDI access.
    346   void OnMidiSysExAccessed(const GURL& reqesting_origin);
    347   void OnMidiSysExAccessBlocked(const GURL& requesting_origin);
    348 
    349   // Adds the given |SiteDataObserver|. The |observer| is notified when a
    350   // locale shared object, like for example a cookie, is accessed.
    351   void AddSiteDataObserver(SiteDataObserver* observer);
    352 
    353   // Removes the given |SiteDataObserver|.
    354   void RemoveSiteDataObserver(SiteDataObserver* observer);
    355 
    356  private:
    357   explicit TabSpecificContentSettings(content::WebContents* tab);
    358   friend class content::WebContentsUserData<TabSpecificContentSettings>;
    359 
    360   // content::NotificationObserver implementation.
    361   virtual void Observe(int type,
    362                        const content::NotificationSource& source,
    363                        const content::NotificationDetails& details) OVERRIDE;
    364 
    365   // Notifies all registered |SiteDataObserver|s.
    366   void NotifySiteDataObservers();
    367 
    368   // All currently registered |SiteDataObserver|s.
    369   ObserverList<SiteDataObserver> observer_list_;
    370 
    371   // Stores which content setting types actually have blocked content.
    372   bool content_blocked_[CONTENT_SETTINGS_NUM_TYPES];
    373 
    374   // Stores if the blocked content was messaged to the user.
    375   bool content_blockage_indicated_to_user_[CONTENT_SETTINGS_NUM_TYPES];
    376 
    377   // Stores which content setting types actually were allowed.
    378   bool content_allowed_[CONTENT_SETTINGS_NUM_TYPES];
    379 
    380   // The profile of the tab.
    381   Profile* profile_;
    382 
    383   // Stores the blocked/allowed cookies.
    384   LocalSharedObjectsContainer allowed_local_shared_objects_;
    385   LocalSharedObjectsContainer blocked_local_shared_objects_;
    386 
    387   // Manages information about Geolocation API usage in this page.
    388   ContentSettingsUsagesState geolocation_usages_state_;
    389 
    390   // Manages information about MIDI usages in this page.
    391   ContentSettingsUsagesState midi_usages_state_;
    392 
    393   // The pending protocol handler, if any. This can be set if
    394   // registerProtocolHandler was invoked without user gesture.
    395   // The |IsEmpty| method will be true if no protocol handler is
    396   // pending registration.
    397   ProtocolHandler pending_protocol_handler_;
    398 
    399   // The previous protocol handler to be replaced by
    400   // the pending_protocol_handler_, if there is one. Empty if
    401   // there is no handler which would be replaced.
    402   ProtocolHandler previous_protocol_handler_;
    403 
    404   // The setting on the pending protocol handler registration. Persisted in case
    405   // the user opens the bubble and makes changes multiple times.
    406   ContentSetting pending_protocol_handler_setting_;
    407 
    408   // Stores whether the user can load blocked plugins on this page.
    409   bool load_plugins_link_enabled_;
    410 
    411   content::NotificationRegistrar registrar_;
    412 
    413   // The origin of the media stream request. Note that we only support handling
    414   // settings for one request per tab. The latest request's origin will be
    415   // stored here. http://crbug.com/259794
    416   GURL media_stream_access_origin_;
    417 
    418   // The devices to be displayed in the media bubble when the media stream
    419   // request is requesting certain specific devices.
    420   std::string media_stream_requested_audio_device_;
    421   std::string media_stream_requested_video_device_;
    422 
    423   DISALLOW_COPY_AND_ASSIGN(TabSpecificContentSettings);
    424 };
    425 
    426 #endif  // CHROME_BROWSER_CONTENT_SETTINGS_TAB_SPECIFIC_CONTENT_SETTINGS_H_
    427