tab_specific_content_settings.h revision 34680572440d7894ef8dafce81d8039ed80726a2
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.
4
5#ifndef CHROME_BROWSER_CONTENT_SETTINGS_TAB_SPECIFIC_CONTENT_SETTINGS_H_
6#define CHROME_BROWSER_CONTENT_SETTINGS_TAB_SPECIFIC_CONTENT_SETTINGS_H_
7
8#include <string>
9
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/content_settings/content_settings_usages_state.h"
16#include "chrome/browser/content_settings/local_shared_objects_container.h"
17#include "chrome/browser/media/media_stream_devices_controller.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/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 "content/public/common/media_stream_request.h"
25#include "net/cookies/canonical_cookie.h"
26
27class CookiesTreeModel;
28class HostContentSettingsMap;
29class Profile;
30
31namespace content {
32class RenderViewHost;
33}
34
35namespace net {
36class CookieOptions;
37}
38
39// This class manages state about permissions, content settings, cookies and
40// site data for a specific WebContents. It tracks which content was accessed
41// and which content was blocked. Based on this it provides information about
42// which types of content were accessed and blocked.
43class TabSpecificContentSettings
44    : public content::WebContentsObserver,
45      public content_settings::Observer,
46      public content::WebContentsUserData<TabSpecificContentSettings> {
47 public:
48  enum MicrophoneCameraState {
49    MICROPHONE_CAMERA_NOT_ACCESSED = 0,
50    MICROPHONE_ACCESSED,
51    CAMERA_ACCESSED,
52    MICROPHONE_CAMERA_ACCESSED,
53    MICROPHONE_BLOCKED,
54    CAMERA_BLOCKED,
55    MICROPHONE_CAMERA_BLOCKED,
56  };
57
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();
66
67    // Called whenever site data is accessed.
68    virtual void OnSiteDataAccessed() = 0;
69
70    TabSpecificContentSettings* tab_specific_content_settings() {
71      return tab_specific_content_settings_;
72    }
73
74    // Called when the TabSpecificContentSettings is destroyed; nulls out
75    // the local reference.
76    void ContentSettingsDestroyed();
77
78   private:
79    TabSpecificContentSettings* tab_specific_content_settings_;
80
81    DISALLOW_COPY_AND_ASSIGN(SiteDataObserver);
82  };
83
84  virtual ~TabSpecificContentSettings();
85
86  // Returns the object given a render view's id.
87  static TabSpecificContentSettings* Get(int render_process_id,
88                                         int render_view_id);
89
90  // Returns the object given a render frame's id.
91  static TabSpecificContentSettings* GetForFrame(int render_process_id,
92                                                 int render_view_id);
93
94  // Static methods called on the UI threads.
95  // Called when cookies for the given URL were read either from within the
96  // current page or while loading it. |blocked_by_policy| should be true, if
97  // reading cookies was blocked due to the user's content settings. In that
98  // case, this function should invoke OnContentBlocked.
99  // |is_for_blocking_resource| indicates whether the cookies read were for a
100  // blocking resource (eg script, css). It is only temporarily added for
101  // diagnostic purposes, per bug 353678. Will be removed again once data
102  // collection is finished.
103  static void CookiesRead(int render_process_id,
104                          int render_frame_id,
105                          const GURL& url,
106                          const GURL& first_party_url,
107                          const net::CookieList& cookie_list,
108                          bool blocked_by_policy,
109                          bool is_for_blocking_resource);
110
111  // Called when a specific cookie in the current page was changed.
112  // |blocked_by_policy| should be true, if the cookie was blocked due to the
113  // user's content settings. In that case, this function should invoke
114  // OnContentBlocked.
115  static void CookieChanged(int render_process_id,
116                            int render_frame_id,
117                            const GURL& url,
118                            const GURL& first_party_url,
119                            const std::string& cookie_line,
120                            const net::CookieOptions& options,
121                            bool blocked_by_policy);
122
123  // Called when a specific Web database in the current page was accessed. If
124  // access was blocked due to the user's content settings,
125  // |blocked_by_policy| should be true, and this function should invoke
126  // OnContentBlocked.
127  static void WebDatabaseAccessed(int render_process_id,
128                                  int render_frame_id,
129                                  const GURL& url,
130                                  const base::string16& name,
131                                  const base::string16& display_name,
132                                  bool blocked_by_policy);
133
134  // Called when a specific DOM storage area in the current page was
135  // accessed. If access was blocked due to the user's content settings,
136  // |blocked_by_policy| should be true, and this function should invoke
137  // OnContentBlocked.
138  static void DOMStorageAccessed(int render_process_id,
139                                 int render_frame_id,
140                                 const GURL& url,
141                                 bool local,
142                                 bool blocked_by_policy);
143
144  // Called when a specific indexed db factory in the current page was
145  // accessed. If access was blocked due to the user's content settings,
146  // |blocked_by_policy| should be true, and this function should invoke
147  // OnContentBlocked.
148  static void IndexedDBAccessed(int render_process_id,
149                                int render_frame_id,
150                                const GURL& url,
151                                const base::string16& description,
152                                bool blocked_by_policy);
153
154  // Called when a specific file system in the current page was accessed.
155  // If access was blocked due to the user's content settings,
156  // |blocked_by_policy| should be true, and this function should invoke
157  // OnContentBlocked.
158  static void FileSystemAccessed(int render_process_id,
159                                 int render_frame_id,
160                                 const GURL& url,
161                                 bool blocked_by_policy);
162
163  // Resets the |content_blocked_| and |content_allowed_| arrays, except for
164  // CONTENT_SETTINGS_TYPE_COOKIES related information.
165  void ClearBlockedContentSettingsExceptForCookies();
166
167  // Resets all cookies related information.
168  void ClearCookieSpecificContentSettings();
169
170  // Clears the Geolocation settings.
171  void ClearGeolocationContentSettings();
172
173  // Clears the MIDI settings.
174  void ClearMidiContentSettings();
175
176  // Changes the |content_blocked_| entry for popups.
177  void SetPopupsBlocked(bool blocked);
178
179  // Changes the |content_blocked_| entry for downloads.
180  void SetDownloadsBlocked(bool blocked);
181
182  // Updates Geolocation settings on navigation.
183  void GeolocationDidNavigate(
184      const content::LoadCommittedDetails& details);
185
186  // Updates MIDI settings on navigation.
187  void MidiDidNavigate(const content::LoadCommittedDetails& details);
188
189  // Returns whether a particular kind of content has been blocked for this
190  // page.
191  bool IsContentBlocked(ContentSettingsType content_type) const;
192
193  // Returns true if content blockage was indicated to the user.
194  bool IsBlockageIndicated(ContentSettingsType content_type) const;
195
196  void SetBlockageHasBeenIndicated(ContentSettingsType content_type);
197
198  // Returns whether a particular kind of content has been allowed. Currently
199  // only tracks cookies.
200  bool IsContentAllowed(ContentSettingsType content_type) const;
201
202  const GURL& media_stream_access_origin() const {
203    return media_stream_access_origin_;
204  }
205
206  const std::string& media_stream_requested_audio_device() const {
207    return media_stream_requested_audio_device_;
208  }
209
210  const std::string& media_stream_requested_video_device() const {
211    return media_stream_requested_video_device_;
212  }
213
214  // Returns the state of the camera and microphone usage.
215  MicrophoneCameraState GetMicrophoneCameraState() const;
216
217  // Returns the ContentSettingsUsagesState that controls the
218  // geolocation API usage on this page.
219  const ContentSettingsUsagesState& geolocation_usages_state() const {
220    return geolocation_usages_state_;
221  }
222
223  // Returns the ContentSettingsUsageState that controls the MIDI usage on
224  // this page.
225  const ContentSettingsUsagesState& midi_usages_state() const {
226    return midi_usages_state_;
227  }
228
229  // Call to indicate that there is a protocol handler pending user approval.
230  void set_pending_protocol_handler(const ProtocolHandler& handler) {
231    pending_protocol_handler_ = handler;
232  }
233
234  const ProtocolHandler& pending_protocol_handler() const {
235    return pending_protocol_handler_;
236  }
237
238  void ClearPendingProtocolHandler() {
239    pending_protocol_handler_ = ProtocolHandler::EmptyProtocolHandler();
240  }
241
242  // Sets the previous protocol handler which will be replaced by the
243  // pending protocol handler.
244  void set_previous_protocol_handler(const ProtocolHandler& handler) {
245    previous_protocol_handler_ = handler;
246  }
247
248  const ProtocolHandler& previous_protocol_handler() const {
249    return previous_protocol_handler_;
250  }
251
252  // Set whether the setting for the pending handler is DEFAULT (ignore),
253  // ALLOW, or DENY.
254  void set_pending_protocol_handler_setting(ContentSetting setting) {
255    pending_protocol_handler_setting_ = setting;
256  }
257
258  ContentSetting pending_protocol_handler_setting() const {
259    return pending_protocol_handler_setting_;
260  }
261
262
263  // Returns a pointer to the |LocalSharedObjectsContainer| that contains all
264  // allowed local shared objects like cookies, local storage, ... .
265  const LocalSharedObjectsContainer& allowed_local_shared_objects() const {
266    return allowed_local_shared_objects_;
267  }
268
269  // Returns a pointer to the |LocalSharedObjectsContainer| that contains all
270  // blocked local shared objects like cookies, local storage, ... .
271  const LocalSharedObjectsContainer& blocked_local_shared_objects() const {
272    return blocked_local_shared_objects_;
273  }
274
275  bool load_plugins_link_enabled() { return load_plugins_link_enabled_; }
276  void set_load_plugins_link_enabled(bool enabled) {
277    load_plugins_link_enabled_ = enabled;
278  }
279
280  // Called to indicate whether access to the Pepper broker was allowed or
281  // blocked.
282  void SetPepperBrokerAllowed(bool allowed);
283
284  // content::WebContentsObserver overrides.
285  virtual void RenderFrameForInterstitialPageCreated(
286      content::RenderFrameHost* render_frame_host) OVERRIDE;
287  virtual bool OnMessageReceived(
288      const IPC::Message& message,
289      content::RenderFrameHost* render_frame_host) OVERRIDE;
290  virtual void DidNavigateMainFrame(
291      const content::LoadCommittedDetails& details,
292      const content::FrameNavigateParams& params) OVERRIDE;
293  virtual void DidStartProvisionalLoadForFrame(
294      content::RenderFrameHost* render_frame_host,
295      const GURL& validated_url,
296      bool is_error_page,
297      bool is_iframe_srcdoc) OVERRIDE;
298  virtual void AppCacheAccessed(const GURL& manifest_url,
299                                bool blocked_by_policy) OVERRIDE;
300
301  // Message handlers. Public for testing.
302  void OnContentBlocked(ContentSettingsType type);
303  void OnContentAllowed(ContentSettingsType type);
304
305  // These methods are invoked on the UI thread by the static functions above.
306  // Public for testing.
307  void OnCookiesRead(const GURL& url,
308                     const GURL& first_party_url,
309                     const net::CookieList& cookie_list,
310                     bool blocked_by_policy);
311  void OnCookieChanged(const GURL& url,
312                       const GURL& first_party_url,
313                       const std::string& cookie_line,
314                       const net::CookieOptions& options,
315                       bool blocked_by_policy);
316  void OnFileSystemAccessed(const GURL& url,
317                            bool blocked_by_policy);
318  void OnIndexedDBAccessed(const GURL& url,
319                           const base::string16& description,
320                           bool blocked_by_policy);
321  void OnLocalStorageAccessed(const GURL& url,
322                              bool local,
323                              bool blocked_by_policy);
324  void OnWebDatabaseAccessed(const GURL& url,
325                             const base::string16& name,
326                             const base::string16& display_name,
327                             bool blocked_by_policy);
328  void OnGeolocationPermissionSet(const GURL& requesting_frame,
329                                  bool allowed);
330#if defined(OS_ANDROID)
331  void OnProtectedMediaIdentifierPermissionSet(const GURL& requesting_frame,
332                                               bool allowed);
333#endif
334
335  // This method is called to update the status about the microphone and
336  // camera stream access. |request_permissions| contains a list of requested
337  // media stream types and the permission for each type.
338  void OnMediaStreamPermissionSet(
339      const GURL& request_origin,
340      const MediaStreamDevicesController::MediaStreamTypeSettingsMap&
341          request_permissions);
342
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);
346
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);
350
351  // Removes the given |SiteDataObserver|.
352  void RemoveSiteDataObserver(SiteDataObserver* observer);
353
354 private:
355  explicit TabSpecificContentSettings(content::WebContents* tab);
356  friend class content::WebContentsUserData<TabSpecificContentSettings>;
357
358  // content_settings::Observer implementation.
359  virtual void OnContentSettingChanged(
360      const ContentSettingsPattern& primary_pattern,
361      const ContentSettingsPattern& secondary_pattern,
362      ContentSettingsType content_type,
363      std::string resource_identifier) OVERRIDE;
364
365  // Notifies all registered |SiteDataObserver|s.
366  void NotifySiteDataObservers();
367
368  // All currently registered |SiteDataObserver|s.
369  ObserverList<SiteDataObserver> observer_list_;
370
371  // Stores which content setting types actually have blocked content.
372  bool content_blocked_[CONTENT_SETTINGS_NUM_TYPES];
373
374  // Stores if the blocked content was messaged to the user.
375  bool content_blockage_indicated_to_user_[CONTENT_SETTINGS_NUM_TYPES];
376
377  // Stores which content setting types actually were allowed.
378  bool content_allowed_[CONTENT_SETTINGS_NUM_TYPES];
379
380  // The profile of the tab.
381  Profile* profile_;
382
383  // Stores the blocked/allowed cookies.
384  LocalSharedObjectsContainer allowed_local_shared_objects_;
385  LocalSharedObjectsContainer blocked_local_shared_objects_;
386
387  // Manages information about Geolocation API usage in this page.
388  ContentSettingsUsagesState geolocation_usages_state_;
389
390  // Manages information about MIDI usages in this page.
391  ContentSettingsUsagesState midi_usages_state_;
392
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_;
398
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_;
403
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_;
407
408  // Stores whether the user can load blocked plugins on this page.
409  bool load_plugins_link_enabled_;
410
411  // The origin of the media stream request. Note that we only support handling
412  // settings for one request per tab. The latest request's origin will be
413  // stored here. http://crbug.com/259794
414  GURL media_stream_access_origin_;
415
416  // The devices to be displayed in the media bubble when the media stream
417  // request is requesting certain specific devices.
418  std::string media_stream_requested_audio_device_;
419  std::string media_stream_requested_video_device_;
420
421  // Observer to watch for content settings changed.
422  ScopedObserver<HostContentSettingsMap, content_settings::Observer> observer_;
423
424  DISALLOW_COPY_AND_ASSIGN(TabSpecificContentSettings);
425};
426
427#endif  // CHROME_BROWSER_CONTENT_SETTINGS_TAB_SPECIFIC_CONTENT_SETTINGS_H_
428