1 // Copyright 2016 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_UI_WEBUI_SITE_SETTINGS_HELPER_H_ 6 #define CHROME_BROWSER_UI_WEBUI_SITE_SETTINGS_HELPER_H_ 7 8 #include <map> 9 #include <memory> 10 #include <set> 11 #include <string> 12 #include <utility> 13 #include <vector> 14 15 #include "base/strings/string16.h" 16 #include "base/values.h" 17 #include "components/content_settings/core/common/content_settings.h" 18 #include "components/content_settings/core/common/content_settings_pattern.h" 19 #include "components/content_settings/core/common/content_settings_types.h" 20 #include "components/prefs/pref_service.h" 21 #include "extensions/common/extension.h" 22 23 class HostContentSettingsMap; 24 class Profile; 25 26 namespace content { 27 class WebUI; 28 } 29 30 namespace extensions { 31 class ExtensionRegistry; 32 } 33 34 namespace permissions { 35 class ChooserContextBase; 36 } 37 38 namespace site_settings { 39 40 // Maps from a secondary pattern to a setting. 41 typedef std::map<ContentSettingsPattern, ContentSetting> OnePatternSettings; 42 // Maps from a primary pattern/source pair to a OnePatternSettings. All the 43 // mappings in OnePatternSettings share the given primary pattern and source. 44 typedef std::map<std::pair<ContentSettingsPattern, std::string>, 45 OnePatternSettings> 46 AllPatternsSettings; 47 48 // TODO(https://crbug.com/854329): Once the Site Settings WebUI is capable of 49 // displaying the new chooser exception object format, remove the typedefs that 50 // are currently used for organizing the chooser exceptions. 51 // Maps from a primary URL pattern/source pair to a set of secondary URL 52 // patterns/incognito status pair. 53 using ChooserExceptionDetails = 54 std::map<std::pair<GURL, std::string>, std::set<std::pair<GURL, bool>>>; 55 56 constexpr char kAllowAll[] = "allowAll"; 57 constexpr char kBlockThirdPartyIncognito[] = "blockThirdPartyIncognito"; 58 constexpr char kBlockThirdParty[] = "blockThirdParty"; 59 constexpr char kBlockAll[] = "blockAll"; 60 constexpr char kSessionOnly[] = "sessionOnly"; 61 constexpr char kChooserType[] = "chooserType"; 62 constexpr char kDisplayName[] = "displayName"; 63 constexpr char kEmbeddingOrigin[] = "embeddingOrigin"; 64 constexpr char kIncognito[] = "incognito"; 65 constexpr char kObject[] = "object"; 66 constexpr char kDisabled[] = "disabled"; 67 constexpr char kOrigin[] = "origin"; 68 constexpr char kOriginForFavicon[] = "originForFavicon"; 69 constexpr char kRecentPermissions[] = "recentPermissions"; 70 constexpr char kSetting[] = "setting"; 71 constexpr char kSites[] = "sites"; 72 constexpr char kPolicyIndicator[] = "indicator"; 73 constexpr char kSource[] = "source"; 74 constexpr char kType[] = "type"; 75 76 enum class SiteSettingSource { 77 kAdsFilterBlacklist, 78 kDefault, 79 kDrmDisabled, 80 kEmbargo, 81 kExtension, 82 kInsecureOrigin, 83 kKillSwitch, 84 kPolicy, 85 kPreference, 86 kNumSources, 87 }; 88 89 // Possible policy indicators that can be shown in settings. 90 // Must be kept in sync with the CrPolicyIndicatorType enum located in 91 // src/ui/webui/resources/cr_elements/policy/cr_policy_indicator_behavior.js 92 enum class PolicyIndicatorType { 93 kDevicePolicy, 94 kExtension, 95 kNone, 96 kOwner, 97 kPrimaryUser, 98 kRecommended, 99 kUserPolicy, 100 kParent, 101 kChildRestriction, 102 kNumIndicators, 103 }; 104 105 // Represents the managed state for a single settings control. 106 struct ManagedState { 107 bool disabled = false; 108 PolicyIndicatorType indicator = PolicyIndicatorType::kNone; 109 }; 110 111 // Represents the manage states for all of the cookie controls. 112 struct CookieControlsManagedState { 113 ManagedState allow_all; 114 ManagedState block_third_party_incognito; 115 ManagedState block_third_party; 116 ManagedState block_all; 117 ManagedState session_only; 118 }; 119 120 // Returns whether a group name has been registered for the given type. 121 bool HasRegisteredGroupName(ContentSettingsType type); 122 123 // Converts a ContentSettingsType to/from its group name identifier. 124 ContentSettingsType ContentSettingsTypeFromGroupName(const std::string& name); 125 std::string ContentSettingsTypeToGroupName(ContentSettingsType type); 126 127 // Converts a ListValue of group names to a list of ContentSettingsTypes 128 std::vector<ContentSettingsType> ContentSettingsTypesFromGroupNames( 129 const base::Value::ConstListView types); 130 131 // Converts a SiteSettingSource to its string identifier. 132 std::string SiteSettingSourceToString(const SiteSettingSource source); 133 134 // Converts a ManagedState to a base::Value suitable for sending to JavaScript. 135 base::Value GetValueForManagedState(const ManagedState& state); 136 137 // Helper function to construct a dictionary for an exception. 138 std::unique_ptr<base::DictionaryValue> GetExceptionForPage( 139 const ContentSettingsPattern& pattern, 140 const ContentSettingsPattern& secondary_pattern, 141 const std::string& display_name, 142 const ContentSetting& setting, 143 const std::string& provider_name, 144 bool incognito); 145 146 // Helper function to construct a dictionary for a hosted app exception. 147 void AddExceptionForHostedApp(const std::string& url_pattern, 148 const extensions::Extension& app, 149 base::ListValue* exceptions); 150 151 // Fills in |exceptions| with Values for the given |type| from |map|. 152 // If |filter| is not null then only exceptions with matching primary patterns 153 // will be returned. 154 void GetExceptionsFromHostContentSettingsMap( 155 const HostContentSettingsMap* map, 156 ContentSettingsType type, 157 const extensions::ExtensionRegistry* extension_registry, 158 content::WebUI* web_ui, 159 bool incognito, 160 const std::string* filter, 161 base::ListValue* exceptions); 162 163 // Fills in object saying what the current settings is for the category (such as 164 // enabled or blocked) and the source of that setting (such preference, policy, 165 // or extension). 166 void GetContentCategorySetting(const HostContentSettingsMap* map, 167 ContentSettingsType content_type, 168 base::DictionaryValue* object); 169 170 // Retrieves the current setting for a given origin, category pair, the source 171 // of that setting, and its display name, which will be different if it's an 172 // extension. Note this is similar to GetContentCategorySetting() above but this 173 // goes through the PermissionManager (preferred, see https://crbug.com/739241). 174 ContentSetting GetContentSettingForOrigin( 175 Profile* profile, 176 const HostContentSettingsMap* map, 177 const GURL& origin, 178 ContentSettingsType content_type, 179 std::string* source_string, 180 const extensions::ExtensionRegistry* extension_registry, 181 std::string* display_name); 182 183 // Returns exceptions constructed from the policy-set allowed URLs 184 // for the content settings |type| mic or camera. 185 void GetPolicyAllowedUrls( 186 ContentSettingsType type, 187 std::vector<std::unique_ptr<base::DictionaryValue>>* exceptions, 188 const extensions::ExtensionRegistry* extension_registry, 189 content::WebUI* web_ui, 190 bool incognito); 191 192 // Returns all site permission exceptions for a given content type 193 std::vector<ContentSettingPatternSource> GetSiteExceptionsForContentType( 194 HostContentSettingsMap* map, 195 ContentSettingsType content_type); 196 197 // This struct facilitates lookup of a chooser context factory function by name 198 // for a given content settings type and is declared early so that it can used 199 // by functions below. 200 struct ChooserTypeNameEntry { 201 permissions::ChooserContextBase* (*get_context)(Profile*); 202 const char* name; 203 }; 204 205 struct ContentSettingsTypeNameEntry { 206 ContentSettingsType type; 207 const char* name; 208 }; 209 210 const ChooserTypeNameEntry* ChooserTypeFromGroupName(const std::string& name); 211 212 // Creates a chooser exception object for the object with |display_name|. The 213 // object contains the following properties 214 // * displayName: string, 215 // * object: Object, 216 // * chooserType: string, 217 // * sites: Array<SiteException> 218 // The structure of the SiteException objects is the same as the objects 219 // returned by GetExceptionForPage(). 220 base::Value CreateChooserExceptionObject( 221 const base::string16& display_name, 222 const base::Value& object, 223 const std::string& chooser_type, 224 const ChooserExceptionDetails& chooser_exception_details); 225 226 // Returns an array of chooser exception objects. 227 base::Value GetChooserExceptionListFromProfile( 228 Profile* profile, 229 const ChooserTypeNameEntry& chooser_type); 230 231 // Returns the cookie controls manage state for a given profile. 232 CookieControlsManagedState GetCookieControlsManagedState(Profile* profile); 233 234 // Concerts a PolicyIndicatorType to its string identifier. 235 std::string PolicyIndicatorTypeToString(const PolicyIndicatorType type); 236 237 // Returns the appropriate indicator for the source of a preference. 238 PolicyIndicatorType GetPolicyIndicatorFromPref( 239 const PrefService::Preference* pref); 240 241 } // namespace site_settings 242 243 #endif // CHROME_BROWSER_UI_WEBUI_SITE_SETTINGS_HELPER_H_ 244