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 "base/scoped_observer.h"
15 #include "chrome/browser/browsing_data/cookies_tree_model.h"
16 #include "chrome/browser/content_settings/content_settings_usages_state.h"
17 #include "chrome/browser/content_settings/local_shared_objects_container.h"
18 #include "chrome/browser/media/media_stream_devices_controller.h"
19 #include "chrome/common/custom_handlers/protocol_handler.h"
20 #include "components/content_settings/core/browser/content_settings_observer.h"
21 #include "components/content_settings/core/common/content_settings.h"
22 #include "components/content_settings/core/common/content_settings_types.h"
23 #include "content/public/browser/web_contents_observer.h"
24 #include "content/public/browser/web_contents_user_data.h"
25 #include "content/public/common/media_stream_request.h"
26 #include "net/cookies/canonical_cookie.h"
28 class HostContentSettingsMap
;
39 // This class manages state about permissions, content settings, cookies and
40 // site data for a specific WebContents. It tracks which content was accessed
41 // and which content was blocked. Based on this it provides information about
42 // which types of content were accessed and blocked.
43 class TabSpecificContentSettings
44 : public content::WebContentsObserver
,
45 public content_settings::Observer
,
46 public content::WebContentsUserData
<TabSpecificContentSettings
> {
48 // Fields describing the current mic/camera state. If a page has attempted to
49 // access a device, the XXX_ACCESSED bit will be set. If access was blocked,
50 // XXX_BLOCKED will be set.
51 typedef uint32_t MicrophoneCameraState
;
52 static const MicrophoneCameraState MICROPHONE_CAMERA_NOT_ACCESSED
= 0;
53 static const MicrophoneCameraState MICROPHONE_ACCESSED
= 1 << 0;
54 static const MicrophoneCameraState MICROPHONE_BLOCKED
= 1 << 1;
55 static const MicrophoneCameraState CAMERA_ACCESSED
= 1 << 2;
56 static const MicrophoneCameraState CAMERA_BLOCKED
= 1 << 3;
58 // Classes that want to be notified about site data events must implement
59 // this abstract class and add themselves as observer to the
60 // |TabSpecificContentSettings|.
61 class SiteDataObserver
{
63 explicit SiteDataObserver(
64 TabSpecificContentSettings
* tab_specific_content_settings
);
65 virtual ~SiteDataObserver();
67 // Called whenever site data is accessed.
68 virtual void OnSiteDataAccessed() = 0;
70 TabSpecificContentSettings
* tab_specific_content_settings() {
71 return tab_specific_content_settings_
;
74 // Called when the TabSpecificContentSettings is destroyed; nulls out
75 // the local reference.
76 void ContentSettingsDestroyed();
79 TabSpecificContentSettings
* tab_specific_content_settings_
;
81 DISALLOW_COPY_AND_ASSIGN(SiteDataObserver
);
84 virtual ~TabSpecificContentSettings();
86 // Returns the object given a render view's id.
87 static TabSpecificContentSettings
* Get(int render_process_id
,
90 // Returns the object given a render frame's id.
91 static TabSpecificContentSettings
* GetForFrame(int render_process_id
,
94 // Static methods called on the UI threads.
95 // Called when cookies for the given URL were read either from within the
96 // current page or while loading it. |blocked_by_policy| should be true, if
97 // reading cookies was blocked due to the user's content settings. In that
98 // case, this function should invoke OnContentBlocked.
99 // |is_for_blocking_resource| indicates whether the cookies read were for a
100 // blocking resource (eg script, css). It is only temporarily added for
101 // diagnostic purposes, per bug 353678. Will be removed again once data
102 // collection is finished.
103 static void CookiesRead(int render_process_id
,
106 const GURL
& first_party_url
,
107 const net::CookieList
& cookie_list
,
108 bool blocked_by_policy
,
109 bool is_for_blocking_resource
);
111 // Called when a specific cookie in the current page was changed.
112 // |blocked_by_policy| should be true, if the cookie was blocked due to the
113 // user's content settings. In that case, this function should invoke
115 static void CookieChanged(int render_process_id
,
118 const GURL
& first_party_url
,
119 const std::string
& cookie_line
,
120 const net::CookieOptions
& options
,
121 bool blocked_by_policy
);
123 // Called when a specific Web database in the current page was accessed. If
124 // access was blocked due to the user's content settings,
125 // |blocked_by_policy| should be true, and this function should invoke
127 static void WebDatabaseAccessed(int render_process_id
,
130 const base::string16
& name
,
131 const base::string16
& display_name
,
132 bool blocked_by_policy
);
134 // Called when a specific DOM storage area 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
138 static void DOMStorageAccessed(int render_process_id
,
142 bool blocked_by_policy
);
144 // Called when a specific indexed db factory in the current page was
145 // accessed. If access was blocked due to the user's content settings,
146 // |blocked_by_policy| should be true, and this function should invoke
148 static void IndexedDBAccessed(int render_process_id
,
151 const base::string16
& description
,
152 bool blocked_by_policy
);
154 // Called when a specific file system in the current page was accessed.
155 // If access was blocked due to the user's content settings,
156 // |blocked_by_policy| should be true, and this function should invoke
158 static void FileSystemAccessed(int render_process_id
,
161 bool blocked_by_policy
);
163 // Resets the |content_blocked_| and |content_allowed_| arrays, except for
164 // CONTENT_SETTINGS_TYPE_COOKIES related information.
165 void ClearBlockedContentSettingsExceptForCookies();
167 // Resets all cookies related information.
168 void ClearCookieSpecificContentSettings();
170 // Clears the Geolocation settings.
171 void ClearGeolocationContentSettings();
173 // Clears the MIDI settings.
174 void ClearMidiContentSettings();
176 // Changes the |content_blocked_| entry for popups.
177 void SetPopupsBlocked(bool blocked
);
179 // Changes the |content_blocked_| entry for downloads.
180 void SetDownloadsBlocked(bool blocked
);
182 // Updates Geolocation settings on navigation.
183 void GeolocationDidNavigate(
184 const content::LoadCommittedDetails
& details
);
186 // Updates MIDI settings on navigation.
187 void MidiDidNavigate(const content::LoadCommittedDetails
& details
);
189 // Returns whether a particular kind of content has been blocked for this
191 bool IsContentBlocked(ContentSettingsType content_type
) const;
193 // Returns true if content blockage was indicated to the user.
194 bool IsBlockageIndicated(ContentSettingsType content_type
) const;
196 void SetBlockageHasBeenIndicated(ContentSettingsType content_type
);
198 // Returns whether a particular kind of content has been allowed. Currently
199 // only tracks cookies.
200 bool IsContentAllowed(ContentSettingsType content_type
) const;
202 const GURL
& media_stream_access_origin() const {
203 return media_stream_access_origin_
;
206 const std::string
& media_stream_requested_audio_device() const {
207 return media_stream_requested_audio_device_
;
210 const std::string
& media_stream_requested_video_device() const {
211 return media_stream_requested_video_device_
;
214 const std::string
& media_stream_selected_audio_device() const {
215 return media_stream_selected_audio_device_
;
218 const std::string
& media_stream_selected_video_device() const {
219 return media_stream_selected_video_device_
;
222 // Returns the state of the camera and microphone usage.
223 MicrophoneCameraState
GetMicrophoneCameraState() const;
225 // Returns whether the state of the camera and microphone usage or device has
227 bool IsMicrophoneCameraStateChanged() const;
229 // Returns the ContentSettingsUsagesState that controls the
230 // geolocation API usage on this page.
231 const ContentSettingsUsagesState
& geolocation_usages_state() const {
232 return geolocation_usages_state_
;
235 // Returns the ContentSettingsUsageState that controls the MIDI usage on
237 const ContentSettingsUsagesState
& midi_usages_state() const {
238 return midi_usages_state_
;
241 // Call to indicate that there is a protocol handler pending user approval.
242 void set_pending_protocol_handler(const ProtocolHandler
& handler
) {
243 pending_protocol_handler_
= handler
;
246 const ProtocolHandler
& pending_protocol_handler() const {
247 return pending_protocol_handler_
;
250 void ClearPendingProtocolHandler() {
251 pending_protocol_handler_
= ProtocolHandler::EmptyProtocolHandler();
254 // Sets the previous protocol handler which will be replaced by the
255 // pending protocol handler.
256 void set_previous_protocol_handler(const ProtocolHandler
& handler
) {
257 previous_protocol_handler_
= handler
;
260 const ProtocolHandler
& previous_protocol_handler() const {
261 return previous_protocol_handler_
;
264 // Set whether the setting for the pending handler is DEFAULT (ignore),
266 void set_pending_protocol_handler_setting(ContentSetting setting
) {
267 pending_protocol_handler_setting_
= setting
;
270 ContentSetting
pending_protocol_handler_setting() const {
271 return pending_protocol_handler_setting_
;
274 // Returns the |LocalSharedObjectsCounter| instances corresponding to all
275 // allowed, and blocked, respectively, local shared objects like cookies,
276 // local storage, ... .
277 const LocalSharedObjectsCounter
& allowed_local_shared_objects() const {
278 return allowed_local_shared_objects_
;
281 const LocalSharedObjectsCounter
& blocked_local_shared_objects() const {
282 return blocked_local_shared_objects_
;
285 // Creates a new copy of a CookiesTreeModel for all allowed, and blocked,
286 // respectively, local shared objects.
287 scoped_ptr
<CookiesTreeModel
> CreateAllowedCookiesTreeModel() const {
288 return allowed_local_shared_objects_
.CreateCookiesTreeModel();
291 scoped_ptr
<CookiesTreeModel
> CreateBlockedCookiesTreeModel() const {
292 return blocked_local_shared_objects_
.CreateCookiesTreeModel();
295 bool load_plugins_link_enabled() { return load_plugins_link_enabled_
; }
296 void set_load_plugins_link_enabled(bool enabled
) {
297 load_plugins_link_enabled_
= enabled
;
300 // Called to indicate whether access to the Pepper broker was allowed or
302 void SetPepperBrokerAllowed(bool allowed
);
304 // content::WebContentsObserver overrides.
305 virtual void RenderFrameForInterstitialPageCreated(
306 content::RenderFrameHost
* render_frame_host
) override
;
307 virtual bool OnMessageReceived(
308 const IPC::Message
& message
,
309 content::RenderFrameHost
* render_frame_host
) override
;
310 virtual void DidNavigateMainFrame(
311 const content::LoadCommittedDetails
& details
,
312 const content::FrameNavigateParams
& params
) override
;
313 virtual void DidStartProvisionalLoadForFrame(
314 content::RenderFrameHost
* render_frame_host
,
315 const GURL
& validated_url
,
317 bool is_iframe_srcdoc
) override
;
318 virtual void AppCacheAccessed(const GURL
& manifest_url
,
319 bool blocked_by_policy
) override
;
321 // Message handlers. Public for testing.
322 void OnContentBlocked(ContentSettingsType type
);
323 void OnContentAllowed(ContentSettingsType type
);
325 // These methods are invoked on the UI thread by the static functions above.
326 // Public for testing.
327 void OnCookiesRead(const GURL
& url
,
328 const GURL
& first_party_url
,
329 const net::CookieList
& cookie_list
,
330 bool blocked_by_policy
);
331 void OnCookieChanged(const GURL
& url
,
332 const GURL
& first_party_url
,
333 const std::string
& cookie_line
,
334 const net::CookieOptions
& options
,
335 bool blocked_by_policy
);
336 void OnFileSystemAccessed(const GURL
& url
,
337 bool blocked_by_policy
);
338 void OnIndexedDBAccessed(const GURL
& url
,
339 const base::string16
& description
,
340 bool blocked_by_policy
);
341 void OnLocalStorageAccessed(const GURL
& url
,
343 bool blocked_by_policy
);
344 void OnWebDatabaseAccessed(const GURL
& url
,
345 const base::string16
& name
,
346 const base::string16
& display_name
,
347 bool blocked_by_policy
);
348 void OnGeolocationPermissionSet(const GURL
& requesting_frame
,
350 #if defined(OS_ANDROID)
351 void OnProtectedMediaIdentifierPermissionSet(const GURL
& requesting_frame
,
355 // This method is called to update the status about the microphone and
356 // camera stream access. |request_permissions| contains a list of requested
357 // media stream types and the permission for each type.
358 void OnMediaStreamPermissionSet(
359 const GURL
& request_origin
,
360 const MediaStreamDevicesController::MediaStreamTypeSettingsMap
&
361 request_permissions
);
363 // There methods are called to update the status about MIDI access.
364 void OnMidiSysExAccessed(const GURL
& reqesting_origin
);
365 void OnMidiSysExAccessBlocked(const GURL
& requesting_origin
);
367 // Adds the given |SiteDataObserver|. The |observer| is notified when a
368 // locale shared object, like for example a cookie, is accessed.
369 void AddSiteDataObserver(SiteDataObserver
* observer
);
371 // Removes the given |SiteDataObserver|.
372 void RemoveSiteDataObserver(SiteDataObserver
* observer
);
375 explicit TabSpecificContentSettings(content::WebContents
* tab
);
376 friend class content::WebContentsUserData
<TabSpecificContentSettings
>;
378 // content_settings::Observer implementation.
379 virtual void OnContentSettingChanged(
380 const ContentSettingsPattern
& primary_pattern
,
381 const ContentSettingsPattern
& secondary_pattern
,
382 ContentSettingsType content_type
,
383 std::string resource_identifier
) override
;
385 // Notifies all registered |SiteDataObserver|s.
386 void NotifySiteDataObservers();
388 // All currently registered |SiteDataObserver|s.
389 ObserverList
<SiteDataObserver
> observer_list_
;
391 // Stores which content setting types actually have blocked content.
392 bool content_blocked_
[CONTENT_SETTINGS_NUM_TYPES
];
394 // Stores if the blocked content was messaged to the user.
395 bool content_blockage_indicated_to_user_
[CONTENT_SETTINGS_NUM_TYPES
];
397 // Stores which content setting types actually were allowed.
398 bool content_allowed_
[CONTENT_SETTINGS_NUM_TYPES
];
400 // The profile of the tab.
403 // Stores the blocked/allowed cookies.
404 LocalSharedObjectsContainer allowed_local_shared_objects_
;
405 LocalSharedObjectsContainer blocked_local_shared_objects_
;
407 // Manages information about Geolocation API usage in this page.
408 ContentSettingsUsagesState geolocation_usages_state_
;
410 // Manages information about MIDI usages in this page.
411 ContentSettingsUsagesState midi_usages_state_
;
413 // The pending protocol handler, if any. This can be set if
414 // registerProtocolHandler was invoked without user gesture.
415 // The |IsEmpty| method will be true if no protocol handler is
416 // pending registration.
417 ProtocolHandler pending_protocol_handler_
;
419 // The previous protocol handler to be replaced by
420 // the pending_protocol_handler_, if there is one. Empty if
421 // there is no handler which would be replaced.
422 ProtocolHandler previous_protocol_handler_
;
424 // The setting on the pending protocol handler registration. Persisted in case
425 // the user opens the bubble and makes changes multiple times.
426 ContentSetting pending_protocol_handler_setting_
;
428 // Stores whether the user can load blocked plugins on this page.
429 bool load_plugins_link_enabled_
;
431 // The origin of the media stream request. Note that we only support handling
432 // settings for one request per tab. The latest request's origin will be
433 // stored here. http://crbug.com/259794
434 GURL media_stream_access_origin_
;
436 // The microphone and camera state at the last media stream request.
437 // This value is composed of MicrophoneCameraState values.
438 MicrophoneCameraState microphone_camera_state_
;
439 // The selected devices at the last media stream request.
440 std::string media_stream_selected_audio_device_
;
441 std::string media_stream_selected_video_device_
;
443 // The devices to be displayed in the media bubble when the media stream
444 // request is requesting certain specific devices.
445 std::string media_stream_requested_audio_device_
;
446 std::string media_stream_requested_video_device_
;
448 // Observer to watch for content settings changed.
449 ScopedObserver
<HostContentSettingsMap
, content_settings::Observer
> observer_
;
451 DISALLOW_COPY_AND_ASSIGN(TabSpecificContentSettings
);
454 #endif // CHROME_BROWSER_CONTENT_SETTINGS_TAB_SPECIFIC_CONTENT_SETTINGS_H_