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/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"
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(const IPC::Message
& message
) OVERRIDE
;
287 virtual void DidNavigateMainFrame(
288 const content::LoadCommittedDetails
& details
,
289 const content::FrameNavigateParams
& params
) OVERRIDE
;
290 virtual void DidStartProvisionalLoadForFrame(
292 int64 parent_frame_id
,
294 const GURL
& validated_url
,
296 bool is_iframe_srcdoc
,
297 content::RenderViewHost
* render_view_host
) OVERRIDE
;
298 virtual void AppCacheAccessed(const GURL
& manifest_url
,
299 bool blocked_by_policy
) OVERRIDE
;
301 // Message handlers. Public for testing.
302 void OnContentBlocked(ContentSettingsType type
);
303 void OnContentAllowed(ContentSettingsType type
);
305 // These methods are invoked on the UI thread by the static functions above.
306 // Public for testing.
307 void OnCookiesRead(const GURL
& url
,
308 const GURL
& first_party_url
,
309 const net::CookieList
& cookie_list
,
310 bool blocked_by_policy
);
311 void OnCookieChanged(const GURL
& url
,
312 const GURL
& first_party_url
,
313 const std::string
& cookie_line
,
314 const net::CookieOptions
& options
,
315 bool blocked_by_policy
);
316 void OnFileSystemAccessed(const GURL
& url
,
317 bool blocked_by_policy
);
318 void OnIndexedDBAccessed(const GURL
& url
,
319 const base::string16
& description
,
320 bool blocked_by_policy
);
321 void OnLocalStorageAccessed(const GURL
& url
,
323 bool blocked_by_policy
);
324 void OnWebDatabaseAccessed(const GURL
& url
,
325 const base::string16
& name
,
326 const base::string16
& display_name
,
327 bool blocked_by_policy
);
328 void OnGeolocationPermissionSet(const GURL
& requesting_frame
,
330 #if defined(OS_ANDROID)
331 void OnProtectedMediaIdentifierPermissionSet(const GURL
& requesting_frame
,
335 // This method is called to update the status about the microphone and
336 // camera stream access. |request_permissions| contains a list of requested
337 // media stream types and the permission for each type.
338 void OnMediaStreamPermissionSet(
339 const GURL
& request_origin
,
340 const MediaStreamDevicesController::MediaStreamTypeSettingsMap
&
341 request_permissions
);
343 // There methods are called to update the status about MIDI access.
344 void OnMidiSysExAccessed(const GURL
& reqesting_origin
);
345 void OnMidiSysExAccessBlocked(const GURL
& requesting_origin
);
347 // Adds the given |SiteDataObserver|. The |observer| is notified when a
348 // locale shared object, like for example a cookie, is accessed.
349 void AddSiteDataObserver(SiteDataObserver
* observer
);
351 // Removes the given |SiteDataObserver|.
352 void RemoveSiteDataObserver(SiteDataObserver
* observer
);
355 explicit TabSpecificContentSettings(content::WebContents
* tab
);
356 friend class content::WebContentsUserData
<TabSpecificContentSettings
>;
358 // content::NotificationObserver implementation.
359 virtual void Observe(int type
,
360 const content::NotificationSource
& source
,
361 const content::NotificationDetails
& details
) OVERRIDE
;
363 // Notifies all registered |SiteDataObserver|s.
364 void NotifySiteDataObservers();
366 // All currently registered |SiteDataObserver|s.
367 ObserverList
<SiteDataObserver
> observer_list_
;
369 // Stores which content setting types actually have blocked content.
370 bool content_blocked_
[CONTENT_SETTINGS_NUM_TYPES
];
372 // Stores if the blocked content was messaged to the user.
373 bool content_blockage_indicated_to_user_
[CONTENT_SETTINGS_NUM_TYPES
];
375 // Stores which content setting types actually were allowed.
376 bool content_allowed_
[CONTENT_SETTINGS_NUM_TYPES
];
378 // The profile of the tab.
381 // Stores the blocked/allowed cookies.
382 LocalSharedObjectsContainer allowed_local_shared_objects_
;
383 LocalSharedObjectsContainer blocked_local_shared_objects_
;
385 // Manages information about Geolocation API usage in this page.
386 ContentSettingsUsagesState geolocation_usages_state_
;
388 // Manages information about MIDI usages in this page.
389 ContentSettingsUsagesState midi_usages_state_
;
391 // The pending protocol handler, if any. This can be set if
392 // registerProtocolHandler was invoked without user gesture.
393 // The |IsEmpty| method will be true if no protocol handler is
394 // pending registration.
395 ProtocolHandler pending_protocol_handler_
;
397 // The previous protocol handler to be replaced by
398 // the pending_protocol_handler_, if there is one. Empty if
399 // there is no handler which would be replaced.
400 ProtocolHandler previous_protocol_handler_
;
402 // The setting on the pending protocol handler registration. Persisted in case
403 // the user opens the bubble and makes changes multiple times.
404 ContentSetting pending_protocol_handler_setting_
;
406 // Stores whether the user can load blocked plugins on this page.
407 bool load_plugins_link_enabled_
;
409 content::NotificationRegistrar registrar_
;
411 // The origin of the media stream request. Note that we only support handling
412 // settings for one request per tab. The latest request's origin will be
413 // stored here. http://crbug.com/259794
414 GURL media_stream_access_origin_
;
416 // The devices to be displayed in the media bubble when the media stream
417 // request is requesting certain specific devices.
418 std::string media_stream_requested_audio_device_
;
419 std::string media_stream_requested_video_device_
;
421 DISALLOW_COPY_AND_ASSIGN(TabSpecificContentSettings
);
424 #endif // CHROME_BROWSER_CONTENT_SETTINGS_TAB_SPECIFIC_CONTENT_SETTINGS_H_