Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / content_settings / tab_specific_content_settings.h
blob4be7a40d7e89b9f060032df42989e0db734918fc
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_
8 #include <map>
9 #include <string>
11 #include "base/basictypes.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/observer_list.h"
15 #include "base/scoped_observer.h"
16 #include "chrome/browser/browsing_data/cookies_tree_model.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/browser/content_settings_usages_state.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 "net/cookies/canonical_cookie.h"
27 class HostContentSettingsMap;
29 namespace content {
30 class RenderViewHost;
33 namespace net {
34 class CookieOptions;
37 // This class manages state about permissions, content settings, cookies and
38 // site data for a specific WebContents. It tracks which content was accessed
39 // and which content was blocked. Based on this it provides information about
40 // which types of content were accessed and blocked.
41 class TabSpecificContentSettings
42 : public content::WebContentsObserver,
43 public content_settings::Observer,
44 public content::WebContentsUserData<TabSpecificContentSettings> {
45 public:
46 // Fields describing the current mic/camera state. If a page has attempted to
47 // access a device, the XXX_ACCESSED bit will be set. If access was blocked,
48 // XXX_BLOCKED will be set.
49 enum MicrophoneCameraStateFlags {
50 MICROPHONE_CAMERA_NOT_ACCESSED = 0,
51 MICROPHONE_ACCESSED = 1 << 0,
52 MICROPHONE_BLOCKED = 1 << 1,
53 CAMERA_ACCESSED = 1 << 2,
54 CAMERA_BLOCKED = 1 << 3,
56 // Use signed int, that's what the enum flags implicitly convert to.
57 typedef int32_t MicrophoneCameraState;
59 // Classes that want to be notified about site data events must implement
60 // this abstract class and add themselves as observer to the
61 // |TabSpecificContentSettings|.
62 class SiteDataObserver {
63 public:
64 explicit SiteDataObserver(
65 TabSpecificContentSettings* tab_specific_content_settings);
66 virtual ~SiteDataObserver();
68 // Called whenever site data is accessed.
69 virtual void OnSiteDataAccessed() = 0;
71 TabSpecificContentSettings* tab_specific_content_settings() {
72 return tab_specific_content_settings_;
75 // Called when the TabSpecificContentSettings is destroyed; nulls out
76 // the local reference.
77 void ContentSettingsDestroyed();
79 private:
80 TabSpecificContentSettings* tab_specific_content_settings_;
82 DISALLOW_COPY_AND_ASSIGN(SiteDataObserver);
85 ~TabSpecificContentSettings() override;
87 // Returns the object given a RenderFrameHost ids.
88 static TabSpecificContentSettings* GetForFrame(int render_process_id,
89 int render_frame_id);
91 // Static methods called on the UI threads.
92 // Called when cookies for the given URL were read either from within the
93 // current page or while loading it. |blocked_by_policy| should be true, if
94 // reading cookies was blocked due to the user's content settings. In that
95 // case, this function should invoke OnContentBlocked.
96 static void CookiesRead(int render_process_id,
97 int render_frame_id,
98 const GURL& url,
99 const GURL& first_party_url,
100 const net::CookieList& cookie_list,
101 bool blocked_by_policy);
103 // Called when a specific cookie in the current page was changed.
104 // |blocked_by_policy| should be true, if the cookie was blocked due to the
105 // user's content settings. In that case, this function should invoke
106 // OnContentBlocked.
107 static void CookieChanged(int render_process_id,
108 int render_frame_id,
109 const GURL& url,
110 const GURL& first_party_url,
111 const std::string& cookie_line,
112 const net::CookieOptions& options,
113 bool blocked_by_policy);
115 // Called when a specific Web database in the current page was accessed. If
116 // access was blocked due to the user's content settings,
117 // |blocked_by_policy| should be true, and this function should invoke
118 // OnContentBlocked.
119 static void WebDatabaseAccessed(int render_process_id,
120 int render_frame_id,
121 const GURL& url,
122 const base::string16& name,
123 const base::string16& display_name,
124 bool blocked_by_policy);
126 // Called when a specific DOM storage area in the current page was
127 // accessed. If access was blocked due to the user's content settings,
128 // |blocked_by_policy| should be true, and this function should invoke
129 // OnContentBlocked.
130 static void DOMStorageAccessed(int render_process_id,
131 int render_frame_id,
132 const GURL& url,
133 bool local,
134 bool blocked_by_policy);
136 // Called when a specific indexed db factory in the current page was
137 // accessed. If access was blocked due to the user's content settings,
138 // |blocked_by_policy| should be true, and this function should invoke
139 // OnContentBlocked.
140 static void IndexedDBAccessed(int render_process_id,
141 int render_frame_id,
142 const GURL& url,
143 const base::string16& description,
144 bool blocked_by_policy);
146 // Called when a specific file system in the current page was accessed.
147 // If access was blocked due to the user's content settings,
148 // |blocked_by_policy| should be true, and this function should invoke
149 // OnContentBlocked.
150 static void FileSystemAccessed(int render_process_id,
151 int render_frame_id,
152 const GURL& url,
153 bool blocked_by_policy);
155 // Called when a specific Service Worker scope was accessed.
156 // If access was blocked due to the user's content settings,
157 // |blocked_by_policy| should be true, and this function should invoke
158 // OnContentBlocked.
159 static void ServiceWorkerAccessed(int render_process_id,
160 int render_frame_id,
161 const GURL& scope,
162 bool blocked_by_policy);
164 // Resets the |content_blocked_| and |content_allowed_| arrays, except for
165 // CONTENT_SETTINGS_TYPE_COOKIES related information.
166 // TODO(vabr): Only public for tests. Move to a test client.
167 void ClearBlockedContentSettingsExceptForCookies();
169 // Resets all cookies related information.
170 // TODO(vabr): Only public for tests. Move to a test client.
171 void ClearCookieSpecificContentSettings();
173 // Changes the |content_blocked_| entry for popups.
174 void SetPopupsBlocked(bool blocked);
176 // Changes the |content_blocked_| entry for downloads.
177 void SetDownloadsBlocked(bool blocked);
179 // Returns whether a particular kind of content has been blocked for this
180 // page.
181 bool IsContentBlocked(ContentSettingsType content_type) const;
183 // Returns true if content blockage was indicated to the user.
184 bool IsBlockageIndicated(ContentSettingsType content_type) const;
186 void SetBlockageHasBeenIndicated(ContentSettingsType content_type);
188 // Returns whether a particular kind of content has been allowed. Currently
189 // only tracks cookies.
190 bool IsContentAllowed(ContentSettingsType content_type) const;
192 // Returns the names of plugins that have been blocked for this tab.
193 const std::vector<base::string16>& blocked_plugin_names() const {
194 return blocked_plugin_names_;
197 const GURL& media_stream_access_origin() const {
198 return media_stream_access_origin_;
201 const std::string& media_stream_requested_audio_device() const {
202 return media_stream_requested_audio_device_;
205 const std::string& media_stream_requested_video_device() const {
206 return media_stream_requested_video_device_;
209 // TODO(vabr): Only public for tests. Move to a test client.
210 const std::string& media_stream_selected_audio_device() const {
211 return media_stream_selected_audio_device_;
214 // TODO(vabr): Only public for tests. Move to a test client.
215 const std::string& media_stream_selected_video_device() const {
216 return media_stream_selected_video_device_;
219 // Returns the state of the camera and microphone usage.
220 // The return value always includes all active media capture devices, on top
221 // of the devices from the last request.
222 MicrophoneCameraState GetMicrophoneCameraState() const;
224 // Returns whether the camera or microphone permission or media device setting
225 // has changed since the last permission request.
226 bool IsMicrophoneCameraStateChanged() const;
228 // Returns the ContentSettingsUsagesState that controls the
229 // geolocation API usage on this page.
230 const ContentSettingsUsagesState& geolocation_usages_state() const {
231 return geolocation_usages_state_;
234 // Returns the ContentSettingsUsageState that controls the MIDI usage on
235 // this page.
236 const ContentSettingsUsagesState& midi_usages_state() const {
237 return midi_usages_state_;
240 // Call to indicate that there is a protocol handler pending user approval.
241 void set_pending_protocol_handler(const ProtocolHandler& handler) {
242 pending_protocol_handler_ = handler;
245 const ProtocolHandler& pending_protocol_handler() const {
246 return pending_protocol_handler_;
249 void ClearPendingProtocolHandler() {
250 pending_protocol_handler_ = ProtocolHandler::EmptyProtocolHandler();
253 // Sets the previous protocol handler which will be replaced by the
254 // pending protocol handler.
255 void set_previous_protocol_handler(const ProtocolHandler& handler) {
256 previous_protocol_handler_ = handler;
259 const ProtocolHandler& previous_protocol_handler() const {
260 return previous_protocol_handler_;
263 // Set whether the setting for the pending handler is DEFAULT (ignore),
264 // ALLOW, or DENY.
265 void set_pending_protocol_handler_setting(ContentSetting setting) {
266 pending_protocol_handler_setting_ = setting;
269 ContentSetting pending_protocol_handler_setting() const {
270 return pending_protocol_handler_setting_;
273 // Returns the |LocalSharedObjectsCounter| instances corresponding to all
274 // allowed, and blocked, respectively, local shared objects like cookies,
275 // local storage, ... .
276 const LocalSharedObjectsCounter& allowed_local_shared_objects() const {
277 return allowed_local_shared_objects_;
280 const LocalSharedObjectsCounter& blocked_local_shared_objects() const {
281 return blocked_local_shared_objects_;
284 // Creates a new copy of a CookiesTreeModel for all allowed, and blocked,
285 // respectively, local shared objects.
286 scoped_ptr<CookiesTreeModel> CreateAllowedCookiesTreeModel() const {
287 return allowed_local_shared_objects_.CreateCookiesTreeModel();
290 scoped_ptr<CookiesTreeModel> CreateBlockedCookiesTreeModel() const {
291 return blocked_local_shared_objects_.CreateCookiesTreeModel();
294 bool load_plugins_link_enabled() { return load_plugins_link_enabled_; }
295 void set_load_plugins_link_enabled(bool enabled) {
296 load_plugins_link_enabled_ = enabled;
299 // Called to indicate whether access to the Pepper broker was allowed or
300 // blocked.
301 void SetPepperBrokerAllowed(bool allowed);
303 // Message handlers.
304 // TODO(vabr): Only public for tests. Move to a test client.
305 void OnContentBlocked(ContentSettingsType type);
306 void OnContentBlockedWithDetail(ContentSettingsType type,
307 const base::string16& details);
308 void OnContentAllowed(ContentSettingsType type);
310 // These methods are invoked on the UI thread by the static functions above.
311 // TODO(vabr): Only public for tests. Move to a test client.
312 void OnCookiesRead(const GURL& url,
313 const GURL& first_party_url,
314 const net::CookieList& cookie_list,
315 bool blocked_by_policy);
316 void OnCookieChanged(const GURL& url,
317 const GURL& first_party_url,
318 const std::string& cookie_line,
319 const net::CookieOptions& options,
320 bool blocked_by_policy);
321 void OnFileSystemAccessed(const GURL& url,
322 bool blocked_by_policy);
323 void OnIndexedDBAccessed(const GURL& url,
324 const base::string16& description,
325 bool blocked_by_policy);
326 void OnLocalStorageAccessed(const GURL& url,
327 bool local,
328 bool blocked_by_policy);
329 void OnServiceWorkerAccessed(const GURL& scope, bool blocked_by_policy);
330 void OnWebDatabaseAccessed(const GURL& url,
331 const base::string16& name,
332 const base::string16& display_name,
333 bool blocked_by_policy);
334 void OnGeolocationPermissionSet(const GURL& requesting_frame,
335 bool allowed);
336 #if defined(OS_ANDROID) || defined(OS_CHROMEOS)
337 void OnProtectedMediaIdentifierPermissionSet(const GURL& requesting_frame,
338 bool allowed);
339 #endif
341 // This method is called to update the status about the microphone and
342 // camera stream access.
343 void OnMediaStreamPermissionSet(
344 const GURL& request_origin,
345 MicrophoneCameraState new_microphone_camera_state,
346 const std::string& media_stream_selected_audio_device,
347 const std::string& media_stream_selected_video_device,
348 const std::string& media_stream_requested_audio_device,
349 const std::string& media_stream_requested_video_device);
351 // There methods are called to update the status about MIDI access.
352 void OnMidiSysExAccessed(const GURL& reqesting_origin);
353 void OnMidiSysExAccessBlocked(const GURL& requesting_origin);
355 // Adds the given |SiteDataObserver|. The |observer| is notified when a
356 // locale shared object, like for example a cookie, is accessed.
357 void AddSiteDataObserver(SiteDataObserver* observer);
359 // Removes the given |SiteDataObserver|.
360 void RemoveSiteDataObserver(SiteDataObserver* observer);
362 private:
363 friend class content::WebContentsUserData<TabSpecificContentSettings>;
365 explicit TabSpecificContentSettings(content::WebContents* tab);
367 // content::WebContentsObserver overrides.
368 void RenderFrameForInterstitialPageCreated(
369 content::RenderFrameHost* render_frame_host) override;
370 bool OnMessageReceived(const IPC::Message& message,
371 content::RenderFrameHost* render_frame_host) override;
372 void DidNavigateMainFrame(
373 const content::LoadCommittedDetails& details,
374 const content::FrameNavigateParams& params) override;
375 void DidStartProvisionalLoadForFrame(
376 content::RenderFrameHost* render_frame_host,
377 const GURL& validated_url,
378 bool is_error_page,
379 bool is_iframe_srcdoc) override;
380 void AppCacheAccessed(const GURL& manifest_url,
381 bool blocked_by_policy) override;
383 // content_settings::Observer implementation.
384 void OnContentSettingChanged(const ContentSettingsPattern& primary_pattern,
385 const ContentSettingsPattern& secondary_pattern,
386 ContentSettingsType content_type,
387 std::string resource_identifier) override;
389 // Notifies all registered |SiteDataObserver|s.
390 void NotifySiteDataObservers();
392 // Clears the Geolocation settings.
393 void ClearGeolocationContentSettings();
395 // Clears the MIDI settings.
396 void ClearMidiContentSettings();
398 // Updates Geolocation settings on navigation.
399 void GeolocationDidNavigate(
400 const content::LoadCommittedDetails& details);
402 // Updates MIDI settings on navigation.
403 void MidiDidNavigate(const content::LoadCommittedDetails& details);
405 // All currently registered |SiteDataObserver|s.
406 base::ObserverList<SiteDataObserver> observer_list_;
408 struct ContentSettingsStatus {
409 bool blocked;
410 bool blockage_indicated_to_user;
411 bool allowed;
413 // Stores which content setting types actually have blocked content.
414 std::map<ContentSettingsType, ContentSettingsStatus> content_settings_status_;
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_