| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Maps [requesting_origin, embedder] to content settings. Written on the UI | 5 // Maps [requesting_origin, embedder] to content settings. Written on the UI |
| 6 // thread and read on any thread. One instance per profile. This is based on | 6 // thread and read on any thread. One instance per profile. This is based on |
| 7 // HostContentSettingsMap but differs significantly in two aspects: | 7 // HostContentSettingsMap but differs significantly in two aspects: |
| 8 // - It maps [requesting_origin.GetOrigin(), embedder.GetOrigin()] => setting | 8 // - It maps [requesting_origin.GetOrigin(), embedder.GetOrigin()] => setting |
| 9 // rather than host => setting. | 9 // rather than host => setting. |
| 10 // - It manages only Geolocation. | 10 // - It manages only Geolocation. |
| 11 | 11 |
| 12 #ifndef CHROME_BROWSER_GEOLOCATION_GEOLOCATION_CONTENT_SETTINGS_MAP_H_ | 12 #ifndef CHROME_BROWSER_GEOLOCATION_GEOLOCATION_CONTENT_SETTINGS_MAP_H_ |
| 13 #define CHROME_BROWSER_GEOLOCATION_GEOLOCATION_CONTENT_SETTINGS_MAP_H_ | 13 #define CHROME_BROWSER_GEOLOCATION_GEOLOCATION_CONTENT_SETTINGS_MAP_H_ |
| 14 | 14 |
| 15 #include <map> | 15 #include <map> |
| 16 #include <string> | 16 #include <string> |
| 17 #include <utility> | |
| 18 #include <vector> | |
| 19 | 17 |
| 20 #include "base/basictypes.h" | 18 #include "base/basictypes.h" |
| 21 #include "base/lock.h" | |
| 22 #include "base/ref_counted.h" | 19 #include "base/ref_counted.h" |
| 23 #include "chrome/common/content_settings.h" | 20 #include "chrome/common/content_settings.h" |
| 24 #include "chrome/common/notification_observer.h" | |
| 25 #include "googleurl/src/gurl.h" | 21 #include "googleurl/src/gurl.h" |
| 26 | 22 |
| 27 class DictionaryValue; | 23 class DictionaryValue; |
| 28 class PrefService; | 24 class PrefService; |
| 29 class Profile; | 25 class Profile; |
| 30 | 26 |
| 31 class GeolocationContentSettingsMap | 27 class GeolocationContentSettingsMap |
| 32 : public NotificationObserver, | 28 : public base::RefCountedThreadSafe<GeolocationContentSettingsMap> { |
| 33 public base::RefCountedThreadSafe<GeolocationContentSettingsMap> { | |
| 34 public: | 29 public: |
| 35 typedef std::map<GURL, ContentSetting> OneOriginSettings; | 30 typedef std::map<GURL, ContentSetting> OneOriginSettings; |
| 36 typedef std::map<GURL, OneOriginSettings> AllOriginsSettings; | 31 typedef std::map<GURL, OneOriginSettings> AllOriginsSettings; |
| 37 | 32 |
| 38 explicit GeolocationContentSettingsMap(Profile* profile); | 33 explicit GeolocationContentSettingsMap(Profile* profile); |
| 39 | 34 |
| 40 static void RegisterUserPrefs(PrefService* prefs); | 35 static void RegisterUserPrefs(PrefService* prefs); |
| 41 | 36 |
| 42 // Return simplified string representing origin. If origin is using http or | 37 // Return simplified string representing origin. If origin is using http or |
| 43 // the standard port, those parts are not included in the output. | 38 // the standard port, those parts are not included in the output. |
| 44 static std::string OriginToString(const GURL& origin); | 39 static std::string OriginToString(const GURL& origin); |
| 45 | 40 |
| 46 // Returns the default setting. | 41 // Returns the default setting. |
| 47 // | 42 // |
| 48 // This may be called on any thread. | 43 // This should only be called on the UI thread. |
| 49 ContentSetting GetDefaultContentSetting() const; | 44 ContentSetting GetDefaultContentSetting() const; |
| 50 | 45 |
| 51 // Returns a single ContentSetting which applies to the given |requesting_url| | 46 // Returns a single ContentSetting which applies to the given |requesting_url| |
| 52 // when embedded in a top-level page from |embedding_url|. To determine the | 47 // when embedded in a top-level page from |embedding_url|. To determine the |
| 53 // setting for a top-level page, as opposed to a frame embedded in a page, | 48 // setting for a top-level page, as opposed to a frame embedded in a page, |
| 54 // pass the page's URL for both arguments. | 49 // pass the page's URL for both arguments. |
| 55 // | 50 // |
| 56 // This may be called on any thread. Both arguments should be valid GURLs. | 51 // This should only be called on the UI thread. |
| 52 // Both arguments should be valid GURLs. |
| 57 ContentSetting GetContentSetting(const GURL& requesting_url, | 53 ContentSetting GetContentSetting(const GURL& requesting_url, |
| 58 const GURL& embedding_url) const; | 54 const GURL& embedding_url) const; |
| 59 | 55 |
| 60 // Returns the settings for all origins with any non-default settings. | 56 // Returns the settings for all origins with any non-default settings. |
| 61 // | 57 // |
| 62 // This may be called on any thread. | 58 // This should only be called on the UI thread. |
| 63 AllOriginsSettings GetAllOriginsSettings() const; | 59 AllOriginsSettings GetAllOriginsSettings() const; |
| 64 | 60 |
| 65 // Sets the default setting. | 61 // Sets the default setting. |
| 66 // | 62 // |
| 67 // This should only be called on the UI thread. | 63 // This should only be called on the UI thread. |
| 68 void SetDefaultContentSetting(ContentSetting setting); | 64 void SetDefaultContentSetting(ContentSetting setting); |
| 69 | 65 |
| 70 // Sets the content setting for a particular (requesting origin, embedding | 66 // Sets the content setting for a particular (requesting origin, embedding |
| 71 // origin) pair. If the embedding origin is the same as the requesting | 67 // origin) pair. If the embedding origin is the same as the requesting |
| 72 // origin, this represents the setting used when the requesting origin is | 68 // origin, this represents the setting used when the requesting origin is |
| 73 // itself the top-level page. If |embedder| is the empty GURL, |setting| | 69 // itself the top-level page. If |embedder| is the empty GURL, |setting| |
| 74 // becomes the default setting for the requesting origin when embedded on any | 70 // becomes the default setting for the requesting origin when embedded on any |
| 75 // page that does not have an explicit setting. Passing | 71 // page that does not have an explicit setting. Passing |
| 76 // CONTENT_SETTING_DEFAULT for |setting| effectively removes that setting and | 72 // CONTENT_SETTING_DEFAULT for |setting| effectively removes that setting and |
| 77 // allows future requests to return the all-embedders or global defaults (as | 73 // allows future requests to return the all-embedders or global defaults (as |
| 78 // applicable). | 74 // applicable). |
| 79 // | 75 // |
| 80 // This should only be called on the UI thread. |requesting_url| should be | 76 // This should only be called on the UI thread. |requesting_url| should be |
| 81 // a valid GURL, and |embedding_url| should be valid or empty. | 77 // a valid GURL, and |embedding_url| should be valid or empty. |
| 82 void SetContentSetting(const GURL& requesting_url, | 78 void SetContentSetting(const GURL& requesting_url, |
| 83 const GURL& embedding_url, | 79 const GURL& embedding_url, |
| 84 ContentSetting setting); | 80 ContentSetting setting); |
| 85 | 81 |
| 86 // Clears all settings for |requesting_origin|. Note: Unlike in the functions | |
| 87 // above, this is expected to be an origin, not some URL of which we'll take | |
| 88 // the origin; this is to prevent ambiguity where callers could think they're | |
| 89 // clearing something wider or narrower than they really are. | |
| 90 // | |
| 91 // This should only be called on the UI thread. |requesting_origin| should be | |
| 92 // a valid GURL. | |
| 93 void ClearOneRequestingOrigin(const GURL& requesting_origin); | |
| 94 | |
| 95 // Resets all settings. | 82 // Resets all settings. |
| 96 // | 83 // |
| 97 // This should only be called on the UI thread. | 84 // This should only be called on the UI thread. |
| 98 void ResetToDefault(); | 85 void ResetToDefault(); |
| 99 | 86 |
| 100 // NotificationObserver implementation. | |
| 101 virtual void Observe(NotificationType type, | |
| 102 const NotificationSource& source, | |
| 103 const NotificationDetails& details); | |
| 104 | |
| 105 private: | 87 private: |
| 106 friend class base::RefCountedThreadSafe<GeolocationContentSettingsMap>; | 88 friend class base::RefCountedThreadSafe<GeolocationContentSettingsMap>; |
| 107 | 89 |
| 108 // The default setting. | 90 // The default setting. |
| 109 static const ContentSetting kDefaultSetting; | 91 static const ContentSetting kDefaultSetting; |
| 110 | 92 |
| 111 ~GeolocationContentSettingsMap(); | 93 ~GeolocationContentSettingsMap(); |
| 112 | 94 |
| 113 // Reads the exceptions from the preference service. | |
| 114 void ReadExceptions(); | |
| 115 | |
| 116 // Sets the fields of |one_origin_settings| based on the values in | 95 // Sets the fields of |one_origin_settings| based on the values in |
| 117 // |dictionary|. | 96 // |dictionary|. |
| 118 static void GetOneOriginSettingsFromDictionary( | 97 static void GetOneOriginSettingsFromDictionary( |
| 119 const DictionaryValue* dictionary, | 98 const DictionaryValue* dictionary, |
| 120 OneOriginSettings* one_origin_settings); | 99 OneOriginSettings* one_origin_settings); |
| 121 | 100 |
| 122 // The profile we're associated with. | 101 // The profile we're associated with. |
| 123 Profile* profile_; | 102 Profile* profile_; |
| 124 | 103 |
| 125 // Copies of the pref data, so that we can read it on the IO thread. | |
| 126 ContentSetting default_content_setting_; | |
| 127 AllOriginsSettings content_settings_; | |
| 128 | |
| 129 // Used around accesses to the settings objects to guarantee thread safety. | |
| 130 mutable Lock lock_; | |
| 131 | |
| 132 // Whether we are currently updating preferences, this is used to ignore | |
| 133 // notifications from the preference service that we triggered ourself. | |
| 134 bool updating_preferences_; | |
| 135 | |
| 136 DISALLOW_COPY_AND_ASSIGN(GeolocationContentSettingsMap); | 104 DISALLOW_COPY_AND_ASSIGN(GeolocationContentSettingsMap); |
| 137 }; | 105 }; |
| 138 | 106 |
| 139 #endif // CHROME_BROWSER_GEOLOCATION_GEOLOCATION_CONTENT_SETTINGS_MAP_H_ | 107 #endif // CHROME_BROWSER_GEOLOCATION_GEOLOCATION_CONTENT_SETTINGS_MAP_H_ |
| OLD | NEW |