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

Side by Side Diff: chrome/browser/ui/webui/settings/site_settings_handler.cc

Issue 1988463002: MD Settings: Convert C++ handlers to be JavaScript-lifecycle aware. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/settings/site_settings_handler.h" 5 #include "chrome/browser/ui/webui/settings/site_settings_handler.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "chrome/browser/browsing_data/browsing_data_local_storage_helper.h" 8 #include "chrome/browser/browsing_data/browsing_data_local_storage_helper.h"
9 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" 9 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
10 #include "chrome/browser/profiles/profile.h" 10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/webui/site_settings_helper.h" 11 #include "chrome/browser/ui/webui/site_settings_helper.h"
12 #include "components/content_settings/core/browser/host_content_settings_map.h" 12 #include "components/content_settings/core/browser/host_content_settings_map.h"
13 #include "components/content_settings/core/common/content_settings_types.h" 13 #include "components/content_settings/core/common/content_settings_types.h"
14 #include "content/public/browser/browser_thread.h" 14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/web_ui.h" 15 #include "content/public/browser/web_ui.h"
16 #include "storage/browser/quota/quota_manager.h" 16 #include "storage/browser/quota/quota_manager.h"
17 #include "storage/common/quota/quota_status_code.h" 17 #include "storage/common/quota/quota_status_code.h"
18 #include "ui/base/text/bytes_formatting.h" 18 #include "ui/base/text/bytes_formatting.h"
19 19
20 namespace settings { 20 namespace settings {
21 21
22 SiteSettingsHandler::SiteSettingsHandler(Profile* profile) 22 SiteSettingsHandler::SiteSettingsHandler(Profile* profile)
23 : profile_(profile), observer_(this) { 23 : profile_(profile), observer_(this) {
24 observer_.Add(HostContentSettingsMapFactory::GetForProfile(profile));
25 if (profile->HasOffTheRecordProfile()) {
26 auto map = HostContentSettingsMapFactory::GetForProfile(
27 profile->GetOffTheRecordProfile());
28 if (!observer_.IsObserving(map))
29 observer_.Add(map);
30 }
31 } 24 }
32 25
33 SiteSettingsHandler::~SiteSettingsHandler() { 26 SiteSettingsHandler::~SiteSettingsHandler() {
34 } 27 }
35 28
36 void SiteSettingsHandler::RegisterMessages() { 29 void SiteSettingsHandler::RegisterMessages() {
37 web_ui()->RegisterMessageCallback( 30 web_ui()->RegisterMessageCallback(
38 "fetchUsageTotal", 31 "fetchUsageTotal",
39 base::Bind(&SiteSettingsHandler::HandleFetchUsageTotal, 32 base::Bind(&SiteSettingsHandler::HandleFetchUsageTotal,
40 base::Unretained(this))); 33 base::Unretained(this)));
(...skipping 20 matching lines...) Expand all
61 web_ui()->RegisterMessageCallback( 54 web_ui()->RegisterMessageCallback(
62 "setCategoryPermissionForOrigin", 55 "setCategoryPermissionForOrigin",
63 base::Bind(&SiteSettingsHandler::HandleSetCategoryPermissionForOrigin, 56 base::Bind(&SiteSettingsHandler::HandleSetCategoryPermissionForOrigin,
64 base::Unretained(this))); 57 base::Unretained(this)));
65 web_ui()->RegisterMessageCallback( 58 web_ui()->RegisterMessageCallback(
66 "isPatternValid", 59 "isPatternValid",
67 base::Bind(&SiteSettingsHandler::HandleIsPatternValid, 60 base::Bind(&SiteSettingsHandler::HandleIsPatternValid,
68 base::Unretained(this))); 61 base::Unretained(this)));
69 } 62 }
70 63
64 void SiteSettingsHandler::OnJavascriptAllowed() {
65 observer_.Add(HostContentSettingsMapFactory::GetForProfile(profile_));
66 if (profile_->HasOffTheRecordProfile()) {
67 auto map = HostContentSettingsMapFactory::GetForProfile(
68 profile_->GetOffTheRecordProfile());
69 if (!observer_.IsObserving(map))
70 observer_.Add(map);
71 }
72 }
73
74 void SiteSettingsHandler::OnJavascriptDisallowed() {
75 observer_.RemoveAll();
76 }
77
71 void SiteSettingsHandler::OnGetUsageInfo( 78 void SiteSettingsHandler::OnGetUsageInfo(
72 const storage::UsageInfoEntries& entries) { 79 const storage::UsageInfoEntries& entries) {
73 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 80 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
74 81
75 for (const auto& entry : entries) { 82 for (const auto& entry : entries) {
76 if (entry.usage <= 0) continue; 83 if (entry.usage <= 0) continue;
77 if (entry.host == usage_host_) { 84 if (entry.host == usage_host_) {
78 web_ui()->CallJavascriptFunction( 85 CallJavascriptFunction("settings.WebsiteUsagePrivateApi.returnUsageTotal",
79 "settings.WebsiteUsagePrivateApi.returnUsageTotal", 86 base::StringValue(entry.host),
80 base::StringValue(entry.host), 87 base::StringValue(ui::FormatBytes(entry.usage)),
81 base::StringValue(ui::FormatBytes(entry.usage)), 88 base::FundamentalValue(entry.type));
82 base::FundamentalValue(entry.type));
83 return; 89 return;
84 } 90 }
85 } 91 }
86 } 92 }
87 93
88 void SiteSettingsHandler::OnUsageInfoCleared(storage::QuotaStatusCode code) { 94 void SiteSettingsHandler::OnUsageInfoCleared(storage::QuotaStatusCode code) {
89 if (code == storage::kQuotaStatusOk) { 95 if (code == storage::kQuotaStatusOk) {
90 web_ui()->CallJavascriptFunction( 96 CallJavascriptFunction("settings.WebsiteUsagePrivateApi.onUsageCleared",
91 "settings.WebsiteUsagePrivateApi.onUsageCleared", 97 base::StringValue(clearing_origin_));
92 base::StringValue(clearing_origin_));
93 } 98 }
94 } 99 }
95 100
96 void SiteSettingsHandler::OnContentSettingChanged( 101 void SiteSettingsHandler::OnContentSettingChanged(
97 const ContentSettingsPattern& primary_pattern, 102 const ContentSettingsPattern& primary_pattern,
98 const ContentSettingsPattern& secondary_pattern, 103 const ContentSettingsPattern& secondary_pattern,
99 ContentSettingsType content_type, 104 ContentSettingsType content_type,
100 std::string resource_identifier) { 105 std::string resource_identifier) {
101 if (primary_pattern.ToString().empty()) { 106 if (primary_pattern.ToString().empty()) {
102 web_ui()->CallJavascriptFunction( 107 CallJavascriptFunction("cr.webUIListenerCallback",
103 "cr.webUIListenerCallback", 108 base::StringValue("contentSettingCategoryChanged"),
104 base::StringValue("contentSettingCategoryChanged"), 109 base::FundamentalValue(content_type));
105 base::FundamentalValue(content_type));
106 } else { 110 } else {
107 web_ui()->CallJavascriptFunction( 111 CallJavascriptFunction(
108 "cr.webUIListenerCallback", 112 "cr.webUIListenerCallback",
109 base::StringValue("contentSettingSitePermissionChanged"), 113 base::StringValue("contentSettingSitePermissionChanged"),
110 base::FundamentalValue(content_type), 114 base::FundamentalValue(content_type),
111 base::StringValue(primary_pattern.ToString())); 115 base::StringValue(primary_pattern.ToString()));
112 } 116 }
113 } 117 }
114 118
115 void SiteSettingsHandler::HandleFetchUsageTotal( 119 void SiteSettingsHandler::HandleFetchUsageTotal(
116 const base::ListValue* args) { 120 const base::ListValue* args) {
121 AllowJavascript();
122
117 CHECK_EQ(1U, args->GetSize()); 123 CHECK_EQ(1U, args->GetSize());
118 std::string host; 124 std::string host;
119 CHECK(args->GetString(0, &host)); 125 CHECK(args->GetString(0, &host));
120 usage_host_ = host; 126 usage_host_ = host;
121 127
122 scoped_refptr<StorageInfoFetcher> storage_info_fetcher 128 scoped_refptr<StorageInfoFetcher> storage_info_fetcher
123 = new StorageInfoFetcher(profile_); 129 = new StorageInfoFetcher(profile_);
124 storage_info_fetcher->FetchStorageInfo( 130 storage_info_fetcher->FetchStorageInfo(
125 base::Bind(&SiteSettingsHandler::OnGetUsageInfo, base::Unretained(this))); 131 base::Bind(&SiteSettingsHandler::OnGetUsageInfo, base::Unretained(this)));
126 } 132 }
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 171
166 HostContentSettingsMap* map = 172 HostContentSettingsMap* map =
167 HostContentSettingsMapFactory::GetForProfile(profile_); 173 HostContentSettingsMapFactory::GetForProfile(profile_);
168 map->SetDefaultContentSetting( 174 map->SetDefaultContentSetting(
169 static_cast<ContentSettingsType>(static_cast<int>(content_type)), 175 static_cast<ContentSettingsType>(static_cast<int>(content_type)),
170 default_setting); 176 default_setting);
171 } 177 }
172 178
173 void SiteSettingsHandler::HandleGetDefaultValueForContentType( 179 void SiteSettingsHandler::HandleGetDefaultValueForContentType(
174 const base::ListValue* args) { 180 const base::ListValue* args) {
181 AllowJavascript();
182
175 CHECK_EQ(2U, args->GetSize()); 183 CHECK_EQ(2U, args->GetSize());
176 const base::Value* callback_id; 184 const base::Value* callback_id;
177 CHECK(args->Get(0, &callback_id)); 185 CHECK(args->Get(0, &callback_id));
178 double type; 186 double type;
179 CHECK(args->GetDouble(1, &type)); 187 CHECK(args->GetDouble(1, &type));
180 188
181 ContentSettingsType content_type = 189 ContentSettingsType content_type =
182 static_cast<ContentSettingsType>(static_cast<int>(type)); 190 static_cast<ContentSettingsType>(static_cast<int>(type));
183 HostContentSettingsMap* map = 191 HostContentSettingsMap* map =
184 HostContentSettingsMapFactory::GetForProfile(profile_); 192 HostContentSettingsMapFactory::GetForProfile(profile_);
185 ContentSetting setting = map->GetDefaultContentSetting(content_type, nullptr); 193 ContentSetting setting = map->GetDefaultContentSetting(content_type, nullptr);
186 194
187 // FullScreen is Allow vs. Ask. 195 // FullScreen is Allow vs. Ask.
188 bool enabled; 196 bool enabled;
189 if (content_type == CONTENT_SETTINGS_TYPE_FULLSCREEN) 197 if (content_type == CONTENT_SETTINGS_TYPE_FULLSCREEN)
190 enabled = setting != CONTENT_SETTING_ASK; 198 enabled = setting != CONTENT_SETTING_ASK;
191 else 199 else
192 enabled = setting != CONTENT_SETTING_BLOCK; 200 enabled = setting != CONTENT_SETTING_BLOCK;
193 201
194 ResolveJavascriptCallback(*callback_id, base::FundamentalValue(enabled)); 202 ResolveJavascriptCallback(*callback_id, base::FundamentalValue(enabled));
195 } 203 }
196 204
197 void SiteSettingsHandler::HandleGetExceptionList(const base::ListValue* args) { 205 void SiteSettingsHandler::HandleGetExceptionList(const base::ListValue* args) {
206 AllowJavascript();
207
198 CHECK_EQ(2U, args->GetSize()); 208 CHECK_EQ(2U, args->GetSize());
199 const base::Value* callback_id; 209 const base::Value* callback_id;
200 CHECK(args->Get(0, &callback_id)); 210 CHECK(args->Get(0, &callback_id));
201 double type; 211 double type;
202 CHECK(args->GetDouble(1, &type)); 212 CHECK(args->GetDouble(1, &type));
203 ContentSettingsType content_type = 213 ContentSettingsType content_type =
204 static_cast<ContentSettingsType>(static_cast<int>(type)); 214 static_cast<ContentSettingsType>(static_cast<int>(type));
205 215
206 HostContentSettingsMap* map = 216 HostContentSettingsMap* map =
207 HostContentSettingsMapFactory::GetForProfile(profile_); 217 HostContentSettingsMapFactory::GetForProfile(profile_);
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 std::string pattern_string; 279 std::string pattern_string;
270 CHECK(args->GetString(1, &pattern_string)); 280 CHECK(args->GetString(1, &pattern_string));
271 281
272 ContentSettingsPattern pattern = 282 ContentSettingsPattern pattern =
273 ContentSettingsPattern::FromString(pattern_string); 283 ContentSettingsPattern::FromString(pattern_string);
274 ResolveJavascriptCallback( 284 ResolveJavascriptCallback(
275 *callback_id, base::FundamentalValue(pattern.IsValid())); 285 *callback_id, base::FundamentalValue(pattern.IsValid()));
276 } 286 }
277 287
278 } // namespace settings 288 } // namespace settings
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698