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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/webui/options/website_settings_handler.h"
7 #include "chrome/browser/content_settings/content_settings_utils.h"
8 #include "chrome/browser/content_settings/host_content_settings_map.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "content/public/browser/web_ui.h"
11 #include "grit/generated_resources.h"
12 #include "ui/base/l10n/time_format.h"
13 #include "ui/base/text/bytes_formatting.h"
17 const int kHttpPort = 80;
18 const int kHttpsPort = 443;
19 const char kPreferencesSource[] = "preference";
20 const char kStorage[] = "storage";
21 const ContentSettingsType kValidTypes[] = {CONTENT_SETTINGS_TYPE_GEOLOCATION,
22 CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
23 CONTENT_SETTINGS_TYPE_MEDIASTREAM};
24 const size_t kValidTypesLength = arraysize(kValidTypes);
29 WebsiteSettingsHandler::WebsiteSettingsHandler()
30 : observer_(this), weak_ptr_factory_(this) {
33 WebsiteSettingsHandler::~WebsiteSettingsHandler() {
36 void WebsiteSettingsHandler::GetLocalizedValues(
37 base::DictionaryValue* localized_strings) {
38 DCHECK(localized_strings);
40 static OptionsStringResource resources[] = {
41 {"websitesOptionsPageTabTitle", IDS_WEBSITES_SETTINGS_TITLE},
42 {"websitesManage", IDS_WEBSITE_SETTINGS_MANAGE},
43 {"websitesSearch", IDS_WEBSITE_SETTINGS_SEARCH_ORIGINS},
44 {"websitesLabelGeolocation", IDS_WEBSITE_SETTINGS_TYPE_LOCATION},
45 {"websitesLabelMediaStream", IDS_WEBSITE_SETTINGS_TYPE_MEDIASTREAM},
46 {"websitesLabelNotifications", IDS_WEBSITE_SETTINGS_TYPE_NOTIFICATIONS},
47 {"websitesLabelStorage", IDS_WEBSITE_SETTINGS_TYPE_STORAGE},
50 RegisterStrings(localized_strings, resources, arraysize(resources));
52 localized_strings, "websiteSettingsPage", IDS_WEBSITES_SETTINGS_TITLE);
55 void WebsiteSettingsHandler::InitializeHandler() {
56 Profile* profile = Profile::FromWebUI(web_ui());
57 HostContentSettingsMap* settings = profile->GetHostContentSettingsMap();
58 observer_.Add(settings);
61 void WebsiteSettingsHandler::RegisterMessages() {
62 web_ui()->RegisterMessageCallback(
64 base::Bind(&WebsiteSettingsHandler::HandleUpdateOrigins,
65 base::Unretained(this)));
67 web_ui()->RegisterMessageCallback(
68 "updateOriginsSearchResults",
69 base::Bind(&WebsiteSettingsHandler::HandleUpdateSearchResults,
70 base::Unretained(this)));
72 web_ui()->RegisterMessageCallback(
74 base::Bind(&WebsiteSettingsHandler::HandleUpdateLocalStorage,
75 base::Unretained(this)));
78 // content_settings::Observer implementation.
79 void WebsiteSettingsHandler::OnContentSettingChanged(
80 const ContentSettingsPattern& primary_pattern,
81 const ContentSettingsPattern& secondary_pattern,
82 ContentSettingsType content_type,
83 std::string resource_identifier) {
87 void WebsiteSettingsHandler::HandleUpdateOrigins(const base::ListValue* args) {
88 std::string content_setting_name;
89 bool rv = args->GetString(0, &content_setting_name);
92 ContentSettingsType content_type;
93 rv = content_settings::GetTypeFromName(content_setting_name, &content_type);
96 kValidTypes + kValidTypesLength,
97 std::find(kValidTypes, kValidTypes + kValidTypesLength, content_type));
99 last_setting_ = content_setting_name;
103 void WebsiteSettingsHandler::HandleUpdateSearchResults(
104 const base::ListValue* args) {
105 bool rv = args->GetString(0, &last_filter_);
111 void WebsiteSettingsHandler::HandleUpdateLocalStorage(
112 const base::ListValue* args) {
113 if (!local_storage_) {
114 Profile* profile = Profile::FromWebUI(web_ui());
115 local_storage_ = new BrowsingDataLocalStorageHelper(profile);
118 last_setting_ = kStorage;
120 local_storage_->StartFetching(
121 base::Bind(&WebsiteSettingsHandler::OnLocalStorageFetched,
122 weak_ptr_factory_.GetWeakPtr()));
125 void WebsiteSettingsHandler::OnLocalStorageFetched(const std::list<
126 BrowsingDataLocalStorageHelper::LocalStorageInfo>& storage) {
127 local_storage_list_ = storage;
128 UpdateLocalStorage();
131 void WebsiteSettingsHandler::Update() {
132 DCHECK(!last_setting_.empty());
133 if (last_setting_ == kStorage)
134 UpdateLocalStorage();
139 void WebsiteSettingsHandler::UpdateOrigins() {
140 Profile* profile = Profile::FromWebUI(web_ui());
141 HostContentSettingsMap* settings = profile->GetHostContentSettingsMap();
143 ContentSettingsForOneType all_settings;
144 ContentSettingsType last_setting;
145 content_settings::GetTypeFromName(last_setting_, &last_setting);
147 if (last_setting == CONTENT_SETTINGS_TYPE_MEDIASTREAM)
148 last_setting = CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC;
150 settings->GetSettingsForOneType(last_setting, std::string(), &all_settings);
152 base::DictionaryValue origins;
153 for (ContentSettingsForOneType::const_iterator it = all_settings.begin();
154 it != all_settings.end();
156 // Don't add default settings.
157 if (it->primary_pattern == ContentSettingsPattern::Wildcard() &&
158 it->secondary_pattern == ContentSettingsPattern::Wildcard() &&
159 it->source != kPreferencesSource) {
163 GURL origin_url(it->primary_pattern.ToString());
164 std::string origin = origin_url.spec();
166 // Hide the port if it is using a standard URL scheme.
167 if ((origin_url.SchemeIs(url::kHttpScheme) &&
168 origin_url.IntPort() == kHttpPort) ||
169 (origin_url.SchemeIs(url::kHttpsScheme) &&
170 origin_url.IntPort() == kHttpsPort)) {
171 url::Replacements<char> replacements;
172 replacements.ClearPort();
173 origin = origin_url.ReplaceComponents(replacements).spec();
176 // Mediastream isn't set unless both mic and camera are set to the same.
177 if (last_setting == CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC) {
178 ContentSetting cam_setting =
179 settings->GetContentSetting(origin_url,
181 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA,
183 if (it->setting != cam_setting)
187 if (origin.find(last_filter_) == base::string16::npos)
190 base::Time last_usage = settings->GetLastUsageByPattern(
191 it->primary_pattern, it->secondary_pattern, last_setting);
193 base::DictionaryValue* origin_entry = new base::DictionaryValue();
194 origin_entry->SetDoubleWithoutPathExpansion("usage",
195 last_usage.ToDoubleT());
196 base::string16 usage_string;
197 if (last_usage.ToDoubleT()) {
198 usage_string = ui::TimeFormat::Simple(ui::TimeFormat::FORMAT_ELAPSED,
199 ui::TimeFormat::LENGTH_SHORT,
200 base::Time::Now() - last_usage);
202 origin_entry->SetStringWithoutPathExpansion("usageString", usage_string);
204 origins.SetWithoutPathExpansion(origin, origin_entry);
207 web_ui()->CallJavascriptFunction("WebsiteSettingsManager.populateOrigins",
211 void WebsiteSettingsHandler::UpdateLocalStorage() {
212 base::DictionaryValue local_storage_map;
213 for (LocalStorageList::const_iterator it = local_storage_list_.begin();
214 it != local_storage_list_.end();
216 std::string origin = it->origin_url.spec();
218 if (origin.find(last_filter_) == base::string16::npos)
221 base::DictionaryValue* origin_entry = new base::DictionaryValue();
222 origin_entry->SetWithoutPathExpansion(
223 "usage", new base::FundamentalValue(static_cast<double>(it->size)));
224 origin_entry->SetWithoutPathExpansion(
225 "usageString", new base::StringValue(ui::FormatBytes(it->size)));
226 local_storage_map.SetWithoutPathExpansion(origin, origin_entry);
228 web_ui()->CallJavascriptFunction("WebsiteSettingsManager.populateOrigins",
232 } // namespace options