Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / options / website_settings_handler.cc
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.
4
5 #include "chrome/browser/ui/webui/options/website_settings_handler.h"
6
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"
14
15 namespace {
16
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);
25 }  // namespace
26
27 namespace options {
28
29 WebsiteSettingsHandler::WebsiteSettingsHandler()
30     : observer_(this), weak_ptr_factory_(this) {
31 }
32
33 WebsiteSettingsHandler::~WebsiteSettingsHandler() {
34 }
35
36 void WebsiteSettingsHandler::GetLocalizedValues(
37     base::DictionaryValue* localized_strings) {
38   DCHECK(localized_strings);
39
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},
48   };
49
50   RegisterStrings(localized_strings, resources, arraysize(resources));
51   RegisterTitle(
52       localized_strings, "websiteSettingsPage", IDS_WEBSITES_SETTINGS_TITLE);
53 }
54
55 void WebsiteSettingsHandler::InitializeHandler() {
56   Profile* profile = Profile::FromWebUI(web_ui());
57   HostContentSettingsMap* settings = profile->GetHostContentSettingsMap();
58   observer_.Add(settings);
59 }
60
61 void WebsiteSettingsHandler::RegisterMessages() {
62   web_ui()->RegisterMessageCallback(
63       "updateOrigins",
64       base::Bind(&WebsiteSettingsHandler::HandleUpdateOrigins,
65                  base::Unretained(this)));
66
67   web_ui()->RegisterMessageCallback(
68       "updateOriginsSearchResults",
69       base::Bind(&WebsiteSettingsHandler::HandleUpdateSearchResults,
70                  base::Unretained(this)));
71
72   web_ui()->RegisterMessageCallback(
73       "updateLocalStorage",
74       base::Bind(&WebsiteSettingsHandler::HandleUpdateLocalStorage,
75                  base::Unretained(this)));
76 }
77
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) {
84   Update();
85 }
86
87 void WebsiteSettingsHandler::HandleUpdateOrigins(const base::ListValue* args) {
88   std::string content_setting_name;
89   bool rv = args->GetString(0, &content_setting_name);
90   DCHECK(rv);
91
92   ContentSettingsType content_type;
93   rv = content_settings::GetTypeFromName(content_setting_name, &content_type);
94   DCHECK(rv);
95   DCHECK_NE(
96       kValidTypes + kValidTypesLength,
97       std::find(kValidTypes, kValidTypes + kValidTypesLength, content_type));
98
99   last_setting_ = content_setting_name;
100   UpdateOrigins();
101 }
102
103 void WebsiteSettingsHandler::HandleUpdateSearchResults(
104     const base::ListValue* args) {
105   bool rv = args->GetString(0, &last_filter_);
106   DCHECK(rv);
107
108   Update();
109 }
110
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);
116   }
117
118   last_setting_ = kStorage;
119
120   local_storage_->StartFetching(
121       base::Bind(&WebsiteSettingsHandler::OnLocalStorageFetched,
122                  weak_ptr_factory_.GetWeakPtr()));
123 }
124
125 void WebsiteSettingsHandler::OnLocalStorageFetched(const std::list<
126     BrowsingDataLocalStorageHelper::LocalStorageInfo>& storage) {
127   local_storage_list_ = storage;
128   UpdateLocalStorage();
129 }
130
131 void WebsiteSettingsHandler::Update() {
132   DCHECK(!last_setting_.empty());
133   if (last_setting_ == kStorage)
134     UpdateLocalStorage();
135   else
136     UpdateOrigins();
137 }
138
139 void WebsiteSettingsHandler::UpdateOrigins() {
140   Profile* profile = Profile::FromWebUI(web_ui());
141   HostContentSettingsMap* settings = profile->GetHostContentSettingsMap();
142
143   ContentSettingsForOneType all_settings;
144   ContentSettingsType last_setting;
145   content_settings::GetTypeFromName(last_setting_, &last_setting);
146
147   if (last_setting == CONTENT_SETTINGS_TYPE_MEDIASTREAM)
148     last_setting = CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC;
149
150   settings->GetSettingsForOneType(last_setting, std::string(), &all_settings);
151
152   base::DictionaryValue origins;
153   for (ContentSettingsForOneType::const_iterator it = all_settings.begin();
154        it != all_settings.end();
155        ++it) {
156     // Don't add default settings.
157     if (it->primary_pattern == ContentSettingsPattern::Wildcard() &&
158         it->secondary_pattern == ContentSettingsPattern::Wildcard() &&
159         it->source != kPreferencesSource) {
160       continue;
161     }
162
163     GURL origin_url(it->primary_pattern.ToString());
164     std::string origin = origin_url.spec();
165
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();
174     }
175
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,
180                                       origin_url,
181                                       CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA,
182                                       std::string());
183       if (it->setting != cam_setting)
184         continue;
185     }
186
187     if (origin.find(last_filter_) == base::string16::npos)
188       continue;
189
190     base::Time last_usage = settings->GetLastUsageByPattern(
191         it->primary_pattern, it->secondary_pattern, last_setting);
192
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);
201     }
202     origin_entry->SetStringWithoutPathExpansion("usageString", usage_string);
203
204     origins.SetWithoutPathExpansion(origin, origin_entry);
205   }
206
207   web_ui()->CallJavascriptFunction("WebsiteSettingsManager.populateOrigins",
208                                    origins);
209 }
210
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();
215        it++) {
216     std::string origin = it->origin_url.spec();
217
218     if (origin.find(last_filter_) == base::string16::npos)
219       continue;
220
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);
227   }
228   web_ui()->CallJavascriptFunction("WebsiteSettingsManager.populateOrigins",
229                                    local_storage_map);
230 }
231
232 }  // namespace options