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/common/custom_handlers/protocol_handler.h"
19 #include "components/content_settings/core/browser/content_settings_observer.h"
20 #include "components/content_settings/core/common/content_settings.h"
21 #include "components/content_settings/core/common/content_settings_types.h"
22 #include "content/public/browser/web_contents_observer.h"
23 #include "content/public/browser/web_contents_user_data.h"
24 #include "net/cookies/canonical_cookie.h"
26 class HostContentSettingsMap
;
36 // This class manages state about permissions, content settings, cookies and
37 // site data for a specific WebContents. It tracks which content was accessed
38 // and which content was blocked. Based on this it provides information about
39 // which types of content were accessed and blocked.
40 class TabSpecificContentSettings
41 : public content::WebContentsObserver
,
42 public content_settings::Observer
,
43 public content::WebContentsUserData
<TabSpecificContentSettings
> {
45 // Fields describing the current mic/camera state. If a page has attempted to
46 // access a device, the XXX_ACCESSED bit will be set. If access was blocked,
47 // XXX_BLOCKED will be set.
48 enum MicrophoneCameraStateFlags
{
49 MICROPHONE_CAMERA_NOT_ACCESSED
= 0,
50 MICROPHONE_ACCESSED
= 1 << 0,
51 MICROPHONE_BLOCKED
= 1 << 1,
52 CAMERA_ACCESSED
= 1 << 2,
53 CAMERA_BLOCKED
= 1 << 3,
55 // Use signed int, that's what the enum flags implicitly convert to.
56 typedef int32_t MicrophoneCameraState
;
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 ~TabSpecificContentSettings() override
;
86 // Returns the object given a render view's id.
87 static TabSpecificContentSettings
* Get(int render_process_id
,
90 // Static methods called on the UI threads.
91 // Called when cookies for the given URL were read either from within the
92 // current page or while loading it. |blocked_by_policy| should be true, if
93 // reading cookies was blocked due to the user's content settings. In that
94 // case, this function should invoke OnContentBlocked.
95 // |is_for_blocking_resource| indicates whether the cookies read were for a
96 // blocking resource (eg script, css). It is only temporarily added for
97 // diagnostic purposes, per bug 353678. Will be removed again once data
98 // collection is finished.
99 static void CookiesRead(int render_process_id
,
102 const GURL
& first_party_url
,
103 const net::CookieList
& cookie_list
,
104 bool blocked_by_policy
,
105 bool is_for_blocking_resource
);
107 // Called when a specific cookie in the current page was changed.
108 // |blocked_by_policy| should be true, if the cookie was blocked due to the
109 // user's content settings. In that case, this function should invoke
111 static void CookieChanged(int render_process_id
,
114 const GURL
& first_party_url
,
115 const std::string
& cookie_line
,
116 const net::CookieOptions
& options
,
117 bool blocked_by_policy
);
119 // Called when a specific Web database in the current page was accessed. If
120 // access was blocked due to the user's content settings,
121 // |blocked_by_policy| should be true, and this function should invoke
123 static void WebDatabaseAccessed(int render_process_id
,
126 const base::string16
& name
,
127 const base::string16
& display_name
,
128 bool blocked_by_policy
);
130 // Called when a specific DOM storage area in the current page was
131 // accessed. If access was blocked due to the user's content settings,
132 // |blocked_by_policy| should be true, and this function should invoke
134 static void DOMStorageAccessed(int render_process_id
,
138 bool blocked_by_policy
);
140 // Called when a specific indexed db factory in the current page was
141 // accessed. If access was blocked due to the user's content settings,
142 // |blocked_by_policy| should be true, and this function should invoke
144 static void IndexedDBAccessed(int render_process_id
,
147 const base::string16
& description
,
148 bool blocked_by_policy
);
150 // Called when a specific file system in the current page was accessed.
151 // If access was blocked due to the user's content settings,
152 // |blocked_by_policy| should be true, and this function should invoke
154 static void FileSystemAccessed(int render_process_id
,
157 bool blocked_by_policy
);
159 // Resets the |content_blocked_| and |content_allowed_| arrays, except for
160 // CONTENT_SETTINGS_TYPE_COOKIES related information.
161 // TODO(vabr): Only public for tests. Move to a test client.
162 void ClearBlockedContentSettingsExceptForCookies();
164 // Resets all cookies related information.
165 // TODO(vabr): Only public for tests. Move to a test client.
166 void ClearCookieSpecificContentSettings();
168 // Changes the |content_blocked_| entry for popups.
169 void SetPopupsBlocked(bool blocked
);
171 // Changes the |content_blocked_| entry for downloads.
172 void SetDownloadsBlocked(bool blocked
);
174 // Returns whether a particular kind of content has been blocked for this
176 bool IsContentBlocked(ContentSettingsType content_type
) const;
178 // Returns true if content blockage was indicated to the user.
179 bool IsBlockageIndicated(ContentSettingsType content_type
) const;
181 void SetBlockageHasBeenIndicated(ContentSettingsType content_type
);
183 // Returns whether a particular kind of content has been allowed. Currently
184 // only tracks cookies.
185 bool IsContentAllowed(ContentSettingsType content_type
) const;
187 const GURL
& media_stream_access_origin() const {
188 return media_stream_access_origin_
;
191 const std::string
& media_stream_requested_audio_device() const {
192 return media_stream_requested_audio_device_
;
195 const std::string
& media_stream_requested_video_device() const {
196 return media_stream_requested_video_device_
;
199 // TODO(vabr): Only public for tests. Move to a test client.
200 const std::string
& media_stream_selected_audio_device() const {
201 return media_stream_selected_audio_device_
;
204 // TODO(vabr): Only public for tests. Move to a test client.
205 const std::string
& media_stream_selected_video_device() const {
206 return media_stream_selected_video_device_
;
209 // Returns the state of the camera and microphone usage.
210 // The return value always includes all active media capture devices, on top
211 // of the devices from the last request.
212 MicrophoneCameraState
GetMicrophoneCameraState() const;
214 // Returns whether the camera or microphone permission or media device setting
215 // has changed since the last permission request.
216 bool IsMicrophoneCameraStateChanged() const;
218 // Returns the ContentSettingsUsagesState that controls the
219 // geolocation API usage on this page.
220 const ContentSettingsUsagesState
& geolocation_usages_state() const {
221 return geolocation_usages_state_
;
224 // Returns the ContentSettingsUsageState that controls the MIDI usage on
226 const ContentSettingsUsagesState
& midi_usages_state() const {
227 return midi_usages_state_
;
230 // Call to indicate that there is a protocol handler pending user approval.
231 void set_pending_protocol_handler(const ProtocolHandler
& handler
) {
232 pending_protocol_handler_
= handler
;
235 const ProtocolHandler
& pending_protocol_handler() const {
236 return pending_protocol_handler_
;
239 void ClearPendingProtocolHandler() {
240 pending_protocol_handler_
= ProtocolHandler::EmptyProtocolHandler();
243 // Sets the previous protocol handler which will be replaced by the
244 // pending protocol handler.
245 void set_previous_protocol_handler(const ProtocolHandler
& handler
) {
246 previous_protocol_handler_
= handler
;
249 const ProtocolHandler
& previous_protocol_handler() const {
250 return previous_protocol_handler_
;
253 // Set whether the setting for the pending handler is DEFAULT (ignore),
255 void set_pending_protocol_handler_setting(ContentSetting setting
) {
256 pending_protocol_handler_setting_
= setting
;
259 ContentSetting
pending_protocol_handler_setting() const {
260 return pending_protocol_handler_setting_
;
263 // Returns the |LocalSharedObjectsCounter| instances corresponding to all
264 // allowed, and blocked, respectively, local shared objects like cookies,
265 // local storage, ... .
266 const LocalSharedObjectsCounter
& allowed_local_shared_objects() const {
267 return allowed_local_shared_objects_
;
270 const LocalSharedObjectsCounter
& blocked_local_shared_objects() const {
271 return blocked_local_shared_objects_
;
274 // Creates a new copy of a CookiesTreeModel for all allowed, and blocked,
275 // respectively, local shared objects.
276 scoped_ptr
<CookiesTreeModel
> CreateAllowedCookiesTreeModel() const {
277 return allowed_local_shared_objects_
.CreateCookiesTreeModel();
280 scoped_ptr
<CookiesTreeModel
> CreateBlockedCookiesTreeModel() const {
281 return blocked_local_shared_objects_
.CreateCookiesTreeModel();
284 bool load_plugins_link_enabled() { return load_plugins_link_enabled_
; }
285 void set_load_plugins_link_enabled(bool enabled
) {
286 load_plugins_link_enabled_
= enabled
;
289 // Called to indicate whether access to the Pepper broker was allowed or
291 void SetPepperBrokerAllowed(bool allowed
);
294 // TODO(vabr): Only public for tests. Move to a test client.
295 void OnContentBlocked(ContentSettingsType type
);
296 void OnContentAllowed(ContentSettingsType type
);
298 // These methods are invoked on the UI thread by the static functions above.
299 // TODO(vabr): Only public for tests. Move to a test client.
300 void OnCookiesRead(const GURL
& url
,
301 const GURL
& first_party_url
,
302 const net::CookieList
& cookie_list
,
303 bool blocked_by_policy
);
304 void OnCookieChanged(const GURL
& url
,
305 const GURL
& first_party_url
,
306 const std::string
& cookie_line
,
307 const net::CookieOptions
& options
,
308 bool blocked_by_policy
);
309 void OnFileSystemAccessed(const GURL
& url
,
310 bool blocked_by_policy
);
311 void OnIndexedDBAccessed(const GURL
& url
,
312 const base::string16
& description
,
313 bool blocked_by_policy
);
314 void OnLocalStorageAccessed(const GURL
& url
,
316 bool blocked_by_policy
);
317 void OnWebDatabaseAccessed(const GURL
& url
,
318 const base::string16
& name
,
319 const base::string16
& display_name
,
320 bool blocked_by_policy
);
321 void OnGeolocationPermissionSet(const GURL
& requesting_frame
,
323 #if defined(OS_ANDROID)
324 void OnProtectedMediaIdentifierPermissionSet(const GURL
& requesting_frame
,
328 // This method is called to update the status about the microphone and
329 // camera stream access.
330 void OnMediaStreamPermissionSet(
331 const GURL
& request_origin
,
332 MicrophoneCameraState new_microphone_camera_state
,
333 const std::string
& media_stream_selected_audio_device
,
334 const std::string
& media_stream_selected_video_device
,
335 const std::string
& media_stream_requested_audio_device
,
336 const std::string
& media_stream_requested_video_device
);
338 // There methods are called to update the status about MIDI access.
339 void OnMidiSysExAccessed(const GURL
& reqesting_origin
);
340 void OnMidiSysExAccessBlocked(const GURL
& requesting_origin
);
342 // Adds the given |SiteDataObserver|. The |observer| is notified when a
343 // locale shared object, like for example a cookie, is accessed.
344 void AddSiteDataObserver(SiteDataObserver
* observer
);
346 // Removes the given |SiteDataObserver|.
347 void RemoveSiteDataObserver(SiteDataObserver
* observer
);
350 friend class content::WebContentsUserData
<TabSpecificContentSettings
>;
352 explicit TabSpecificContentSettings(content::WebContents
* tab
);
354 // content::WebContentsObserver overrides.
355 void RenderFrameForInterstitialPageCreated(
356 content::RenderFrameHost
* render_frame_host
) override
;
357 bool OnMessageReceived(const IPC::Message
& message
,
358 content::RenderFrameHost
* render_frame_host
) override
;
359 void DidNavigateMainFrame(
360 const content::LoadCommittedDetails
& details
,
361 const content::FrameNavigateParams
& params
) override
;
362 void DidStartProvisionalLoadForFrame(
363 content::RenderFrameHost
* render_frame_host
,
364 const GURL
& validated_url
,
366 bool is_iframe_srcdoc
) override
;
367 void AppCacheAccessed(const GURL
& manifest_url
,
368 bool blocked_by_policy
) override
;
370 // content_settings::Observer implementation.
371 void OnContentSettingChanged(const ContentSettingsPattern
& primary_pattern
,
372 const ContentSettingsPattern
& secondary_pattern
,
373 ContentSettingsType content_type
,
374 std::string resource_identifier
) override
;
376 // Notifies all registered |SiteDataObserver|s.
377 void NotifySiteDataObservers();
379 // Clears the Geolocation settings.
380 void ClearGeolocationContentSettings();
382 // Clears the MIDI settings.
383 void ClearMidiContentSettings();
385 // Updates Geolocation settings on navigation.
386 void GeolocationDidNavigate(
387 const content::LoadCommittedDetails
& details
);
389 // Updates MIDI settings on navigation.
390 void MidiDidNavigate(const content::LoadCommittedDetails
& details
);
392 // All currently registered |SiteDataObserver|s.
393 ObserverList
<SiteDataObserver
> observer_list_
;
395 // Stores which content setting types actually have blocked content.
396 bool content_blocked_
[CONTENT_SETTINGS_NUM_TYPES
];
398 // Stores if the blocked content was messaged to the user.
399 bool content_blockage_indicated_to_user_
[CONTENT_SETTINGS_NUM_TYPES
];
401 // Stores which content setting types actually were allowed.
402 bool content_allowed_
[CONTENT_SETTINGS_NUM_TYPES
];
404 // Stores the blocked/allowed cookies.
405 LocalSharedObjectsContainer allowed_local_shared_objects_
;
406 LocalSharedObjectsContainer blocked_local_shared_objects_
;
408 // Manages information about Geolocation API usage in this page.
409 ContentSettingsUsagesState geolocation_usages_state_
;
411 // Manages information about MIDI usages in this page.
412 ContentSettingsUsagesState midi_usages_state_
;
414 // The pending protocol handler, if any. This can be set if
415 // registerProtocolHandler was invoked without user gesture.
416 // The |IsEmpty| method will be true if no protocol handler is
417 // pending registration.
418 ProtocolHandler pending_protocol_handler_
;
420 // The previous protocol handler to be replaced by
421 // the pending_protocol_handler_, if there is one. Empty if
422 // there is no handler which would be replaced.
423 ProtocolHandler previous_protocol_handler_
;
425 // The setting on the pending protocol handler registration. Persisted in case
426 // the user opens the bubble and makes changes multiple times.
427 ContentSetting pending_protocol_handler_setting_
;
429 // Stores whether the user can load blocked plugins on this page.
430 bool load_plugins_link_enabled_
;
432 // The origin of the media stream request. Note that we only support handling
433 // settings for one request per tab. The latest request's origin will be
434 // stored here. http://crbug.com/259794
435 GURL media_stream_access_origin_
;
437 // The microphone and camera state at the last media stream request.
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_