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

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

Issue 408493003: Show local storage usage on the Website Settings options page. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Nit-be-gone. Created 6 years, 5 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
« no previous file with comments | « chrome/browser/ui/webui/options/website_settings_handler.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/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};
21 const size_t kValidTypesLength = arraysize(kValidTypes);
17 } // namespace 22 } // namespace
18 23
19 namespace options { 24 namespace options {
20 25
21 WebsiteSettingsHandler::WebsiteSettingsHandler() { 26 WebsiteSettingsHandler::WebsiteSettingsHandler() : weak_ptr_factory_(this) {
22 } 27 }
23 28
24 WebsiteSettingsHandler::~WebsiteSettingsHandler() { 29 WebsiteSettingsHandler::~WebsiteSettingsHandler() {
25 } 30 }
26 31
27 void WebsiteSettingsHandler::GetLocalizedValues( 32 void WebsiteSettingsHandler::GetLocalizedValues(
28 base::DictionaryValue* localized_strings) { 33 base::DictionaryValue* localized_strings) {
29 DCHECK(localized_strings); 34 DCHECK(localized_strings);
30 35
31 static OptionsStringResource resources[] = { 36 static OptionsStringResource resources[] = {
32 {"websitesOptionsPageTabTitle", IDS_WEBSITES_SETTINGS_TITLE}, 37 {"websitesOptionsPageTabTitle", IDS_WEBSITES_SETTINGS_TITLE},
33 {"websitesManage", IDS_WEBSITE_SETTINGS_MANAGE}, 38 {"websitesManage", IDS_WEBSITE_SETTINGS_MANAGE},
34 {"websitesSearch", IDS_WEBSITE_SETTINGS_SEARCH_ORIGINS}, 39 {"websitesSearch", IDS_WEBSITE_SETTINGS_SEARCH_ORIGINS},
35 {"websitesLabelGeolocation", IDS_WEBSITE_SETTINGS_TYPE_LOCATION}, 40 {"websitesLabelGeolocation", IDS_WEBSITE_SETTINGS_TYPE_LOCATION},
36 {"websitesLabelMediaStream", IDS_WEBSITE_SETTINGS_TYPE_MEDIASTREAM}, 41 {"websitesLabelMediaStream", IDS_WEBSITE_SETTINGS_TYPE_MEDIASTREAM},
37 {"websitesLabelNotifications", IDS_WEBSITE_SETTINGS_TYPE_NOTIFICATIONS}, 42 {"websitesLabelNotifications", IDS_WEBSITE_SETTINGS_TYPE_NOTIFICATIONS},
43 {"websitesLabelStorage", IDS_WEBSITE_SETTINGS_TYPE_STORAGE},
38 }; 44 };
39 45
40 RegisterStrings(localized_strings, resources, arraysize(resources)); 46 RegisterStrings(localized_strings, resources, arraysize(resources));
41 RegisterTitle( 47 RegisterTitle(
42 localized_strings, "websiteSettingsPage", IDS_WEBSITES_SETTINGS_TITLE); 48 localized_strings, "websiteSettingsPage", IDS_WEBSITES_SETTINGS_TITLE);
43 } 49 }
44 50
45 void WebsiteSettingsHandler::RegisterMessages() { 51 void WebsiteSettingsHandler::RegisterMessages() {
46 web_ui()->RegisterMessageCallback( 52 web_ui()->RegisterMessageCallback(
47 "updateOrigins", 53 "updateOrigins",
48 base::Bind(&WebsiteSettingsHandler::HandleUpdateOrigins, 54 base::Bind(&WebsiteSettingsHandler::HandleUpdateOrigins,
49 base::Unretained(this))); 55 base::Unretained(this)));
50 56
51 web_ui()->RegisterMessageCallback( 57 web_ui()->RegisterMessageCallback(
52 "updateOriginsSearchResults", 58 "updateOriginsSearchResults",
53 base::Bind(&WebsiteSettingsHandler::HandleUpdateSearchResults, 59 base::Bind(&WebsiteSettingsHandler::HandleUpdateSearchResults,
54 base::Unretained(this))); 60 base::Unretained(this)));
61
62 web_ui()->RegisterMessageCallback(
63 "updateLocalStorage",
64 base::Bind(&WebsiteSettingsHandler::HandleUpdateLocalStorage,
65 base::Unretained(this)));
55 } 66 }
56 67
57 void WebsiteSettingsHandler::HandleUpdateOrigins(const base::ListValue* args) { 68 void WebsiteSettingsHandler::HandleUpdateOrigins(const base::ListValue* args) {
58 std::string content_setting_name; 69 std::string content_setting_name;
59 bool rv = args->GetString(0, &content_setting_name); 70 bool rv = args->GetString(0, &content_setting_name);
60 DCHECK(rv); 71 DCHECK(rv);
72
61 ContentSettingsType content_type; 73 ContentSettingsType content_type;
62 content_settings::GetTypeFromName(content_setting_name, &content_type); 74 rv = content_settings::GetTypeFromName(content_setting_name, &content_type);
75 DCHECK(rv);
76 DCHECK_NE(
77 kValidTypes + kValidTypesLength,
78 std::find(kValidTypes, kValidTypes + kValidTypesLength, content_type));
63 79
64 last_setting_ = content_type; 80 last_setting_ = content_setting_name;
65 81 UpdateOrigins();
66 UpdateOrigins(std::string());
67 } 82 }
68 83
69 void WebsiteSettingsHandler::HandleUpdateSearchResults( 84 void WebsiteSettingsHandler::HandleUpdateSearchResults(
70 const base::ListValue* args) { 85 const base::ListValue* args) {
71 std::string filter; 86 bool rv = args->GetString(0, &last_filter_);
72 bool rv = args->GetString(0, &filter);
73 DCHECK(rv); 87 DCHECK(rv);
74 88
75 UpdateOrigins(filter); 89 if (last_setting_ == kStorage)
90 UpdateLocalStorage();
91 else
92 UpdateOrigins();
76 } 93 }
77 94
78 void WebsiteSettingsHandler::UpdateOrigins(const std::string& filter) { 95 void WebsiteSettingsHandler::HandleUpdateLocalStorage(
96 const base::ListValue* args) {
97 if (!local_storage_) {
98 Profile* profile = Profile::FromWebUI(web_ui());
99 local_storage_ = new BrowsingDataLocalStorageHelper(profile);
100 }
101
102 last_setting_ = kStorage;
103
104 local_storage_->StartFetching(
105 base::Bind(&WebsiteSettingsHandler::OnLocalStorageFetched,
106 weak_ptr_factory_.GetWeakPtr()));
107 }
108
109 void WebsiteSettingsHandler::OnLocalStorageFetched(const std::list<
110 BrowsingDataLocalStorageHelper::LocalStorageInfo>& storage) {
111 local_storage_list_ = storage;
112 UpdateLocalStorage();
113 }
114
115 void WebsiteSettingsHandler::UpdateOrigins() {
79 Profile* profile = Profile::FromWebUI(web_ui()); 116 Profile* profile = Profile::FromWebUI(web_ui());
80 HostContentSettingsMap* settings = profile->GetHostContentSettingsMap(); 117 HostContentSettingsMap* settings = profile->GetHostContentSettingsMap();
81 118
82 ContentSettingsForOneType all_settings; 119 ContentSettingsForOneType all_settings;
83 settings->GetSettingsForOneType(last_setting_, std::string(), &all_settings); 120 ContentSettingsType last_setting;
121 content_settings::GetTypeFromName(last_setting_, &last_setting);
122 settings->GetSettingsForOneType(last_setting, std::string(), &all_settings);
84 123
85 base::DictionaryValue origins; 124 base::DictionaryValue origins;
86 for (ContentSettingsForOneType::const_iterator it = all_settings.begin(); 125 for (ContentSettingsForOneType::const_iterator it = all_settings.begin();
87 it != all_settings.end(); 126 it != all_settings.end();
88 ++it) { 127 ++it) {
89 // Don't add default settings. 128 // Don't add default settings.
90 if (it->primary_pattern == ContentSettingsPattern::Wildcard() && 129 if (it->primary_pattern == ContentSettingsPattern::Wildcard() &&
91 it->secondary_pattern == ContentSettingsPattern::Wildcard() && 130 it->secondary_pattern == ContentSettingsPattern::Wildcard() &&
92 it->source != kPreferencesSource) { 131 it->source != kPreferencesSource) {
93 continue; 132 continue;
94 } 133 }
95 134
96 std::string origin = it->primary_pattern.ToString(); 135 std::string origin = it->primary_pattern.ToString();
97 136
98 if (origin.find(filter) == base::string16::npos) 137 if (origin.find(last_filter_) == base::string16::npos)
99 continue; 138 continue;
100 139
101 // TODO(dhnishi): Change 0 to a timestamp representing last API usage. 140 // TODO(dhnishi): Change 0 to a timestamp representing last API usage.
102 origins.SetDoubleWithoutPathExpansion(origin, 0); 141 origins.SetDoubleWithoutPathExpansion(origin, 0);
103 } 142 }
104 143
105 web_ui()->CallJavascriptFunction("WebsiteSettingsManager.populateOrigins", 144 web_ui()->CallJavascriptFunction("WebsiteSettingsManager.populateOrigins",
106 origins); 145 origins);
107 } 146 }
108 147
148 void WebsiteSettingsHandler::UpdateLocalStorage() {
149 base::DictionaryValue local_storage_map;
150 for (LocalStorageList::const_iterator it = local_storage_list_.begin();
151 it != local_storage_list_.end();
152 it++) {
153 std::string origin = it->origin_url.spec();
154
155 if (origin.find(last_filter_) == base::string16::npos)
156 continue;
157
158 base::DictionaryValue* origin_entry = new base::DictionaryValue();
159 origin_entry->SetWithoutPathExpansion(
160 "usage", new base::FundamentalValue(static_cast<double>(it->size)));
161 origin_entry->SetWithoutPathExpansion(
162 "usageString", new base::StringValue(ui::FormatBytes(it->size)));
163 local_storage_map.SetWithoutPathExpansion(origin, origin_entry);
164 }
165 web_ui()->CallJavascriptFunction(
166 "WebsiteSettingsManager.populateLocalStorage", local_storage_map);
167 }
168
109 } // namespace options 169 } // namespace options
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/options/website_settings_handler.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698