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 static void CookiesRead(int render_process_id
,
101 const GURL
& first_party_url
,
102 const net::CookieList
& cookie_list
,
103 bool blocked_by_policy
);
105 // Called when a specific cookie in the current page was changed.
106 // |blocked_by_policy| should be true, if the cookie was blocked due to the
107 // user's content settings. In that case, this function should invoke
109 static void CookieChanged(int render_process_id
,
112 const GURL
& first_party_url
,
113 const std::string
& cookie_line
,
114 const net::CookieOptions
& options
,
115 bool blocked_by_policy
);
117 // Called when a specific Web database in the current page was accessed. If
118 // access was blocked due to the user's content settings,
119 // |blocked_by_policy| should be true, and this function should invoke
121 static void WebDatabaseAccessed(int render_process_id
,
124 const base::string16
& name
,
125 const base::string16
& display_name
,
126 bool blocked_by_policy
);
128 // Called when a specific DOM storage area in the current page was
129 // accessed. If access was blocked due to the user's content settings,
130 // |blocked_by_policy| should be true, and this function should invoke
132 static void DOMStorageAccessed(int render_process_id
,
136 bool blocked_by_policy
);
138 // Called when a specific indexed db factory in the current page was
139 // accessed. If access was blocked due to the user's content settings,
140 // |blocked_by_policy| should be true, and this function should invoke
142 static void IndexedDBAccessed(int render_process_id
,
145 const base::string16
& description
,
146 bool blocked_by_policy
);
148 // Called when a specific file system in the current page was accessed.
149 // If access was blocked due to the user's content settings,
150 // |blocked_by_policy| should be true, and this function should invoke
152 static void FileSystemAccessed(int render_process_id
,
155 bool blocked_by_policy
);
157 // Resets the |content_blocked_| and |content_allowed_| arrays, except for
158 // CONTENT_SETTINGS_TYPE_COOKIES related information.
159 void ClearBlockedContentSettingsExceptForCookies();
161 // Resets all cookies related information.
162 void ClearCookieSpecificContentSettings();
164 // Clears the Geolocation settings.
165 void ClearGeolocationContentSettings();
167 // Clears the MIDI settings.
168 void ClearMIDIContentSettings();
170 // Changes the |content_blocked_| entry for popups.
171 void SetPopupsBlocked(bool blocked
);
173 // Changes the |content_blocked_| entry for downloads.
174 void SetDownloadsBlocked(bool blocked
);
176 // Updates Geolocation settings on navigation.
177 void GeolocationDidNavigate(
178 const content::LoadCommittedDetails
& details
);
180 // Updates MIDI settings on navigation.
181 void MIDIDidNavigate(const content::LoadCommittedDetails
& details
);
183 // Returns whether a particular kind of content has been blocked for this
185 bool IsContentBlocked(ContentSettingsType content_type
) const;
187 // Returns true if content blockage was indicated to the user.
188 bool IsBlockageIndicated(ContentSettingsType content_type
) const;
190 void SetBlockageHasBeenIndicated(ContentSettingsType content_type
);
192 // Returns whether a particular kind of content has been allowed. Currently
193 // only tracks cookies.
194 bool IsContentAllowed(ContentSettingsType content_type
) const;
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 // Returns the state of the camera and microphone usage.
209 MicrophoneCameraState
GetMicrophoneCameraState() const;
211 // Returns the ContentSettingsUsagesState that controls the
212 // geolocation API usage on this page.
213 const ContentSettingsUsagesState
& geolocation_usages_state() const {
214 return geolocation_usages_state_
;
217 // Returns the ContentSettingsUsageState that controls the MIDI usage on
219 const ContentSettingsUsagesState
& midi_usages_state() const {
220 return midi_usages_state_
;
223 // Call to indicate that there is a protocol handler pending user approval.
224 void set_pending_protocol_handler(const ProtocolHandler
& handler
) {
225 pending_protocol_handler_
= handler
;
228 const ProtocolHandler
& pending_protocol_handler() const {
229 return pending_protocol_handler_
;
232 void ClearPendingProtocolHandler() {
233 pending_protocol_handler_
= ProtocolHandler::EmptyProtocolHandler();
236 // Sets the previous protocol handler which will be replaced by the
237 // pending protocol handler.
238 void set_previous_protocol_handler(const ProtocolHandler
& handler
) {
239 previous_protocol_handler_
= handler
;
242 const ProtocolHandler
& previous_protocol_handler() const {
243 return previous_protocol_handler_
;
246 // Set whether the setting for the pending handler is DEFAULT (ignore),
248 void set_pending_protocol_handler_setting(ContentSetting setting
) {
249 pending_protocol_handler_setting_
= setting
;
252 ContentSetting
pending_protocol_handler_setting() const {
253 return pending_protocol_handler_setting_
;
257 // Returns a pointer to the |LocalSharedObjectsContainer| that contains all
258 // allowed local shared objects like cookies, local storage, ... .
259 const LocalSharedObjectsContainer
& allowed_local_shared_objects() const {
260 return allowed_local_shared_objects_
;
263 // Returns a pointer to the |LocalSharedObjectsContainer| that contains all
264 // blocked local shared objects like cookies, local storage, ... .
265 const LocalSharedObjectsContainer
& blocked_local_shared_objects() const {
266 return blocked_local_shared_objects_
;
269 bool load_plugins_link_enabled() { return load_plugins_link_enabled_
; }
270 void set_load_plugins_link_enabled(bool enabled
) {
271 load_plugins_link_enabled_
= enabled
;
274 // Called to indicate whether access to the Pepper broker was allowed or
276 void SetPepperBrokerAllowed(bool allowed
);
278 // content::WebContentsObserver overrides.
279 virtual void RenderFrameForInterstitialPageCreated(
280 content::RenderFrameHost
* render_frame_host
) OVERRIDE
;
281 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
282 virtual void DidNavigateMainFrame(
283 const content::LoadCommittedDetails
& details
,
284 const content::FrameNavigateParams
& params
) OVERRIDE
;
285 virtual void DidStartProvisionalLoadForFrame(
287 int64 parent_frame_id
,
289 const GURL
& validated_url
,
291 bool is_iframe_srcdoc
,
292 content::RenderViewHost
* render_view_host
) OVERRIDE
;
293 virtual void AppCacheAccessed(const GURL
& manifest_url
,
294 bool blocked_by_policy
) OVERRIDE
;
296 // Message handlers. Public for testing.
297 void OnContentBlocked(ContentSettingsType type
);
298 void OnContentAllowed(ContentSettingsType type
);
300 // These methods are invoked on the UI thread by the static functions above.
301 // Public for testing.
302 void OnCookiesRead(const GURL
& url
,
303 const GURL
& first_party_url
,
304 const net::CookieList
& cookie_list
,
305 bool blocked_by_policy
);
306 void OnCookieChanged(const GURL
& url
,
307 const GURL
& first_party_url
,
308 const std::string
& cookie_line
,
309 const net::CookieOptions
& options
,
310 bool blocked_by_policy
);
311 void OnFileSystemAccessed(const GURL
& url
,
312 bool blocked_by_policy
);
313 void OnIndexedDBAccessed(const GURL
& url
,
314 const base::string16
& description
,
315 bool blocked_by_policy
);
316 void OnLocalStorageAccessed(const GURL
& url
,
318 bool blocked_by_policy
);
319 void OnWebDatabaseAccessed(const GURL
& url
,
320 const base::string16
& name
,
321 const base::string16
& display_name
,
322 bool blocked_by_policy
);
323 void OnGeolocationPermissionSet(const GURL
& requesting_frame
,
325 #if defined(OS_ANDROID)
326 void OnProtectedMediaIdentifierPermissionSet(const GURL
& requesting_frame
,
330 // This method is called to update the status about the microphone and
331 // camera stream access. |request_permissions| contains a list of requested
332 // media stream types and the permission for each type.
333 void OnMediaStreamPermissionSet(
334 const GURL
& request_origin
,
335 const MediaStreamDevicesController::MediaStreamTypeSettingsMap
&
336 request_permissions
);
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 explicit TabSpecificContentSettings(content::WebContents
* tab
);
351 friend class content::WebContentsUserData
<TabSpecificContentSettings
>;
353 // content::NotificationObserver implementation.
354 virtual void Observe(int type
,
355 const content::NotificationSource
& source
,
356 const content::NotificationDetails
& details
) OVERRIDE
;
358 // Notifies all registered |SiteDataObserver|s.
359 void NotifySiteDataObservers();
361 // All currently registered |SiteDataObserver|s.
362 ObserverList
<SiteDataObserver
> observer_list_
;
364 // Stores which content setting types actually have blocked content.
365 bool content_blocked_
[CONTENT_SETTINGS_NUM_TYPES
];
367 // Stores if the blocked content was messaged to the user.
368 bool content_blockage_indicated_to_user_
[CONTENT_SETTINGS_NUM_TYPES
];
370 // Stores which content setting types actually were allowed.
371 bool content_allowed_
[CONTENT_SETTINGS_NUM_TYPES
];
373 // The profile of the tab.
376 // Stores the blocked/allowed cookies.
377 LocalSharedObjectsContainer allowed_local_shared_objects_
;
378 LocalSharedObjectsContainer blocked_local_shared_objects_
;
380 // Manages information about Geolocation API usage in this page.
381 ContentSettingsUsagesState geolocation_usages_state_
;
383 // Manages information about MIDI usages in this page.
384 ContentSettingsUsagesState midi_usages_state_
;
386 // The pending protocol handler, if any. This can be set if
387 // registerProtocolHandler was invoked without user gesture.
388 // The |IsEmpty| method will be true if no protocol handler is
389 // pending registration.
390 ProtocolHandler pending_protocol_handler_
;
392 // The previous protocol handler to be replaced by
393 // the pending_protocol_handler_, if there is one. Empty if
394 // there is no handler which would be replaced.
395 ProtocolHandler previous_protocol_handler_
;
397 // The setting on the pending protocol handler registration. Persisted in case
398 // the user opens the bubble and makes changes multiple times.
399 ContentSetting pending_protocol_handler_setting_
;
401 // Stores whether the user can load blocked plugins on this page.
402 bool load_plugins_link_enabled_
;
404 content::NotificationRegistrar registrar_
;
406 // The origin of the media stream request. Note that we only support handling
407 // settings for one request per tab. The latest request's origin will be
408 // stored here. http://crbug.com/259794
409 GURL media_stream_access_origin_
;
411 // The devices to be displayed in the media bubble when the media stream
412 // request is requesting certain specific devices.
413 std::string media_stream_requested_audio_device_
;
414 std::string media_stream_requested_video_device_
;
416 DISALLOW_COPY_AND_ASSIGN(TabSpecificContentSettings
);
419 #endif // CHROME_BROWSER_CONTENT_SETTINGS_TAB_SPECIFIC_CONTENT_SETTINGS_H_