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