OLD | NEW |
---|---|
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/text/bytes_formatting.h" | |
12 | 13 |
13 namespace { | 14 namespace { |
14 | 15 |
15 const char kPreferencesSource[] = "preference"; | 16 const char kPreferencesSource[] = "preference"; |
16 | 17 const char kStorage[] = "storage"; |
18 const ContentSettingsType kValidTypes[] = {CONTENT_SETTINGS_TYPE_GEOLOCATION, | |
19 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
20 CONTENT_SETTINGS_TYPE_MEDIASTREAM}; | |
17 } // namespace | 21 } // namespace |
18 | 22 |
19 namespace options { | 23 namespace options { |
20 | 24 |
21 WebsiteSettingsHandler::WebsiteSettingsHandler() { | 25 WebsiteSettingsHandler::WebsiteSettingsHandler() : weak_ptr_factory_(this) { |
22 } | 26 } |
23 | 27 |
24 WebsiteSettingsHandler::~WebsiteSettingsHandler() { | 28 WebsiteSettingsHandler::~WebsiteSettingsHandler() { |
25 } | 29 } |
26 | 30 |
27 void WebsiteSettingsHandler::GetLocalizedValues( | 31 void WebsiteSettingsHandler::GetLocalizedValues( |
28 base::DictionaryValue* localized_strings) { | 32 base::DictionaryValue* localized_strings) { |
29 DCHECK(localized_strings); | 33 DCHECK(localized_strings); |
30 | 34 |
31 static OptionsStringResource resources[] = { | 35 static OptionsStringResource resources[] = { |
32 {"websitesOptionsPageTabTitle", IDS_WEBSITES_SETTINGS_TITLE}, | 36 {"websitesOptionsPageTabTitle", IDS_WEBSITES_SETTINGS_TITLE}, |
33 {"websitesManage", IDS_WEBSITE_SETTINGS_MANAGE}, | 37 {"websitesManage", IDS_WEBSITE_SETTINGS_MANAGE}, |
34 {"websitesSearch", IDS_WEBSITE_SETTINGS_SEARCH_ORIGINS}, | 38 {"websitesSearch", IDS_WEBSITE_SETTINGS_SEARCH_ORIGINS}, |
35 {"websitesLabelGeolocation", IDS_WEBSITE_SETTINGS_TYPE_LOCATION}, | 39 {"websitesLabelGeolocation", IDS_WEBSITE_SETTINGS_TYPE_LOCATION}, |
36 {"websitesLabelMediaStream", IDS_WEBSITE_SETTINGS_TYPE_MEDIASTREAM}, | 40 {"websitesLabelMediaStream", IDS_WEBSITE_SETTINGS_TYPE_MEDIASTREAM}, |
37 {"websitesLabelNotifications", IDS_WEBSITE_SETTINGS_TYPE_NOTIFICATIONS}, | 41 {"websitesLabelNotifications", IDS_WEBSITE_SETTINGS_TYPE_NOTIFICATIONS}, |
42 {"websitesLabelStorage", IDS_WEBSITE_SETTINGS_TYPE_STORAGE}, | |
38 }; | 43 }; |
39 | 44 |
40 RegisterStrings(localized_strings, resources, arraysize(resources)); | 45 RegisterStrings(localized_strings, resources, arraysize(resources)); |
41 RegisterTitle( | 46 RegisterTitle( |
42 localized_strings, "websiteSettingsPage", IDS_WEBSITES_SETTINGS_TITLE); | 47 localized_strings, "websiteSettingsPage", IDS_WEBSITES_SETTINGS_TITLE); |
43 } | 48 } |
44 | 49 |
45 void WebsiteSettingsHandler::RegisterMessages() { | 50 void WebsiteSettingsHandler::RegisterMessages() { |
46 web_ui()->RegisterMessageCallback( | 51 web_ui()->RegisterMessageCallback( |
47 "updateOrigins", | 52 "updateOrigins", |
48 base::Bind(&WebsiteSettingsHandler::HandleUpdateOrigins, | 53 base::Bind(&WebsiteSettingsHandler::HandleUpdateOrigins, |
49 base::Unretained(this))); | 54 base::Unretained(this))); |
50 | 55 |
51 web_ui()->RegisterMessageCallback( | 56 web_ui()->RegisterMessageCallback( |
52 "updateOriginsSearchResults", | 57 "updateOriginsSearchResults", |
53 base::Bind(&WebsiteSettingsHandler::HandleUpdateSearchResults, | 58 base::Bind(&WebsiteSettingsHandler::HandleUpdateSearchResults, |
54 base::Unretained(this))); | 59 base::Unretained(this))); |
60 | |
61 web_ui()->RegisterMessageCallback( | |
62 "updateLocalStorage", | |
63 base::Bind(&WebsiteSettingsHandler::HandleUpdateLocalStorage, | |
64 base::Unretained(this))); | |
55 } | 65 } |
56 | 66 |
57 void WebsiteSettingsHandler::HandleUpdateOrigins(const base::ListValue* args) { | 67 void WebsiteSettingsHandler::HandleUpdateOrigins(const base::ListValue* args) { |
58 std::string content_setting_name; | 68 std::string content_setting_name; |
59 bool rv = args->GetString(0, &content_setting_name); | 69 bool rv = args->GetString(0, &content_setting_name); |
60 DCHECK(rv); | 70 DCHECK(rv); |
71 | |
61 ContentSettingsType content_type; | 72 ContentSettingsType content_type; |
62 content_settings::GetTypeFromName(content_setting_name, &content_type); | 73 rv = content_settings::GetTypeFromName(content_setting_name, &content_type); |
74 DCHECK(rv); | |
75 DCHECK_NE( | |
76 std::end(kValidTypes), | |
Bernhard Bauer
2014/07/22 08:51:48
Can you run some tryjobs for this, in particular o
| |
77 std::find(std::begin(kValidTypes), std::end(kValidTypes), content_type)); | |
Daniel Nishi
2014/07/22 22:30:56
I've removed the end iterator after running the tr
| |
63 | 78 |
64 last_setting_ = content_type; | 79 last_setting_ = content_setting_name; |
65 | 80 UpdateOrigins(last_filter_); |
66 UpdateOrigins(std::string()); | |
67 } | 81 } |
68 | 82 |
69 void WebsiteSettingsHandler::HandleUpdateSearchResults( | 83 void WebsiteSettingsHandler::HandleUpdateSearchResults( |
70 const base::ListValue* args) { | 84 const base::ListValue* args) { |
71 std::string filter; | 85 bool rv = args->GetString(0, &last_filter_); |
72 bool rv = args->GetString(0, &filter); | |
73 DCHECK(rv); | 86 DCHECK(rv); |
74 | 87 |
75 UpdateOrigins(filter); | 88 if (last_setting_ == kStorage) |
89 UpdateLocalStorage(last_filter_); | |
90 else | |
91 UpdateOrigins(last_filter_); | |
92 } | |
93 | |
94 void WebsiteSettingsHandler::HandleUpdateLocalStorage( | |
95 const base::ListValue* args) { | |
96 if (!local_storage_) { | |
97 Profile* profile = Profile::FromWebUI(web_ui()); | |
98 local_storage_ = new BrowsingDataLocalStorageHelper(profile); | |
99 } | |
100 | |
101 last_setting_ = kStorage; | |
102 | |
103 local_storage_->StartFetching( | |
104 base::Bind(&WebsiteSettingsHandler::OnLocalStorageFetched, | |
105 weak_ptr_factory_.GetWeakPtr())); | |
106 } | |
107 | |
108 void WebsiteSettingsHandler::OnLocalStorageFetched(const std::list< | |
109 BrowsingDataLocalStorageHelper::LocalStorageInfo>& storage) { | |
110 local_storage_list_ = storage; | |
111 UpdateLocalStorage(last_filter_); | |
76 } | 112 } |
77 | 113 |
78 void WebsiteSettingsHandler::UpdateOrigins(const std::string& filter) { | 114 void WebsiteSettingsHandler::UpdateOrigins(const std::string& filter) { |
79 Profile* profile = Profile::FromWebUI(web_ui()); | 115 Profile* profile = Profile::FromWebUI(web_ui()); |
80 HostContentSettingsMap* settings = profile->GetHostContentSettingsMap(); | 116 HostContentSettingsMap* settings = profile->GetHostContentSettingsMap(); |
81 | 117 |
82 ContentSettingsForOneType all_settings; | 118 ContentSettingsForOneType all_settings; |
83 settings->GetSettingsForOneType(last_setting_, std::string(), &all_settings); | 119 ContentSettingsType last_setting; |
120 content_settings::GetTypeFromName(last_setting_, &last_setting); | |
121 settings->GetSettingsForOneType(last_setting, std::string(), &all_settings); | |
84 | 122 |
85 base::DictionaryValue origins; | 123 base::DictionaryValue origins; |
86 for (ContentSettingsForOneType::const_iterator it = all_settings.begin(); | 124 for (ContentSettingsForOneType::const_iterator it = all_settings.begin(); |
87 it != all_settings.end(); | 125 it != all_settings.end(); |
88 ++it) { | 126 ++it) { |
89 // Don't add default settings. | 127 // Don't add default settings. |
90 if (it->primary_pattern == ContentSettingsPattern::Wildcard() && | 128 if (it->primary_pattern == ContentSettingsPattern::Wildcard() && |
91 it->secondary_pattern == ContentSettingsPattern::Wildcard() && | 129 it->secondary_pattern == ContentSettingsPattern::Wildcard() && |
92 it->source != kPreferencesSource) { | 130 it->source != kPreferencesSource) { |
93 continue; | 131 continue; |
94 } | 132 } |
95 | 133 |
96 std::string origin = it->primary_pattern.ToString(); | 134 std::string origin = it->primary_pattern.ToString(); |
97 | 135 |
98 if (origin.find(filter) == base::string16::npos) | 136 if (origin.find(filter) == base::string16::npos) |
99 continue; | 137 continue; |
100 | 138 |
101 // TODO(dhnishi): Change 0 to a timestamp representing last API usage. | 139 // TODO(dhnishi): Change 0 to a timestamp representing last API usage. |
102 origins.SetDoubleWithoutPathExpansion(origin, 0); | 140 origins.SetDoubleWithoutPathExpansion(origin, 0); |
103 } | 141 } |
104 | 142 |
105 web_ui()->CallJavascriptFunction("WebsiteSettingsManager.populateOrigins", | 143 web_ui()->CallJavascriptFunction("WebsiteSettingsManager.populateOrigins", |
106 origins); | 144 origins); |
107 } | 145 } |
108 | 146 |
147 void WebsiteSettingsHandler::UpdateLocalStorage(const std::string& filter) { | |
Bernhard Bauer
2014/07/22 08:51:48
You don't need to pass the filter as an argument n
Daniel Nishi
2014/07/22 22:30:56
Done.
| |
148 base::DictionaryValue local_storage_map; | |
149 for (LocalStorageList::const_iterator it = local_storage_list_.begin(); | |
150 it != local_storage_list_.end(); | |
151 it++) { | |
152 std::string origin = it->origin_url.spec(); | |
153 | |
154 if (origin.find(filter) == base::string16::npos) | |
155 continue; | |
156 | |
157 base::DictionaryValue* origin_entry = new base::DictionaryValue(); | |
158 origin_entry->SetWithoutPathExpansion( | |
159 "usage", new base::FundamentalValue(static_cast<double>(it->size))); | |
160 origin_entry->SetWithoutPathExpansion( | |
161 "usageString", new base::StringValue(ui::FormatBytes(it->size))); | |
162 local_storage_map.SetWithoutPathExpansion(origin, origin_entry); | |
163 } | |
164 web_ui()->CallJavascriptFunction( | |
165 "WebsiteSettingsManager.populateLocalStorage", local_storage_map); | |
166 } | |
167 | |
109 } // namespace options | 168 } // namespace options |
OLD | NEW |