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

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

Issue 7484072: Migrate geolocation settings to host content settings map. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: " Created 9 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 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/content_settings_handler.h" 5 #include "chrome/browser/ui/webui/options/content_settings_handler.h"
6 6
7 #include <map>
7 #include <string> 8 #include <string>
8 #include <vector> 9 #include <vector>
9 10
10 #include "base/callback.h" 11 #include "base/callback.h"
11 #include "base/command_line.h" 12 #include "base/command_line.h"
12 #include "base/utf_string_conversions.h" 13 #include "base/utf_string_conversions.h"
13 #include "base/values.h" 14 #include "base/values.h"
14 #include "chrome/browser/browser_process.h" 15 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/content_settings/content_settings_details.h" 16 #include "chrome/browser/content_settings/content_settings_details.h"
17 #include "chrome/browser/content_settings/content_settings_pattern.h"
16 #include "chrome/browser/content_settings/content_settings_utils.h" 18 #include "chrome/browser/content_settings/content_settings_utils.h"
17 #include "chrome/browser/content_settings/host_content_settings_map.h" 19 #include "chrome/browser/content_settings/host_content_settings_map.h"
18 #include "chrome/browser/custom_handlers/protocol_handler_registry.h" 20 #include "chrome/browser/custom_handlers/protocol_handler_registry.h"
19 #include "chrome/browser/geolocation/geolocation_content_settings_map.h"
20 #include "chrome/browser/notifications/desktop_notification_service.h" 21 #include "chrome/browser/notifications/desktop_notification_service.h"
21 #include "chrome/browser/notifications/desktop_notification_service_factory.h" 22 #include "chrome/browser/notifications/desktop_notification_service_factory.h"
22 #include "chrome/browser/profiles/profile.h" 23 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/ui/browser_list.h" 24 #include "chrome/browser/ui/browser_list.h"
24 #include "chrome/common/chrome_notification_types.h" 25 #include "chrome/common/chrome_notification_types.h"
25 #include "chrome/common/chrome_switches.h" 26 #include "chrome/common/chrome_switches.h"
26 #include "chrome/common/content_settings_helper.h" 27 #include "chrome/common/content_settings_helper.h"
27 #include "chrome/common/pref_names.h" 28 #include "chrome/common/pref_names.h"
28 #include "chrome/common/url_constants.h" 29 #include "chrome/common/url_constants.h"
29 #include "content/common/content_notification_types.h" 30 #include "content/common/content_notification_types.h"
30 #include "content/common/notification_service.h" 31 #include "content/common/notification_service.h"
31 #include "content/common/notification_source.h" 32 #include "content/common/notification_source.h"
32 #include "grit/generated_resources.h" 33 #include "grit/generated_resources.h"
33 #include "grit/locale_settings.h" 34 #include "grit/locale_settings.h"
34 #include "ui/base/l10n/l10n_util.h" 35 #include "ui/base/l10n/l10n_util.h"
35 36
36 namespace { 37 namespace {
37 38
38 struct ContentSettingsTypeNameEntry { 39 struct ContentSettingsTypeNameEntry {
39 ContentSettingsType type; 40 ContentSettingsType type;
40 const char* name; 41 const char* name;
41 }; 42 };
42 43
44 typedef std::map<ContentSettingsPattern, ContentSetting> OnePatternSettings;
45 typedef std::map<ContentSettingsPattern, OnePatternSettings>
46 AllPatternsSettings;
47
43 const char* kDisplayPattern = "displayPattern"; 48 const char* kDisplayPattern = "displayPattern";
44 const char* kSetting = "setting"; 49 const char* kSetting = "setting";
45 const char* kOrigin = "origin"; 50 const char* kOrigin = "origin";
46 const char* kSource = "source"; 51 const char* kSource = "source";
47 const char* kEmbeddingOrigin = "embeddingOrigin"; 52 const char* kEmbeddingOrigin = "embeddingOrigin";
48 53
49 const ContentSettingsTypeNameEntry kContentSettingsTypeGroupNames[] = { 54 const ContentSettingsTypeNameEntry kContentSettingsTypeGroupNames[] = {
50 {CONTENT_SETTINGS_TYPE_COOKIES, "cookies"}, 55 {CONTENT_SETTINGS_TYPE_COOKIES, "cookies"},
51 {CONTENT_SETTINGS_TYPE_IMAGES, "images"}, 56 {CONTENT_SETTINGS_TYPE_IMAGES, "images"},
52 {CONTENT_SETTINGS_TYPE_JAVASCRIPT, "javascript"}, 57 {CONTENT_SETTINGS_TYPE_JAVASCRIPT, "javascript"},
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 return CONTENT_SETTING_ASK; 97 return CONTENT_SETTING_ASK;
93 if (name == "block") 98 if (name == "block")
94 return CONTENT_SETTING_BLOCK; 99 return CONTENT_SETTING_BLOCK;
95 if (name == "session") 100 if (name == "session")
96 return CONTENT_SETTING_SESSION_ONLY; 101 return CONTENT_SETTING_SESSION_ONLY;
97 102
98 NOTREACHED() << name << " is not a recognized content setting."; 103 NOTREACHED() << name << " is not a recognized content setting.";
99 return CONTENT_SETTING_DEFAULT; 104 return CONTENT_SETTING_DEFAULT;
100 } 105 }
101 106
102 std::string GeolocationExceptionToString(const GURL& origin, 107 std::string GeolocationExceptionToString(
103 const GURL& embedding_origin) { 108 const ContentSettingsPattern& origin,
109 const ContentSettingsPattern& embedding_origin) {
104 if (origin == embedding_origin) 110 if (origin == embedding_origin)
105 return content_settings_helper::OriginToString(origin); 111 return origin.ToString();
106 112
107 // TODO(estade): the page needs to use CSS to indent the string. 113 // TODO(estade): the page needs to use CSS to indent the string.
108 std::string indent(" "); 114 std::string indent(" ");
109 if (embedding_origin.is_empty()) { 115
116 if (embedding_origin == ContentSettingsPattern::Wildcard()) {
110 // NOTE: As long as the user cannot add/edit entries from the exceptions 117 // NOTE: As long as the user cannot add/edit entries from the exceptions
111 // dialog, it's impossible to actually have a non-default setting for some 118 // dialog, it's impossible to actually have a non-default setting for some
112 // origin "embedded on any other site", so this row will never appear. If 119 // origin "embedded on any other site", so this row will never appear. If
113 // we add the ability to add/edit exceptions, we'll need to decide when to 120 // we add the ability to add/edit exceptions, we'll need to decide when to
114 // display this and how "removing" it will function. 121 // display this and how "removing" it will function.
115 return indent + 122 return indent +
116 l10n_util::GetStringUTF8(IDS_EXCEPTIONS_GEOLOCATION_EMBEDDED_ANY_OTHER); 123 l10n_util::GetStringUTF8(IDS_EXCEPTIONS_GEOLOCATION_EMBEDDED_ANY_OTHER);
117 } 124 }
118 125
119 return indent + l10n_util::GetStringFUTF8( 126 return indent + l10n_util::GetStringFUTF8(
120 IDS_EXCEPTIONS_GEOLOCATION_EMBEDDED_ON_HOST, 127 IDS_EXCEPTIONS_GEOLOCATION_EMBEDDED_ON_HOST,
121 UTF8ToUTF16(content_settings_helper::OriginToString(embedding_origin))); 128 UTF8ToUTF16(embedding_origin.ToString()));
122 } 129 }
123 130
124 // Create a DictionaryValue* that will act as a data source for a single row 131 // Create a DictionaryValue* that will act as a data source for a single row
125 // in a HostContentSettingsMap-controlled exceptions table (e.g., cookies). 132 // in a HostContentSettingsMap-controlled exceptions table (e.g., cookies).
126 // Ownership of the pointer is passed to the caller. 133 // Ownership of the pointer is passed to the caller.
127 DictionaryValue* GetExceptionForPage( 134 DictionaryValue* GetExceptionForPage(
128 const ContentSettingsPattern& pattern, 135 const ContentSettingsPattern& pattern,
129 ContentSetting setting, 136 ContentSetting setting,
130 std::string provider_name) { 137 std::string provider_name) {
131 DictionaryValue* exception = new DictionaryValue(); 138 DictionaryValue* exception = new DictionaryValue();
132 exception->Set( 139 exception->SetString(kDisplayPattern, pattern.ToString());
133 kDisplayPattern, 140 exception->SetString(kSetting, ContentSettingToString(setting));
134 new StringValue(pattern.ToString())); 141 exception->SetString(kSource, provider_name);
135 exception->Set(
136 kSetting,
137 new StringValue(ContentSettingToString(setting)));
138 exception->Set(
139 kSource,
140 new StringValue(provider_name));
141 return exception; 142 return exception;
142 } 143 }
143 144
144 // Create a DictionaryValue* that will act as a data source for a single row 145 // Create a DictionaryValue* that will act as a data source for a single row
145 // in the Geolocation exceptions table. Ownership of the pointer is passed to 146 // in the Geolocation exceptions table. Ownership of the pointer is passed to
146 // the caller. 147 // the caller.
147 DictionaryValue* GetGeolocationExceptionForPage(const GURL& origin, 148 DictionaryValue* GetGeolocationExceptionForPage(
148 const GURL& embedding_origin, 149 const ContentSettingsPattern& origin,
149 ContentSetting setting) { 150 const ContentSettingsPattern& embedding_origin,
151 ContentSetting setting) {
150 DictionaryValue* exception = new DictionaryValue(); 152 DictionaryValue* exception = new DictionaryValue();
151 exception->Set( 153 exception->SetString(kDisplayPattern,
152 kDisplayPattern, 154 GeolocationExceptionToString(origin, embedding_origin));
153 new StringValue(GeolocationExceptionToString(origin, embedding_origin))); 155 exception->SetString(kSetting, ContentSettingToString(setting));
154 exception->Set( 156 exception->SetString(kOrigin, origin.ToString());
155 kSetting, 157 exception->SetString(kEmbeddingOrigin, embedding_origin.ToString());
156 new StringValue(ContentSettingToString(setting)));
157 exception->Set(
158 kOrigin,
159 new StringValue(origin.spec()));
160 exception->Set(
161 kEmbeddingOrigin,
162 new StringValue(embedding_origin.spec()));
163 return exception; 158 return exception;
164 } 159 }
165 160
166 // Create a DictionaryValue* that will act as a data source for a single row 161 // Create a DictionaryValue* that will act as a data source for a single row
167 // in the desktop notifications exceptions table. Ownership of the pointer is 162 // in the desktop notifications exceptions table. Ownership of the pointer is
168 // passed to the caller. 163 // passed to the caller.
169 DictionaryValue* GetNotificationExceptionForPage( 164 DictionaryValue* GetNotificationExceptionForPage(
170 const GURL& url, 165 const GURL& url,
171 ContentSetting setting) { 166 ContentSetting setting) {
172 DictionaryValue* exception = new DictionaryValue(); 167 DictionaryValue* exception = new DictionaryValue();
173 exception->Set( 168 exception->SetString(kDisplayPattern,
174 kDisplayPattern, 169 content_settings_helper::OriginToString(url));
175 new StringValue(content_settings_helper::OriginToString(url))); 170 exception->SetString(kSetting, ContentSettingToString(setting));
176 exception->Set( 171 exception->SetString(kOrigin, url.spec());
177 kSetting,
178 new StringValue(ContentSettingToString(setting)));
179 exception->Set(
180 kOrigin,
181 new StringValue(url.spec()));
182 return exception; 172 return exception;
183 } 173 }
184 174
185 } // namespace 175 } // namespace
186 176
187 ContentSettingsHandler::ContentSettingsHandler() { 177 ContentSettingsHandler::ContentSettingsHandler() {
188 } 178 }
189 179
190 ContentSettingsHandler::~ContentSettingsHandler() { 180 ContentSettingsHandler::~ContentSettingsHandler() {
191 } 181 }
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 case CONTENT_SETTINGS_TYPE_GEOLOCATION: 415 case CONTENT_SETTINGS_TYPE_GEOLOCATION:
426 case CONTENT_SETTINGS_TYPE_NOTIFICATIONS: 416 case CONTENT_SETTINGS_TYPE_NOTIFICATIONS:
427 break; 417 break;
428 default: 418 default:
429 UpdateExceptionsViewFromOTRHostContentSettingsMap(type); 419 UpdateExceptionsViewFromOTRHostContentSettingsMap(type);
430 break; 420 break;
431 } 421 }
432 } 422 }
433 423
434 void ContentSettingsHandler::UpdateGeolocationExceptionsView() { 424 void ContentSettingsHandler::UpdateGeolocationExceptionsView() {
435 GeolocationContentSettingsMap* map = 425 HostContentSettingsMap* map =
436 web_ui_->GetProfile()->GetGeolocationContentSettingsMap(); 426 web_ui_->GetProfile()->GetHostContentSettingsMap();
437 GeolocationContentSettingsMap::AllOriginsSettings all_settings = 427
438 map->GetAllOriginsSettings(); 428 HostContentSettingsMap::SettingsForOneType all_settings;
439 GeolocationContentSettingsMap::AllOriginsSettings::const_iterator i; 429 map->GetSettingsForOneType(
430 CONTENT_SETTINGS_TYPE_GEOLOCATION,
431 std::string(),
432 &all_settings);
433
434 // Group geolocation settings by primary_pattern.
435 AllPatternsSettings all_patterns_settings;
436 for (HostContentSettingsMap::SettingsForOneType::iterator i =
437 all_settings.begin();
438 i != all_settings.end();
439 ++i) {
440 all_patterns_settings[i->a][i->b] = i->c;
441 }
440 442
441 ListValue exceptions; 443 ListValue exceptions;
442 for (i = all_settings.begin(); i != all_settings.end(); ++i) { 444 for (AllPatternsSettings::iterator i = all_patterns_settings.begin();
443 const GURL& origin = i->first; 445 i != all_patterns_settings.end();
444 const GeolocationContentSettingsMap::OneOriginSettings& one_settings = 446 ++i) {
445 i->second; 447 const ContentSettingsPattern& primary_pattern = i->first;
448 const OnePatternSettings& one_settings = i->second;
446 449
447 GeolocationContentSettingsMap::OneOriginSettings::const_iterator parent = 450 OnePatternSettings::const_iterator parent =
448 one_settings.find(origin); 451 one_settings.find(primary_pattern);
449 452
450 // Add the "parent" entry for the non-embedded setting. 453 // Add the "parent" entry for the non-embedded setting.
451 ContentSetting parent_setting = 454 ContentSetting parent_setting =
452 parent == one_settings.end() ? CONTENT_SETTING_DEFAULT : parent->second; 455 parent == one_settings.end() ? CONTENT_SETTING_DEFAULT : parent->second;
453 exceptions.Append( 456 exceptions.Append(GetGeolocationExceptionForPage(primary_pattern,
454 GetGeolocationExceptionForPage(origin, origin, parent_setting)); 457 primary_pattern,
458 parent_setting));
455 459
456 // Add the "children" for any embedded settings. 460 // Add the "children" for any embedded settings.
457 GeolocationContentSettingsMap::OneOriginSettings::const_iterator j; 461 for (OnePatternSettings::const_iterator j = one_settings.begin();
458 for (j = one_settings.begin(); j != one_settings.end(); ++j) { 462 j != one_settings.end();
463 ++j) {
459 // Skip the non-embedded setting which we already added above. 464 // Skip the non-embedded setting which we already added above.
460 if (j == parent) 465 if (j == parent)
461 continue; 466 continue;
462 467
463 exceptions.Append( 468 exceptions.Append(
464 GetGeolocationExceptionForPage(origin, j->first, j->second)); 469 GetGeolocationExceptionForPage(primary_pattern, j->first, j->second));
465 } 470 }
466 } 471 }
467 472
468 StringValue type_string( 473 StringValue type_string(
469 ContentSettingsTypeToGroupName(CONTENT_SETTINGS_TYPE_GEOLOCATION)); 474 ContentSettingsTypeToGroupName(CONTENT_SETTINGS_TYPE_GEOLOCATION));
470 web_ui_->CallJavascriptFunction("ContentSettings.setExceptions", 475 web_ui_->CallJavascriptFunction("ContentSettings.setExceptions",
471 type_string, exceptions); 476 type_string, exceptions);
472 477
473 // This is mainly here to keep this function ideologically parallel to 478 // This is mainly here to keep this function ideologically parallel to
474 // UpdateExceptionsViewFromHostContentSettingsMap(). 479 // UpdateExceptionsViewFromHostContentSettingsMap().
(...skipping 27 matching lines...) Expand all
502 UpdateSettingDefaultFromModel(CONTENT_SETTINGS_TYPE_NOTIFICATIONS); 507 UpdateSettingDefaultFromModel(CONTENT_SETTINGS_TYPE_NOTIFICATIONS);
503 } 508 }
504 509
505 void ContentSettingsHandler::UpdateExceptionsViewFromHostContentSettingsMap( 510 void ContentSettingsHandler::UpdateExceptionsViewFromHostContentSettingsMap(
506 ContentSettingsType type) { 511 ContentSettingsType type) {
507 HostContentSettingsMap::SettingsForOneType entries; 512 HostContentSettingsMap::SettingsForOneType entries;
508 GetContentSettingsMap()->GetSettingsForOneType(type, "", &entries); 513 GetContentSettingsMap()->GetSettingsForOneType(type, "", &entries);
509 514
510 ListValue exceptions; 515 ListValue exceptions;
511 for (size_t i = 0; i < entries.size(); ++i) { 516 for (size_t i = 0; i < entries.size(); ++i) {
512 exceptions.Append( 517 // The content settings UI does not support secondary content settings
513 GetExceptionForPage(entries[i].a, entries[i].c, entries[i].d)); 518 // pattern yet. For content settings set through the content settings UI the
519 // secondary pattern is by default a wildcard pattern. Hence users are not
520 // able to modify content settings with a secondary pattern other than the
521 // wildcard pattern. So only show settings that the user is able to modify.
522 if (entries[i].b == ContentSettingsPattern::Wildcard()) {
523 exceptions.Append(
524 GetExceptionForPage(entries[i].a, entries[i].c, entries[i].d));
525 }
514 } 526 }
515 527
516 StringValue type_string(ContentSettingsTypeToGroupName(type)); 528 StringValue type_string(ContentSettingsTypeToGroupName(type));
517 web_ui_->CallJavascriptFunction("ContentSettings.setExceptions", type_string, 529 web_ui_->CallJavascriptFunction("ContentSettings.setExceptions", type_string,
518 exceptions); 530 exceptions);
519 531
520 UpdateExceptionsViewFromOTRHostContentSettingsMap(type); 532 UpdateExceptionsViewFromOTRHostContentSettingsMap(type);
521 533
522 // The default may also have changed (we won't get a separate notification). 534 // The default may also have changed (we won't get a separate notification).
523 // If it hasn't changed, this call will be harmless. 535 // If it hasn't changed, this call will be harmless.
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
587 599
588 ContentSettingsType type = ContentSettingsTypeFromGroupName(type_string); 600 ContentSettingsType type = ContentSettingsTypeFromGroupName(type_string);
589 if (type == CONTENT_SETTINGS_TYPE_GEOLOCATION) { 601 if (type == CONTENT_SETTINGS_TYPE_GEOLOCATION) {
590 std::string origin; 602 std::string origin;
591 std::string embedding_origin; 603 std::string embedding_origin;
592 bool rv = args->GetString(arg_i++, &origin); 604 bool rv = args->GetString(arg_i++, &origin);
593 DCHECK(rv); 605 DCHECK(rv);
594 rv = args->GetString(arg_i++, &embedding_origin); 606 rv = args->GetString(arg_i++, &embedding_origin);
595 DCHECK(rv); 607 DCHECK(rv);
596 608
597 web_ui_->GetProfile()->GetGeolocationContentSettingsMap()-> 609 web_ui_->GetProfile()->GetHostContentSettingsMap()->
598 SetContentSetting(GURL(origin), 610 SetContentSetting(ContentSettingsPattern::FromString(origin),
599 GURL(embedding_origin), 611 ContentSettingsPattern::FromString(embedding_origin),
612 CONTENT_SETTINGS_TYPE_GEOLOCATION,
613 std::string(),
600 CONTENT_SETTING_DEFAULT); 614 CONTENT_SETTING_DEFAULT);
601 } else if (type == CONTENT_SETTINGS_TYPE_NOTIFICATIONS) { 615 } else if (type == CONTENT_SETTINGS_TYPE_NOTIFICATIONS) {
602 std::string origin; 616 std::string origin;
603 std::string setting; 617 std::string setting;
604 bool rv = args->GetString(arg_i++, &origin); 618 bool rv = args->GetString(arg_i++, &origin);
605 DCHECK(rv); 619 DCHECK(rv);
606 rv = args->GetString(arg_i++, &setting); 620 rv = args->GetString(arg_i++, &setting);
607 DCHECK(rv); 621 DCHECK(rv);
608 ContentSetting content_setting = ContentSettingFromString(setting); 622 ContentSetting content_setting = ContentSettingFromString(setting);
609 if (content_setting == CONTENT_SETTING_ALLOW) { 623 if (content_setting == CONTENT_SETTING_ALLOW) {
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
717 return web_ui_->GetProfile()->GetProtocolHandlerRegistry(); 731 return web_ui_->GetProfile()->GetProtocolHandlerRegistry();
718 } 732 }
719 733
720 HostContentSettingsMap* 734 HostContentSettingsMap*
721 ContentSettingsHandler::GetOTRContentSettingsMap() { 735 ContentSettingsHandler::GetOTRContentSettingsMap() {
722 Profile* profile = web_ui_->GetProfile(); 736 Profile* profile = web_ui_->GetProfile();
723 if (profile->HasOffTheRecordProfile()) 737 if (profile->HasOffTheRecordProfile())
724 return profile->GetOffTheRecordProfile()->GetHostContentSettingsMap(); 738 return profile->GetOffTheRecordProfile()->GetHostContentSettingsMap();
725 return NULL; 739 return NULL;
726 } 740 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698