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/prefs/pref_service.h"
11 #include "base/strings/string16.h"
12 #include "base/values.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/browsing_data/browsing_data_helper.h"
15 #include "chrome/browser/browsing_data/browsing_data_remover.h"
16 #include "chrome/browser/google/google_util.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/common/pref_names.h"
19 #include "content/public/browser/notification_details.h"
20 #include "content/public/browser/web_ui.h"
21 #include "grit/chromium_strings.h"
22 #include "grit/generated_resources.h"
23 #include "grit/locale_settings.h"
24 #include "ui/base/accelerators/accelerator.h"
25 #include "ui/base/l10n/l10n_util.h"
28 const char kClearBrowsingDataLearnMoreUrl
[] =
29 "https://support.google.com/chrome/?p=settings_clear_browsing_data";
34 ClearBrowserDataHandler::ClearBrowserDataHandler()
38 ClearBrowserDataHandler::~ClearBrowserDataHandler() {
40 remover_
->RemoveObserver(this);
43 void ClearBrowserDataHandler::InitializeHandler() {
44 PrefService
* prefs
= Profile::FromWebUI(web_ui())->GetPrefs();
45 clear_plugin_lso_data_enabled_
.Init(prefs::kClearPluginLSODataEnabled
, prefs
);
46 pepper_flash_settings_enabled_
.Init(prefs::kPepperFlashSettingsEnabled
,
48 allow_deleting_browser_history_
.Init(
49 prefs::kAllowDeletingBrowserHistory
,
51 base::Bind(&ClearBrowserDataHandler::OnBrowsingHistoryPrefChanged
,
52 base::Unretained(this)));
55 void ClearBrowserDataHandler::InitializePage() {
56 UpdateInfoBannerVisibility();
57 OnBrowsingHistoryPrefChanged();
60 void ClearBrowserDataHandler::UpdateInfoBannerVisibility() {
61 Profile
* profile
= Profile::FromWebUI(web_ui());
62 base::Time lastClearBrowsingDataTime
= base::Time::FromInternalValue(
63 profile
->GetPrefs()->GetInt64(prefs::kLastClearBrowsingDataTime
));
65 const int64 kHoursPerDay
= 24;
66 bool visible
= (base::Time::Now() - lastClearBrowsingDataTime
) <=
67 base::TimeDelta::FromHours(kHoursPerDay
);
70 args
.Append(base::Value::CreateBooleanValue(visible
));
71 web_ui()->CallJavascriptFunction(
72 "ClearBrowserDataOverlay.setBannerVisibility", args
);
75 void ClearBrowserDataHandler::GetLocalizedValues(
76 base::DictionaryValue
* localized_strings
) {
77 DCHECK(localized_strings
);
79 static OptionsStringResource resources
[] = {
80 { "clearBrowserDataLabel", IDS_CLEAR_BROWSING_DATA_LABEL
},
81 { "contentSettingsAndSearchEnginesRemain",
82 IDS_CLEAR_BROWSING_DATA_SOME_STUFF_REMAINS
},
83 { "deleteBrowsingHistoryCheckbox", IDS_DEL_BROWSING_HISTORY_CHKBOX
},
84 { "deleteDownloadHistoryCheckbox", IDS_DEL_DOWNLOAD_HISTORY_CHKBOX
},
85 { "deleteCacheCheckbox", IDS_DEL_CACHE_CHKBOX
},
86 { "deleteCookiesCheckbox", IDS_DEL_COOKIES_CHKBOX
},
87 { "deleteCookiesFlashCheckbox", IDS_DEL_COOKIES_FLASH_CHKBOX
},
88 { "deletePasswordsCheckbox", IDS_DEL_PASSWORDS_CHKBOX
},
89 { "deleteFormDataCheckbox", IDS_DEL_FORM_DATA_CHKBOX
},
90 { "deleteHostedAppsDataCheckbox", IDS_DEL_HOSTED_APPS_DATA_CHKBOX
},
91 { "deauthorizeContentLicensesCheckbox",
92 IDS_DEAUTHORIZE_CONTENT_LICENSES_CHKBOX
},
93 { "clearBrowserDataCommit", IDS_CLEAR_BROWSING_DATA_COMMIT
},
94 { "flash_storage_url", IDS_FLASH_STORAGE_URL
},
97 RegisterStrings(localized_strings
, resources
, arraysize(resources
));
98 RegisterTitle(localized_strings
, "clearBrowserDataOverlay",
99 IDS_CLEAR_BROWSING_DATA_TITLE
);
100 localized_strings
->SetString(
101 "clearBrowsingDataLearnMoreUrl",
102 google_util::StringAppendGoogleLocaleParam(
103 kClearBrowsingDataLearnMoreUrl
));
105 ui::Accelerator
acc(ui::VKEY_N
, ui::EF_CONTROL_DOWN
| ui::EF_SHIFT_DOWN
);
106 localized_strings
->SetString(
107 "clearBrowserDataInfoBar",
108 l10n_util::GetStringFUTF16(
109 IDS_CLEAR_BROWSING_DATA_INFO_BAR_TEXT
,
110 acc
.GetShortcutText()));
112 base::ListValue
* time_list
= new base::ListValue
;
113 for (int i
= 0; i
< 5; i
++) {
114 base::string16 label_string
;
117 label_string
= l10n_util::GetStringUTF16(IDS_CLEAR_DATA_HOUR
);
120 label_string
= l10n_util::GetStringUTF16(IDS_CLEAR_DATA_DAY
);
123 label_string
= l10n_util::GetStringUTF16(IDS_CLEAR_DATA_WEEK
);
126 label_string
= l10n_util::GetStringUTF16(IDS_CLEAR_DATA_4WEEKS
);
129 label_string
= l10n_util::GetStringUTF16(IDS_CLEAR_DATA_EVERYTHING
);
132 base::ListValue
* option
= new base::ListValue();
133 option
->Append(new base::FundamentalValue(i
));
134 option
->Append(new base::StringValue(label_string
));
135 time_list
->Append(option
);
137 localized_strings
->Set("clearBrowserDataTimeList", time_list
);
138 localized_strings
->SetBoolean("showDeleteBrowsingHistoryCheckboxes",
139 !Profile::FromWebUI(web_ui())->IsManaged());
142 void ClearBrowserDataHandler::RegisterMessages() {
143 // Setup handlers specific to this panel.
144 web_ui()->RegisterMessageCallback("performClearBrowserData",
145 base::Bind(&ClearBrowserDataHandler::HandleClearBrowserData
,
146 base::Unretained(this)));
149 void ClearBrowserDataHandler::HandleClearBrowserData(
150 const base::ListValue
* value
) {
153 Profile
* profile
= Profile::FromWebUI(web_ui());
154 PrefService
* prefs
= profile
->GetPrefs();
156 int site_data_mask
= BrowsingDataRemover::REMOVE_SITE_DATA
;
157 // Don't try to clear LSO data if it's not supported.
158 if (!*clear_plugin_lso_data_enabled_
)
159 site_data_mask
&= ~BrowsingDataRemover::REMOVE_PLUGIN_DATA
;
163 if (prefs
->GetBoolean(prefs::kDeleteBrowsingHistory
) &&
164 *allow_deleting_browser_history_
) {
165 remove_mask
|= BrowsingDataRemover::REMOVE_HISTORY
;
167 if (prefs
->GetBoolean(prefs::kDeleteDownloadHistory
) &&
168 *allow_deleting_browser_history_
) {
169 remove_mask
|= BrowsingDataRemover::REMOVE_DOWNLOADS
;
171 if (prefs
->GetBoolean(prefs::kDeleteCache
))
172 remove_mask
|= BrowsingDataRemover::REMOVE_CACHE
;
173 if (prefs
->GetBoolean(prefs::kDeleteCookies
)) {
174 remove_mask
|= site_data_mask
;
175 origin_mask
|= BrowsingDataHelper::UNPROTECTED_WEB
;
177 if (prefs
->GetBoolean(prefs::kDeletePasswords
))
178 remove_mask
|= BrowsingDataRemover::REMOVE_PASSWORDS
;
179 if (prefs
->GetBoolean(prefs::kDeleteFormData
))
180 remove_mask
|= BrowsingDataRemover::REMOVE_FORM_DATA
;
181 // Clearing Content Licenses is only supported in Pepper Flash.
182 if (prefs
->GetBoolean(prefs::kDeauthorizeContentLicenses
) &&
183 *pepper_flash_settings_enabled_
) {
184 remove_mask
|= BrowsingDataRemover::REMOVE_CONTENT_LICENSES
;
186 if (prefs
->GetBoolean(prefs::kDeleteHostedAppsData
)) {
187 remove_mask
|= site_data_mask
;
188 origin_mask
|= BrowsingDataHelper::PROTECTED_WEB
;
191 // BrowsingDataRemover deletes itself when done.
192 int period_selected
= prefs
->GetInteger(prefs::kDeleteTimePeriod
);
193 remover_
= BrowsingDataRemover::CreateForPeriod(profile
,
194 static_cast<BrowsingDataRemover::TimePeriod
>(period_selected
));
195 remover_
->AddObserver(this);
196 remover_
->Remove(remove_mask
, origin_mask
);
198 // Store the clear browsing data time. Next time the clear browsing data
199 // dialog is open, this time is used to decide whether to display an info
201 prefs
->SetInt64(prefs::kLastClearBrowsingDataTime
,
202 base::Time::Now().ToInternalValue());
205 void ClearBrowserDataHandler::OnBrowsingDataRemoverDone() {
206 // No need to remove ourselves as an observer as BrowsingDataRemover deletes
207 // itself after we return.
209 web_ui()->CallJavascriptFunction("ClearBrowserDataOverlay.doneClearing");
212 void ClearBrowserDataHandler::OnBrowsingHistoryPrefChanged() {
213 web_ui()->CallJavascriptFunction(
214 "ClearBrowserDataOverlay.updateHistoryCheckboxes",
215 base::FundamentalValue(*allow_deleting_browser_history_
));
218 } // namespace options