Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / content_settings / tab_specific_content_settings.h
blob8851b8945d7f932d3733e46b0513a708fd6d0a80
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 static void CookiesRead(int render_process_id,
96 int render_frame_id,
97 const GURL& url,
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
105 // OnContentBlocked.
106 static void CookieChanged(int render_process_id,
107 int render_frame_id,
108 const GURL& url,
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
117 // OnContentBlocked.
118 static void WebDatabaseAccessed(int render_process_id,
119 int render_frame_id,
120 const GURL& url,
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
128 // OnContentBlocked.
129 static void DOMStorageAccessed(int render_process_id,
130 int render_frame_id,
131 const GURL& url,
132 bool local,
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
138 // OnContentBlocked.
139 static void IndexedDBAccessed(int render_process_id,
140 int render_frame_id,
141 const GURL& url,
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
148 // OnContentBlocked.
149 static void FileSystemAccessed(int render_process_id,
150 int render_frame_id,
151 const GURL& url,
152 bool blocked_by_policy);
154 // Resets the |content_blocked_| and |content_allowed_| arrays, except for
155 // CONTENT_SETTINGS_TYPE_COOKIES related information.
156 // TODO(vabr): Only public for tests. Move to a test client.
157 void ClearBlockedContentSettingsExceptForCookies();
159 // Resets all cookies related information.
160 // TODO(vabr): Only public for tests. Move to a test client.
161 void ClearCookieSpecificContentSettings();
163 // Changes the |content_blocked_| entry for popups.
164 void SetPopupsBlocked(bool blocked);
166 // Changes the |content_blocked_| entry for downloads.
167 void SetDownloadsBlocked(bool blocked);
169 // Returns whether a particular kind of content has been blocked for this
170 // page.
171 bool IsContentBlocked(ContentSettingsType content_type) const;
173 // Returns true if content blockage was indicated to the user.
174 bool IsBlockageIndicated(ContentSettingsType content_type) const;
176 void SetBlockageHasBeenIndicated(ContentSettingsType content_type);
178 // Returns whether a particular kind of content has been allowed. Currently
179 // only tracks cookies.
180 bool IsContentAllowed(ContentSettingsType content_type) const;
182 // Returns the names of plugins that have been blocked for this tab.
183 const std::vector<base::string16>& blocked_plugin_names() const {
184 return blocked_plugin_names_;
187 const GURL& media_stream_access_origin() const {
188 return media_stream_access_origin_;
191 const std::string& media_stream_requested_audio_device() const {
192 return media_stream_requested_audio_device_;
195 const std::string& media_stream_requested_video_device() const {
196 return media_stream_requested_video_device_;
199 // TODO(vabr): Only public for tests. Move to a test client.
200 const std::string& media_stream_selected_audio_device() const {
201 return media_stream_selected_audio_device_;
204 // TODO(vabr): Only public for tests. Move to a test client.
205 const std::string& media_stream_selected_video_device() const {
206 return media_stream_selected_video_device_;
209 // Returns the state of the camera and microphone usage.
210 // The return value always includes all active media capture devices, on top
211 // of the devices from the last request.
212 MicrophoneCameraState GetMicrophoneCameraState() const;
214 // Returns whether the camera or microphone permission or media device setting
215 // has changed since the last permission request.
216 bool IsMicrophoneCameraStateChanged() const;
218 // Returns the ContentSettingsUsagesState that controls the
219 // geolocation API usage on this page.
220 const ContentSettingsUsagesState& geolocation_usages_state() const {
221 return geolocation_usages_state_;
224 // Returns the ContentSettingsUsageState that controls the MIDI usage on
225 // this page.
226 const ContentSettingsUsagesState& midi_usages_state() const {
227 return midi_usages_state_;
230 // Call to indicate that there is a protocol handler pending user approval.
231 void set_pending_protocol_handler(const ProtocolHandler& handler) {
232 pending_protocol_handler_ = handler;
235 const ProtocolHandler& pending_protocol_handler() const {
236 return pending_protocol_handler_;
239 void ClearPendingProtocolHandler() {
240 pending_protocol_handler_ = ProtocolHandler::EmptyProtocolHandler();
243 // Sets the previous protocol handler which will be replaced by the
244 // pending protocol handler.
245 void set_previous_protocol_handler(const ProtocolHandler& handler) {
246 previous_protocol_handler_ = handler;
249 const ProtocolHandler& previous_protocol_handler() const {
250 return previous_protocol_handler_;
253 // Set whether the setting for the pending handler is DEFAULT (ignore),
254 // ALLOW, or DENY.
255 void set_pending_protocol_handler_setting(ContentSetting setting) {
256 pending_protocol_handler_setting_ = setting;
259 ContentSetting pending_protocol_handler_setting() const {
260 return pending_protocol_handler_setting_;
263 // Returns the |LocalSharedObjectsCounter| instances corresponding to all
264 // allowed, and blocked, respectively, local shared objects like cookies,
265 // local storage, ... .
266 const LocalSharedObjectsCounter& allowed_local_shared_objects() const {
267 return allowed_local_shared_objects_;
270 const LocalSharedObjectsCounter& blocked_local_shared_objects() const {
271 return blocked_local_shared_objects_;
274 // Creates a new copy of a CookiesTreeModel for all allowed, and blocked,
275 // respectively, local shared objects.
276 scoped_ptr<CookiesTreeModel> CreateAllowedCookiesTreeModel() const {
277 return allowed_local_shared_objects_.CreateCookiesTreeModel();
280 scoped_ptr<CookiesTreeModel> CreateBlockedCookiesTreeModel() const {
281 return blocked_local_shared_objects_.CreateCookiesTreeModel();
284 bool load_plugins_link_enabled() { return load_plugins_link_enabled_; }
285 void set_load_plugins_link_enabled(bool enabled) {
286 load_plugins_link_enabled_ = enabled;
289 // Called to indicate whether access to the Pepper broker was allowed or
290 // blocked.
291 void SetPepperBrokerAllowed(bool allowed);
293 // Message handlers.
294 // TODO(vabr): Only public for tests. Move to a test client.
295 void OnContentBlocked(ContentSettingsType type);
296 void OnContentBlockedWithDetail(ContentSettingsType type,
297 const base::string16& details);
298 void OnContentAllowed(ContentSettingsType type);
300 // These methods are invoked on the UI thread by the static functions above.
301 // TODO(vabr): Only public for tests. Move to a test client.
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,
317 bool local,
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,
324 bool allowed);
325 #if defined(OS_ANDROID) || defined(OS_CHROMEOS)
326 void OnProtectedMediaIdentifierPermissionSet(const GURL& requesting_frame,
327 bool allowed);
328 #endif
330 // This method is called to update the status about the microphone and
331 // camera stream access.
332 void OnMediaStreamPermissionSet(
333 const GURL& request_origin,
334 MicrophoneCameraState new_microphone_camera_state,
335 const std::string& media_stream_selected_audio_device,
336 const std::string& media_stream_selected_video_device,
337 const std::string& media_stream_requested_audio_device,
338 const std::string& media_stream_requested_video_device);
340 // There methods are called to update the status about MIDI access.
341 void OnMidiSysExAccessed(const GURL& reqesting_origin);
342 void OnMidiSysExAccessBlocked(const GURL& requesting_origin);
344 // Adds the given |SiteDataObserver|. The |observer| is notified when a
345 // locale shared object, like for example a cookie, is accessed.
346 void AddSiteDataObserver(SiteDataObserver* observer);
348 // Removes the given |SiteDataObserver|.
349 void RemoveSiteDataObserver(SiteDataObserver* observer);
351 private:
352 friend class content::WebContentsUserData<TabSpecificContentSettings>;
354 explicit TabSpecificContentSettings(content::WebContents* tab);
356 // content::WebContentsObserver overrides.
357 void RenderFrameForInterstitialPageCreated(
358 content::RenderFrameHost* render_frame_host) override;
359 bool OnMessageReceived(const IPC::Message& message,
360 content::RenderFrameHost* render_frame_host) override;
361 void DidNavigateMainFrame(
362 const content::LoadCommittedDetails& details,
363 const content::FrameNavigateParams& params) override;
364 void DidStartProvisionalLoadForFrame(
365 content::RenderFrameHost* render_frame_host,
366 const GURL& validated_url,
367 bool is_error_page,
368 bool is_iframe_srcdoc) override;
369 void AppCacheAccessed(const GURL& manifest_url,
370 bool blocked_by_policy) override;
372 // content_settings::Observer implementation.
373 void OnContentSettingChanged(const ContentSettingsPattern& primary_pattern,
374 const ContentSettingsPattern& secondary_pattern,
375 ContentSettingsType content_type,
376 std::string resource_identifier) override;
378 // Notifies all registered |SiteDataObserver|s.
379 void NotifySiteDataObservers();
381 // Clears the Geolocation settings.
382 void ClearGeolocationContentSettings();
384 // Clears the MIDI settings.
385 void ClearMidiContentSettings();
387 // Updates Geolocation settings on navigation.
388 void GeolocationDidNavigate(
389 const content::LoadCommittedDetails& details);
391 // Updates MIDI settings on navigation.
392 void MidiDidNavigate(const content::LoadCommittedDetails& details);
394 // All currently registered |SiteDataObserver|s.
395 ObserverList<SiteDataObserver> observer_list_;
397 // Stores which content setting types actually have blocked content.
398 bool content_blocked_[CONTENT_SETTINGS_NUM_TYPES];
400 // Stores if the blocked content was messaged to the user.
401 bool content_blockage_indicated_to_user_[CONTENT_SETTINGS_NUM_TYPES];
403 // Stores which content setting types actually were allowed.
404 bool content_allowed_[CONTENT_SETTINGS_NUM_TYPES];
406 // Stores the blocked/allowed cookies.
407 LocalSharedObjectsContainer allowed_local_shared_objects_;
408 LocalSharedObjectsContainer blocked_local_shared_objects_;
410 // Manages information about Geolocation API usage in this page.
411 ContentSettingsUsagesState geolocation_usages_state_;
413 // Manages information about MIDI usages in this page.
414 ContentSettingsUsagesState midi_usages_state_;
416 // The pending protocol handler, if any. This can be set if
417 // registerProtocolHandler was invoked without user gesture.
418 // The |IsEmpty| method will be true if no protocol handler is
419 // pending registration.
420 ProtocolHandler pending_protocol_handler_;
422 // The previous protocol handler to be replaced by
423 // the pending_protocol_handler_, if there is one. Empty if
424 // there is no handler which would be replaced.
425 ProtocolHandler previous_protocol_handler_;
427 // The setting on the pending protocol handler registration. Persisted in case
428 // the user opens the bubble and makes changes multiple times.
429 ContentSetting pending_protocol_handler_setting_;
431 // The name(s) of the plugin(s) being blocked.
432 std::vector<base::string16> blocked_plugin_names_;
434 // Stores whether the user can load blocked plugins on this page.
435 bool load_plugins_link_enabled_;
437 // The origin of the media stream request. Note that we only support handling
438 // settings for one request per tab. The latest request's origin will be
439 // stored here. http://crbug.com/259794
440 GURL media_stream_access_origin_;
442 // The microphone and camera state at the last media stream request.
443 MicrophoneCameraState microphone_camera_state_;
444 // The selected devices at the last media stream request.
445 std::string media_stream_selected_audio_device_;
446 std::string media_stream_selected_video_device_;
448 // The devices to be displayed in the media bubble when the media stream
449 // request is requesting certain specific devices.
450 std::string media_stream_requested_audio_device_;
451 std::string media_stream_requested_video_device_;
453 // Observer to watch for content settings changed.
454 ScopedObserver<HostContentSettingsMap, content_settings::Observer> observer_;
456 DISALLOW_COPY_AND_ASSIGN(TabSpecificContentSettings);
459 #endif // CHROME_BROWSER_CONTENT_SETTINGS_TAB_SPECIFIC_CONTENT_SETTINGS_H_