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

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

Issue 493383004: Add a button to clear local storage for a given origin to Website Settings dialog. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Split the if-statement. Created 6 years, 4 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 "apps/app_window_registry.h"
7 #include "chrome/browser/content_settings/content_settings_utils.h" 8 #include "chrome/browser/content_settings/content_settings_utils.h"
8 #include "chrome/browser/content_settings/host_content_settings_map.h" 9 #include "chrome/browser/content_settings/host_content_settings_map.h"
9 #include "chrome/browser/profiles/profile.h" 10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/browser_iterator.h"
13 #include "chrome/browser/ui/tabs/tab_strip_model.h"
14 #include "content/public/browser/dom_storage_context.h"
15 #include "content/public/browser/storage_partition.h"
16 #include "content/public/browser/web_contents.h"
10 #include "content/public/browser/web_ui.h" 17 #include "content/public/browser/web_ui.h"
18 #include "extensions/browser/extension_registry.h"
19 #include "extensions/common/constants.h"
20 #include "extensions/common/extension.h"
11 #include "grit/generated_resources.h" 21 #include "grit/generated_resources.h"
12 #include "ui/base/l10n/l10n_util.h" 22 #include "ui/base/l10n/l10n_util.h"
13 #include "ui/base/l10n/time_format.h" 23 #include "ui/base/l10n/time_format.h"
14 #include "ui/base/text/bytes_formatting.h" 24 #include "ui/base/text/bytes_formatting.h"
15 25
16 namespace { 26 namespace {
17 27
18 const int kHttpPort = 80; 28 const int kHttpPort = 80;
19 const int kHttpsPort = 443; 29 const int kHttpsPort = 443;
20 const char kPreferencesSource[] = "preference"; 30 const char kPreferencesSource[] = "preference";
(...skipping 25 matching lines...) Expand all
46 {"websitesLabelGeolocation", IDS_WEBSITE_SETTINGS_TYPE_LOCATION}, 56 {"websitesLabelGeolocation", IDS_WEBSITE_SETTINGS_TYPE_LOCATION},
47 {"websitesLabelMediaStream", IDS_WEBSITE_SETTINGS_TYPE_MEDIASTREAM}, 57 {"websitesLabelMediaStream", IDS_WEBSITE_SETTINGS_TYPE_MEDIASTREAM},
48 {"websitesLabelNotifications", IDS_WEBSITE_SETTINGS_TYPE_NOTIFICATIONS}, 58 {"websitesLabelNotifications", IDS_WEBSITE_SETTINGS_TYPE_NOTIFICATIONS},
49 {"websitesLabelStorage", IDS_WEBSITE_SETTINGS_TYPE_STORAGE}, 59 {"websitesLabelStorage", IDS_WEBSITE_SETTINGS_TYPE_STORAGE},
50 {"websitesLocationDescription", 60 {"websitesLocationDescription",
51 IDS_WEBSITE_SETTINGS_LOCATION_DESCRIPTION}, 61 IDS_WEBSITE_SETTINGS_LOCATION_DESCRIPTION},
52 {"websitesMediastreamDescription", 62 {"websitesMediastreamDescription",
53 IDS_WEBSITE_SETTINGS_MEDIASTREAM_DESCRIPTION}, 63 IDS_WEBSITE_SETTINGS_MEDIASTREAM_DESCRIPTION},
54 {"websitesNotificationsDescription", 64 {"websitesNotificationsDescription",
55 IDS_WEBSITE_SETTINGS_NOTIFICATIONS_DESCRIPTION}, 65 IDS_WEBSITE_SETTINGS_NOTIFICATIONS_DESCRIPTION},
66 {"websitesButtonClear", IDS_WEBSITE_SETTINGS_STORAGE_CLEAR_BUTTON},
56 }; 67 };
57 68
58 RegisterStrings(localized_strings, resources, arraysize(resources)); 69 RegisterStrings(localized_strings, resources, arraysize(resources));
59 RegisterTitle( 70 RegisterTitle(
60 localized_strings, "websiteSettingsPage", IDS_WEBSITE_SETTINGS_TITLE); 71 localized_strings, "websiteSettingsPage", IDS_WEBSITE_SETTINGS_TITLE);
61 } 72 }
62 73
63 void WebsiteSettingsHandler::InitializeHandler() { 74 void WebsiteSettingsHandler::InitializeHandler() {
64 Profile* profile = Profile::FromWebUI(web_ui()); 75 Profile* profile = Profile::FromWebUI(web_ui());
65 HostContentSettingsMap* settings = profile->GetHostContentSettingsMap(); 76 HostContentSettingsMap* settings = profile->GetHostContentSettingsMap();
(...skipping 23 matching lines...) Expand all
89 100
90 web_ui()->RegisterMessageCallback( 101 web_ui()->RegisterMessageCallback(
91 "setOriginPermission", 102 "setOriginPermission",
92 base::Bind(&WebsiteSettingsHandler::HandleSetOriginPermission, 103 base::Bind(&WebsiteSettingsHandler::HandleSetOriginPermission,
93 base::Unretained(this))); 104 base::Unretained(this)));
94 105
95 web_ui()->RegisterMessageCallback( 106 web_ui()->RegisterMessageCallback(
96 "maybeShowEditPage", 107 "maybeShowEditPage",
97 base::Bind(&WebsiteSettingsHandler::HandleMaybeShowEditPage, 108 base::Bind(&WebsiteSettingsHandler::HandleMaybeShowEditPage,
98 base::Unretained(this))); 109 base::Unretained(this)));
110
111 web_ui()->RegisterMessageCallback(
112 "deleteLocalStorage",
113 base::Bind(&WebsiteSettingsHandler::HandleDeleteLocalStorage,
114 base::Unretained(this)));
115
116 web_ui()->RegisterMessageCallback(
117 "stopOrigin",
118 base::Bind(&WebsiteSettingsHandler::HandleStopOrigin,
119 base::Unretained(this)));
99 } 120 }
100 121
101 // content_settings::Observer implementation. 122 // content_settings::Observer implementation.
102 void WebsiteSettingsHandler::OnContentSettingChanged( 123 void WebsiteSettingsHandler::OnContentSettingChanged(
103 const ContentSettingsPattern& primary_pattern, 124 const ContentSettingsPattern& primary_pattern,
104 const ContentSettingsPattern& secondary_pattern, 125 const ContentSettingsPattern& secondary_pattern,
105 ContentSettingsType content_type, 126 ContentSettingsType content_type,
106 std::string resource_identifier) { 127 std::string resource_identifier) {
107 Update(); 128 Update();
108 } 129 }
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 178
158 last_site_ = last_site; 179 last_site_ = last_site;
159 base::StringValue site_value(site); 180 base::StringValue site_value(site);
160 web_ui()->CallJavascriptFunction("WebsiteSettingsManager.showEditPage", 181 web_ui()->CallJavascriptFunction("WebsiteSettingsManager.showEditPage",
161 site_value); 182 site_value);
162 } 183 }
163 184
164 void WebsiteSettingsHandler::OnLocalStorageFetched(const std::list< 185 void WebsiteSettingsHandler::OnLocalStorageFetched(const std::list<
165 BrowsingDataLocalStorageHelper::LocalStorageInfo>& storage) { 186 BrowsingDataLocalStorageHelper::LocalStorageInfo>& storage) {
166 local_storage_list_ = storage; 187 local_storage_list_ = storage;
167 UpdateLocalStorage(); 188 Update();
189 GetInfoForOrigin(last_site_, false);
168 } 190 }
169 191
170 void WebsiteSettingsHandler::Update() { 192 void WebsiteSettingsHandler::Update() {
171 DCHECK(!last_setting_.empty()); 193 DCHECK(!last_setting_.empty());
172 if (last_setting_ == kStorage) 194 if (last_setting_ == kStorage)
173 UpdateLocalStorage(); 195 UpdateLocalStorage();
174 else 196 else
175 UpdateOrigins(); 197 UpdateOrigins();
176 } 198 }
177 199
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 271
250 void WebsiteSettingsHandler::HandleGetOriginInfo(const base::ListValue* args) { 272 void WebsiteSettingsHandler::HandleGetOriginInfo(const base::ListValue* args) {
251 std::string url; 273 std::string url;
252 bool rv = args->GetString(0, &url); 274 bool rv = args->GetString(0, &url);
253 DCHECK(rv); 275 DCHECK(rv);
254 GURL origin(url); 276 GURL origin(url);
255 277
256 if (!origin.is_valid()) 278 if (!origin.is_valid())
257 return; 279 return;
258 280
259 GetInfoForOrigin(origin); 281 GetInfoForOrigin(origin, true);
260 } 282 }
261 283
262 void WebsiteSettingsHandler::HandleSetOriginPermission( 284 void WebsiteSettingsHandler::HandleSetOriginPermission(
263 const base::ListValue* args) { 285 const base::ListValue* args) {
264 std::string setting_name; 286 std::string setting_name;
265 bool rv = args->GetString(0, &setting_name); 287 bool rv = args->GetString(0, &setting_name);
266 DCHECK(rv); 288 DCHECK(rv);
267 ContentSettingsType settings_type; 289 ContentSettingsType settings_type;
268 rv = content_settings::GetTypeFromName(setting_name, &settings_type); 290 rv = content_settings::GetTypeFromName(setting_name, &settings_type);
269 DCHECK(rv); 291 DCHECK(rv);
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 scoped_ptr<base::Value> v(map->GetWebsiteSetting( 339 scoped_ptr<base::Value> v(map->GetWebsiteSetting(
318 last_site_, last_site_, settings_type, std::string(), &info)); 340 last_site_, last_site_, settings_type, std::string(), &info));
319 map->SetNarrowestWebsiteSetting(primary_pattern, 341 map->SetNarrowestWebsiteSetting(primary_pattern,
320 secondary_pattern, 342 secondary_pattern,
321 settings_type, 343 settings_type,
322 std::string(), 344 std::string(),
323 setting, 345 setting,
324 info); 346 info);
325 } 347 }
326 348
327 void WebsiteSettingsHandler::GetInfoForOrigin(const GURL& site_url) { 349 void WebsiteSettingsHandler::HandleDeleteLocalStorage(
350 const base::ListValue* args) {
351 DCHECK(!last_site_.is_empty());
352 DeleteLocalStorage(last_site_);
353 }
354
355 void WebsiteSettingsHandler::HandleStopOrigin(const base::ListValue* args) {
356 DCHECK(!last_site_.is_empty());
357 StopOrigin(last_site_);
358 }
359
360 void WebsiteSettingsHandler::GetInfoForOrigin(const GURL& site_url,
361 bool show_page) {
328 Profile* profile = Profile::FromWebUI(web_ui()); 362 Profile* profile = Profile::FromWebUI(web_ui());
329 HostContentSettingsMap* map = profile->GetHostContentSettingsMap(); 363 HostContentSettingsMap* map = profile->GetHostContentSettingsMap();
330 364
331 double storage = 0.0; 365 double storage = 0.0;
332 for (LocalStorageList::const_iterator it = local_storage_list_.begin(); 366 for (LocalStorageList::const_iterator it = local_storage_list_.begin();
333 it != local_storage_list_.end(); 367 it != local_storage_list_.end();
334 it++) { 368 it++) {
335 if (it->origin_url == site_url) { 369 if (it->origin_url == site_url) {
336 storage = static_cast<double>(it->size); 370 storage = static_cast<double>(it->size);
337 break; 371 break;
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
389 permission_info->SetWithoutPathExpansion("options", options); 423 permission_info->SetWithoutPathExpansion("options", options);
390 permission_info->SetBooleanWithoutPathExpansion( 424 permission_info->SetBooleanWithoutPathExpansion(
391 "editable", info.source == content_settings::SETTING_SOURCE_USER); 425 "editable", info.source == content_settings::SETTING_SOURCE_USER);
392 permissions->SetWithoutPathExpansion( 426 permissions->SetWithoutPathExpansion(
393 content_settings::GetTypeName(permission_type), permission_info); 427 content_settings::GetTypeName(permission_type), permission_info);
394 } 428 }
395 429
396 base::Value* storage_used = new base::StringValue(l10n_util::GetStringFUTF16( 430 base::Value* storage_used = new base::StringValue(l10n_util::GetStringFUTF16(
397 IDS_WEBSITE_SETTINGS_STORAGE_USED, ui::FormatBytes(storage))); 431 IDS_WEBSITE_SETTINGS_STORAGE_USED, ui::FormatBytes(storage)));
398 432
399 web_ui()->CallJavascriptFunction( 433 web_ui()->CallJavascriptFunction("WebsiteSettingsEditor.populateOrigin",
400 "WebsiteSettingsEditor.populateOrigin", *storage_used, *permissions); 434 *storage_used,
435 *permissions,
436 base::FundamentalValue(show_page));
401 } 437 }
402 438
403 void WebsiteSettingsHandler::UpdateLocalStorage() { 439 void WebsiteSettingsHandler::UpdateLocalStorage() {
404 base::DictionaryValue local_storage_map; 440 base::DictionaryValue local_storage_map;
405 for (LocalStorageList::const_iterator it = local_storage_list_.begin(); 441 for (LocalStorageList::const_iterator it = local_storage_list_.begin();
406 it != local_storage_list_.end(); 442 it != local_storage_list_.end();
407 it++) { 443 it++) {
408 std::string origin = it->origin_url.spec(); 444 std::string origin = it->origin_url.spec();
409 445
410 if (origin.find(last_filter_) == base::string16::npos) 446 if (origin.find(last_filter_) == base::string16::npos)
411 continue; 447 continue;
412 448
413 base::DictionaryValue* origin_entry = new base::DictionaryValue(); 449 base::DictionaryValue* origin_entry = new base::DictionaryValue();
414 origin_entry->SetWithoutPathExpansion( 450 origin_entry->SetWithoutPathExpansion(
415 "usage", new base::FundamentalValue(static_cast<double>(it->size))); 451 "usage", new base::FundamentalValue(static_cast<double>(it->size)));
416 origin_entry->SetWithoutPathExpansion( 452 origin_entry->SetWithoutPathExpansion(
417 "usageString", new base::StringValue(ui::FormatBytes(it->size))); 453 "usageString", new base::StringValue(ui::FormatBytes(it->size)));
418 local_storage_map.SetWithoutPathExpansion(origin, origin_entry); 454 local_storage_map.SetWithoutPathExpansion(origin, origin_entry);
419 } 455 }
420 web_ui()->CallJavascriptFunction("WebsiteSettingsManager.populateOrigins", 456 web_ui()->CallJavascriptFunction("WebsiteSettingsManager.populateOrigins",
421 local_storage_map); 457 local_storage_map);
422 } 458 }
423 459
460 void WebsiteSettingsHandler::StopOrigin(const GURL& site_url) {
461 Profile* profile = Profile::FromWebUI(web_ui());
462 if (site_url.SchemeIs(extensions::kExtensionScheme)) {
463 const extensions::Extension* extension =
464 extensions::ExtensionRegistry::Get(profile)
465 ->enabled_extensions()
466 .GetHostedAppByURL(site_url);
467 if (extension) {
468 apps::AppWindowRegistry::Get(profile)
469 ->CloseAllAppWindowsForApp(extension->id());
470 }
471 }
472
473 for (chrome::BrowserIterator it; !it.done(); it.Next()) {
474 Browser* browser = *it;
475 TabStripModel* model = browser->tab_strip_model();
476 for (int idx = 0; idx < model->count(); idx++) {
477 content::WebContents* web_contents = model->GetWebContentsAt(idx);
478 // Can't discard tabs that are already discarded or active.
479 if (model->IsTabDiscarded(idx) || (model->active_index() == idx))
480 continue;
481
482 // Don't discard tabs that belong to other profiles or other origins.
483 if (web_contents->GetLastCommittedURL().GetOrigin() != site_url ||
484 profile !=
485 Profile::FromBrowserContext(web_contents->GetBrowserContext())) {
486 continue;
487 }
488 model->DiscardWebContentsAt(idx);
489 }
490 }
491 }
492
493 void WebsiteSettingsHandler::DeleteLocalStorage(const GURL& site_url) {
494 Profile* profile = Profile::FromWebUI(web_ui());
495 content::DOMStorageContext* dom_storage_context_ =
496 content::BrowserContext::GetDefaultStoragePartition(profile)
497 ->GetDOMStorageContext();
498 dom_storage_context_->DeleteLocalStorage(site_url);
499
500 // Load a new BrowsingDataLocalStorageHelper to update.
501 local_storage_ = new BrowsingDataLocalStorageHelper(profile);
502
503 local_storage_->StartFetching(
504 base::Bind(&WebsiteSettingsHandler::OnLocalStorageFetched,
505 weak_ptr_factory_.GetWeakPtr()));
506 }
507
424 } // namespace options 508 } // 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