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/prefs/pref_service.h"
13 #include "base/strings/string16.h"
14 #include "base/values.h"
15 #include "chrome/app/chrome_command_ids.h"
16 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/browsing_data/browsing_data_counter.h"
18 #include "chrome/browser/browsing_data/browsing_data_helper.h"
19 #include "chrome/browser/browsing_data/browsing_data_remover.h"
20 #include "chrome/browser/browsing_data/passwords_counter.h"
21 #include "chrome/browser/prefs/incognito_mode_prefs.h"
22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/ui/accelerator_utils.h"
24 #include "chrome/common/chrome_switches.h"
25 #include "chrome/common/pref_names.h"
26 #include "chrome/grit/generated_resources.h"
27 #include "chrome/grit/locale_settings.h"
28 #include "components/google/core/browser/google_util.h"
29 #include "content/public/browser/notification_details.h"
30 #include "content/public/browser/web_ui.h"
31 #include "ui/base/accelerators/accelerator.h"
32 #include "ui/base/l10n/l10n_util.h"
33 #include "ui/events/keycodes/keyboard_codes.h"
37 const char kClearBrowsingDataLearnMoreUrl
[] =
38 "https://support.google.com/chrome/?p=settings_clear_browsing_data";
40 bool AreCountersEnabled() {
41 return base::CommandLine::ForCurrentProcess()->HasSwitch(
42 switches::kEnableClearBrowsingDataCounters
);
49 ClearBrowserDataHandler::ClearBrowserDataHandler()
53 ClearBrowserDataHandler::~ClearBrowserDataHandler() {
55 remover_
->RemoveObserver(this);
58 void ClearBrowserDataHandler::InitializeHandler() {
59 PrefService
* prefs
= Profile::FromWebUI(web_ui())->GetPrefs();
60 clear_plugin_lso_data_enabled_
.Init(prefs::kClearPluginLSODataEnabled
, prefs
);
61 pepper_flash_settings_enabled_
.Init(prefs::kPepperFlashSettingsEnabled
,
63 allow_deleting_browser_history_
.Init(
64 prefs::kAllowDeletingBrowserHistory
,
66 base::Bind(&ClearBrowserDataHandler::OnBrowsingHistoryPrefChanged
,
67 base::Unretained(this)));
69 if (AreCountersEnabled()) {
71 make_scoped_ptr(new PasswordsCounter()), IDS_DEL_PASSWORDS_COUNTER
);
72 // TODO(msramek): Add counters for browsing history and cache.
76 void ClearBrowserDataHandler::InitializePage() {
77 UpdateInfoBannerVisibility();
78 OnBrowsingHistoryPrefChanged();
79 bool removal_in_progress
= !!remover_
;
80 web_ui()->CallJavascriptFunction("ClearBrowserDataOverlay.setClearing",
81 base::FundamentalValue(removal_in_progress
));
83 web_ui()->CallJavascriptFunction(
84 "ClearBrowserDataOverlay.markInitializationComplete");
87 void ClearBrowserDataHandler::UpdateInfoBannerVisibility() {
90 Profile
* profile
= Profile::FromWebUI(web_ui());
91 auto availability
= IncognitoModePrefs::GetAvailability(profile
->GetPrefs());
92 if (availability
== IncognitoModePrefs::ENABLED
) {
93 base::Time last_clear_time
= base::Time::FromInternalValue(
94 profile
->GetPrefs()->GetInt64(prefs::kLastClearBrowsingDataTime
));
96 const base::TimeDelta since_clear
= base::Time::Now() - last_clear_time
;
97 if (since_clear
< base::TimeDelta::FromHours(base::Time::kHoursPerDay
)) {
98 ui::Accelerator acc
= chrome::GetPrimaryChromeAcceleratorForCommandId(
99 IDC_NEW_INCOGNITO_WINDOW
);
100 DCHECK_NE(ui::VKEY_UNKNOWN
, acc
.key_code());
101 text
= l10n_util::GetStringFUTF16(IDS_CLEAR_BROWSING_DATA_INFO_BAR_TEXT
,
102 acc
.GetShortcutText());
106 web_ui()->CallJavascriptFunction("ClearBrowserDataOverlay.setBannerText",
107 base::StringValue(text
));
110 void ClearBrowserDataHandler::GetLocalizedValues(
111 base::DictionaryValue
* localized_strings
) {
112 DCHECK(localized_strings
);
114 static OptionsStringResource resources
[] = {
115 { "clearBrowserDataLabel", IDS_CLEAR_BROWSING_DATA_LABEL
},
116 { "contentSettingsAndSearchEnginesRemain",
117 IDS_CLEAR_BROWSING_DATA_SOME_STUFF_REMAINS
},
118 { "deleteBrowsingHistoryCheckbox", IDS_DEL_BROWSING_HISTORY_CHKBOX
},
119 { "deleteDownloadHistoryCheckbox", IDS_DEL_DOWNLOAD_HISTORY_CHKBOX
},
120 { "deleteCacheCheckbox", IDS_DEL_CACHE_CHKBOX
},
121 { "deleteCookiesCheckbox", IDS_DEL_COOKIES_CHKBOX
},
122 { "deleteCookiesFlashCheckbox", IDS_DEL_COOKIES_FLASH_CHKBOX
},
123 { "deletePasswordsCheckbox", IDS_DEL_PASSWORDS_CHKBOX
},
124 { "deleteFormDataCheckbox", IDS_DEL_FORM_DATA_CHKBOX
},
125 { "deleteHostedAppsDataCheckbox", IDS_DEL_HOSTED_APPS_DATA_CHKBOX
},
126 { "deauthorizeContentLicensesCheckbox",
127 IDS_DEAUTHORIZE_CONTENT_LICENSES_CHKBOX
},
128 { "clearBrowserDataCommit", IDS_CLEAR_BROWSING_DATA_COMMIT
},
129 { "flashStorageUrl", IDS_FLASH_STORAGE_URL
},
132 RegisterStrings(localized_strings
, resources
, arraysize(resources
));
133 RegisterTitle(localized_strings
, "clearBrowserDataOverlay",
134 IDS_CLEAR_BROWSING_DATA_TITLE
);
135 localized_strings
->SetString("clearBrowsingDataLearnMoreUrl",
136 kClearBrowsingDataLearnMoreUrl
);
138 base::ListValue
* time_list
= new base::ListValue
;
139 for (int i
= 0; i
< 5; i
++) {
140 base::string16 label_string
;
143 label_string
= l10n_util::GetStringUTF16(IDS_CLEAR_DATA_HOUR
);
146 label_string
= l10n_util::GetStringUTF16(IDS_CLEAR_DATA_DAY
);
149 label_string
= l10n_util::GetStringUTF16(IDS_CLEAR_DATA_WEEK
);
152 label_string
= l10n_util::GetStringUTF16(IDS_CLEAR_DATA_4WEEKS
);
155 label_string
= l10n_util::GetStringUTF16(IDS_CLEAR_DATA_EVERYTHING
);
158 base::ListValue
* option
= new base::ListValue();
159 option
->Append(new base::FundamentalValue(i
));
160 option
->Append(new base::StringValue(label_string
));
161 time_list
->Append(option
);
163 localized_strings
->Set("clearBrowserDataTimeList", time_list
);
164 localized_strings
->SetBoolean("showDeleteBrowsingHistoryCheckboxes",
165 !Profile::FromWebUI(web_ui())->IsSupervised());
168 void ClearBrowserDataHandler::RegisterMessages() {
169 // Setup handlers specific to this panel.
170 web_ui()->RegisterMessageCallback("performClearBrowserData",
171 base::Bind(&ClearBrowserDataHandler::HandleClearBrowserData
,
172 base::Unretained(this)));
175 void ClearBrowserDataHandler::HandleClearBrowserData(
176 const base::ListValue
* value
) {
177 // We should never be called when the previous clearing has not yet finished.
180 Profile
* profile
= Profile::FromWebUI(web_ui());
181 PrefService
* prefs
= profile
->GetPrefs();
183 int site_data_mask
= BrowsingDataRemover::REMOVE_SITE_DATA
;
184 // Don't try to clear LSO data if it's not supported.
185 if (!*clear_plugin_lso_data_enabled_
)
186 site_data_mask
&= ~BrowsingDataRemover::REMOVE_PLUGIN_DATA
;
190 if (prefs
->GetBoolean(prefs::kDeleteBrowsingHistory
) &&
191 *allow_deleting_browser_history_
) {
192 remove_mask
|= BrowsingDataRemover::REMOVE_HISTORY
;
194 if (prefs
->GetBoolean(prefs::kDeleteDownloadHistory
) &&
195 *allow_deleting_browser_history_
) {
196 remove_mask
|= BrowsingDataRemover::REMOVE_DOWNLOADS
;
198 if (prefs
->GetBoolean(prefs::kDeleteCache
))
199 remove_mask
|= BrowsingDataRemover::REMOVE_CACHE
;
200 if (prefs
->GetBoolean(prefs::kDeleteCookies
)) {
201 remove_mask
|= site_data_mask
;
202 origin_mask
|= BrowsingDataHelper::UNPROTECTED_WEB
;
204 if (prefs
->GetBoolean(prefs::kDeletePasswords
))
205 remove_mask
|= BrowsingDataRemover::REMOVE_PASSWORDS
;
206 if (prefs
->GetBoolean(prefs::kDeleteFormData
))
207 remove_mask
|= BrowsingDataRemover::REMOVE_FORM_DATA
;
208 // Clearing Content Licenses is only supported in Pepper Flash.
209 if (prefs
->GetBoolean(prefs::kDeauthorizeContentLicenses
) &&
210 *pepper_flash_settings_enabled_
) {
211 remove_mask
|= BrowsingDataRemover::REMOVE_CONTENT_LICENSES
;
213 if (prefs
->GetBoolean(prefs::kDeleteHostedAppsData
)) {
214 remove_mask
|= site_data_mask
;
215 origin_mask
|= BrowsingDataHelper::PROTECTED_WEB
;
218 // Record the deletion of cookies and cache.
219 BrowsingDataRemover::CookieOrCacheDeletionChoice choice
=
220 BrowsingDataRemover::NEITHER_COOKIES_NOR_CACHE
;
221 if (prefs
->GetBoolean(prefs::kDeleteCookies
)) {
222 choice
= prefs
->GetBoolean(prefs::kDeleteCache
)
223 ? BrowsingDataRemover::BOTH_COOKIES_AND_CACHE
224 : BrowsingDataRemover::ONLY_COOKIES
;
225 } else if (prefs
->GetBoolean(prefs::kDeleteCache
)) {
226 choice
= BrowsingDataRemover::ONLY_CACHE
;
228 UMA_HISTOGRAM_ENUMERATION(
229 "History.ClearBrowsingData.UserDeletedCookieOrCacheFromDialog",
230 choice
, BrowsingDataRemover::MAX_CHOICE_VALUE
);
232 // BrowsingDataRemover deletes itself when done.
233 int period_selected
= prefs
->GetInteger(prefs::kDeleteTimePeriod
);
234 remover_
= BrowsingDataRemover::CreateForPeriod(profile
,
235 static_cast<BrowsingDataRemover::TimePeriod
>(period_selected
));
236 remover_
->AddObserver(this);
237 remover_
->Remove(remove_mask
, origin_mask
);
239 // Store the clear browsing data time. Next time the clear browsing data
240 // dialog is open, this time is used to decide whether to display an info
242 prefs
->SetInt64(prefs::kLastClearBrowsingDataTime
,
243 base::Time::Now().ToInternalValue());
246 void ClearBrowserDataHandler::OnBrowsingDataRemoverDone() {
247 // No need to remove ourselves as an observer as BrowsingDataRemover deletes
248 // itself after we return.
250 web_ui()->CallJavascriptFunction("ClearBrowserDataOverlay.doneClearing");
253 void ClearBrowserDataHandler::OnBrowsingHistoryPrefChanged() {
254 web_ui()->CallJavascriptFunction(
255 "ClearBrowserDataOverlay.setAllowDeletingHistory",
256 base::FundamentalValue(*allow_deleting_browser_history_
));
259 void ClearBrowserDataHandler::AddCounter(
260 scoped_ptr
<BrowsingDataCounter
> counter
, int text_grd_id
) {
261 DCHECK(AreCountersEnabled());
264 Profile::FromWebUI(web_ui()),
265 base::Bind(&ClearBrowserDataHandler::UpdateCounterText
,
266 base::Unretained(this),
267 counter
->GetPrefName(),
269 counters_
.push_back(counter
.Pass());
272 void ClearBrowserDataHandler::UpdateCounterText(const std::string
& pref_name
,
276 DCHECK(AreCountersEnabled());
277 base::string16 text
= finished
278 ? l10n_util::GetPluralStringFUTF16(text_grd_id
, count
)
279 : l10n_util::GetStringUTF16(IDS_CLEAR_BROWSING_DATA_CALCULATING
);
281 web_ui()->CallJavascriptFunction(
282 "ClearBrowserDataOverlay.updateCounter",
283 base::StringValue(pref_name
),
284 base::StringValue(text
));
287 } // namespace options