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.
5 #ifndef CHROME_BROWSER_CONTENT_SETTINGS_TAB_SPECIFIC_CONTENT_SETTINGS_H_
6 #define CHROME_BROWSER_CONTENT_SETTINGS_TAB_SPECIFIC_CONTENT_SETTINGS_H_
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/custom_handlers/protocol_handler.h"
19 #include "components/content_settings/core/common/content_settings_types.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"
27 class CookiesTreeModel;
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> {
47 enum MicrophoneCameraState {
48 MICROPHONE_CAMERA_NOT_ACCESSED = 0,
51 MICROPHONE_CAMERA_ACCESSED,
54 MICROPHONE_CAMERA_BLOCKED,
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 {
62 explicit SiteDataObserver(
63 TabSpecificContentSettings* tab_specific_content_settings);
64 virtual ~SiteDataObserver();
66 // Called whenever site data is accessed.
67 virtual void OnSiteDataAccessed() = 0;
69 TabSpecificContentSettings* tab_specific_content_settings() {
70 return tab_specific_content_settings_;
73 // Called when the TabSpecificContentSettings is destroyed; nulls out
74 // the local reference.
75 void ContentSettingsDestroyed();
78 TabSpecificContentSettings* tab_specific_content_settings_;
80 DISALLOW_COPY_AND_ASSIGN(SiteDataObserver);
83 virtual ~TabSpecificContentSettings();
85 // Returns the object given a render view's id.
86 static TabSpecificContentSettings* Get(int render_process_id,
89 // Returns the object given a render frame's id.
90 static TabSpecificContentSettings* GetForFrame(int render_process_id,
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,
105 const GURL& first_party_url,
106 const net::CookieList& cookie_list,
107 bool blocked_by_policy,
108 bool is_for_blocking_resource);
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
114 static void CookieChanged(int render_process_id,
117 const GURL& first_party_url,
118 const std::string& cookie_line,
119 const net::CookieOptions& options,
120 bool blocked_by_policy);
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
126 static void WebDatabaseAccessed(int render_process_id,
129 const base::string16& name,
130 const base::string16& display_name,
131 bool blocked_by_policy);
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
137 static void DOMStorageAccessed(int render_process_id,
141 bool blocked_by_policy);
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
147 static void IndexedDBAccessed(int render_process_id,
150 const base::string16& description,
151 bool blocked_by_policy);
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
157 static void FileSystemAccessed(int render_process_id,
160 bool blocked_by_policy);
162 // Resets the |content_blocked_| and |content_allowed_| arrays, except for
163 // CONTENT_SETTINGS_TYPE_COOKIES related information.
164 void ClearBlockedContentSettingsExceptForCookies();
166 // Resets all cookies related information.
167 void ClearCookieSpecificContentSettings();
169 // Clears the Geolocation settings.
170 void ClearGeolocationContentSettings();
172 // Clears the MIDI settings.
173 void ClearMidiContentSettings();
175 // Changes the |content_blocked_| entry for popups.
176 void SetPopupsBlocked(bool blocked);
178 // Changes the |content_blocked_| entry for downloads.
179 void SetDownloadsBlocked(bool blocked);
181 // Updates Geolocation settings on navigation.
182 void GeolocationDidNavigate(
183 const content::LoadCommittedDetails& details);
185 // Updates MIDI settings on navigation.
186 void MidiDidNavigate(const content::LoadCommittedDetails& details);
188 // Returns whether a particular kind of content has been blocked for this
190 bool IsContentBlocked(ContentSettingsType content_type) const;
192 // Returns true if content blockage was indicated to the user.
193 bool IsBlockageIndicated(ContentSettingsType content_type) const;
195 void SetBlockageHasBeenIndicated(ContentSettingsType content_type);
197 // Returns whether a particular kind of content has been allowed. Currently
198 // only tracks cookies.
199 bool IsContentAllowed(ContentSettingsType content_type) const;
201 const GURL& media_stream_access_origin() const {
202 return media_stream_access_origin_;
205 const std::string& media_stream_requested_audio_device() const {
206 return media_stream_requested_audio_device_;
209 const std::string& media_stream_requested_video_device() const {
210 return media_stream_requested_video_device_;
213 // Returns the state of the camera and microphone usage.
214 MicrophoneCameraState GetMicrophoneCameraState() const;
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_;
222 // Returns the ContentSettingsUsageState that controls the MIDI usage on
224 const ContentSettingsUsagesState& midi_usages_state() const {
225 return midi_usages_state_;
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;
233 const ProtocolHandler& pending_protocol_handler() const {
234 return pending_protocol_handler_;
237 void ClearPendingProtocolHandler() {
238 pending_protocol_handler_ = ProtocolHandler::EmptyProtocolHandler();
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;
247 const ProtocolHandler& previous_protocol_handler() const {
248 return previous_protocol_handler_;
251 // Set whether the setting for the pending handler is DEFAULT (ignore),
253 void set_pending_protocol_handler_setting(ContentSetting setting) {
254 pending_protocol_handler_setting_ = setting;
257 ContentSetting pending_protocol_handler_setting() const {
258 return pending_protocol_handler_setting_;
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_;
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_;
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;
279 // Called to indicate whether access to the Pepper broker was allowed or
281 void SetPepperBrokerAllowed(bool allowed);
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 content::RenderFrameHost* render_frame_host,
294 const GURL& validated_url,
296 bool is_iframe_srcdoc) OVERRIDE;
297 virtual void AppCacheAccessed(const GURL& manifest_url,
298 bool blocked_by_policy) OVERRIDE;
300 // Message handlers. Public for testing.
301 void OnContentBlocked(ContentSettingsType type);
302 void OnContentAllowed(ContentSettingsType type);
304 // These methods are invoked on the UI thread by the static functions above.
305 // Public for testing.
306 void OnCookiesRead(const GURL& url,
307 const GURL& first_party_url,
308 const net::CookieList& cookie_list,
309 bool blocked_by_policy);
310 void OnCookieChanged(const GURL& url,
311 const GURL& first_party_url,
312 const std::string& cookie_line,
313 const net::CookieOptions& options,
314 bool blocked_by_policy);
315 void OnFileSystemAccessed(const GURL& url,
316 bool blocked_by_policy);
317 void OnIndexedDBAccessed(const GURL& url,
318 const base::string16& description,
319 bool blocked_by_policy);
320 void OnLocalStorageAccessed(const GURL& url,
322 bool blocked_by_policy);
323 void OnWebDatabaseAccessed(const GURL& url,
324 const base::string16& name,
325 const base::string16& display_name,
326 bool blocked_by_policy);
327 void OnGeolocationPermissionSet(const GURL& requesting_frame,
329 #if defined(OS_ANDROID)
330 void OnProtectedMediaIdentifierPermissionSet(const GURL& requesting_frame,
334 // This method is called to update the status about the microphone and
335 // camera stream access. |request_permissions| contains a list of requested
336 // media stream types and the permission for each type.
337 void OnMediaStreamPermissionSet(
338 const GURL& request_origin,
339 const MediaStreamDevicesController::MediaStreamTypeSettingsMap&
340 request_permissions);
342 // There methods are called to update the status about MIDI access.
343 void OnMidiSysExAccessed(const GURL& reqesting_origin);
344 void OnMidiSysExAccessBlocked(const GURL& requesting_origin);
346 // Adds the given |SiteDataObserver|. The |observer| is notified when a
347 // locale shared object, like for example a cookie, is accessed.
348 void AddSiteDataObserver(SiteDataObserver* observer);
350 // Removes the given |SiteDataObserver|.
351 void RemoveSiteDataObserver(SiteDataObserver* observer);
354 explicit TabSpecificContentSettings(content::WebContents* tab);
355 friend class content::WebContentsUserData<TabSpecificContentSettings>;
357 // content::NotificationObserver implementation.
358 virtual void Observe(int type,
359 const content::NotificationSource& source,
360 const content::NotificationDetails& details) OVERRIDE;
362 // Notifies all registered |SiteDataObserver|s.
363 void NotifySiteDataObservers();
365 // All currently registered |SiteDataObserver|s.
366 ObserverList<SiteDataObserver> observer_list_;
368 // Stores which content setting types actually have blocked content.
369 bool content_blocked_[CONTENT_SETTINGS_NUM_TYPES];
371 // Stores if the blocked content was messaged to the user.
372 bool content_blockage_indicated_to_user_[CONTENT_SETTINGS_NUM_TYPES];
374 // Stores which content setting types actually were allowed.
375 bool content_allowed_[CONTENT_SETTINGS_NUM_TYPES];
377 // The profile of the tab.
380 // Stores the blocked/allowed cookies.
381 LocalSharedObjectsContainer allowed_local_shared_objects_;
382 LocalSharedObjectsContainer blocked_local_shared_objects_;
384 // Manages information about Geolocation API usage in this page.
385 ContentSettingsUsagesState geolocation_usages_state_;
387 // Manages information about MIDI usages in this page.
388 ContentSettingsUsagesState midi_usages_state_;
390 // The pending protocol handler, if any. This can be set if
391 // registerProtocolHandler was invoked without user gesture.
392 // The |IsEmpty| method will be true if no protocol handler is
393 // pending registration.
394 ProtocolHandler pending_protocol_handler_;
396 // The previous protocol handler to be replaced by
397 // the pending_protocol_handler_, if there is one. Empty if
398 // there is no handler which would be replaced.
399 ProtocolHandler previous_protocol_handler_;
401 // The setting on the pending protocol handler registration. Persisted in case
402 // the user opens the bubble and makes changes multiple times.
403 ContentSetting pending_protocol_handler_setting_;
405 // Stores whether the user can load blocked plugins on this page.
406 bool load_plugins_link_enabled_;
408 content::NotificationRegistrar registrar_;
410 // The origin of the media stream request. Note that we only support handling
411 // settings for one request per tab. The latest request's origin will be
412 // stored here. http://crbug.com/259794
413 GURL media_stream_access_origin_;
415 // The devices to be displayed in the media bubble when the media stream
416 // request is requesting certain specific devices.
417 std::string media_stream_requested_audio_device_;
418 std::string media_stream_requested_video_device_;
420 DISALLOW_COPY_AND_ASSIGN(TabSpecificContentSettings);
423 #endif // CHROME_BROWSER_CONTENT_SETTINGS_TAB_SPECIFIC_CONTENT_SETTINGS_H_