1 // Copyright (c) 2013 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/profile_resetter/profile_resetter.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/prefs/scoped_user_pref_update.h"
11 #include "base/synchronization/cancellation_flag.h"
12 #include "chrome/browser/browsing_data/browsing_data_helper.h"
13 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
14 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/google/google_url_tracker_factory.h"
16 #include "chrome/browser/profile_resetter/brandcoded_default_settings.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/search_engines/template_url_service_factory.h"
19 #include "chrome/browser/ui/browser.h"
20 #include "chrome/browser/ui/browser_iterator.h"
21 #include "chrome/browser/ui/tabs/tab_strip_model.h"
22 #include "chrome/common/pref_names.h"
23 #include "chrome/installer/util/browser_distribution.h"
24 #include "components/content_settings/core/browser/host_content_settings_map.h"
25 #include "components/content_settings/core/browser/website_settings_info.h"
26 #include "components/content_settings/core/browser/website_settings_registry.h"
27 #include "components/google/core/browser/google_url_tracker.h"
28 #include "components/search_engines/search_engines_pref_names.h"
29 #include "components/search_engines/template_url_prepopulate_data.h"
30 #include "components/search_engines/template_url_service.h"
31 #include "content/public/browser/browser_thread.h"
32 #include "extensions/browser/extension_system.h"
33 #include "extensions/browser/management_policy.h"
36 #include "base/base_paths.h"
37 #include "base/path_service.h"
38 #include "chrome/installer/util/shell_util.h"
42 void ResetShortcutsOnFileThread() {
43 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE);
44 // Get full path of chrome.
45 base::FilePath chrome_exe
;
46 if (!PathService::Get(base::FILE_EXE
, &chrome_exe
))
48 BrowserDistribution
* dist
= BrowserDistribution::GetSpecificDistribution(
49 BrowserDistribution::CHROME_BROWSER
);
50 for (int location
= ShellUtil::SHORTCUT_LOCATION_FIRST
;
51 location
< ShellUtil::NUM_SHORTCUT_LOCATIONS
; ++location
) {
52 ShellUtil::ShortcutListMaybeRemoveUnknownArgs(
53 static_cast<ShellUtil::ShortcutLocation
>(location
),
55 ShellUtil::CURRENT_USER
,
64 #endif // defined(OS_WIN)
66 ProfileResetter::ProfileResetter(Profile
* profile
)
68 template_url_service_(TemplateURLServiceFactory::GetForProfile(profile_
)),
69 pending_reset_flags_(0),
70 cookies_remover_(NULL
),
71 weak_ptr_factory_(this) {
72 DCHECK(CalledOnValidThread());
76 ProfileResetter::~ProfileResetter() {
78 cookies_remover_
->RemoveObserver(this);
81 void ProfileResetter::Reset(
82 ProfileResetter::ResettableFlags resettable_flags
,
83 scoped_ptr
<BrandcodedDefaultSettings
> master_settings
,
84 bool accepted_send_feedback
,
85 const base::Closure
& callback
) {
86 DCHECK(CalledOnValidThread());
87 DCHECK(master_settings
);
89 // We should never be called with unknown flags.
90 CHECK_EQ(static_cast<ResettableFlags
>(0), resettable_flags
& ~ALL
);
92 // We should never be called when a previous reset has not finished.
93 CHECK_EQ(static_cast<ResettableFlags
>(0), pending_reset_flags_
);
95 if (!resettable_flags
) {
96 content::BrowserThread::PostTask(content::BrowserThread::UI
, FROM_HERE
,
101 master_settings_
.swap(master_settings
);
102 callback_
= callback
;
104 // These flags are set to false by the individual reset functions.
105 pending_reset_flags_
= resettable_flags
;
109 void (ProfileResetter::*method
)();
111 {DEFAULT_SEARCH_ENGINE
, &ProfileResetter::ResetDefaultSearchEngine
},
112 {HOMEPAGE
, &ProfileResetter::ResetHomepage
},
113 {CONTENT_SETTINGS
, &ProfileResetter::ResetContentSettings
},
114 {COOKIES_AND_SITE_DATA
, &ProfileResetter::ResetCookiesAndSiteData
},
115 {EXTENSIONS
, &ProfileResetter::ResetExtensions
},
116 {STARTUP_PAGES
, &ProfileResetter::ResetStartupPages
},
117 {PINNED_TABS
, &ProfileResetter::ResetPinnedTabs
},
118 {SHORTCUTS
, &ProfileResetter::ResetShortcuts
},
121 ResettableFlags reset_triggered_for_flags
= 0;
122 for (size_t i
= 0; i
< arraysize(flagToMethod
); ++i
) {
123 if (resettable_flags
& flagToMethod
[i
].flag
) {
124 reset_triggered_for_flags
|= flagToMethod
[i
].flag
;
125 (this->*flagToMethod
[i
].method
)();
129 DCHECK_EQ(resettable_flags
, reset_triggered_for_flags
);
132 bool ProfileResetter::IsActive() const {
133 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
134 return pending_reset_flags_
!= 0;
137 void ProfileResetter::MarkAsDone(Resettable resettable
) {
138 DCHECK(CalledOnValidThread());
140 // Check that we are never called twice or unexpectedly.
141 CHECK(pending_reset_flags_
& resettable
);
143 pending_reset_flags_
&= ~resettable
;
145 if (!pending_reset_flags_
) {
146 content::BrowserThread::PostTask(content::BrowserThread::UI
, FROM_HERE
,
149 master_settings_
.reset();
150 template_url_service_sub_
.reset();
154 void ProfileResetter::ResetDefaultSearchEngine() {
155 DCHECK(CalledOnValidThread());
156 DCHECK(template_url_service_
);
157 // If TemplateURLServiceFactory is ready we can clean it right now.
158 // Otherwise, load it and continue from ProfileResetter::Observe.
159 if (template_url_service_
->loaded()) {
160 PrefService
* prefs
= profile_
->GetPrefs();
162 TemplateURLPrepopulateData::ClearPrepopulatedEnginesInPrefs(
163 profile_
->GetPrefs());
164 scoped_ptr
<base::ListValue
> search_engines(
165 master_settings_
->GetSearchProviderOverrides());
166 if (search_engines
) {
167 // This Chrome distribution channel provides a custom search engine. We
169 ListPrefUpdate
update(prefs
, prefs::kSearchProviderOverrides
);
170 update
->Swap(search_engines
.get());
173 template_url_service_
->RepairPrepopulatedSearchEngines();
175 // Reset Google search URL.
176 const TemplateURL
* default_search_provider
=
177 template_url_service_
->GetDefaultSearchProvider();
178 if (default_search_provider
&&
179 default_search_provider
->HasGoogleBaseURLs(
180 template_url_service_
->search_terms_data())) {
181 GoogleURLTracker
* tracker
=
182 GoogleURLTrackerFactory::GetForProfile(profile_
);
184 tracker
->RequestServerCheck(true);
187 MarkAsDone(DEFAULT_SEARCH_ENGINE
);
189 template_url_service_sub_
=
190 template_url_service_
->RegisterOnLoadedCallback(
191 base::Bind(&ProfileResetter::OnTemplateURLServiceLoaded
,
192 weak_ptr_factory_
.GetWeakPtr()));
193 template_url_service_
->Load();
197 void ProfileResetter::ResetHomepage() {
198 DCHECK(CalledOnValidThread());
199 PrefService
* prefs
= profile_
->GetPrefs();
201 std::string homepage
;
202 bool homepage_is_ntp
, show_home_button
;
204 if (master_settings_
->GetHomepage(&homepage
))
205 prefs
->SetString(prefs::kHomePage
, homepage
);
207 if (master_settings_
->GetHomepageIsNewTab(&homepage_is_ntp
))
208 prefs
->SetBoolean(prefs::kHomePageIsNewTabPage
, homepage_is_ntp
);
210 prefs
->ClearPref(prefs::kHomePageIsNewTabPage
);
212 if (master_settings_
->GetShowHomeButton(&show_home_button
))
213 prefs
->SetBoolean(prefs::kShowHomeButton
, show_home_button
);
215 prefs
->ClearPref(prefs::kShowHomeButton
);
216 MarkAsDone(HOMEPAGE
);
219 void ProfileResetter::ResetContentSettings() {
220 DCHECK(CalledOnValidThread());
221 PrefService
* prefs
= profile_
->GetPrefs();
222 HostContentSettingsMap
* map
=
223 HostContentSettingsMapFactory::GetForProfile(profile_
);
225 content_settings::WebsiteSettingsRegistry
* registry
=
226 content_settings::WebsiteSettingsRegistry::GetInstance();
227 for (const content_settings::WebsiteSettingsInfo
* info
: *registry
) {
228 map
->ClearSettingsForOneType(info
->type());
229 if (HostContentSettingsMap::IsSettingAllowedForType(
230 prefs
, CONTENT_SETTING_DEFAULT
, info
->type())) {
231 // TODO(raymes): Why don't we just set this to
232 // info->intial_default_value().
233 map
->SetDefaultContentSetting(info
->type(), CONTENT_SETTING_DEFAULT
);
236 MarkAsDone(CONTENT_SETTINGS
);
239 void ProfileResetter::ResetCookiesAndSiteData() {
240 DCHECK(CalledOnValidThread());
241 DCHECK(!cookies_remover_
);
243 cookies_remover_
= BrowsingDataRemover::CreateForUnboundedRange(profile_
);
244 cookies_remover_
->AddObserver(this);
245 int remove_mask
= BrowsingDataRemover::REMOVE_SITE_DATA
|
246 BrowsingDataRemover::REMOVE_CACHE
;
247 PrefService
* prefs
= profile_
->GetPrefs();
249 // Don't try to clear LSO data if it's not supported.
250 if (!prefs
->GetBoolean(prefs::kClearPluginLSODataEnabled
))
251 remove_mask
&= ~BrowsingDataRemover::REMOVE_PLUGIN_DATA
;
252 cookies_remover_
->Remove(remove_mask
, BrowsingDataHelper::UNPROTECTED_WEB
);
255 void ProfileResetter::ResetExtensions() {
256 DCHECK(CalledOnValidThread());
258 std::vector
<std::string
> brandcode_extensions
;
259 master_settings_
->GetExtensions(&brandcode_extensions
);
261 ExtensionService
* extension_service
=
262 extensions::ExtensionSystem::Get(profile_
)->extension_service();
263 DCHECK(extension_service
);
264 extension_service
->DisableUserExtensions(brandcode_extensions
);
266 MarkAsDone(EXTENSIONS
);
269 void ProfileResetter::ResetStartupPages() {
270 DCHECK(CalledOnValidThread());
271 PrefService
* prefs
= profile_
->GetPrefs();
273 scoped_ptr
<base::ListValue
> url_list(
274 master_settings_
->GetUrlsToRestoreOnStartup());
276 ListPrefUpdate(prefs
, prefs::kURLsToRestoreOnStartup
)->Swap(url_list
.get());
278 int restore_on_startup
;
279 if (master_settings_
->GetRestoreOnStartup(&restore_on_startup
))
280 prefs
->SetInteger(prefs::kRestoreOnStartup
, restore_on_startup
);
282 prefs
->ClearPref(prefs::kRestoreOnStartup
);
284 prefs
->SetBoolean(prefs::kRestoreOnStartupMigrated
, true);
285 MarkAsDone(STARTUP_PAGES
);
288 void ProfileResetter::ResetPinnedTabs() {
289 // Unpin all the tabs.
290 for (chrome::BrowserIterator it
; !it
.done(); it
.Next()) {
291 if (it
->is_type_tabbed() && it
->profile() == profile_
) {
292 TabStripModel
* tab_model
= it
->tab_strip_model();
293 // Here we assume that indexof(any mini tab) < indexof(any normal tab).
294 // If we unpin the tab, it can be moved to the right. Thus traversing in
295 // reverse direction is correct.
296 for (int i
= tab_model
->count() - 1; i
>= 0; --i
) {
297 if (tab_model
->IsTabPinned(i
))
298 tab_model
->SetTabPinned(i
, false);
302 MarkAsDone(PINNED_TABS
);
305 void ProfileResetter::ResetShortcuts() {
307 content::BrowserThread::PostTaskAndReply(
308 content::BrowserThread::FILE,
310 base::Bind(&ResetShortcutsOnFileThread
),
311 base::Bind(&ProfileResetter::MarkAsDone
,
312 weak_ptr_factory_
.GetWeakPtr(),
315 MarkAsDone(SHORTCUTS
);
319 void ProfileResetter::OnTemplateURLServiceLoaded() {
320 // TemplateURLService has loaded. If we need to clean search engines, it's
322 DCHECK(CalledOnValidThread());
323 template_url_service_sub_
.reset();
324 if (pending_reset_flags_
& DEFAULT_SEARCH_ENGINE
)
325 ResetDefaultSearchEngine();
328 void ProfileResetter::OnBrowsingDataRemoverDone() {
329 cookies_remover_
= NULL
;
330 MarkAsDone(COOKIES_AND_SITE_DATA
);
333 std::vector
<ShortcutCommand
> GetChromeLaunchShortcuts(
334 const scoped_refptr
<SharedCancellationFlag
>& cancel
) {
335 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE);
337 // Get full path of chrome.
338 base::FilePath chrome_exe
;
339 if (!PathService::Get(base::FILE_EXE
, &chrome_exe
))
340 return std::vector
<ShortcutCommand
>();
341 BrowserDistribution
* dist
= BrowserDistribution::GetSpecificDistribution(
342 BrowserDistribution::CHROME_BROWSER
);
343 std::vector
<ShortcutCommand
> shortcuts
;
344 for (int location
= ShellUtil::SHORTCUT_LOCATION_FIRST
;
345 location
< ShellUtil::NUM_SHORTCUT_LOCATIONS
; ++location
) {
346 if (cancel
.get() && cancel
->data
.IsSet())
348 ShellUtil::ShortcutListMaybeRemoveUnknownArgs(
349 static_cast<ShellUtil::ShortcutLocation
>(location
),
351 ShellUtil::CURRENT_USER
,
359 return std::vector
<ShortcutCommand
>();