Commit 99296108 authored by Gayane Petrosyan's avatar Gayane Petrosyan Committed by Commit Bot

Reland: Reset NTP customizations when all settings are reset.

This relands crrev.com/c/1472450 which was reverted because of a crash
crbug.com/936830. The cause of the crash was  fixed separately at
crrev.com/c/1501493.

Bug: 931806
Change-Id: Id659440b7798fe6f0a3c233c3b29a584565cdba3

TBR=ramyan@chromium.org, engedy@chromium.org

Change-Id: Id659440b7798fe6f0a3c233c3b29a584565cdba3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1506107Reviewed-by: default avatarGayane Petrosyan <gayane@chromium.org>
Commit-Queue: Gayane Petrosyan <gayane@chromium.org>
Cr-Commit-Position: refs/heads/master@{#638221}
parent d52a9885
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include "chrome/browser/extensions/extension_service.h" #include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/profile_resetter/brandcoded_default_settings.h" #include "chrome/browser/profile_resetter/brandcoded_default_settings.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search/instant_service_factory.h"
#include "chrome/browser/search_engines/template_url_service_factory.h" #include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h" #include "chrome/browser/ui/browser_list.h"
...@@ -81,6 +82,7 @@ ProfileResetter::ProfileResetter(Profile* profile) ...@@ -81,6 +82,7 @@ ProfileResetter::ProfileResetter(Profile* profile)
template_url_service_(TemplateURLServiceFactory::GetForProfile(profile_)), template_url_service_(TemplateURLServiceFactory::GetForProfile(profile_)),
pending_reset_flags_(0), pending_reset_flags_(0),
cookies_remover_(nullptr), cookies_remover_(nullptr),
ntp_service_(InstantServiceFactory::GetForProfile(profile)),
weak_ptr_factory_(this) { weak_ptr_factory_(this) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(profile_); DCHECK(profile_);
...@@ -128,6 +130,7 @@ void ProfileResetter::Reset( ...@@ -128,6 +130,7 @@ void ProfileResetter::Reset(
{STARTUP_PAGES, &ProfileResetter::ResetStartupPages}, {STARTUP_PAGES, &ProfileResetter::ResetStartupPages},
{PINNED_TABS, &ProfileResetter::ResetPinnedTabs}, {PINNED_TABS, &ProfileResetter::ResetPinnedTabs},
{SHORTCUTS, &ProfileResetter::ResetShortcuts}, {SHORTCUTS, &ProfileResetter::ResetShortcuts},
{NTP_CUSTOMIZATIONS, &ProfileResetter::ResetNtpCustomizations},
}; };
ResettableFlags reset_triggered_for_flags = 0; ResettableFlags reset_triggered_for_flags = 0;
...@@ -334,6 +337,11 @@ void ProfileResetter::ResetShortcuts() { ...@@ -334,6 +337,11 @@ void ProfileResetter::ResetShortcuts() {
#endif #endif
} }
void ProfileResetter::ResetNtpCustomizations() {
ntp_service_->ResetToDefault();
MarkAsDone(NTP_CUSTOMIZATIONS);
}
void ProfileResetter::OnTemplateURLServiceLoaded() { void ProfileResetter::OnTemplateURLServiceLoaded() {
// TemplateURLService has loaded. If we need to clean search engines, it's // TemplateURLService has loaded. If we need to clean search engines, it's
// time to go on. // time to go on.
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "base/sequence_checker.h" #include "base/sequence_checker.h"
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "chrome/browser/profile_resetter/brandcoded_default_settings.h" #include "chrome/browser/profile_resetter/brandcoded_default_settings.h"
#include "chrome/browser/search/instant_service.h"
#include "components/search_engines/template_url_service.h" #include "components/search_engines/template_url_service.h"
#include "content/public/browser/browsing_data_remover.h" #include "content/public/browser/browsing_data_remover.h"
...@@ -28,6 +29,10 @@ namespace base { ...@@ -28,6 +29,10 @@ namespace base {
class CancellationFlag; class CancellationFlag;
} }
namespace {
FORWARD_DECLARE_TEST(ProfileResetterTest, ResetNTPCustomizationsTest);
}
// This class allows resetting certain aspects of a profile to default values. // This class allows resetting certain aspects of a profile to default values.
// It is used in case the profile has been damaged due to malware or bad user // It is used in case the profile has been damaged due to malware or bad user
// settings. // settings.
...@@ -43,11 +48,12 @@ class ProfileResetter : public content::BrowsingDataRemover::Observer { ...@@ -43,11 +48,12 @@ class ProfileResetter : public content::BrowsingDataRemover::Observer {
STARTUP_PAGES = 1 << 5, STARTUP_PAGES = 1 << 5,
PINNED_TABS = 1 << 6, PINNED_TABS = 1 << 6,
SHORTCUTS = 1 << 7, SHORTCUTS = 1 << 7,
NTP_CUSTOMIZATIONS = 1 << 8,
// Update ALL if you add new values and check whether the type of // Update ALL if you add new values and check whether the type of
// ResettableFlags needs to be enlarged. // ResettableFlags needs to be enlarged.
ALL = DEFAULT_SEARCH_ENGINE | HOMEPAGE | CONTENT_SETTINGS | ALL = DEFAULT_SEARCH_ENGINE | HOMEPAGE | CONTENT_SETTINGS |
COOKIES_AND_SITE_DATA | EXTENSIONS | STARTUP_PAGES | PINNED_TABS | COOKIES_AND_SITE_DATA | EXTENSIONS | STARTUP_PAGES | PINNED_TABS |
SHORTCUTS SHORTCUTS | NTP_CUSTOMIZATIONS
}; };
// Bit vector for Resettable enum. // Bit vector for Resettable enum.
...@@ -69,6 +75,8 @@ class ProfileResetter : public content::BrowsingDataRemover::Observer { ...@@ -69,6 +75,8 @@ class ProfileResetter : public content::BrowsingDataRemover::Observer {
virtual bool IsActive() const; virtual bool IsActive() const;
private: private:
FRIEND_TEST_ALL_PREFIXES(::ProfileResetterTest, ResetNTPCustomizationsTest);
// Marks |resettable| as done and triggers |callback_| if all pending jobs // Marks |resettable| as done and triggers |callback_| if all pending jobs
// have completed. // have completed.
void MarkAsDone(Resettable resettable); void MarkAsDone(Resettable resettable);
...@@ -81,6 +89,7 @@ class ProfileResetter : public content::BrowsingDataRemover::Observer { ...@@ -81,6 +89,7 @@ class ProfileResetter : public content::BrowsingDataRemover::Observer {
void ResetStartupPages(); void ResetStartupPages();
void ResetPinnedTabs(); void ResetPinnedTabs();
void ResetShortcuts(); void ResetShortcuts();
void ResetNtpCustomizations();
// BrowsingDataRemover::Observer: // BrowsingDataRemover::Observer:
void OnBrowsingDataRemoverDone() override; void OnBrowsingDataRemoverDone() override;
...@@ -107,6 +116,9 @@ class ProfileResetter : public content::BrowsingDataRemover::Observer { ...@@ -107,6 +116,9 @@ class ProfileResetter : public content::BrowsingDataRemover::Observer {
SEQUENCE_CHECKER(sequence_checker_); SEQUENCE_CHECKER(sequence_checker_);
// Used for resetting NTP customizations.
InstantService* ntp_service_;
base::WeakPtrFactory<ProfileResetter> weak_ptr_factory_; base::WeakPtrFactory<ProfileResetter> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(ProfileResetter); DISALLOW_COPY_AND_ASSIGN(ProfileResetter);
......
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include "chrome/browser/profile_resetter/profile_reset_report.pb.h" #include "chrome/browser/profile_resetter/profile_reset_report.pb.h"
#include "chrome/browser/profile_resetter/profile_resetter_test_base.h" #include "chrome/browser/profile_resetter/profile_resetter_test_base.h"
#include "chrome/browser/profile_resetter/resettable_settings_snapshot.h" #include "chrome/browser/profile_resetter/resettable_settings_snapshot.h"
#include "chrome/browser/search/instant_service.h"
#include "chrome/browser/search_engines/template_url_service_factory.h" #include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/themes/theme_service.h" #include "chrome/browser/themes/theme_service.h"
#include "chrome/browser/themes/theme_service_factory.h" #include "chrome/browser/themes/theme_service_factory.h"
...@@ -320,6 +321,14 @@ void ShortcutHandler::Delete() { ...@@ -320,6 +321,14 @@ void ShortcutHandler::Delete() {
} }
#endif // defined(OS_WIN) #endif // defined(OS_WIN)
// MockInstantService
class MockInstantService : public InstantService {
public:
explicit MockInstantService(Profile* profile) : InstantService(profile) {}
~MockInstantService() override = default;
MOCK_METHOD0(ResetToDefault, void());
};
// helper functions ----------------------------------------------------------- // helper functions -----------------------------------------------------------
...@@ -1018,4 +1027,12 @@ TEST_F(ProfileResetterTest, DestroySnapshotFast) { ...@@ -1018,4 +1027,12 @@ TEST_F(ProfileResetterTest, DestroySnapshotFast) {
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
} }
TEST_F(ProfileResetterTest, ResetNTPCustomizationsTest) {
MockInstantService mock_ntp_service(profile());
resetter_->ntp_service_ = &mock_ntp_service;
EXPECT_CALL(mock_ntp_service, ResetToDefault());
ResetAndWait(ProfileResetter::NTP_CUSTOMIZATIONS);
}
} // namespace } // namespace
...@@ -714,6 +714,11 @@ bool InstantService::IsCustomBackgroundSet() { ...@@ -714,6 +714,11 @@ bool InstantService::IsCustomBackgroundSet() {
return true; return true;
} }
void InstantService::ResetToDefault() {
ResetCustomLinks();
ResetCustomBackgroundThemeInfo();
}
bool InstantService::IsCustomBackgroundPrefValid(GURL& custom_background_url) { bool InstantService::IsCustomBackgroundPrefValid(GURL& custom_background_url) {
const base::DictionaryValue* background_info = const base::DictionaryValue* background_info =
profile_->GetPrefs()->GetDictionary(prefs::kNtpCustomBackgroundDict); profile_->GetPrefs()->GetDictionary(prefs::kNtpCustomBackgroundDict);
......
...@@ -101,8 +101,8 @@ class InstantService : public KeyedService, ...@@ -101,8 +101,8 @@ class InstantService : public KeyedService,
bool UndoCustomLinkAction(); bool UndoCustomLinkAction();
// Invoked when the Instant page wants to delete all custom links and use Most // Invoked when the Instant page wants to delete all custom links and use Most
// Visited sites instead. Returns false and does nothing if the profile is // Visited sites instead. Returns false and does nothing if the profile is
// using a non-Google search provider. // using a non-Google search provider. Marked virtual for mocking in tests.
bool ResetCustomLinks(); virtual bool ResetCustomLinks();
// Invoked by the InstantController to update theme information for NTP. // Invoked by the InstantController to update theme information for NTP.
// //
...@@ -146,6 +146,10 @@ class InstantService : public KeyedService, ...@@ -146,6 +146,10 @@ class InstantService : public KeyedService,
// Check if a custom background has been set by the user. // Check if a custom background has been set by the user.
bool IsCustomBackgroundSet(); bool IsCustomBackgroundSet();
// Reset all NTP customizations to default. Marked virtual for mocking in
// tests.
virtual void ResetToDefault();
private: private:
class SearchProviderObserver; class SearchProviderObserver;
...@@ -191,7 +195,8 @@ class InstantService : public KeyedService, ...@@ -191,7 +195,8 @@ class InstantService : public KeyedService,
void ApplyCustomBackgroundThemeInfo(); void ApplyCustomBackgroundThemeInfo();
void ResetCustomBackgroundThemeInfo(); // Marked virtual for mocking in tests.
virtual void ResetCustomBackgroundThemeInfo();
void FallbackToDefaultThemeInfo(); void FallbackToDefaultThemeInfo();
......
...@@ -46,6 +46,15 @@ base::DictionaryValue GetBackgroundInfoAsDict(const GURL& background_url) { ...@@ -46,6 +46,15 @@ base::DictionaryValue GetBackgroundInfoAsDict(const GURL& background_url) {
return background_info; return background_info;
} }
class MockInstantService : public InstantService {
public:
explicit MockInstantService(Profile* profile) : InstantService(profile) {}
~MockInstantService() override = default;
MOCK_METHOD0(ResetCustomLinks, bool());
MOCK_METHOD0(ResetCustomBackgroundThemeInfo, void());
};
} // namespace } // namespace
using InstantServiceTest = InstantUnitTestBase; using InstantServiceTest = InstantUnitTestBase;
...@@ -440,6 +449,13 @@ TEST_F(InstantServiceTest, TestNoThemeInfo) { ...@@ -440,6 +449,13 @@ TEST_F(InstantServiceTest, TestNoThemeInfo) {
EXPECT_NE(nullptr, instant_service_->theme_info_); EXPECT_NE(nullptr, instant_service_->theme_info_);
} }
TEST_F(InstantServiceTest, TestResetToDefault) {
MockInstantService mock_instant_service_(profile());
EXPECT_CALL(mock_instant_service_, ResetCustomLinks());
EXPECT_CALL(mock_instant_service_, ResetCustomBackgroundThemeInfo());
mock_instant_service_.ResetToDefault();
}
class InstantServiceThemeTest : public InstantServiceTest { class InstantServiceThemeTest : public InstantServiceTest {
public: public:
InstantServiceThemeTest() {} InstantServiceThemeTest() {}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment