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