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 |
17 } // namespace | 18 } // namespace |
18 | 19 |
19 namespace options { | 20 namespace options { |
20 | 21 |
21 WebsiteSettingsHandler::WebsiteSettingsHandler() { | 22 WebsiteSettingsHandler::WebsiteSettingsHandler() { |
22 } | 23 } |
23 | 24 |
24 WebsiteSettingsHandler::~WebsiteSettingsHandler() { | 25 WebsiteSettingsHandler::~WebsiteSettingsHandler() { |
25 } | 26 } |
26 | 27 |
27 void WebsiteSettingsHandler::GetLocalizedValues( | 28 void WebsiteSettingsHandler::GetLocalizedValues( |
28 base::DictionaryValue* localized_strings) { | 29 base::DictionaryValue* localized_strings) { |
29 DCHECK(localized_strings); | 30 DCHECK(localized_strings); |
30 | 31 |
31 static OptionsStringResource resources[] = { | 32 static OptionsStringResource resources[] = { |
32 {"websitesOptionsPageTabTitle", IDS_WEBSITES_SETTINGS_TITLE}, | 33 {"websitesOptionsPageTabTitle", IDS_WEBSITES_SETTINGS_TITLE}, |
33 {"websitesManage", IDS_WEBSITE_SETTINGS_MANAGE}, | 34 {"websitesManage", IDS_WEBSITE_SETTINGS_MANAGE}, |
34 {"websitesSearch", IDS_WEBSITE_SETTINGS_SEARCH_ORIGINS}, | 35 {"websitesSearch", IDS_WEBSITE_SETTINGS_SEARCH_ORIGINS}, |
35 {"websitesLabelGeolocation", IDS_WEBSITE_SETTINGS_TYPE_LOCATION}, | 36 {"websitesLabelGeolocation", IDS_WEBSITE_SETTINGS_TYPE_LOCATION}, |
36 {"websitesLabelMediaStream", IDS_WEBSITE_SETTINGS_TYPE_MEDIASTREAM}, | 37 {"websitesLabelMediaStream", IDS_WEBSITE_SETTINGS_TYPE_MEDIASTREAM}, |
37 {"websitesLabelNotifications", IDS_WEBSITE_SETTINGS_TYPE_NOTIFICATIONS}, | 38 {"websitesLabelNotifications", IDS_WEBSITE_SETTINGS_TYPE_NOTIFICATIONS}, |
39 {"websitesLabelStorage", IDS_WEBSITE_SETTINGS_TYPE_STORAGE}, | |
38 }; | 40 }; |
39 | 41 |
40 RegisterStrings(localized_strings, resources, arraysize(resources)); | 42 RegisterStrings(localized_strings, resources, arraysize(resources)); |
41 RegisterTitle( | 43 RegisterTitle( |
42 localized_strings, "websiteSettingsPage", IDS_WEBSITES_SETTINGS_TITLE); | 44 localized_strings, "websiteSettingsPage", IDS_WEBSITES_SETTINGS_TITLE); |
43 } | 45 } |
44 | 46 |
45 void WebsiteSettingsHandler::RegisterMessages() { | 47 void WebsiteSettingsHandler::RegisterMessages() { |
46 web_ui()->RegisterMessageCallback( | 48 web_ui()->RegisterMessageCallback( |
47 "updateOrigins", | 49 "updateOrigins", |
48 base::Bind(&WebsiteSettingsHandler::HandleUpdateOrigins, | 50 base::Bind(&WebsiteSettingsHandler::HandleUpdateOrigins, |
49 base::Unretained(this))); | 51 base::Unretained(this))); |
50 | 52 |
51 web_ui()->RegisterMessageCallback( | 53 web_ui()->RegisterMessageCallback( |
52 "updateOriginsSearchResults", | 54 "updateOriginsSearchResults", |
53 base::Bind(&WebsiteSettingsHandler::HandleUpdateSearchResults, | 55 base::Bind(&WebsiteSettingsHandler::HandleUpdateSearchResults, |
54 base::Unretained(this))); | 56 base::Unretained(this))); |
57 | |
58 web_ui()->RegisterMessageCallback( | |
59 "updateLocalStorage", | |
60 base::Bind(&WebsiteSettingsHandler::HandleUpdateLocalStorage, | |
61 base::Unretained(this))); | |
55 } | 62 } |
56 | 63 |
57 void WebsiteSettingsHandler::HandleUpdateOrigins(const base::ListValue* args) { | 64 void WebsiteSettingsHandler::HandleUpdateOrigins(const base::ListValue* args) { |
58 std::string content_setting_name; | 65 std::string content_setting_name; |
59 bool rv = args->GetString(0, &content_setting_name); | 66 bool rv = args->GetString(0, &content_setting_name); |
60 DCHECK(rv); | 67 DCHECK(rv); |
61 ContentSettingsType content_type; | 68 ContentSettingsType content_type; |
62 content_settings::GetTypeFromName(content_setting_name, &content_type); | 69 content_settings::GetTypeFromName(content_setting_name, &content_type); |
Bernhard Bauer
2014/07/21 11:29:30
Can we add some more checks in here? I would like
Daniel Nishi
2014/07/21 17:53:37
I added a DCHECK that GetTypeFromName got a valid
| |
63 | 70 |
64 last_setting_ = content_type; | 71 last_setting_ = content_setting_name; |
65 | 72 |
66 UpdateOrigins(std::string()); | 73 UpdateOrigins(std::string()); |
67 } | 74 } |
68 | 75 |
69 void WebsiteSettingsHandler::HandleUpdateSearchResults( | 76 void WebsiteSettingsHandler::HandleUpdateSearchResults( |
70 const base::ListValue* args) { | 77 const base::ListValue* args) { |
71 std::string filter; | 78 std::string filter; |
72 bool rv = args->GetString(0, &filter); | 79 bool rv = args->GetString(0, &filter); |
73 DCHECK(rv); | 80 DCHECK(rv); |
74 | 81 |
75 UpdateOrigins(filter); | 82 if (last_setting_ == "storage") |
Bernhard Bauer
2014/07/21 11:29:30
Can you extract "storage" to a constant?
Daniel Nishi
2014/07/21 17:53:36
Done.
| |
83 UpdateLocalStorage(filter); | |
Bernhard Bauer
2014/07/21 11:29:29
So, this means that every time the filter string c
Daniel Nishi
2014/07/21 17:53:37
I've changed the implementation to cache the local
| |
84 else | |
85 UpdateOrigins(filter); | |
86 } | |
87 | |
88 void WebsiteSettingsHandler::HandleUpdateLocalStorage( | |
89 const base::ListValue* args) { | |
90 if (!local_storage_.get()) { | |
Bernhard Bauer
2014/07/21 11:29:30
.get() is not necessary; scoped_refptr (and scoped
Daniel Nishi
2014/07/21 17:53:37
Done.
| |
91 Profile* profile = Profile::FromWebUI(web_ui()); | |
92 local_storage_ = (new BrowsingDataLocalStorageHelper(profile)); | |
Bernhard Bauer
2014/07/21 11:29:30
Parentheses are not necessary.
Daniel Nishi
2014/07/21 17:53:37
Done.
| |
93 } | |
94 | |
95 last_setting_ = "storage"; | |
96 | |
97 UpdateLocalStorage(std::string()); | |
Bernhard Bauer
2014/07/21 11:29:29
What if I had previously typed something into the
Daniel Nishi
2014/07/21 17:53:37
Last query now stored and reused.
| |
98 } | |
99 | |
100 void WebsiteSettingsHandler::OnLocalStorageFetch( | |
101 const std::string& filter, | |
102 const std::list<BrowsingDataLocalStorageHelper::LocalStorageInfo>& | |
103 storage) { | |
104 base::DictionaryValue local_storage_map; | |
105 for (std::list< | |
106 BrowsingDataLocalStorageHelper::LocalStorageInfo>::const_iterator | |
107 it = storage.begin(); | |
108 it != storage.end(); | |
109 it++) { | |
110 std::string origin = (*it).origin_url.spec(); | |
Bernhard Bauer
2014/07/21 11:29:29
This can be `it->...`.
Daniel Nishi
2014/07/21 17:53:37
Done and replaced in other places where I used (*i
| |
111 | |
112 if (origin.find(filter) == base::string16::npos) | |
113 continue; | |
114 | |
115 base::DictionaryValue* origin_entry = new base::DictionaryValue(); | |
116 origin_entry->SetWithoutPathExpansion( | |
117 "usage", new base::FundamentalValue(static_cast<double>((*it).size))); | |
118 origin_entry->SetWithoutPathExpansion( | |
119 "usage_string", new base::StringValue(ui::FormatBytes((*it).size))); | |
Bernhard Bauer
2014/07/21 11:29:30
Nit: Javascript properties use lowercasedCamelCase
Daniel Nishi
2014/07/21 17:53:37
Done.
| |
120 local_storage_map.SetWithoutPathExpansion(origin, origin_entry); | |
121 } | |
122 web_ui()->CallJavascriptFunction("WebsiteSettingsManager.updateLocalStorage", | |
123 local_storage_map); | |
76 } | 124 } |
77 | 125 |
78 void WebsiteSettingsHandler::UpdateOrigins(const std::string& filter) { | 126 void WebsiteSettingsHandler::UpdateOrigins(const std::string& filter) { |
79 Profile* profile = Profile::FromWebUI(web_ui()); | 127 Profile* profile = Profile::FromWebUI(web_ui()); |
80 HostContentSettingsMap* settings = profile->GetHostContentSettingsMap(); | 128 HostContentSettingsMap* settings = profile->GetHostContentSettingsMap(); |
81 | 129 |
82 ContentSettingsForOneType all_settings; | 130 ContentSettingsForOneType all_settings; |
83 settings->GetSettingsForOneType(last_setting_, std::string(), &all_settings); | 131 ContentSettingsType last_setting; |
132 content_settings::GetTypeFromName(last_setting_, &last_setting); | |
133 settings->GetSettingsForOneType(last_setting, std::string(), &all_settings); | |
84 | 134 |
85 base::DictionaryValue origins; | 135 base::DictionaryValue origins; |
86 for (ContentSettingsForOneType::const_iterator it = all_settings.begin(); | 136 for (ContentSettingsForOneType::const_iterator it = all_settings.begin(); |
87 it != all_settings.end(); | 137 it != all_settings.end(); |
88 ++it) { | 138 ++it) { |
89 // Don't add default settings. | 139 // Don't add default settings. |
90 if (it->primary_pattern == ContentSettingsPattern::Wildcard() && | 140 if (it->primary_pattern == ContentSettingsPattern::Wildcard() && |
91 it->secondary_pattern == ContentSettingsPattern::Wildcard() && | 141 it->secondary_pattern == ContentSettingsPattern::Wildcard() && |
92 it->source != kPreferencesSource) { | 142 it->source != kPreferencesSource) { |
93 continue; | 143 continue; |
94 } | 144 } |
95 | 145 |
96 std::string origin = it->primary_pattern.ToString(); | 146 std::string origin = it->primary_pattern.ToString(); |
97 | 147 |
98 if (origin.find(filter) == base::string16::npos) | 148 if (origin.find(filter) == base::string16::npos) |
99 continue; | 149 continue; |
100 | 150 |
101 // TODO(dhnishi): Change 0 to a timestamp representing last API usage. | 151 // TODO(dhnishi): Change 0 to a timestamp representing last API usage. |
102 origins.SetDoubleWithoutPathExpansion(origin, 0); | 152 origins.SetDoubleWithoutPathExpansion(origin, 0); |
103 } | 153 } |
104 | 154 |
105 web_ui()->CallJavascriptFunction("WebsiteSettingsManager.populateOrigins", | 155 web_ui()->CallJavascriptFunction("WebsiteSettingsManager.populateOrigins", |
106 origins); | 156 origins); |
107 } | 157 } |
108 | 158 |
159 void WebsiteSettingsHandler::UpdateLocalStorage(const std::string& filter) { | |
160 local_storage_->StartFetching( | |
161 base::Bind(&WebsiteSettingsHandler::OnLocalStorageFetch, | |
162 base::Unretained(this), | |
Bernhard Bauer
2014/07/21 11:29:29
Uh-oh... |local_storage_| might call this callback
Daniel Nishi
2014/07/21 17:53:37
I have it using a weak_ptr now. If another browsin
| |
163 filter)); | |
164 } | |
165 | |
109 } // namespace options | 166 } // namespace options |
OLD | NEW |