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