Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(96)

Side by Side Diff: chrome/browser/ui/webui/options/website_settings_handler.cc

Issue 475193002: Add a single site view page to view and modify content settings. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/ui/webui/options/website_settings_handler.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 #include "chrome/browser/ui/webui/options/website_settings_handler.h" 5 #include "chrome/browser/ui/webui/options/website_settings_handler.h"
6 6
7 #include "chrome/browser/content_settings/content_settings_utils.h" 7 #include "chrome/browser/content_settings/content_settings_utils.h"
8 #include "chrome/browser/content_settings/host_content_settings_map.h" 8 #include "chrome/browser/content_settings/host_content_settings_map.h"
9 #include "chrome/browser/profiles/profile.h" 9 #include "chrome/browser/profiles/profile.h"
10 #include "content/public/browser/web_ui.h" 10 #include "content/public/browser/web_ui.h"
11 #include "grit/generated_resources.h" 11 #include "grit/generated_resources.h"
12 #include "ui/base/l10n/l10n_util.h"
12 #include "ui/base/l10n/time_format.h" 13 #include "ui/base/l10n/time_format.h"
13 #include "ui/base/text/bytes_formatting.h" 14 #include "ui/base/text/bytes_formatting.h"
14 15
15 namespace { 16 namespace {
16 17
17 const int kHttpPort = 80; 18 const int kHttpPort = 80;
18 const int kHttpsPort = 443; 19 const int kHttpsPort = 443;
19 const char kPreferencesSource[] = "preference"; 20 const char kPreferencesSource[] = "preference";
20 const char kStorage[] = "storage"; 21 const char kStorage[] = "storage";
21 const ContentSettingsType kValidTypes[] = {CONTENT_SETTINGS_TYPE_GEOLOCATION, 22 const ContentSettingsType kValidTypes[] = {CONTENT_SETTINGS_TYPE_GEOLOCATION,
22 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, 23 CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
23 CONTENT_SETTINGS_TYPE_MEDIASTREAM}; 24 CONTENT_SETTINGS_TYPE_MEDIASTREAM};
24 const size_t kValidTypesLength = arraysize(kValidTypes); 25 const size_t kValidTypesLength = arraysize(kValidTypes);
25 } // namespace 26 } // namespace
26 27
27 namespace options { 28 namespace options {
28 29
29 WebsiteSettingsHandler::WebsiteSettingsHandler() 30 WebsiteSettingsHandler::WebsiteSettingsHandler()
30 : observer_(this), weak_ptr_factory_(this) { 31 : observer_(this), weak_ptr_factory_(this) {
31 } 32 }
32 33
33 WebsiteSettingsHandler::~WebsiteSettingsHandler() { 34 WebsiteSettingsHandler::~WebsiteSettingsHandler() {
34 } 35 }
35 36
36 void WebsiteSettingsHandler::GetLocalizedValues( 37 void WebsiteSettingsHandler::GetLocalizedValues(
37 base::DictionaryValue* localized_strings) { 38 base::DictionaryValue* localized_strings) {
38 DCHECK(localized_strings); 39 DCHECK(localized_strings);
39 40
40 static OptionsStringResource resources[] = { 41 static OptionsStringResource resources[] = {
41 {"websitesOptionsPageTabTitle", IDS_WEBSITES_SETTINGS_TITLE}, 42 {"websitesOptionsPageTabTitle", IDS_WEBSITE_SETTINGS_TITLE},
43 {"websitesSettingsEditPage", IDS_WEBSITE_SETTINGS_EDIT_TITLE},
42 {"websitesManage", IDS_WEBSITE_SETTINGS_MANAGE}, 44 {"websitesManage", IDS_WEBSITE_SETTINGS_MANAGE},
43 {"websitesSearch", IDS_WEBSITE_SETTINGS_SEARCH_ORIGINS}, 45 {"websitesSearch", IDS_WEBSITE_SETTINGS_SEARCH_ORIGINS},
44 {"websitesLabelGeolocation", IDS_WEBSITE_SETTINGS_TYPE_LOCATION}, 46 {"websitesLabelGeolocation", IDS_WEBSITE_SETTINGS_TYPE_LOCATION},
45 {"websitesLabelMediaStream", IDS_WEBSITE_SETTINGS_TYPE_MEDIASTREAM}, 47 {"websitesLabelMediaStream", IDS_WEBSITE_SETTINGS_TYPE_MEDIASTREAM},
46 {"websitesLabelNotifications", IDS_WEBSITE_SETTINGS_TYPE_NOTIFICATIONS}, 48 {"websitesLabelNotifications", IDS_WEBSITE_SETTINGS_TYPE_NOTIFICATIONS},
47 {"websitesLabelStorage", IDS_WEBSITE_SETTINGS_TYPE_STORAGE}, 49 {"websitesLabelStorage", IDS_WEBSITE_SETTINGS_TYPE_STORAGE},
50 {"websitesLocationDescription",
51 IDS_WEBSITE_SETTINGS_LOCATION_DESCRIPTION},
52 {"websitesMediastreamDescription",
53 IDS_WEBSITE_SETTINGS_MEDIASTREAM_DESCRIPTION},
54 {"websitesNotificationsDescription",
55 IDS_WEBSITE_SETTINGS_NOTIFICATIONS_DESCRIPTION},
48 }; 56 };
49 57
50 RegisterStrings(localized_strings, resources, arraysize(resources)); 58 RegisterStrings(localized_strings, resources, arraysize(resources));
51 RegisterTitle( 59 RegisterTitle(
52 localized_strings, "websiteSettingsPage", IDS_WEBSITES_SETTINGS_TITLE); 60 localized_strings, "websiteSettingsPage", IDS_WEBSITE_SETTINGS_TITLE);
53 } 61 }
54 62
55 void WebsiteSettingsHandler::InitializeHandler() { 63 void WebsiteSettingsHandler::InitializeHandler() {
56 Profile* profile = Profile::FromWebUI(web_ui()); 64 Profile* profile = Profile::FromWebUI(web_ui());
57 HostContentSettingsMap* settings = profile->GetHostContentSettingsMap(); 65 HostContentSettingsMap* settings = profile->GetHostContentSettingsMap();
58 observer_.Add(settings); 66 observer_.Add(settings);
59 } 67 }
60 68
61 void WebsiteSettingsHandler::RegisterMessages() { 69 void WebsiteSettingsHandler::RegisterMessages() {
62 web_ui()->RegisterMessageCallback( 70 web_ui()->RegisterMessageCallback(
63 "updateOrigins", 71 "updateOrigins",
64 base::Bind(&WebsiteSettingsHandler::HandleUpdateOrigins, 72 base::Bind(&WebsiteSettingsHandler::HandleUpdateOrigins,
65 base::Unretained(this))); 73 base::Unretained(this)));
66 74
67 web_ui()->RegisterMessageCallback( 75 web_ui()->RegisterMessageCallback(
68 "updateOriginsSearchResults", 76 "updateOriginsSearchResults",
69 base::Bind(&WebsiteSettingsHandler::HandleUpdateSearchResults, 77 base::Bind(&WebsiteSettingsHandler::HandleUpdateSearchResults,
70 base::Unretained(this))); 78 base::Unretained(this)));
71 79
72 web_ui()->RegisterMessageCallback( 80 web_ui()->RegisterMessageCallback(
73 "updateLocalStorage", 81 "updateLocalStorage",
74 base::Bind(&WebsiteSettingsHandler::HandleUpdateLocalStorage, 82 base::Bind(&WebsiteSettingsHandler::HandleUpdateLocalStorage,
75 base::Unretained(this))); 83 base::Unretained(this)));
84
85 web_ui()->RegisterMessageCallback(
86 "getOriginInfo",
87 base::Bind(&WebsiteSettingsHandler::HandleGetOriginInfo,
88 base::Unretained(this)));
89
90 web_ui()->RegisterMessageCallback(
91 "setOriginPermission",
92 base::Bind(&WebsiteSettingsHandler::HandleSetOriginPermission,
93 base::Unretained(this)));
94
95 web_ui()->RegisterMessageCallback(
96 "maybeShowEditPage",
97 base::Bind(&WebsiteSettingsHandler::HandleMaybeShowEditPage,
98 base::Unretained(this)));
76 } 99 }
77 100
78 // content_settings::Observer implementation. 101 // content_settings::Observer implementation.
79 void WebsiteSettingsHandler::OnContentSettingChanged( 102 void WebsiteSettingsHandler::OnContentSettingChanged(
80 const ContentSettingsPattern& primary_pattern, 103 const ContentSettingsPattern& primary_pattern,
81 const ContentSettingsPattern& secondary_pattern, 104 const ContentSettingsPattern& secondary_pattern,
82 ContentSettingsType content_type, 105 ContentSettingsType content_type,
83 std::string resource_identifier) { 106 std::string resource_identifier) {
84 Update(); 107 Update();
85 } 108 }
(...skipping 29 matching lines...) Expand all
115 local_storage_ = new BrowsingDataLocalStorageHelper(profile); 138 local_storage_ = new BrowsingDataLocalStorageHelper(profile);
116 } 139 }
117 140
118 last_setting_ = kStorage; 141 last_setting_ = kStorage;
119 142
120 local_storage_->StartFetching( 143 local_storage_->StartFetching(
121 base::Bind(&WebsiteSettingsHandler::OnLocalStorageFetched, 144 base::Bind(&WebsiteSettingsHandler::OnLocalStorageFetched,
122 weak_ptr_factory_.GetWeakPtr())); 145 weak_ptr_factory_.GetWeakPtr()));
123 } 146 }
124 147
148 void WebsiteSettingsHandler::HandleMaybeShowEditPage(
149 const base::ListValue* args) {
150 std::string site;
151 bool rv = args->GetString(0, &site);
152 DCHECK(rv);
153
154 GURL last_site(site);
155 if (!last_site.is_valid())
156 return;
157
158 last_site_ = last_site;
159 base::StringValue site_value(site);
160 web_ui()->CallJavascriptFunction("WebsiteSettingsManager.showEditPage",
161 site_value);
162 }
163
125 void WebsiteSettingsHandler::OnLocalStorageFetched(const std::list< 164 void WebsiteSettingsHandler::OnLocalStorageFetched(const std::list<
126 BrowsingDataLocalStorageHelper::LocalStorageInfo>& storage) { 165 BrowsingDataLocalStorageHelper::LocalStorageInfo>& storage) {
127 local_storage_list_ = storage; 166 local_storage_list_ = storage;
128 UpdateLocalStorage(); 167 UpdateLocalStorage();
129 } 168 }
130 169
131 void WebsiteSettingsHandler::Update() { 170 void WebsiteSettingsHandler::Update() {
132 DCHECK(!last_setting_.empty()); 171 DCHECK(!last_setting_.empty());
133 if (last_setting_ == kStorage) 172 if (last_setting_ == kStorage)
134 UpdateLocalStorage(); 173 UpdateLocalStorage();
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 } 240 }
202 origin_entry->SetStringWithoutPathExpansion("usageString", usage_string); 241 origin_entry->SetStringWithoutPathExpansion("usageString", usage_string);
203 242
204 origins.SetWithoutPathExpansion(origin, origin_entry); 243 origins.SetWithoutPathExpansion(origin, origin_entry);
205 } 244 }
206 245
207 web_ui()->CallJavascriptFunction("WebsiteSettingsManager.populateOrigins", 246 web_ui()->CallJavascriptFunction("WebsiteSettingsManager.populateOrigins",
208 origins); 247 origins);
209 } 248 }
210 249
250 void WebsiteSettingsHandler::HandleGetOriginInfo(const base::ListValue* args) {
251 std::string url;
252 bool rv = args->GetString(0, &url);
253 DCHECK(rv);
254 GURL origin(url);
255
256 if (!origin.is_valid())
257 return;
258
259 GetInfoForOrigin(origin);
260 }
261
262 void WebsiteSettingsHandler::HandleSetOriginPermission(
263 const base::ListValue* args) {
264 std::string setting_name;
265 bool rv = args->GetString(0, &setting_name);
266 DCHECK(rv);
267 ContentSettingsType settings_type;
268 rv = content_settings::GetTypeFromName(setting_name, &settings_type);
269 DCHECK(rv);
270
271 std::string value;
272 rv = args->GetString(1, &value);
273 DCHECK(rv);
274
275 ContentSetting setting = content_settings::ContentSettingFromString(value);
276 Profile* profile = Profile::FromWebUI(web_ui());
277 HostContentSettingsMap* map = profile->GetHostContentSettingsMap();
278 ContentSetting default_value =
279 map->GetDefaultContentSetting(settings_type, NULL);
280
281 // Users are not allowed to be the source of the "ask" setting. It is an
282 // ephemeral setting which is removed once the question is asked.
283 if (setting == CONTENT_SETTING_ASK && setting == default_value)
284 setting = CONTENT_SETTING_DEFAULT;
285
286 ContentSettingsPattern primary_pattern;
287 ContentSettingsPattern secondary_pattern;
288 switch (settings_type) {
289 case CONTENT_SETTINGS_TYPE_NOTIFICATIONS:
290 primary_pattern = ContentSettingsPattern::FromURLNoWildcard(last_site_);
291 secondary_pattern = ContentSettingsPattern::Wildcard();
292 break;
293 case CONTENT_SETTINGS_TYPE_GEOLOCATION:
294 primary_pattern = ContentSettingsPattern::FromURLNoWildcard(last_site_);
295 secondary_pattern = ContentSettingsPattern::FromURLNoWildcard(last_site_);
296 break;
297 case CONTENT_SETTINGS_TYPE_MEDIASTREAM:
298 primary_pattern = ContentSettingsPattern::FromURLNoWildcard(last_site_);
299 secondary_pattern = ContentSettingsPattern::Wildcard();
300 map->SetContentSetting(primary_pattern,
301 secondary_pattern,
302 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC,
303 std::string(),
304 setting);
305 map->SetContentSetting(primary_pattern,
306 secondary_pattern,
307 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA,
308 std::string(),
309 setting);
310 return;
311 default:
312 NOTREACHED() << "Content settings type not yet supported.";
313 break;
314 }
315
316 content_settings::SettingInfo info;
317 scoped_ptr<base::Value> v(map->GetWebsiteSetting(
318 last_site_, last_site_, settings_type, std::string(), &info));
319 map->SetNarrowestWebsiteSetting(primary_pattern,
320 secondary_pattern,
321 settings_type,
322 std::string(),
323 setting,
324 info);
325 }
326
327 void WebsiteSettingsHandler::GetInfoForOrigin(const GURL& site_url) {
328 Profile* profile = Profile::FromWebUI(web_ui());
329 HostContentSettingsMap* map = profile->GetHostContentSettingsMap();
330
331 double storage = 0.0;
332 for (LocalStorageList::const_iterator it = local_storage_list_.begin();
333 it != local_storage_list_.end();
334 it++) {
335 if (it->origin_url == site_url) {
336 storage = static_cast<double>(it->size);
337 break;
338 }
339 }
340
341 base::DictionaryValue* permissions = new base::DictionaryValue;
342 for (size_t i = 0; i < arraysize(kValidTypes); ++i) {
343 ContentSettingsType permission_type = kValidTypes[i];
344
345 // Append the possible settings.
346 base::ListValue* options = new base::ListValue;
347 ContentSetting default_value =
348 map->GetDefaultContentSetting(permission_type, NULL);
349 if (default_value != CONTENT_SETTING_ALLOW &&
350 default_value != CONTENT_SETTING_BLOCK) {
351 options->AppendString(
352 content_settings::ContentSettingToString(default_value));
353 }
354 options->AppendString(
355 content_settings::ContentSettingToString(CONTENT_SETTING_ALLOW));
356 options->AppendString(
357 content_settings::ContentSettingToString(CONTENT_SETTING_BLOCK));
358
359 ContentSetting permission;
360 content_settings::SettingInfo info;
361 if (permission_type == CONTENT_SETTINGS_TYPE_MEDIASTREAM) {
362 scoped_ptr<base::Value> mic_value(
363 map->GetWebsiteSetting(site_url,
364 site_url,
365 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC,
366 std::string(),
367 &info));
368 ContentSetting mic_setting =
369 content_settings::ValueToContentSetting(mic_value.get());
370 ContentSetting cam_setting =
371 map->GetContentSetting(site_url,
372 site_url,
373 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA,
374 std::string());
375
376 if (mic_setting != cam_setting)
377 permission = CONTENT_SETTING_ASK;
378 else
379 permission = mic_setting;
380 } else {
381 scoped_ptr<base::Value> v(map->GetWebsiteSetting(
382 site_url, site_url, permission_type, std::string(), &info));
383 permission = content_settings::ValueToContentSetting(v.get());
384 }
385
386 base::DictionaryValue* permission_info = new base::DictionaryValue;
387 permission_info->SetStringWithoutPathExpansion(
388 "setting", content_settings::ContentSettingToString(permission));
389 permission_info->SetWithoutPathExpansion("options", options);
390 permission_info->SetBooleanWithoutPathExpansion(
391 "editable", info.source == content_settings::SETTING_SOURCE_USER);
392 permissions->SetWithoutPathExpansion(
393 content_settings::GetTypeName(permission_type), permission_info);
394 }
395
396 base::Value* storage_used = new base::StringValue(l10n_util::GetStringFUTF16(
397 IDS_WEBSITE_SETTINGS_STORAGE_USED, ui::FormatBytes(storage)));
398
399 web_ui()->CallJavascriptFunction(
400 "WebsiteSettingsEditor.populateOrigin", *storage_used, *permissions);
401 }
402
211 void WebsiteSettingsHandler::UpdateLocalStorage() { 403 void WebsiteSettingsHandler::UpdateLocalStorage() {
212 base::DictionaryValue local_storage_map; 404 base::DictionaryValue local_storage_map;
213 for (LocalStorageList::const_iterator it = local_storage_list_.begin(); 405 for (LocalStorageList::const_iterator it = local_storage_list_.begin();
214 it != local_storage_list_.end(); 406 it != local_storage_list_.end();
215 it++) { 407 it++) {
216 std::string origin = it->origin_url.spec(); 408 std::string origin = it->origin_url.spec();
217 409
218 if (origin.find(last_filter_) == base::string16::npos) 410 if (origin.find(last_filter_) == base::string16::npos)
219 continue; 411 continue;
220 412
221 base::DictionaryValue* origin_entry = new base::DictionaryValue(); 413 base::DictionaryValue* origin_entry = new base::DictionaryValue();
222 origin_entry->SetWithoutPathExpansion( 414 origin_entry->SetWithoutPathExpansion(
223 "usage", new base::FundamentalValue(static_cast<double>(it->size))); 415 "usage", new base::FundamentalValue(static_cast<double>(it->size)));
224 origin_entry->SetWithoutPathExpansion( 416 origin_entry->SetWithoutPathExpansion(
225 "usageString", new base::StringValue(ui::FormatBytes(it->size))); 417 "usageString", new base::StringValue(ui::FormatBytes(it->size)));
226 local_storage_map.SetWithoutPathExpansion(origin, origin_entry); 418 local_storage_map.SetWithoutPathExpansion(origin, origin_entry);
227 } 419 }
228 web_ui()->CallJavascriptFunction("WebsiteSettingsManager.populateOrigins", 420 web_ui()->CallJavascriptFunction("WebsiteSettingsManager.populateOrigins",
229 local_storage_map); 421 local_storage_map);
230 } 422 }
231 423
232 } // namespace options 424 } // namespace options
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/options/website_settings_handler.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698