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/local_shared_objects_container.h"
17 #include "chrome/common/custom_handlers/protocol_handler.h"
18 #include "components/content_settings/core/browser/content_settings_observer.h"
19 #include "components/content_settings/core/browser/content_settings_usages_state.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 RenderFrameHost ids.
87 static TabSpecificContentSettings
* GetForFrame(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 static void CookiesRead(int render_process_id
,
98 const GURL
& first_party_url
,
99 const net::CookieList
& cookie_list
,
100 bool blocked_by_policy
);
102 // Called when a specific cookie in the current page was changed.
103 // |blocked_by_policy| should be true, if the cookie was blocked due to the
104 // user's content settings. In that case, this function should invoke
106 static void CookieChanged(int render_process_id
,
109 const GURL
& first_party_url
,
110 const std::string
& cookie_line
,
111 const net::CookieOptions
& options
,
112 bool blocked_by_policy
);
114 // Called when a specific Web database in the current page was accessed. If
115 // access was blocked due to the user's content settings,
116 // |blocked_by_policy| should be true, and this function should invoke
118 static void WebDatabaseAccessed(int render_process_id
,
121 const base::string16
& name
,
122 const base::string16
& display_name
,
123 bool blocked_by_policy
);
125 // Called when a specific DOM storage area in the current page was
126 // accessed. If access was blocked due to the user's content settings,
127 // |blocked_by_policy| should be true, and this function should invoke
129 static void DOMStorageAccessed(int render_process_id
,
133 bool blocked_by_policy
);
135 // Called when a specific indexed db factory in the current page was
136 // accessed. If access was blocked due to the user's content settings,
137 // |blocked_by_policy| should be true, and this function should invoke
139 static void IndexedDBAccessed(int render_process_id
,
142 const base::string16
& description
,
143 bool blocked_by_policy
);
145 // Called when a specific file system in the current page was accessed.
146 // If access was blocked due to the user's content settings,
147 // |blocked_by_policy| should be true, and this function should invoke
149 static void FileSystemAccessed(int render_process_id
,
152 bool blocked_by_policy
);
154 // Called when a specific Service Worker scope 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 ServiceWorkerAccessed(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 // TODO(vabr): Only public for tests. Move to a test client.
166 void ClearBlockedContentSettingsExceptForCookies();
168 // Resets all cookies related information.
169 // TODO(vabr): Only public for tests. Move to a test client.
170 void ClearCookieSpecificContentSettings();
172 // Changes the |content_blocked_| entry for popups.
173 void SetPopupsBlocked(bool blocked
);
175 // Changes the |content_blocked_| entry for downloads.
176 void SetDownloadsBlocked(bool blocked
);
178 // Returns whether a particular kind of content has been blocked for this
180 bool IsContentBlocked(ContentSettingsType content_type
) const;
182 // Returns true if content blockage was indicated to the user.
183 bool IsBlockageIndicated(ContentSettingsType content_type
) const;
185 void SetBlockageHasBeenIndicated(ContentSettingsType content_type
);
187 // Returns whether a particular kind of content has been allowed. Currently
188 // only tracks cookies.
189 bool IsContentAllowed(ContentSettingsType content_type
) const;
191 // Returns the names of plugins that have been blocked for this tab.
192 const std::vector
<base::string16
>& blocked_plugin_names() const {
193 return blocked_plugin_names_
;
196 const GURL
& media_stream_access_origin() const {
197 return media_stream_access_origin_
;
200 const std::string
& media_stream_requested_audio_device() const {
201 return media_stream_requested_audio_device_
;
204 const std::string
& media_stream_requested_video_device() const {
205 return media_stream_requested_video_device_
;
208 // TODO(vabr): Only public for tests. Move to a test client.
209 const std::string
& media_stream_selected_audio_device() const {
210 return media_stream_selected_audio_device_
;
213 // TODO(vabr): Only public for tests. Move to a test client.
214 const std::string
& media_stream_selected_video_device() const {
215 return media_stream_selected_video_device_
;
218 // Returns the state of the camera and microphone usage.
219 // The return value always includes all active media capture devices, on top
220 // of the devices from the last request.
221 MicrophoneCameraState
GetMicrophoneCameraState() const;
223 // Returns whether the camera or microphone permission or media device setting
224 // has changed since the last permission request.
225 bool IsMicrophoneCameraStateChanged() const;
227 // Returns the ContentSettingsUsagesState that controls the
228 // geolocation API usage on this page.
229 const ContentSettingsUsagesState
& geolocation_usages_state() const {
230 return geolocation_usages_state_
;
233 // Returns the ContentSettingsUsageState that controls the MIDI usage on
235 const ContentSettingsUsagesState
& midi_usages_state() const {
236 return midi_usages_state_
;
239 // Call to indicate that there is a protocol handler pending user approval.
240 void set_pending_protocol_handler(const ProtocolHandler
& handler
) {
241 pending_protocol_handler_
= handler
;
244 const ProtocolHandler
& pending_protocol_handler() const {
245 return pending_protocol_handler_
;
248 void ClearPendingProtocolHandler() {
249 pending_protocol_handler_
= ProtocolHandler::EmptyProtocolHandler();
252 // Sets the previous protocol handler which will be replaced by the
253 // pending protocol handler.
254 void set_previous_protocol_handler(const ProtocolHandler
& handler
) {
255 previous_protocol_handler_
= handler
;
258 const ProtocolHandler
& previous_protocol_handler() const {
259 return previous_protocol_handler_
;
262 // Set whether the setting for the pending handler is DEFAULT (ignore),
264 void set_pending_protocol_handler_setting(ContentSetting setting
) {
265 pending_protocol_handler_setting_
= setting
;
268 ContentSetting
pending_protocol_handler_setting() const {
269 return pending_protocol_handler_setting_
;
272 // Returns the |LocalSharedObjectsCounter| instances corresponding to all
273 // allowed, and blocked, respectively, local shared objects like cookies,
274 // local storage, ... .
275 const LocalSharedObjectsCounter
& allowed_local_shared_objects() const {
276 return allowed_local_shared_objects_
;
279 const LocalSharedObjectsCounter
& blocked_local_shared_objects() const {
280 return blocked_local_shared_objects_
;
283 // Creates a new copy of a CookiesTreeModel for all allowed, and blocked,
284 // respectively, local shared objects.
285 scoped_ptr
<CookiesTreeModel
> CreateAllowedCookiesTreeModel() const {
286 return allowed_local_shared_objects_
.CreateCookiesTreeModel();
289 scoped_ptr
<CookiesTreeModel
> CreateBlockedCookiesTreeModel() const {
290 return blocked_local_shared_objects_
.CreateCookiesTreeModel();
293 bool load_plugins_link_enabled() { return load_plugins_link_enabled_
; }
294 void set_load_plugins_link_enabled(bool enabled
) {
295 load_plugins_link_enabled_
= enabled
;
298 // Called to indicate whether access to the Pepper broker was allowed or
300 void SetPepperBrokerAllowed(bool allowed
);
303 // TODO(vabr): Only public for tests. Move to a test client.
304 void OnContentBlocked(ContentSettingsType type
);
305 void OnContentBlockedWithDetail(ContentSettingsType type
,
306 const base::string16
& details
);
307 void OnContentAllowed(ContentSettingsType type
);
309 // These methods are invoked on the UI thread by the static functions above.
310 // TODO(vabr): Only public for tests. Move to a test client.
311 void OnCookiesRead(const GURL
& url
,
312 const GURL
& first_party_url
,
313 const net::CookieList
& cookie_list
,
314 bool blocked_by_policy
);
315 void OnCookieChanged(const GURL
& url
,
316 const GURL
& first_party_url
,
317 const std::string
& cookie_line
,
318 const net::CookieOptions
& options
,
319 bool blocked_by_policy
);
320 void OnFileSystemAccessed(const GURL
& url
,
321 bool blocked_by_policy
);
322 void OnIndexedDBAccessed(const GURL
& url
,
323 const base::string16
& description
,
324 bool blocked_by_policy
);
325 void OnLocalStorageAccessed(const GURL
& url
,
327 bool blocked_by_policy
);
328 void OnServiceWorkerAccessed(const GURL
& scope
, bool blocked_by_policy
);
329 void OnWebDatabaseAccessed(const GURL
& url
,
330 const base::string16
& name
,
331 const base::string16
& display_name
,
332 bool blocked_by_policy
);
333 void OnGeolocationPermissionSet(const GURL
& requesting_frame
,
335 #if defined(OS_ANDROID) || defined(OS_CHROMEOS)
336 void OnProtectedMediaIdentifierPermissionSet(const GURL
& requesting_frame
,
340 // This method is called to update the status about the microphone and
341 // camera stream access.
342 void OnMediaStreamPermissionSet(
343 const GURL
& request_origin
,
344 MicrophoneCameraState new_microphone_camera_state
,
345 const std::string
& media_stream_selected_audio_device
,
346 const std::string
& media_stream_selected_video_device
,
347 const std::string
& media_stream_requested_audio_device
,
348 const std::string
& media_stream_requested_video_device
);
350 // There methods are called to update the status about MIDI access.
351 void OnMidiSysExAccessed(const GURL
& reqesting_origin
);
352 void OnMidiSysExAccessBlocked(const GURL
& requesting_origin
);
354 // Adds the given |SiteDataObserver|. The |observer| is notified when a
355 // locale shared object, like for example a cookie, is accessed.
356 void AddSiteDataObserver(SiteDataObserver
* observer
);
358 // Removes the given |SiteDataObserver|.
359 void RemoveSiteDataObserver(SiteDataObserver
* observer
);
362 friend class content::WebContentsUserData
<TabSpecificContentSettings
>;
364 explicit TabSpecificContentSettings(content::WebContents
* tab
);
366 // content::WebContentsObserver overrides.
367 void RenderFrameForInterstitialPageCreated(
368 content::RenderFrameHost
* render_frame_host
) override
;
369 bool OnMessageReceived(const IPC::Message
& message
,
370 content::RenderFrameHost
* render_frame_host
) override
;
371 void DidNavigateMainFrame(
372 const content::LoadCommittedDetails
& details
,
373 const content::FrameNavigateParams
& params
) override
;
374 void DidStartProvisionalLoadForFrame(
375 content::RenderFrameHost
* render_frame_host
,
376 const GURL
& validated_url
,
378 bool is_iframe_srcdoc
) override
;
379 void AppCacheAccessed(const GURL
& manifest_url
,
380 bool blocked_by_policy
) override
;
382 // content_settings::Observer implementation.
383 void OnContentSettingChanged(const ContentSettingsPattern
& primary_pattern
,
384 const ContentSettingsPattern
& secondary_pattern
,
385 ContentSettingsType content_type
,
386 std::string resource_identifier
) override
;
388 // Notifies all registered |SiteDataObserver|s.
389 void NotifySiteDataObservers();
391 // Clears the Geolocation settings.
392 void ClearGeolocationContentSettings();
394 // Clears the MIDI settings.
395 void ClearMidiContentSettings();
397 // Updates Geolocation settings on navigation.
398 void GeolocationDidNavigate(
399 const content::LoadCommittedDetails
& details
);
401 // Updates MIDI settings on navigation.
402 void MidiDidNavigate(const content::LoadCommittedDetails
& details
);
404 // All currently registered |SiteDataObserver|s.
405 base::ObserverList
<SiteDataObserver
> observer_list_
;
407 // Stores which content setting types actually have blocked content.
408 bool content_blocked_
[CONTENT_SETTINGS_NUM_TYPES
];
410 // Stores if the blocked content was messaged to the user.
411 bool content_blockage_indicated_to_user_
[CONTENT_SETTINGS_NUM_TYPES
];
413 // Stores which content setting types actually were allowed.
414 bool content_allowed_
[CONTENT_SETTINGS_NUM_TYPES
];
416 // Stores the blocked/allowed cookies.
417 LocalSharedObjectsContainer allowed_local_shared_objects_
;
418 LocalSharedObjectsContainer blocked_local_shared_objects_
;
420 // Manages information about Geolocation API usage in this page.
421 ContentSettingsUsagesState geolocation_usages_state_
;
423 // Manages information about MIDI usages in this page.
424 ContentSettingsUsagesState midi_usages_state_
;
426 // The pending protocol handler, if any. This can be set if
427 // registerProtocolHandler was invoked without user gesture.
428 // The |IsEmpty| method will be true if no protocol handler is
429 // pending registration.
430 ProtocolHandler pending_protocol_handler_
;
432 // The previous protocol handler to be replaced by
433 // the pending_protocol_handler_, if there is one. Empty if
434 // there is no handler which would be replaced.
435 ProtocolHandler previous_protocol_handler_
;
437 // The setting on the pending protocol handler registration. Persisted in case
438 // the user opens the bubble and makes changes multiple times.
439 ContentSetting pending_protocol_handler_setting_
;
441 // The name(s) of the plugin(s) being blocked.
442 std::vector
<base::string16
> blocked_plugin_names_
;
444 // Stores whether the user can load blocked plugins on this page.
445 bool load_plugins_link_enabled_
;
447 // The origin of the media stream request. Note that we only support handling
448 // settings for one request per tab. The latest request's origin will be
449 // stored here. http://crbug.com/259794
450 GURL media_stream_access_origin_
;
452 // The microphone and camera state at the last media stream request.
453 MicrophoneCameraState microphone_camera_state_
;
454 // The selected devices at the last media stream request.
455 std::string media_stream_selected_audio_device_
;
456 std::string media_stream_selected_video_device_
;
458 // The devices to be displayed in the media bubble when the media stream
459 // request is requesting certain specific devices.
460 std::string media_stream_requested_audio_device_
;
461 std::string media_stream_requested_video_device_
;
463 // Observer to watch for content settings changed.
464 ScopedObserver
<HostContentSettingsMap
, content_settings::Observer
> observer_
;
466 DISALLOW_COPY_AND_ASSIGN(TabSpecificContentSettings
);
469 #endif // CHROME_BROWSER_CONTENT_SETTINGS_TAB_SPECIFIC_CONTENT_SETTINGS_H_