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/profiles/profile.h"
17 #include "chrome/common/pref_names.h"
18 #include "chrome/grit/generated_resources.h"
19 #include "chrome/grit/locale_settings.h"
20 #include "components/google/core/browser/google_util.h"
21 #include "content/public/browser/notification_details.h"
22 #include "content/public/browser/web_ui.h"
23 #include "ui/base/accelerators/accelerator.h"
24 #include "ui/base/l10n/l10n_util.h"
27 const char kClearBrowsingDataLearnMoreUrl
[] =
28 "https://support.google.com/chrome/?p=settings_clear_browsing_data";
33 ClearBrowserDataHandler::ClearBrowserDataHandler()
37 ClearBrowserDataHandler::~ClearBrowserDataHandler() {
39 remover_
->RemoveObserver(this);
42 void ClearBrowserDataHandler::InitializeHandler() {
43 PrefService
* prefs
= Profile::FromWebUI(web_ui())->GetPrefs();
44 clear_plugin_lso_data_enabled_
.Init(prefs::kClearPluginLSODataEnabled
, prefs
);
45 pepper_flash_settings_enabled_
.Init(prefs::kPepperFlashSettingsEnabled
,
47 allow_deleting_browser_history_
.Init(
48 prefs::kAllowDeletingBrowserHistory
,
50 base::Bind(&ClearBrowserDataHandler::OnBrowsingHistoryPrefChanged
,
51 base::Unretained(this)));
54 void ClearBrowserDataHandler::InitializePage() {
55 UpdateInfoBannerVisibility();
56 OnBrowsingHistoryPrefChanged();
57 bool removal_in_progress
= !!remover_
;
58 web_ui()->CallJavascriptFunction("ClearBrowserDataOverlay.setClearing",
59 base::FundamentalValue(removal_in_progress
));
61 web_ui()->CallJavascriptFunction(
62 "ClearBrowserDataOverlay.markInitializationComplete");
65 void ClearBrowserDataHandler::UpdateInfoBannerVisibility() {
66 Profile
* profile
= Profile::FromWebUI(web_ui());
67 base::Time lastClearBrowsingDataTime
= base::Time::FromInternalValue(
68 profile
->GetPrefs()->GetInt64(prefs::kLastClearBrowsingDataTime
));
70 bool visible
= (base::Time::Now() - lastClearBrowsingDataTime
) <=
71 base::TimeDelta::FromHours(base::Time::kHoursPerDay
);
74 args
.AppendBoolean(visible
);
75 web_ui()->CallJavascriptFunction(
76 "ClearBrowserDataOverlay.setBannerVisibility", args
);
79 void ClearBrowserDataHandler::GetLocalizedValues(
80 base::DictionaryValue
* localized_strings
) {
81 DCHECK(localized_strings
);
83 static OptionsStringResource resources
[] = {
84 { "clearBrowserDataLabel", IDS_CLEAR_BROWSING_DATA_LABEL
},
85 { "contentSettingsAndSearchEnginesRemain",
86 IDS_CLEAR_BROWSING_DATA_SOME_STUFF_REMAINS
},
87 { "deleteBrowsingHistoryCheckbox", IDS_DEL_BROWSING_HISTORY_CHKBOX
},
88 { "deleteDownloadHistoryCheckbox", IDS_DEL_DOWNLOAD_HISTORY_CHKBOX
},
89 { "deleteCacheCheckbox", IDS_DEL_CACHE_CHKBOX
},
90 { "deleteCookiesCheckbox", IDS_DEL_COOKIES_CHKBOX
},
91 { "deleteCookiesFlashCheckbox", IDS_DEL_COOKIES_FLASH_CHKBOX
},
92 { "deletePasswordsCheckbox", IDS_DEL_PASSWORDS_CHKBOX
},
93 { "deleteFormDataCheckbox", IDS_DEL_FORM_DATA_CHKBOX
},
94 { "deleteHostedAppsDataCheckbox", IDS_DEL_HOSTED_APPS_DATA_CHKBOX
},
95 { "deauthorizeContentLicensesCheckbox",
96 IDS_DEAUTHORIZE_CONTENT_LICENSES_CHKBOX
},
97 { "clearBrowserDataCommit", IDS_CLEAR_BROWSING_DATA_COMMIT
},
98 { "flashStorageUrl", IDS_FLASH_STORAGE_URL
},
101 RegisterStrings(localized_strings
, resources
, arraysize(resources
));
102 RegisterTitle(localized_strings
, "clearBrowserDataOverlay",
103 IDS_CLEAR_BROWSING_DATA_TITLE
);
104 localized_strings
->SetString("clearBrowsingDataLearnMoreUrl",
105 kClearBrowsingDataLearnMoreUrl
);
107 ui::Accelerator
acc(ui::VKEY_N
, ui::EF_CONTROL_DOWN
| ui::EF_SHIFT_DOWN
);
108 localized_strings
->SetString(
109 "clearBrowserDataInfoBar",
110 l10n_util::GetStringFUTF16(
111 IDS_CLEAR_BROWSING_DATA_INFO_BAR_TEXT
,
112 acc
.GetShortcutText()));
114 base::ListValue
* time_list
= new base::ListValue
;
115 for (int i
= 0; i
< 5; i
++) {
116 base::string16 label_string
;
119 label_string
= l10n_util::GetStringUTF16(IDS_CLEAR_DATA_HOUR
);
122 label_string
= l10n_util::GetStringUTF16(IDS_CLEAR_DATA_DAY
);
125 label_string
= l10n_util::GetStringUTF16(IDS_CLEAR_DATA_WEEK
);
128 label_string
= l10n_util::GetStringUTF16(IDS_CLEAR_DATA_4WEEKS
);
131 label_string
= l10n_util::GetStringUTF16(IDS_CLEAR_DATA_EVERYTHING
);
134 base::ListValue
* option
= new base::ListValue();
135 option
->Append(new base::FundamentalValue(i
));
136 option
->Append(new base::StringValue(label_string
));
137 time_list
->Append(option
);
139 localized_strings
->Set("clearBrowserDataTimeList", time_list
);
140 localized_strings
->SetBoolean("showDeleteBrowsingHistoryCheckboxes",
141 !Profile::FromWebUI(web_ui())->IsSupervised());
144 void ClearBrowserDataHandler::RegisterMessages() {
145 // Setup handlers specific to this panel.
146 web_ui()->RegisterMessageCallback("performClearBrowserData",
147 base::Bind(&ClearBrowserDataHandler::HandleClearBrowserData
,
148 base::Unretained(this)));
151 void ClearBrowserDataHandler::HandleClearBrowserData(
152 const base::ListValue
* value
) {
153 // We should never be called when the previous clearing has not yet finished.
156 Profile
* profile
= Profile::FromWebUI(web_ui());
157 PrefService
* prefs
= profile
->GetPrefs();
159 int site_data_mask
= BrowsingDataRemover::REMOVE_SITE_DATA
;
160 // Don't try to clear LSO data if it's not supported.
161 if (!*clear_plugin_lso_data_enabled_
)
162 site_data_mask
&= ~BrowsingDataRemover::REMOVE_PLUGIN_DATA
;
166 if (prefs
->GetBoolean(prefs::kDeleteBrowsingHistory
) &&
167 *allow_deleting_browser_history_
) {
168 remove_mask
|= BrowsingDataRemover::REMOVE_HISTORY
;
170 if (prefs
->GetBoolean(prefs::kDeleteDownloadHistory
) &&
171 *allow_deleting_browser_history_
) {
172 remove_mask
|= BrowsingDataRemover::REMOVE_DOWNLOADS
;
174 if (prefs
->GetBoolean(prefs::kDeleteCache
))
175 remove_mask
|= BrowsingDataRemover::REMOVE_CACHE
;
176 if (prefs
->GetBoolean(prefs::kDeleteCookies
)) {
177 remove_mask
|= site_data_mask
;
178 origin_mask
|= BrowsingDataHelper::UNPROTECTED_WEB
;
180 if (prefs
->GetBoolean(prefs::kDeletePasswords
))
181 remove_mask
|= BrowsingDataRemover::REMOVE_PASSWORDS
;
182 if (prefs
->GetBoolean(prefs::kDeleteFormData
))
183 remove_mask
|= BrowsingDataRemover::REMOVE_FORM_DATA
;
184 // Clearing Content Licenses is only supported in Pepper Flash.
185 if (prefs
->GetBoolean(prefs::kDeauthorizeContentLicenses
) &&
186 *pepper_flash_settings_enabled_
) {
187 remove_mask
|= BrowsingDataRemover::REMOVE_CONTENT_LICENSES
;
189 if (prefs
->GetBoolean(prefs::kDeleteHostedAppsData
)) {
190 remove_mask
|= site_data_mask
;
191 origin_mask
|= BrowsingDataHelper::PROTECTED_WEB
;
194 // BrowsingDataRemover deletes itself when done.
195 int period_selected
= prefs
->GetInteger(prefs::kDeleteTimePeriod
);
196 remover_
= BrowsingDataRemover::CreateForPeriod(profile
,
197 static_cast<BrowsingDataRemover::TimePeriod
>(period_selected
));
198 remover_
->AddObserver(this);
199 remover_
->Remove(remove_mask
, origin_mask
);
201 // Store the clear browsing data time. Next time the clear browsing data
202 // dialog is open, this time is used to decide whether to display an info
204 prefs
->SetInt64(prefs::kLastClearBrowsingDataTime
,
205 base::Time::Now().ToInternalValue());
208 void ClearBrowserDataHandler::OnBrowsingDataRemoverDone() {
209 // No need to remove ourselves as an observer as BrowsingDataRemover deletes
210 // itself after we return.
212 web_ui()->CallJavascriptFunction("ClearBrowserDataOverlay.doneClearing");
215 void ClearBrowserDataHandler::OnBrowsingHistoryPrefChanged() {
216 web_ui()->CallJavascriptFunction(
217 "ClearBrowserDataOverlay.setAllowDeletingHistory",
218 base::FundamentalValue(*allow_deleting_browser_history_
));
221 } // namespace options