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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/webui/options/clear_browser_data_handler.h"
7 #include "base/basictypes.h"
9 #include "base/bind_helpers.h"
10 #include "base/command_line.h"
11 #include "base/metrics/histogram_macros.h"
12 #include "base/metrics/sparse_histogram.h"
13 #include "base/prefs/pref_service.h"
14 #include "base/strings/string16.h"
15 #include "base/values.h"
16 #include "chrome/app/chrome_command_ids.h"
17 #include "chrome/browser/browser_process.h"
18 #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_remover.h"
21 #include "chrome/browser/browsing_data/passwords_counter.h"
22 #include "chrome/browser/prefs/incognito_mode_prefs.h"
23 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/ui/accelerator_utils.h"
25 #include "chrome/common/chrome_switches.h"
26 #include "chrome/common/pref_names.h"
27 #include "chrome/grit/generated_resources.h"
28 #include "chrome/grit/locale_settings.h"
29 #include "components/google/core/browser/google_util.h"
30 #include "content/public/browser/notification_details.h"
31 #include "content/public/browser/web_ui.h"
32 #include "ui/base/accelerators/accelerator.h"
33 #include "ui/base/l10n/l10n_util.h"
34 #include "ui/events/keycodes/keyboard_codes.h"
38 const char kClearBrowsingDataLearnMoreUrl
[] =
39 "https://support.google.com/chrome/?p=settings_clear_browsing_data";
41 bool AreCountersEnabled() {
42 return base::CommandLine::ForCurrentProcess()->HasSwitch(
43 switches::kEnableClearBrowsingDataCounters
);
50 ClearBrowserDataHandler::ClearBrowserDataHandler()
54 ClearBrowserDataHandler::~ClearBrowserDataHandler() {
56 remover_
->RemoveObserver(this);
59 void ClearBrowserDataHandler::InitializeHandler() {
60 PrefService
* prefs
= Profile::FromWebUI(web_ui())->GetPrefs();
61 clear_plugin_lso_data_enabled_
.Init(prefs::kClearPluginLSODataEnabled
, prefs
);
62 pepper_flash_settings_enabled_
.Init(prefs::kPepperFlashSettingsEnabled
,
64 allow_deleting_browser_history_
.Init(
65 prefs::kAllowDeletingBrowserHistory
,
67 base::Bind(&ClearBrowserDataHandler::OnBrowsingHistoryPrefChanged
,
68 base::Unretained(this)));
70 if (AreCountersEnabled()) {
72 make_scoped_ptr(new PasswordsCounter()), IDS_DEL_PASSWORDS_COUNTER
);
73 // TODO(msramek): Add counters for browsing history and cache.
77 void ClearBrowserDataHandler::InitializePage() {
78 UpdateInfoBannerVisibility();
79 OnBrowsingHistoryPrefChanged();
80 bool removal_in_progress
= !!remover_
;
81 web_ui()->CallJavascriptFunction("ClearBrowserDataOverlay.setClearing",
82 base::FundamentalValue(removal_in_progress
));
84 for (BrowsingDataCounter
* counter
: counters_
) {
85 DCHECK(AreCountersEnabled());
88 web_ui()->CallJavascriptFunction(
89 "ClearBrowserDataOverlay.markInitializationComplete");
92 void ClearBrowserDataHandler::UpdateInfoBannerVisibility() {
95 Profile
* profile
= Profile::FromWebUI(web_ui());
96 auto availability
= IncognitoModePrefs::GetAvailability(profile
->GetPrefs());
97 if (availability
== IncognitoModePrefs::ENABLED
) {
98 base::Time last_clear_time
= base::Time::FromInternalValue(
99 profile
->GetPrefs()->GetInt64(prefs::kLastClearBrowsingDataTime
));
101 const base::TimeDelta since_clear
= base::Time::Now() - last_clear_time
;
102 if (since_clear
< base::TimeDelta::FromHours(base::Time::kHoursPerDay
)) {
103 ui::Accelerator acc
= chrome::GetPrimaryChromeAcceleratorForCommandId(
104 IDC_NEW_INCOGNITO_WINDOW
);
105 DCHECK_NE(ui::VKEY_UNKNOWN
, acc
.key_code());
106 text
= l10n_util::GetStringFUTF16(IDS_CLEAR_BROWSING_DATA_INFO_BAR_TEXT
,
107 acc
.GetShortcutText());
111 web_ui()->CallJavascriptFunction("ClearBrowserDataOverlay.setBannerText",
112 base::StringValue(text
));
115 void ClearBrowserDataHandler::GetLocalizedValues(
116 base::DictionaryValue
* localized_strings
) {
117 DCHECK(localized_strings
);
119 static OptionsStringResource resources
[] = {
120 { "clearBrowserDataLabel", IDS_CLEAR_BROWSING_DATA_LABEL
},
121 { "contentSettingsAndSearchEnginesRemain",
122 IDS_CLEAR_BROWSING_DATA_SOME_STUFF_REMAINS
},
123 { "deleteBrowsingHistoryCheckbox", IDS_DEL_BROWSING_HISTORY_CHKBOX
},
124 { "deleteDownloadHistoryCheckbox", IDS_DEL_DOWNLOAD_HISTORY_CHKBOX
},
125 { "deleteCacheCheckbox", IDS_DEL_CACHE_CHKBOX
},
126 { "deleteCookiesCheckbox", IDS_DEL_COOKIES_CHKBOX
},
127 { "deleteCookiesFlashCheckbox", IDS_DEL_COOKIES_FLASH_CHKBOX
},
128 { "deletePasswordsCheckbox", IDS_DEL_PASSWORDS_CHKBOX
},
129 { "deleteFormDataCheckbox", IDS_DEL_FORM_DATA_CHKBOX
},
130 { "deleteHostedAppsDataCheckbox", IDS_DEL_HOSTED_APPS_DATA_CHKBOX
},
131 { "deauthorizeContentLicensesCheckbox",
132 IDS_DEAUTHORIZE_CONTENT_LICENSES_CHKBOX
},
133 { "clearBrowserDataCommit", IDS_CLEAR_BROWSING_DATA_COMMIT
},
134 { "flashStorageUrl", IDS_FLASH_STORAGE_URL
},
137 RegisterStrings(localized_strings
, resources
, arraysize(resources
));
138 RegisterTitle(localized_strings
, "clearBrowserDataOverlay",
139 IDS_CLEAR_BROWSING_DATA_TITLE
);
140 localized_strings
->SetString("clearBrowsingDataLearnMoreUrl",
141 kClearBrowsingDataLearnMoreUrl
);
143 base::ListValue
* time_list
= new base::ListValue
;
144 for (int i
= 0; i
< 5; i
++) {
145 base::string16 label_string
;
148 label_string
= l10n_util::GetStringUTF16(IDS_CLEAR_DATA_HOUR
);
151 label_string
= l10n_util::GetStringUTF16(IDS_CLEAR_DATA_DAY
);
154 label_string
= l10n_util::GetStringUTF16(IDS_CLEAR_DATA_WEEK
);
157 label_string
= l10n_util::GetStringUTF16(IDS_CLEAR_DATA_4WEEKS
);
160 label_string
= l10n_util::GetStringUTF16(IDS_CLEAR_DATA_EVERYTHING
);
163 base::ListValue
* option
= new base::ListValue();
164 option
->Append(new base::FundamentalValue(i
));
165 option
->Append(new base::StringValue(label_string
));
166 time_list
->Append(option
);
168 localized_strings
->Set("clearBrowserDataTimeList", time_list
);
169 localized_strings
->SetBoolean("showDeleteBrowsingHistoryCheckboxes",
170 !Profile::FromWebUI(web_ui())->IsSupervised());
173 void ClearBrowserDataHandler::RegisterMessages() {
174 // Setup handlers specific to this panel.
175 web_ui()->RegisterMessageCallback("performClearBrowserData",
176 base::Bind(&ClearBrowserDataHandler::HandleClearBrowserData
,
177 base::Unretained(this)));
180 void ClearBrowserDataHandler::HandleClearBrowserData(
181 const base::ListValue
* value
) {
182 // We should never be called when the previous clearing has not yet finished.
185 Profile
* profile
= Profile::FromWebUI(web_ui());
186 PrefService
* prefs
= profile
->GetPrefs();
188 int site_data_mask
= BrowsingDataRemover::REMOVE_SITE_DATA
;
189 // Don't try to clear LSO data if it's not supported.
190 if (!*clear_plugin_lso_data_enabled_
)
191 site_data_mask
&= ~BrowsingDataRemover::REMOVE_PLUGIN_DATA
;
195 if (prefs
->GetBoolean(prefs::kDeleteBrowsingHistory
) &&
196 *allow_deleting_browser_history_
) {
197 remove_mask
|= BrowsingDataRemover::REMOVE_HISTORY
;
199 if (prefs
->GetBoolean(prefs::kDeleteDownloadHistory
) &&
200 *allow_deleting_browser_history_
) {
201 remove_mask
|= BrowsingDataRemover::REMOVE_DOWNLOADS
;
203 if (prefs
->GetBoolean(prefs::kDeleteCache
))
204 remove_mask
|= BrowsingDataRemover::REMOVE_CACHE
;
205 if (prefs
->GetBoolean(prefs::kDeleteCookies
)) {
206 remove_mask
|= site_data_mask
;
207 origin_mask
|= BrowsingDataHelper::UNPROTECTED_WEB
;
209 if (prefs
->GetBoolean(prefs::kDeletePasswords
))
210 remove_mask
|= BrowsingDataRemover::REMOVE_PASSWORDS
;
211 if (prefs
->GetBoolean(prefs::kDeleteFormData
))
212 remove_mask
|= BrowsingDataRemover::REMOVE_FORM_DATA
;
213 // Clearing Content Licenses is only supported in Pepper Flash.
214 if (prefs
->GetBoolean(prefs::kDeauthorizeContentLicenses
) &&
215 *pepper_flash_settings_enabled_
) {
216 remove_mask
|= BrowsingDataRemover::REMOVE_CONTENT_LICENSES
;
218 if (prefs
->GetBoolean(prefs::kDeleteHostedAppsData
)) {
219 remove_mask
|= site_data_mask
;
220 origin_mask
|= BrowsingDataHelper::PROTECTED_WEB
;
223 // Record the deletion of cookies and cache.
224 BrowsingDataRemover::CookieOrCacheDeletionChoice choice
=
225 BrowsingDataRemover::NEITHER_COOKIES_NOR_CACHE
;
226 if (prefs
->GetBoolean(prefs::kDeleteCookies
)) {
227 choice
= prefs
->GetBoolean(prefs::kDeleteCache
)
228 ? BrowsingDataRemover::BOTH_COOKIES_AND_CACHE
229 : BrowsingDataRemover::ONLY_COOKIES
;
230 } else if (prefs
->GetBoolean(prefs::kDeleteCache
)) {
231 choice
= BrowsingDataRemover::ONLY_CACHE
;
233 UMA_HISTOGRAM_ENUMERATION(
234 "History.ClearBrowsingData.UserDeletedCookieOrCacheFromDialog",
235 choice
, BrowsingDataRemover::MAX_CHOICE_VALUE
);
237 // Record the circumstances under which passwords are deleted.
238 if (prefs
->GetBoolean(prefs::kDeletePasswords
)) {
239 static const char* other_types
[] = {
240 prefs::kDeleteBrowsingHistory
,
241 prefs::kDeleteDownloadHistory
,
243 prefs::kDeleteCookies
,
244 prefs::kDeleteFormData
,
245 prefs::kDeleteHostedAppsData
,
246 prefs::kDeauthorizeContentLicenses
,
248 static size_t num_other_types
= arraysize(other_types
);
249 int checked_other_types
= std::count_if(
251 other_types
+ num_other_types
,
252 [prefs
](const std::string
& pref
) { return prefs
->GetBoolean(pref
); });
253 UMA_HISTOGRAM_SPARSE_SLOWLY(
254 "History.ClearBrowsingData.PasswordsDeletion.AdditionalDatatypesCount",
255 checked_other_types
);
258 // BrowsingDataRemover deletes itself when done.
259 int period_selected
= prefs
->GetInteger(prefs::kDeleteTimePeriod
);
260 remover_
= BrowsingDataRemover::CreateForPeriod(profile
,
261 static_cast<BrowsingDataRemover::TimePeriod
>(period_selected
));
262 remover_
->AddObserver(this);
263 remover_
->Remove(remove_mask
, origin_mask
);
265 // Store the clear browsing data time. Next time the clear browsing data
266 // dialog is open, this time is used to decide whether to display an info
268 prefs
->SetInt64(prefs::kLastClearBrowsingDataTime
,
269 base::Time::Now().ToInternalValue());
272 void ClearBrowserDataHandler::OnBrowsingDataRemoverDone() {
273 // No need to remove ourselves as an observer as BrowsingDataRemover deletes
274 // itself after we return.
276 web_ui()->CallJavascriptFunction("ClearBrowserDataOverlay.doneClearing");
279 void ClearBrowserDataHandler::OnBrowsingHistoryPrefChanged() {
280 web_ui()->CallJavascriptFunction(
281 "ClearBrowserDataOverlay.setAllowDeletingHistory",
282 base::FundamentalValue(*allow_deleting_browser_history_
));
285 void ClearBrowserDataHandler::AddCounter(
286 scoped_ptr
<BrowsingDataCounter
> counter
, int text_grd_id
) {
287 DCHECK(AreCountersEnabled());
290 Profile::FromWebUI(web_ui()),
291 base::Bind(&ClearBrowserDataHandler::UpdateCounterText
,
292 base::Unretained(this),
293 counter
->GetPrefName(),
295 counters_
.push_back(counter
.Pass());
298 void ClearBrowserDataHandler::UpdateCounterText(const std::string
& pref_name
,
302 DCHECK(AreCountersEnabled());
303 base::string16 text
= finished
304 ? l10n_util::GetPluralStringFUTF16(text_grd_id
, count
)
305 : l10n_util::GetStringUTF16(IDS_CLEAR_BROWSING_DATA_CALCULATING
);
307 web_ui()->CallJavascriptFunction(
308 "ClearBrowserDataOverlay.updateCounter",
309 base::StringValue(pref_name
),
310 base::StringValue(text
));
313 } // namespace options