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