Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(102)

Side by Side Diff: chrome/browser/safe_browsing/settings_reset_prompt/settings_reset_prompt_model.cc

Issue 2850103002: Settings reset prompt: Add a test for the default settings fetcher. (Closed)
Patch Set: Nits Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/safe_browsing/settings_reset_prompt/settings_reset_prom pt_model.h" 5 #include "chrome/browser/safe_browsing/settings_reset_prompt/settings_reset_prom pt_model.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind_helpers.h" 9 #include "base/bind_helpers.h"
10 #include "base/callback.h" 10 #include "base/callback.h"
11 #include "base/metrics/histogram_macros.h" 11 #include "base/metrics/histogram_macros.h"
12 #include "chrome/browser/extensions/extension_service.h" 12 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/google/google_brand.h"
14 #include "chrome/browser/prefs/session_startup_pref.h" 13 #include "chrome/browser/prefs/session_startup_pref.h"
15 #include "chrome/browser/profile_resetter/brandcode_config_fetcher.h"
16 #include "chrome/browser/profile_resetter/brandcoded_default_settings.h" 14 #include "chrome/browser/profile_resetter/brandcoded_default_settings.h"
17 #include "chrome/browser/profile_resetter/resettable_settings_snapshot.h" 15 #include "chrome/browser/profile_resetter/resettable_settings_snapshot.h"
18 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/safe_browsing/settings_reset_prompt/default_settings_fe tcher.h"
19 #include "chrome/browser/safe_browsing/settings_reset_prompt/settings_reset_prom pt_config.h" 18 #include "chrome/browser/safe_browsing/settings_reset_prompt/settings_reset_prom pt_config.h"
20 #include "chrome/browser/search_engines/template_url_service_factory.h" 19 #include "chrome/browser/search_engines/template_url_service_factory.h"
21 #include "chrome/common/extensions/manifest_handlers/settings_overrides_handler. h" 20 #include "chrome/common/extensions/manifest_handlers/settings_overrides_handler. h"
22 #include "chrome/common/pref_names.h" 21 #include "chrome/common/pref_names.h"
23 #include "components/prefs/pref_service.h" 22 #include "components/prefs/pref_service.h"
24 #include "components/search_engines/template_url_service.h" 23 #include "components/search_engines/template_url_service.h"
25 #include "components/url_formatter/url_fixer.h" 24 #include "components/url_formatter/url_fixer.h"
26 #include "content/public/browser/browser_thread.h" 25 #include "content/public/browser/browser_thread.h"
27 #include "extensions/browser/extension_registry.h" 26 #include "extensions/browser/extension_registry.h"
28 #include "extensions/browser/extension_system.h" 27 #include "extensions/browser/extension_system.h"
29 #include "extensions/common/extension.h" 28 #include "extensions/common/extension.h"
30 #include "extensions/common/extension_id.h" 29 #include "extensions/common/extension_id.h"
31 #include "extensions/common/extension_set.h" 30 #include "extensions/common/extension_set.h"
32 31
33 namespace safe_browsing { 32 namespace safe_browsing {
34 33
35 namespace { 34 namespace {
36 35
37 #if defined(GOOGLE_CHROME_BUILD)
38 constexpr char kOmahaUrl[] = "https://tools.google.com/service/update2";
39 #endif // defined(GOOGLE_CHROME_BUILD)
40
41 // These values are used for UMA metrics reporting. New enum values can be 36 // These values are used for UMA metrics reporting. New enum values can be
42 // added, but existing enums must never be renumbered or deleted and reused. 37 // added, but existing enums must never be renumbered or deleted and reused.
43 enum SettingsReset { 38 enum SettingsReset {
44 SETTINGS_RESET_HOMEPAGE = 1, 39 SETTINGS_RESET_HOMEPAGE = 1,
45 SETTINGS_RESET_DEFAULT_SEARCH = 2, 40 SETTINGS_RESET_DEFAULT_SEARCH = 2,
46 SETTINGS_RESET_STARTUP_URLS = 3, 41 SETTINGS_RESET_STARTUP_URLS = 3,
47 SETTINGS_RESET_MAX, 42 SETTINGS_RESET_MAX,
48 }; 43 };
49 44
50 // Used to keep track of which settings types have been initialized in 45 // Used to keep track of which settings types have been initialized in
51 // |SettingsResetPromptModel|. 46 // |SettingsResetPromptModel|.
52 enum SettingsType : uint32_t { 47 enum SettingsType : uint32_t {
53 SETTINGS_TYPE_HOMEPAGE = 1 << 0, 48 SETTINGS_TYPE_HOMEPAGE = 1 << 0,
54 SETTINGS_TYPE_DEFAULT_SEARCH = 1 << 1, 49 SETTINGS_TYPE_DEFAULT_SEARCH = 1 << 1,
55 SETTINGS_TYPE_STARTUP_URLS = 1 << 2, 50 SETTINGS_TYPE_STARTUP_URLS = 1 << 2,
56 SETTINGS_TYPE_ALL = SETTINGS_TYPE_HOMEPAGE | SETTINGS_TYPE_DEFAULT_SEARCH | 51 SETTINGS_TYPE_ALL = SETTINGS_TYPE_HOMEPAGE | SETTINGS_TYPE_DEFAULT_SEARCH |
57 SETTINGS_TYPE_STARTUP_URLS, 52 SETTINGS_TYPE_STARTUP_URLS,
58 }; 53 };
59 54
60 // A helper class that fetches default settings to be used for the settings
61 // reset prompt. The static |FetchDefaultSettings()| function will create and
62 // manage the lifetime of |DefaultSettingsFetcher| instances.
63 class DefaultSettingsFetcher {
64 public:
65 using SettingsCallback =
66 base::Callback<void(std::unique_ptr<BrandcodedDefaultSettings>)>;
67
68 // Fetches default settings and passes the corresponding
69 // |BrandcodedDefaultSettings| object to |callback| on the UI thread. The
70 // function should be called on the UI thread as well.
71 static void FetchDefaultSettings(SettingsCallback callback);
72
73 private:
74 // Instances of |DefaultSettingsFetcher| own themselves and will delete
75 // themselves once default settings have been fetched and |callback| has been
76 // posted on the UI thread.
77 //
78 // The main reason for this design is that |BrandcodeConfigFetcher| takes a
79 // callback and initiates the fetching process in its constructor, and we need
80 // to hold on to the instance of the fetcher until settings have been
81 // fetched. This design saves us from having to explicitly manage global
82 // |BrandcodeConfigFetcher| instances.
83 explicit DefaultSettingsFetcher(SettingsCallback callback);
84 ~DefaultSettingsFetcher();
85
86 // Starts the process of fetching default settings and will ensure that
87 // |PostCallbackAndDeleteSelf| is called once settings have been fetched.
88 void Start();
89 void OnSettingsFetched();
90 // Posts a call to |callback_| on the UI thread, passing to it
91 // |default_settings|, and deletes |this|.
92 void PostCallbackAndDeleteSelf(
93 std::unique_ptr<BrandcodedDefaultSettings> default_settings);
94
95 std::unique_ptr<BrandcodeConfigFetcher> config_fetcher_;
96 SettingsCallback callback_;
97 };
98
99 // static
100 void DefaultSettingsFetcher::FetchDefaultSettings(SettingsCallback callback) {
101 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
102
103 // |settings_fetcher| will delete itself when default settings have been
104 // fetched after the call to |Start()|.
105 DefaultSettingsFetcher* settings_fetcher =
106 new DefaultSettingsFetcher(std::move(callback));
107 settings_fetcher->Start();
108 }
109
110 DefaultSettingsFetcher::DefaultSettingsFetcher(SettingsCallback callback)
111 : callback_(std::move(callback)) {}
112
113 DefaultSettingsFetcher::~DefaultSettingsFetcher() {}
114
115 void DefaultSettingsFetcher::Start() {
116 DCHECK(!config_fetcher_);
117
118 #if defined(GOOGLE_CHROME_BUILD)
119 std::string brandcode;
120 if (google_brand::GetBrand(&brandcode) && !brandcode.empty()) {
121 config_fetcher_.reset(new BrandcodeConfigFetcher(
122 base::Bind(&DefaultSettingsFetcher::OnSettingsFetched,
123 base::Unretained(this)),
124 GURL(kOmahaUrl), brandcode));
125 return;
126 }
127 #endif // defined(GOOGLE_CHROME_BUILD)
128
129 // For non Google Chrome builds and cases with an empty |brandcode|, we create
130 // a default-constructed |BrandcodedDefaultSettings| object and post the
131 // callback immediately.
132 PostCallbackAndDeleteSelf(base::MakeUnique<BrandcodedDefaultSettings>());
133 }
134
135 void DefaultSettingsFetcher::OnSettingsFetched() {
136 DCHECK(config_fetcher_);
137 DCHECK(!config_fetcher_->IsActive());
138
139 std::unique_ptr<BrandcodedDefaultSettings> settings(
140 config_fetcher_->GetSettings());
141 // Use default settings if fetching of BrandcodedDefaultSettings fails.
142 if (!settings)
143 settings.reset(new BrandcodedDefaultSettings());
144
145 PostCallbackAndDeleteSelf(std::move(settings));
146 }
147
148 void DefaultSettingsFetcher::PostCallbackAndDeleteSelf(
149 std::unique_ptr<BrandcodedDefaultSettings> default_settings) {
150 DCHECK(default_settings);
151 content::BrowserThread::PostTask(
152 content::BrowserThread::UI, FROM_HERE,
153 base::BindOnce(std::move(callback_), base::Passed(&default_settings)));
154 delete this;
155 }
156
157 const extensions::Extension* GetExtension( 55 const extensions::Extension* GetExtension(
158 Profile* profile, 56 Profile* profile,
159 const extensions::ExtensionId& extension_id) { 57 const extensions::ExtensionId& extension_id) {
160 return extensions::ExtensionRegistry::Get(profile)->GetInstalledExtension( 58 return extensions::ExtensionRegistry::Get(profile)->GetInstalledExtension(
161 extension_id); 59 extension_id);
162 } 60 }
163 61
164 GURL FixupUrl(const std::string& url_text) { 62 GURL FixupUrl(const std::string& url_text) {
165 return url_formatter::FixupURL(url_text, /*desired_tld=*/std::string()); 63 return url_formatter::FixupURL(url_text, /*desired_tld=*/std::string());
166 } 64 }
(...skipping 392 matching lines...) Expand 10 before | Expand all | Expand 10 after
559 GetExtension(profile_, extension_id); 457 GetExtension(profile_, extension_id);
560 if (extension && management_policy->MustRemainEnabled(extension, nullptr)) 458 if (extension && management_policy->MustRemainEnabled(extension, nullptr))
561 return true; 459 return true;
562 } 460 }
563 } 461 }
564 462
565 return false; 463 return false;
566 } 464 }
567 465
568 } // namespace safe_browsing. 466 } // namespace safe_browsing.
OLDNEW
« no previous file with comments | « chrome/browser/safe_browsing/settings_reset_prompt/default_settings_fetcher_browsertest.cc ('k') | chrome/test/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698