| OLD | NEW | 
|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/clear_browser_data_handler.h" | 5 #include "chrome/browser/ui/webui/options/clear_browser_data_handler.h" | 
| 6 | 6 | 
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" | 
| 8 #include "base/bind.h" | 8 #include "base/bind.h" | 
| 9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" | 
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" | 
| 11 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" | 
| 12 #include "base/metrics/sparse_histogram.h" | 12 #include "base/metrics/sparse_histogram.h" | 
| 13 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" | 
| 14 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" | 
|  | 15 #include "base/strings/string_number_conversions.h" | 
| 15 #include "base/values.h" | 16 #include "base/values.h" | 
| 16 #include "chrome/app/chrome_command_ids.h" | 17 #include "chrome/app/chrome_command_ids.h" | 
| 17 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" | 
| 18 #include "chrome/browser/browsing_data/browsing_data_counter.h" | 19 #include "chrome/browser/browsing_data/browsing_data_counter.h" | 
| 19 #include "chrome/browser/browsing_data/browsing_data_helper.h" | 20 #include "chrome/browser/browsing_data/browsing_data_helper.h" | 
| 20 #include "chrome/browser/browsing_data/browsing_data_remover.h" | 21 #include "chrome/browser/browsing_data/browsing_data_remover.h" | 
| 21 #include "chrome/browser/browsing_data/cache_counter.h" | 22 #include "chrome/browser/browsing_data/cache_counter.h" | 
| 22 #include "chrome/browser/browsing_data/history_counter.h" | 23 #include "chrome/browser/browsing_data/history_counter.h" | 
| 23 #include "chrome/browser/browsing_data/passwords_counter.h" | 24 #include "chrome/browser/browsing_data/passwords_counter.h" | 
| 24 #include "chrome/browser/prefs/incognito_mode_prefs.h" | 25 #include "chrome/browser/prefs/incognito_mode_prefs.h" | 
| 25 #include "chrome/browser/profiles/profile.h" | 26 #include "chrome/browser/profiles/profile.h" | 
| 26 #include "chrome/browser/sync/profile_sync_service_factory.h" | 27 #include "chrome/browser/sync/profile_sync_service_factory.h" | 
| 27 #include "chrome/browser/ui/accelerator_utils.h" | 28 #include "chrome/browser/ui/accelerator_utils.h" | 
| 28 #include "chrome/common/chrome_switches.h" | 29 #include "chrome/common/chrome_switches.h" | 
| 29 #include "chrome/common/pref_names.h" | 30 #include "chrome/common/pref_names.h" | 
| 30 #include "chrome/grit/generated_resources.h" | 31 #include "chrome/grit/generated_resources.h" | 
| 31 #include "chrome/grit/locale_settings.h" | 32 #include "chrome/grit/locale_settings.h" | 
| 32 #include "components/google/core/browser/google_util.h" | 33 #include "components/google/core/browser/google_util.h" | 
| 33 #include "content/public/browser/notification_details.h" | 34 #include "content/public/browser/notification_details.h" | 
| 34 #include "content/public/browser/web_ui.h" | 35 #include "content/public/browser/web_ui.h" | 
| 35 #include "ui/base/accelerators/accelerator.h" | 36 #include "ui/base/accelerators/accelerator.h" | 
| 36 #include "ui/base/l10n/l10n_util.h" | 37 #include "ui/base/l10n/l10n_util.h" | 
|  | 38 #include "ui/base/text/bytes_formatting.h" | 
| 37 #include "ui/events/keycodes/keyboard_codes.h" | 39 #include "ui/events/keycodes/keyboard_codes.h" | 
| 38 | 40 | 
| 39 namespace { | 41 namespace { | 
| 40 | 42 | 
| 41 const char kClearBrowsingDataLearnMoreUrl[] = | 43 const char kClearBrowsingDataLearnMoreUrl[] = | 
| 42     "https://support.google.com/chrome/?p=settings_clear_browsing_data"; | 44     "https://support.google.com/chrome/?p=settings_clear_browsing_data"; | 
| 43 | 45 | 
| 44 bool AreCountersEnabled() { | 46 bool AreCountersEnabled() { | 
| 45   return base::CommandLine::ForCurrentProcess()->HasSwitch( | 47   return base::CommandLine::ForCurrentProcess()->HasSwitch( | 
| 46       switches::kEnableClearBrowsingDataCounters); | 48       switches::kEnableClearBrowsingDataCounters); | 
| (...skipping 20 matching lines...) Expand all  Loading... | 
| 67   clear_plugin_lso_data_enabled_.Init(prefs::kClearPluginLSODataEnabled, prefs); | 69   clear_plugin_lso_data_enabled_.Init(prefs::kClearPluginLSODataEnabled, prefs); | 
| 68   pepper_flash_settings_enabled_.Init(prefs::kPepperFlashSettingsEnabled, | 70   pepper_flash_settings_enabled_.Init(prefs::kPepperFlashSettingsEnabled, | 
| 69                                       prefs); | 71                                       prefs); | 
| 70   allow_deleting_browser_history_.Init( | 72   allow_deleting_browser_history_.Init( | 
| 71       prefs::kAllowDeletingBrowserHistory, | 73       prefs::kAllowDeletingBrowserHistory, | 
| 72       prefs, | 74       prefs, | 
| 73       base::Bind(&ClearBrowserDataHandler::OnBrowsingHistoryPrefChanged, | 75       base::Bind(&ClearBrowserDataHandler::OnBrowsingHistoryPrefChanged, | 
| 74                  base::Unretained(this))); | 76                  base::Unretained(this))); | 
| 75 | 77 | 
| 76   if (AreCountersEnabled()) { | 78   if (AreCountersEnabled()) { | 
| 77     AddCounter(make_scoped_ptr(new PasswordsCounter()), | 79     AddCounter(make_scoped_ptr(new PasswordsCounter())); | 
| 78                IDS_DEL_PASSWORDS_COUNTER); | 80     AddCounter(make_scoped_ptr(new HistoryCounter())); | 
| 79     AddCounter(make_scoped_ptr(new HistoryCounter()), | 81     AddCounter(make_scoped_ptr(new CacheCounter())); | 
| 80                IDS_DEL_BROWSING_HISTORY_COUNTER); |  | 
| 81     AddCounter(make_scoped_ptr(new CacheCounter()), IDS_DEL_CACHE_COUNTER); |  | 
| 82 | 82 | 
| 83     sync_service_ = | 83     sync_service_ = | 
| 84         ProfileSyncServiceFactory::GetForProfile(Profile::FromWebUI(web_ui())); | 84         ProfileSyncServiceFactory::GetForProfile(Profile::FromWebUI(web_ui())); | 
| 85     if (sync_service_) | 85     if (sync_service_) | 
| 86       sync_service_->AddObserver(this); | 86       sync_service_->AddObserver(this); | 
| 87   } | 87   } | 
| 88 } | 88 } | 
| 89 | 89 | 
| 90 void ClearBrowserDataHandler::InitializePage() { | 90 void ClearBrowserDataHandler::InitializePage() { | 
| 91   web_ui()->CallJavascriptFunction( | 91   web_ui()->CallJavascriptFunction( | 
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 301   web_ui()->CallJavascriptFunction("ClearBrowserDataOverlay.doneClearing"); | 301   web_ui()->CallJavascriptFunction("ClearBrowserDataOverlay.doneClearing"); | 
| 302 } | 302 } | 
| 303 | 303 | 
| 304 void ClearBrowserDataHandler::OnBrowsingHistoryPrefChanged() { | 304 void ClearBrowserDataHandler::OnBrowsingHistoryPrefChanged() { | 
| 305   web_ui()->CallJavascriptFunction( | 305   web_ui()->CallJavascriptFunction( | 
| 306       "ClearBrowserDataOverlay.setAllowDeletingHistory", | 306       "ClearBrowserDataOverlay.setAllowDeletingHistory", | 
| 307       base::FundamentalValue(*allow_deleting_browser_history_)); | 307       base::FundamentalValue(*allow_deleting_browser_history_)); | 
| 308 } | 308 } | 
| 309 | 309 | 
| 310 void ClearBrowserDataHandler::AddCounter( | 310 void ClearBrowserDataHandler::AddCounter( | 
| 311     scoped_ptr<BrowsingDataCounter> counter, int text_grd_id) { | 311     scoped_ptr<BrowsingDataCounter> counter) { | 
| 312   DCHECK(AreCountersEnabled()); | 312   DCHECK(AreCountersEnabled()); | 
| 313 | 313 | 
| 314   counter->Init( | 314   counter->Init( | 
| 315       Profile::FromWebUI(web_ui()), | 315       Profile::FromWebUI(web_ui()), | 
| 316       base::Bind(&ClearBrowserDataHandler::UpdateCounterText, | 316       base::Bind(&ClearBrowserDataHandler::UpdateCounterText, | 
| 317                  base::Unretained(this), | 317                  base::Unretained(this))); | 
| 318                  counter->GetPrefName(), |  | 
| 319                  text_grd_id)); |  | 
| 320   counters_.push_back(counter.Pass()); | 318   counters_.push_back(counter.Pass()); | 
| 321 } | 319 } | 
| 322 | 320 | 
| 323 void ClearBrowserDataHandler::UpdateCounterText( | 321 void ClearBrowserDataHandler::UpdateCounterText( | 
| 324     const std::string& pref_name, | 322     scoped_ptr<BrowsingDataCounter::Result> result) { | 
| 325     int text_grd_id, |  | 
| 326     bool finished, |  | 
| 327     BrowsingDataCounter::ResultInt count) { |  | 
| 328   DCHECK(AreCountersEnabled()); | 323   DCHECK(AreCountersEnabled()); | 
| 329   base::string16 text = finished | 324   base::string16 text; | 
| 330       ? l10n_util::GetPluralStringFUTF16(text_grd_id, count) | 325   std::string pref_name = result->source()->GetPrefName(); | 
| 331       : l10n_util::GetStringUTF16(IDS_CLEAR_BROWSING_DATA_CALCULATING); | 326 | 
|  | 327   if (!result->Finished()) { | 
|  | 328     // The counter is still counting. | 
|  | 329     text = l10n_util::GetStringUTF16(IDS_CLEAR_BROWSING_DATA_CALCULATING); | 
|  | 330 | 
|  | 331   } else if (pref_name == prefs::kDeletePasswords) { | 
|  | 332     // Passwords counter. | 
|  | 333     BrowsingDataCounter::ResultInt passwords_count = | 
|  | 334         static_cast<BrowsingDataCounter::FinishedResult*>( | 
|  | 335             result.get())->Value(); | 
|  | 336     text = l10n_util::GetPluralStringFUTF16( | 
|  | 337         IDS_DEL_PASSWORDS_COUNTER, passwords_count); | 
|  | 338 | 
|  | 339   } else if (pref_name == prefs::kDeleteCache) { | 
|  | 340     // Cache counter. | 
|  | 341     BrowsingDataCounter::ResultInt cache_size_bytes = | 
|  | 342         static_cast<BrowsingDataCounter::FinishedResult*>( | 
|  | 343             result.get())->Value(); | 
|  | 344 | 
|  | 345     PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs(); | 
|  | 346     BrowsingDataRemover::TimePeriod time_period = | 
|  | 347         static_cast<BrowsingDataRemover::TimePeriod>( | 
|  | 348             prefs->GetInteger(prefs::kDeleteTimePeriod)); | 
|  | 349 | 
|  | 350     // Three cases: Nonzero result for the entire cache, nonzero result for | 
|  | 351     // a subset of cache (i.e. a finite time interval), and almost zero (< 1MB). | 
|  | 352     static const int kBytesInAMegabyte = 1024 * 1024; | 
|  | 353     if (cache_size_bytes >= kBytesInAMegabyte) { | 
|  | 354       base::string16 formatted_size = ui::FormatBytes(cache_size_bytes); | 
|  | 355       text = time_period == BrowsingDataRemover::EVERYTHING | 
|  | 356           ? formatted_size | 
|  | 357           : l10n_util::GetStringFUTF16(IDS_DEL_CACHE_COUNTER_UPPER_ESTIMATE, | 
|  | 358                                        formatted_size); | 
|  | 359     } else { | 
|  | 360       base::string16 formatted_size = ui::FormatBytes(kBytesInAMegabyte); | 
|  | 361       text = l10n_util::GetStringFUTF16(IDS_DEL_CACHE_COUNTER_UPPER_ESTIMATE, | 
|  | 362                                         formatted_size); | 
|  | 363     } | 
|  | 364 | 
|  | 365   } else if (pref_name == prefs::kDeleteBrowsingHistory) { | 
|  | 366     // History counter. | 
|  | 367     HistoryCounter::HistoryResult* history_result = | 
|  | 368         static_cast<HistoryCounter::HistoryResult*>(result.get()); | 
|  | 369     BrowsingDataCounter::ResultInt local_item_count = history_result->Value(); | 
|  | 370     bool has_synced_visits = history_result->has_synced_visits(); | 
|  | 371 | 
|  | 372     text = has_synced_visits | 
|  | 373         ? l10n_util::GetPluralStringFUTF16( | 
|  | 374               IDS_DEL_BROWSING_HISTORY_COUNTER_SYNCED, local_item_count) | 
|  | 375         : l10n_util::GetPluralStringFUTF16( | 
|  | 376               IDS_DEL_BROWSING_HISTORY_COUNTER, local_item_count); | 
|  | 377   } | 
| 332 | 378 | 
| 333   web_ui()->CallJavascriptFunction( | 379   web_ui()->CallJavascriptFunction( | 
| 334       "ClearBrowserDataOverlay.updateCounter", | 380       "ClearBrowserDataOverlay.updateCounter", | 
| 335       base::StringValue(pref_name), | 381       base::StringValue(pref_name), | 
| 336       base::StringValue(text)); | 382       base::StringValue(text)); | 
| 337 } | 383 } | 
| 338 | 384 | 
| 339 void ClearBrowserDataHandler::OnStateChanged() { | 385 void ClearBrowserDataHandler::OnStateChanged() { | 
| 340   web_ui()->CallJavascriptFunction( | 386   web_ui()->CallJavascriptFunction( | 
| 341       "ClearBrowserDataOverlay.updateSyncWarning", | 387       "ClearBrowserDataOverlay.updateSyncWarning", | 
| 342       base::FundamentalValue(sync_service_ && sync_service_->IsSyncActive())); | 388       base::FundamentalValue(sync_service_ && sync_service_->IsSyncActive())); | 
| 343 } | 389 } | 
| 344 | 390 | 
| 345 }  // namespace options | 391 }  // namespace options | 
| OLD | NEW | 
|---|