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

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: 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
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(
251 const base::ListValue* args) {
252 std::string url;
253 bool rv = args->GetString(0, &url);
254 DCHECK(rv);
255 GURL origin(url);
256
257 if (!origin.is_valid())
258 return;
259
260 GetInfoForOrigin(origin);
261 }
262
263 void WebsiteSettingsHandler::HandleSetOriginPermission(
264 const base::ListValue* args) {
265 std::string setting_name;
266 bool rv = args->GetString(0, &setting_name);
267 DCHECK(rv);
268 ContentSettingsType settings_type;
269 rv = content_settings::GetTypeFromName(setting_name, &settings_type);
270 DCHECK(rv);
271
272 std::string value;
273 rv = args->GetString(1, &value);
274 DCHECK(rv);
275
276 ContentSetting setting = content_settings::ContentSettingFromString(value);
277 Profile* profile = Profile::FromWebUI(web_ui());
278 HostContentSettingsMap* map = profile->GetHostContentSettingsMap();
279 ContentSetting default_value =
280 map->GetDefaultContentSetting(settings_type, NULL);
281
282 // Users are not allowed to be the source of the "ask" setting. It is an
283 // ephemeral setting which is removed once the question is asked.
284 if (setting == CONTENT_SETTING_ASK && setting == default_value)
285 setting = CONTENT_SETTING_DEFAULT;
286
287 ContentSettingsPattern primary_pattern;
288 ContentSettingsPattern secondary_pattern;
289 switch (settings_type) {
290 case CONTENT_SETTINGS_TYPE_NOTIFICATIONS:
291 primary_pattern = ContentSettingsPattern::FromURLNoWildcard(last_site_);
292 secondary_pattern = ContentSettingsPattern::Wildcard();
293 break;
294 case CONTENT_SETTINGS_TYPE_GEOLOCATION:
295 primary_pattern = ContentSettingsPattern::FromURLNoWildcard(last_site_);
296 secondary_pattern = ContentSettingsPattern::FromURLNoWildcard(last_site_);
297 break;
298 case CONTENT_SETTINGS_TYPE_MEDIASTREAM:
299 primary_pattern = ContentSettingsPattern::FromURLNoWildcard(last_site_);
300 secondary_pattern = ContentSettingsPattern::Wildcard();
301 map->SetContentSetting(primary_pattern,
302 secondary_pattern,
303 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC,
304 std::string(),
305 setting);
306 map->SetContentSetting(primary_pattern,
307 secondary_pattern,
308 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA,
309 std::string(),
310 setting);
311 return;
312 default:
313 NOTREACHED() << "Content settings type not yet supported.";
314 break;
315 }
316
317 content_settings::SettingInfo info;
318 scoped_ptr<base::Value> v(map->GetWebsiteSetting(
319 last_site_, last_site_, settings_type, std::string(), &info));
320 map->SetNarrowestWebsiteSetting(primary_pattern,
321 secondary_pattern,
322 settings_type,
323 std::string(),
324 setting,
325 info);
326 }
327
328 void WebsiteSettingsHandler::GetInfoForOrigin(const GURL& site_url) {
329 Profile* profile = Profile::FromWebUI(web_ui());
330 HostContentSettingsMap* map = profile->GetHostContentSettingsMap();
331
332 double storage = 0.0;
333 for (LocalStorageList::const_iterator it = local_storage_list_.begin();
334 it != local_storage_list_.end();
335 it++) {
336 if (it->origin_url == site_url) {
337 storage = static_cast<double>(it->size);
338 break;
339 }
340 }
341
342 base::ListValue args;
343 args.AppendString(l10n_util::GetStringFUTF16(
344 IDS_WEBSITE_SETTINGS_STORAGE_USED, ui::FormatBytes(storage)));
345
346 base::DictionaryValue* permissions = new base::DictionaryValue;
347 for (size_t i = 0; i < arraysize(kValidTypes); ++i) {
348 ContentSettingsType permission_type = kValidTypes[i];
349
350 // Append the possible settings.
351 base::ListValue* options = new base::ListValue;
352 ContentSetting default_value =
353 map->GetDefaultContentSetting(permission_type, NULL);
354 if (default_value != CONTENT_SETTING_ALLOW &&
355 default_value != CONTENT_SETTING_BLOCK) {
356 options->AppendString(
357 content_settings::ContentSettingToString(default_value));
358 }
359 options->AppendString(
360 content_settings::ContentSettingToString(CONTENT_SETTING_ALLOW));
361 options->AppendString(
362 content_settings::ContentSettingToString(CONTENT_SETTING_BLOCK));
363
364 ContentSetting permission;
365 content_settings::SettingInfo info;
366 if (permission_type == CONTENT_SETTINGS_TYPE_MEDIASTREAM) {
367 scoped_ptr<base::Value> mic_value(
368 map->GetWebsiteSetting(site_url,
369 site_url,
370 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC,
371 std::string(),
372 &info));
373 ContentSetting mic_setting =
374 content_settings::ValueToContentSetting(mic_value.get());
375 ContentSetting cam_setting =
376 map->GetContentSetting(site_url,
377 site_url,
378 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA,
379 std::string());
380
381 if (mic_setting != cam_setting)
382 permission = CONTENT_SETTING_ASK;
383 else
384 permission = mic_setting;
385 } else {
386 scoped_ptr<base::Value> v(map->GetWebsiteSetting(
387 site_url, site_url, permission_type, std::string(), &info));
388 permission = content_settings::ValueToContentSetting(v.get());
389 }
390
391 base::DictionaryValue* permission_info = new base::DictionaryValue;
392 permission_info->SetStringWithoutPathExpansion(
393 "setting", content_settings::ContentSettingToString(permission));
394 permission_info->SetWithoutPathExpansion("options", options);
395 permission_info->SetBooleanWithoutPathExpansion(
396 "editable", info.source == content_settings::SETTING_SOURCE_USER);
397 permissions->SetWithoutPathExpansion(
398 content_settings::GetTypeName(permission_type), permission_info);
399 }
400
401 args.Append(permissions);
402
403 web_ui()->CallJavascriptFunction("WebsiteSettingsEditor.populateOrigin",
404 args);
405 }
406
211 void WebsiteSettingsHandler::UpdateLocalStorage() { 407 void WebsiteSettingsHandler::UpdateLocalStorage() {
212 base::DictionaryValue local_storage_map; 408 base::DictionaryValue local_storage_map;
213 for (LocalStorageList::const_iterator it = local_storage_list_.begin(); 409 for (LocalStorageList::const_iterator it = local_storage_list_.begin();
214 it != local_storage_list_.end(); 410 it != local_storage_list_.end();
215 it++) { 411 it++) {
216 std::string origin = it->origin_url.spec(); 412 std::string origin = it->origin_url.spec();
217 413
218 if (origin.find(last_filter_) == base::string16::npos) 414 if (origin.find(last_filter_) == base::string16::npos)
219 continue; 415 continue;
220 416
221 base::DictionaryValue* origin_entry = new base::DictionaryValue(); 417 base::DictionaryValue* origin_entry = new base::DictionaryValue();
222 origin_entry->SetWithoutPathExpansion( 418 origin_entry->SetWithoutPathExpansion(
223 "usage", new base::FundamentalValue(static_cast<double>(it->size))); 419 "usage", new base::FundamentalValue(static_cast<double>(it->size)));
224 origin_entry->SetWithoutPathExpansion( 420 origin_entry->SetWithoutPathExpansion(
225 "usageString", new base::StringValue(ui::FormatBytes(it->size))); 421 "usageString", new base::StringValue(ui::FormatBytes(it->size)));
226 local_storage_map.SetWithoutPathExpansion(origin, origin_entry); 422 local_storage_map.SetWithoutPathExpansion(origin, origin_entry);
227 } 423 }
228 web_ui()->CallJavascriptFunction("WebsiteSettingsManager.populateOrigins", 424 web_ui()->CallJavascriptFunction("WebsiteSettingsManager.populateOrigins",
229 local_storage_map); 425 local_storage_map);
230 } 426 }
231 427
232 } // namespace options 428 } // namespace options
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698