Add more checks to investigate SupervisedUserPrefStore crash at startup.
[chromium-blink-merge.git] / chrome / browser / content_settings / tab_specific_content_settings.h
blobcc2e20010ffbddcbb9dc78747b372e083fa9cdfa
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 <string>
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;
28 namespace content {
29 class RenderViewHost;
32 namespace net {
33 class CookieOptions;
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> {
44 public:
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 {
62 public:
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();
78 private:
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,
88 int render_view_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,
100 int render_frame_id,
101 const GURL& url,
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
110 // OnContentBlocked.
111 static void CookieChanged(int render_process_id,
112 int render_frame_id,
113 const GURL& url,
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
122 // OnContentBlocked.
123 static void WebDatabaseAccessed(int render_process_id,
124 int render_frame_id,
125 const GURL& url,
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
133 // OnContentBlocked.
134 static void DOMStorageAccessed(int render_process_id,
135 int render_frame_id,
136 const GURL& url,
137 bool local,
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
143 // OnContentBlocked.
144 static void IndexedDBAccessed(int render_process_id,
145 int render_frame_id,
146 const GURL& url,
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
153 // OnContentBlocked.
154 static void FileSystemAccessed(int render_process_id,
155 int render_frame_id,
156 const GURL& url,
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
175 // page.
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 // Returns the names of plugins that have been blocked for this tab.
188 const base::string16 GetBlockedPluginNames() const;
190 const GURL& media_stream_access_origin() const {
191 return media_stream_access_origin_;
194 const std::string& media_stream_requested_audio_device() const {
195 return media_stream_requested_audio_device_;
198 const std::string& media_stream_requested_video_device() const {
199 return media_stream_requested_video_device_;
202 // TODO(vabr): Only public for tests. Move to a test client.
203 const std::string& media_stream_selected_audio_device() const {
204 return media_stream_selected_audio_device_;
207 // TODO(vabr): Only public for tests. Move to a test client.
208 const std::string& media_stream_selected_video_device() const {
209 return media_stream_selected_video_device_;
212 // Returns the state of the camera and microphone usage.
213 // The return value always includes all active media capture devices, on top
214 // of the devices from the last request.
215 MicrophoneCameraState GetMicrophoneCameraState() const;
217 // Returns whether the camera or microphone permission or media device setting
218 // has changed since the last permission request.
219 bool IsMicrophoneCameraStateChanged() const;
221 // Returns the ContentSettingsUsagesState that controls the
222 // geolocation API usage on this page.
223 const ContentSettingsUsagesState& geolocation_usages_state() const {
224 return geolocation_usages_state_;
227 // Returns the ContentSettingsUsageState that controls the MIDI usage on
228 // this page.
229 const ContentSettingsUsagesState& midi_usages_state() const {
230 return midi_usages_state_;
233 // Call to indicate that there is a protocol handler pending user approval.
234 void set_pending_protocol_handler(const ProtocolHandler& handler) {
235 pending_protocol_handler_ = handler;
238 const ProtocolHandler& pending_protocol_handler() const {
239 return pending_protocol_handler_;
242 void ClearPendingProtocolHandler() {
243 pending_protocol_handler_ = ProtocolHandler::EmptyProtocolHandler();
246 // Sets the previous protocol handler which will be replaced by the
247 // pending protocol handler.
248 void set_previous_protocol_handler(const ProtocolHandler& handler) {
249 previous_protocol_handler_ = handler;
252 const ProtocolHandler& previous_protocol_handler() const {
253 return previous_protocol_handler_;
256 // Set whether the setting for the pending handler is DEFAULT (ignore),
257 // ALLOW, or DENY.
258 void set_pending_protocol_handler_setting(ContentSetting setting) {
259 pending_protocol_handler_setting_ = setting;
262 ContentSetting pending_protocol_handler_setting() const {
263 return pending_protocol_handler_setting_;
266 // Returns the |LocalSharedObjectsCounter| instances corresponding to all
267 // allowed, and blocked, respectively, local shared objects like cookies,
268 // local storage, ... .
269 const LocalSharedObjectsCounter& allowed_local_shared_objects() const {
270 return allowed_local_shared_objects_;
273 const LocalSharedObjectsCounter& blocked_local_shared_objects() const {
274 return blocked_local_shared_objects_;
277 // Creates a new copy of a CookiesTreeModel for all allowed, and blocked,
278 // respectively, local shared objects.
279 scoped_ptr<CookiesTreeModel> CreateAllowedCookiesTreeModel() const {
280 return allowed_local_shared_objects_.CreateCookiesTreeModel();
283 scoped_ptr<CookiesTreeModel> CreateBlockedCookiesTreeModel() const {
284 return blocked_local_shared_objects_.CreateCookiesTreeModel();
287 bool load_plugins_link_enabled() { return load_plugins_link_enabled_; }
288 void set_load_plugins_link_enabled(bool enabled) {
289 load_plugins_link_enabled_ = enabled;
292 // Called to indicate whether access to the Pepper broker was allowed or
293 // blocked.
294 void SetPepperBrokerAllowed(bool allowed);
296 // Message handlers.
297 // TODO(vabr): Only public for tests. Move to a test client.
298 void OnContentBlocked(ContentSettingsType type);
299 void OnContentBlockedWithDetail(ContentSettingsType type,
300 const base::string16& details);
301 void OnContentAllowed(ContentSettingsType type);
303 // These methods are invoked on the UI thread by the static functions above.
304 // TODO(vabr): Only public for tests. Move to a test client.
305 void OnCookiesRead(const GURL& url,
306 const GURL& first_party_url,
307 const net::CookieList& cookie_list,
308 bool blocked_by_policy);
309 void OnCookieChanged(const GURL& url,
310 const GURL& first_party_url,
311 const std::string& cookie_line,
312 const net::CookieOptions& options,
313 bool blocked_by_policy);
314 void OnFileSystemAccessed(const GURL& url,
315 bool blocked_by_policy);
316 void OnIndexedDBAccessed(const GURL& url,
317 const base::string16& description,
318 bool blocked_by_policy);
319 void OnLocalStorageAccessed(const GURL& url,
320 bool local,
321 bool blocked_by_policy);
322 void OnWebDatabaseAccessed(const GURL& url,
323 const base::string16& name,
324 const base::string16& display_name,
325 bool blocked_by_policy);
326 void OnGeolocationPermissionSet(const GURL& requesting_frame,
327 bool allowed);
328 #if defined(OS_ANDROID) || defined(OS_CHROMEOS)
329 void OnProtectedMediaIdentifierPermissionSet(const GURL& requesting_frame,
330 bool allowed);
331 #endif
333 // This method is called to update the status about the microphone and
334 // camera stream access.
335 void OnMediaStreamPermissionSet(
336 const GURL& request_origin,
337 MicrophoneCameraState new_microphone_camera_state,
338 const std::string& media_stream_selected_audio_device,
339 const std::string& media_stream_selected_video_device,
340 const std::string& media_stream_requested_audio_device,
341 const std::string& media_stream_requested_video_device);
343 // There methods are called to update the status about MIDI access.
344 void OnMidiSysExAccessed(const GURL& reqesting_origin);
345 void OnMidiSysExAccessBlocked(const GURL& requesting_origin);
347 // Adds the given |SiteDataObserver|. The |observer| is notified when a
348 // locale shared object, like for example a cookie, is accessed.
349 void AddSiteDataObserver(SiteDataObserver* observer);
351 // Removes the given |SiteDataObserver|.
352 void RemoveSiteDataObserver(SiteDataObserver* observer);
354 private:
355 friend class content::WebContentsUserData<TabSpecificContentSettings>;
357 explicit TabSpecificContentSettings(content::WebContents* tab);
359 // content::WebContentsObserver overrides.
360 void RenderFrameForInterstitialPageCreated(
361 content::RenderFrameHost* render_frame_host) override;
362 bool OnMessageReceived(const IPC::Message& message,
363 content::RenderFrameHost* render_frame_host) override;
364 void DidNavigateMainFrame(
365 const content::LoadCommittedDetails& details,
366 const content::FrameNavigateParams& params) override;
367 void DidStartProvisionalLoadForFrame(
368 content::RenderFrameHost* render_frame_host,
369 const GURL& validated_url,
370 bool is_error_page,
371 bool is_iframe_srcdoc) override;
372 void AppCacheAccessed(const GURL& manifest_url,
373 bool blocked_by_policy) override;
375 // content_settings::Observer implementation.
376 void OnContentSettingChanged(const ContentSettingsPattern& primary_pattern,
377 const ContentSettingsPattern& secondary_pattern,
378 ContentSettingsType content_type,
379 std::string resource_identifier) override;
381 // Notifies all registered |SiteDataObserver|s.
382 void NotifySiteDataObservers();
384 // Clears the Geolocation settings.
385 void ClearGeolocationContentSettings();
387 // Clears the MIDI settings.
388 void ClearMidiContentSettings();
390 // Updates Geolocation settings on navigation.
391 void GeolocationDidNavigate(
392 const content::LoadCommittedDetails& details);
394 // Updates MIDI settings on navigation.
395 void MidiDidNavigate(const content::LoadCommittedDetails& details);
397 // All currently registered |SiteDataObserver|s.
398 ObserverList<SiteDataObserver> observer_list_;
400 // Stores which content setting types actually have blocked content.
401 bool content_blocked_[CONTENT_SETTINGS_NUM_TYPES];
403 // Stores if the blocked content was messaged to the user.
404 bool content_blockage_indicated_to_user_[CONTENT_SETTINGS_NUM_TYPES];
406 // Stores which content setting types actually were allowed.
407 bool content_allowed_[CONTENT_SETTINGS_NUM_TYPES];
409 // Stores the blocked/allowed cookies.
410 LocalSharedObjectsContainer allowed_local_shared_objects_;
411 LocalSharedObjectsContainer blocked_local_shared_objects_;
413 // Manages information about Geolocation API usage in this page.
414 ContentSettingsUsagesState geolocation_usages_state_;
416 // Manages information about MIDI usages in this page.
417 ContentSettingsUsagesState midi_usages_state_;
419 // The pending protocol handler, if any. This can be set if
420 // registerProtocolHandler was invoked without user gesture.
421 // The |IsEmpty| method will be true if no protocol handler is
422 // pending registration.
423 ProtocolHandler pending_protocol_handler_;
425 // The previous protocol handler to be replaced by
426 // the pending_protocol_handler_, if there is one. Empty if
427 // there is no handler which would be replaced.
428 ProtocolHandler previous_protocol_handler_;
430 // The setting on the pending protocol handler registration. Persisted in case
431 // the user opens the bubble and makes changes multiple times.
432 ContentSetting pending_protocol_handler_setting_;
434 // The name(s) of the plugin(s) being blocked.
435 std::vector<base::string16> blocked_plugin_names_;
437 // Stores whether the user can load blocked plugins on this page.
438 bool load_plugins_link_enabled_;
440 // The origin of the media stream request. Note that we only support handling
441 // settings for one request per tab. The latest request's origin will be
442 // stored here. http://crbug.com/259794
443 GURL media_stream_access_origin_;
445 // The microphone and camera state at the last media stream request.
446 MicrophoneCameraState microphone_camera_state_;
447 // The selected devices at the last media stream request.
448 std::string media_stream_selected_audio_device_;
449 std::string media_stream_selected_video_device_;
451 // The devices to be displayed in the media bubble when the media stream
452 // request is requesting certain specific devices.
453 std::string media_stream_requested_audio_device_;
454 std::string media_stream_requested_video_device_;
456 // Observer to watch for content settings changed.
457 ScopedObserver<HostContentSettingsMap, content_settings::Observer> observer_;
459 DISALLOW_COPY_AND_ASSIGN(TabSpecificContentSettings);
462 #endif // CHROME_BROWSER_CONTENT_SETTINGS_TAB_SPECIFIC_CONTENT_SETTINGS_H_