OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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 #include "chrome/browser/extensions/extension_content_settings_api.h" | |
6 | |
7 #include "base/values.h" | |
8 #include "chrome/browser/content_settings/host_content_settings_map.h" | |
9 #include "chrome/browser/extensions/extension_service.h" | |
10 #include "chrome/browser/extensions/extension_content_settings_api_constants.h" | |
11 #include "chrome/browser/extensions/extension_content_settings_helpers.h" | |
12 #include "chrome/browser/extensions/extension_content_settings_store.h" | |
13 #include "chrome/browser/extensions/extension_preference_api_constants.h" | |
14 #include "chrome/browser/extensions/extension_preference_helpers.h" | |
15 #include "chrome/browser/profiles/profile.h" | |
16 #include "chrome/common/extensions/extension_error_utils.h" | |
17 | |
18 namespace helpers = extension_content_settings_helpers; | |
19 namespace keys = extension_content_settings_api_constants; | |
20 namespace pref_helpers = extension_preference_helpers; | |
21 namespace pref_keys = extension_preference_api_constants; | |
22 | |
23 bool ClearContentSettingsFunction::RunImpl() { | |
24 std::string content_type_str; | |
25 EXTENSION_FUNCTION_VALIDATE( | |
26 args_->GetString(0, &content_type_str)); | |
battre
2011/06/06 23:45:13
nit: single line?
Bernhard Bauer
2011/06/07 13:48:13
Done.
| |
27 ContentSettingsType content_type = | |
28 helpers::StringToContentSettingsType(content_type_str); | |
29 EXTENSION_FUNCTION_VALIDATE(content_type != CONTENT_SETTINGS_TYPE_DEFAULT); | |
30 | |
31 DictionaryValue* details; | |
battre
2011/06/06 23:45:13
nit: initialize with NULL
Bernhard Bauer
2011/06/07 13:48:13
Done.
| |
32 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); | |
33 | |
34 extension_prefs_scope::Scope scope = extension_prefs_scope::kRegular; | |
35 if (details->HasKey(pref_keys::kScopeKey)) { | |
36 std::string scope_str; | |
37 EXTENSION_FUNCTION_VALIDATE(details->GetString(pref_keys::kScopeKey, | |
38 &scope_str)); | |
39 | |
40 EXTENSION_FUNCTION_VALIDATE(pref_helpers::StringToScope(scope_str, &scope)); | |
41 EXTENSION_FUNCTION_VALIDATE( | |
42 scope != extension_prefs_scope::kIncognitoPersistent); | |
43 } | |
44 | |
45 bool incognito = (scope == extension_prefs_scope::kIncognitoPersistent || | |
46 scope == extension_prefs_scope::kIncognitoSessionOnly); | |
47 if (incognito) { | |
48 // We don't check incognito permissions here, as an extension should be | |
49 // always allowed to clear its own settings. | |
50 } else { | |
51 // Incognito profiles can't access regular mode ever, they only exist in | |
52 // split mode. | |
53 if (profile()->IsOffTheRecord()) { | |
54 error_ = "Can't modify regular settings from an incognito context."; | |
battre
2011/06/06 23:45:13
nit: export this like keys::kInvalidUrlError?
Bernhard Bauer
2011/06/07 13:48:13
Done.
| |
55 return false; | |
56 } | |
57 } | |
58 | |
59 ExtensionContentSettingsStore* store = | |
60 profile_->GetExtensionService()->GetExtensionContentSettingsStore(); | |
61 store->ClearContentSettingsForExtension(extension_id(), scope); | |
62 | |
63 return true; | |
64 } | |
65 | |
66 bool GetContentSettingFunction::RunImpl() { | |
67 std::string content_type_str; | |
68 EXTENSION_FUNCTION_VALIDATE( | |
69 args_->GetString(0, &content_type_str)); | |
battre
2011/06/06 23:45:13
nit: single line?
Bernhard Bauer
2011/06/07 13:48:13
Done.
| |
70 ContentSettingsType content_type = | |
71 helpers::StringToContentSettingsType(content_type_str); | |
72 EXTENSION_FUNCTION_VALIDATE(content_type != CONTENT_SETTINGS_TYPE_DEFAULT); | |
73 | |
74 DictionaryValue* details = NULL; | |
75 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); | |
76 | |
77 std::string embedded_url_spec; | |
78 EXTENSION_FUNCTION_VALIDATE( | |
79 details->GetString(keys::kEmbeddedUrlKey, &embedded_url_spec)); | |
80 GURL embedded_url(embedded_url_spec); | |
81 if (!embedded_url.is_valid()) { | |
82 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kInvalidUrlError, | |
83 embedded_url_spec); | |
84 return false; | |
85 } | |
86 | |
87 std::string top_level_url_spec; | |
88 EXTENSION_FUNCTION_VALIDATE( | |
89 details->GetString(keys::kTopLevelUrlKey, &top_level_url_spec)); | |
90 GURL top_level_url(top_level_url_spec); | |
91 if (!top_level_url.is_valid()) { | |
92 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kInvalidUrlError, | |
93 top_level_url_spec); | |
94 return false; | |
95 } | |
96 | |
97 std::string resource_identifier; | |
98 if (details->HasKey(keys::kResourceIdentifierKey)) { | |
99 DictionaryValue* resource_identifier_dict = NULL; | |
100 EXTENSION_FUNCTION_VALIDATE( | |
101 details->GetDictionary(keys::kResourceIdentifierKey, | |
102 &resource_identifier_dict)); | |
103 EXTENSION_FUNCTION_VALIDATE( | |
104 resource_identifier_dict->GetString(keys::kIdKey, | |
105 &resource_identifier)); | |
106 } | |
107 | |
108 bool incognito = false; | |
109 if (details->HasKey(pref_keys::kIncognitoKey)) { | |
110 EXTENSION_FUNCTION_VALIDATE( | |
111 details->GetBoolean(pref_keys::kIncognitoKey, &incognito)); | |
112 } | |
113 if (incognito && !include_incognito()) { | |
114 error_ = pref_keys::kIncognitoErrorMessage; | |
115 return false; | |
116 } | |
117 | |
118 HostContentSettingsMap* map; | |
119 if (incognito) { | |
120 if (!profile()->HasOffTheRecordProfile()) { | |
121 // TODO(bauerb): Allow reading incognito content settings | |
122 // outside of an incognito session. | |
123 error_ = "You cannot read incognito content settings " | |
124 "when no incognito window is open."; | |
battre
2011/06/06 23:45:13
nit: export error message?
Bernhard Bauer
2011/06/07 13:48:13
Done.
| |
125 return false; | |
126 } | |
127 map = profile()->GetOffTheRecordProfile()->GetHostContentSettingsMap(); | |
128 } else { | |
129 map = profile()->GetHostContentSettingsMap(); | |
130 } | |
131 | |
132 ContentSetting setting; | |
133 if (content_type == CONTENT_SETTINGS_TYPE_COOKIES) { | |
134 // TODO(jochen): Do we return the value for setting or for reading cookies? | |
battre
2011/06/06 23:45:13
add an optional parameter?
Bernhard Bauer
2011/06/07 13:48:13
Hm. I'm not sure adding an additional parameter fo
| |
135 bool setting_cookie = false; | |
136 setting = map->GetCookieContentSetting(embedded_url, top_level_url, | |
137 setting_cookie); | |
138 } else { | |
139 setting = map->GetContentSetting(top_level_url, content_type, | |
140 resource_identifier); | |
141 } | |
142 | |
143 DictionaryValue* result = new DictionaryValue(); | |
144 result->SetString(keys::kContentSettingKey, | |
145 helpers::ContentSettingToString(setting)); | |
146 | |
147 result_.reset(result); | |
148 | |
149 return true; | |
150 } | |
151 | |
152 bool SetContentSettingFunction::RunImpl() { | |
153 std::string content_type_str; | |
154 EXTENSION_FUNCTION_VALIDATE( | |
155 args_->GetString(0, &content_type_str)); | |
battre
2011/06/06 23:45:13
nit: single line?
Bernhard Bauer
2011/06/07 13:48:13
Done.
| |
156 ContentSettingsType content_type = | |
157 helpers::StringToContentSettingsType(content_type_str); | |
158 EXTENSION_FUNCTION_VALIDATE(content_type != CONTENT_SETTINGS_TYPE_DEFAULT); | |
159 | |
160 DictionaryValue* details = NULL; | |
161 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); | |
162 | |
163 DictionaryValue* top_level_pattern_dict = NULL; | |
164 EXTENSION_FUNCTION_VALIDATE( | |
165 details->GetDictionary(keys::kTopLevelPatternKey, | |
166 &top_level_pattern_dict)); | |
167 std::string top_level_pattern_str; | |
168 EXTENSION_FUNCTION_VALIDATE( | |
169 top_level_pattern_dict->GetString(keys::kPatternKey, | |
170 &top_level_pattern_str)); | |
171 ContentSettingsPattern top_level_pattern = | |
172 ContentSettingsPattern::FromString(top_level_pattern_str); | |
173 EXTENSION_FUNCTION_VALIDATE(top_level_pattern.IsValid()); | |
174 | |
175 DictionaryValue* embedded_pattern_dict; | |
battre
2011/06/06 23:45:13
nit: initialize with NULL
Bernhard Bauer
2011/06/07 13:48:13
Done.
| |
176 EXTENSION_FUNCTION_VALIDATE( | |
177 details->GetDictionary(keys::kEmbeddedPatternKey, | |
178 &embedded_pattern_dict)); | |
179 std::string embedded_pattern_str; | |
180 EXTENSION_FUNCTION_VALIDATE( | |
181 embedded_pattern_dict->GetString(keys::kPatternKey, | |
182 &embedded_pattern_str)); | |
183 ContentSettingsPattern embedded_pattern = | |
184 ContentSettingsPattern::FromString(embedded_pattern_str); | |
185 EXTENSION_FUNCTION_VALIDATE(embedded_pattern.IsValid()); | |
186 | |
187 std::string resource_identifier; | |
188 if (details->HasKey(keys::kResourceIdentifierKey)) { | |
189 DictionaryValue* resource_identifier_dict = NULL; | |
190 EXTENSION_FUNCTION_VALIDATE( | |
191 details->GetDictionary(keys::kResourceIdentifierKey, | |
192 &resource_identifier_dict)); | |
193 EXTENSION_FUNCTION_VALIDATE( | |
194 resource_identifier_dict->GetString(keys::kIdKey, | |
195 &resource_identifier)); | |
196 } | |
197 std::string setting_str; | |
198 EXTENSION_FUNCTION_VALIDATE( | |
199 details->GetString(keys::kContentSettingKey, &setting_str)); | |
200 ContentSetting setting = CONTENT_SETTING_DEFAULT; | |
201 EXTENSION_FUNCTION_VALIDATE( | |
202 helpers::StringToContentSetting(setting_str, &setting)); | |
203 EXTENSION_FUNCTION_VALIDATE( | |
204 HostContentSettingsMap::IsSettingAllowedForType(content_type, setting)); | |
205 | |
206 extension_prefs_scope::Scope scope = extension_prefs_scope::kRegular; | |
207 if (details->HasKey(pref_keys::kScopeKey)) { | |
208 std::string scope_str; | |
209 EXTENSION_FUNCTION_VALIDATE(details->GetString(pref_keys::kScopeKey, | |
210 &scope_str)); | |
211 | |
212 EXTENSION_FUNCTION_VALIDATE(pref_helpers::StringToScope(scope_str, &scope)); | |
213 EXTENSION_FUNCTION_VALIDATE( | |
214 scope != extension_prefs_scope::kIncognitoPersistent); | |
battre
2011/06/06 23:45:13
So we do not want to allow setting default Content
Bernhard Bauer
2011/06/07 13:48:13
Not yet. Supporting this requires the incognito Ho
| |
215 } | |
216 | |
217 bool incognito = (scope == extension_prefs_scope::kIncognitoPersistent || | |
218 scope == extension_prefs_scope::kIncognitoSessionOnly); | |
219 if (incognito) { | |
220 // Regular profiles can't access incognito unless include_incognito is true. | |
221 if (!profile()->IsOffTheRecord() && !include_incognito()) { | |
222 error_ = pref_keys::kIncognitoErrorMessage; | |
223 return false; | |
224 } | |
225 } else { | |
226 // Incognito profiles can't access regular mode ever, they only exist in | |
227 // split mode. | |
228 if (profile()->IsOffTheRecord()) { | |
229 error_ = "Can't modify regular settings from an incognito context."; | |
battre
2011/06/06 23:45:13
nit: externalize error message?
Bernhard Bauer
2011/06/07 13:48:13
Done.
| |
230 return false; | |
231 } | |
232 } | |
233 | |
234 if (scope == extension_prefs_scope::kIncognitoSessionOnly && | |
235 !profile_->HasOffTheRecordProfile()) { | |
236 error_ = pref_keys::kIncognitoSessionOnlyErrorMessage; | |
237 return false; | |
238 } | |
239 | |
240 ExtensionContentSettingsStore* store = | |
241 profile_->GetExtensionService()->GetExtensionContentSettingsStore(); | |
242 store->SetExtensionContentSetting(extension_id(), top_level_pattern, | |
battre
2011/06/06 23:45:13
no distinction between setting and reading cookies
Bernhard Bauer
2011/06/07 13:48:13
For getting the content setting, HostContentSettin
| |
243 embedded_pattern, content_type, | |
244 resource_identifier, setting, scope); | |
245 return true; | |
246 } | |
OLD | NEW |