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

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

Issue 2918053003: Settings reset prompt: Fetch default settings only when needed. (Closed)
Patch Set: Addressed Chris's comment Created 3 years, 6 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_controller.h" 5 #include "chrome/browser/safe_browsing/settings_reset_prompt/settings_reset_prom pt_controller.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
11 #include "base/location.h" 11 #include "base/location.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/memory/ptr_util.h"
13 #include "base/metrics/histogram_macros.h" 14 #include "base/metrics/histogram_macros.h"
14 #include "base/metrics/user_metrics.h" 15 #include "base/metrics/user_metrics.h"
15 #include "base/metrics/user_metrics_action.h" 16 #include "base/metrics/user_metrics_action.h"
16 #include "base/time/time.h" 17 #include "base/time/time.h"
18 #include "chrome/browser/profile_resetter/brandcoded_default_settings.h"
19 #include "chrome/browser/profile_resetter/profile_resetter.h"
17 #include "chrome/browser/profiles/profile.h" 20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/safe_browsing/settings_reset_prompt/default_settings_fe tcher.h"
18 #include "chrome/browser/safe_browsing/settings_reset_prompt/settings_reset_prom pt_config.h" 22 #include "chrome/browser/safe_browsing/settings_reset_prompt/settings_reset_prom pt_config.h"
19 #include "chrome/browser/safe_browsing/settings_reset_prompt/settings_reset_prom pt_model.h" 23 #include "chrome/browser/safe_browsing/settings_reset_prompt/settings_reset_prom pt_model.h"
20 #include "chrome/browser/ui/browser.h" 24 #include "chrome/browser/ui/browser.h"
21 #include "chrome/browser/ui/browser_finder.h" 25 #include "chrome/browser/ui/browser_finder.h"
22 #include "chrome/browser/ui/browser_window.h" 26 #include "chrome/browser/ui/browser_window.h"
23 #include "chrome/grit/generated_resources.h" 27 #include "chrome/grit/generated_resources.h"
24 #include "components/url_formatter/elide_url.h" 28 #include "components/url_formatter/elide_url.h"
25 #include "content/public/browser/browser_thread.h" 29 #include "content/public/browser/browser_thread.h"
26 #include "ui/base/l10n/l10n_util.h" 30 #include "ui/base/l10n/l10n_util.h"
27 #include "url/gurl.h" 31 #include "url/gurl.h"
(...skipping 15 matching lines...) Expand all
43 bool ResetStartupUrlsEnabled(const SettingsResetPromptModel& model) { 47 bool ResetStartupUrlsEnabled(const SettingsResetPromptModel& model) {
44 return model.startup_urls_reset_state() == 48 return model.startup_urls_reset_state() ==
45 SettingsResetPromptModel::RESET_REQUIRED; 49 SettingsResetPromptModel::RESET_REQUIRED;
46 } 50 }
47 51
48 bool ResetHomepageEnabled(const SettingsResetPromptModel& model) { 52 bool ResetHomepageEnabled(const SettingsResetPromptModel& model) {
49 return model.homepage_reset_state() == 53 return model.homepage_reset_state() ==
50 SettingsResetPromptModel::RESET_REQUIRED; 54 SettingsResetPromptModel::RESET_REQUIRED;
51 } 55 }
52 56
53 // Function that is passed the newly created |SettingsResetPromptModel| object 57 void TryToShowSettingsResetPrompt(
54 // as a result of a call to |MaybeShowSettingsResetPrompt()| below. Will display 58 std::unique_ptr<SettingsResetPromptModel> model,
55 // the settings reset prompt if required by the model and there is at least one 59 std::unique_ptr<BrandcodedDefaultSettings> default_settings) {
56 // non-incognito browser available for the corresponding profile. 60 DCHECK(model);
57 void OnModelCreated(std::unique_ptr<SettingsResetPromptModel> model) { 61 DCHECK(default_settings);
58 if (!model)
59 return;
60
61 model->ReportUmaMetrics();
62
63 if (!model->ShouldPromptForReset())
64 return;
65
66 Profile* profile = model->profile();
67 62
68 // Ensure that there is at least one non-incognito browser open for the 63 // Ensure that there is at least one non-incognito browser open for the
69 // profile before attempting to show the dialog. 64 // profile before attempting to show the dialog.
70 Browser* browser = 65 Browser* browser = chrome::FindTabbedBrowser(
71 chrome::FindTabbedBrowser(profile, /*match_original_profiles=*/false); 66 model->profile(), /*match_original_profiles=*/false);
72 if (!browser) 67 if (!browser)
73 return; 68 return;
74 69
75 // First, show the browser window, and only then show the dialog so that the 70 // First, show the browser window, and only then show the dialog so that the
76 // dialog will have focus. 71 // dialog will have focus.
77 if (browser->window()->IsMinimized()) 72 if (browser->window()->IsMinimized())
78 browser->window()->Restore(); 73 browser->window()->Restore();
79 browser->window()->Show(); 74 browser->window()->Show();
80 75
81 // The |SettingsResetPromptController| object will delete itself after the 76 // The |SettingsResetPromptController| object will delete itself after the
82 // reset prompt dialog has been closed. 77 // reset prompt dialog has been closed.
83 SettingsResetPromptController::ShowSettingsResetPrompt( 78 SettingsResetPromptController::ShowSettingsResetPrompt(
84 browser, new SettingsResetPromptController(std::move(model))); 79 browser, new SettingsResetPromptController(std::move(model),
80 std::move(default_settings)));
85 } 81 }
86 82
83 // Will display the settings reset prompt if required and if there is at least
84 // one non-incognito browser available for the corresponding profile.
87 void MaybeShowSettingsResetPrompt( 85 void MaybeShowSettingsResetPrompt(
88 std::unique_ptr<SettingsResetPromptConfig> config) { 86 std::unique_ptr<SettingsResetPromptConfig> config) {
89 DCHECK(config); 87 DCHECK(config);
90 88
91 Browser* browser = chrome::FindLastActive(); 89 Browser* browser = chrome::FindLastActive();
92 if (!browser) 90 if (!browser)
93 return; 91 return;
94 92
95 // Get the original profile in case the last active browser was incognito. We 93 // Get the original profile in case the last active browser was incognito. We
96 // ensure that there is at least one non-incognito browser open before 94 // ensure that there is at least one non-incognito browser open before
97 // displaying the dialog. 95 // displaying the dialog.
98 Profile* profile = browser->profile()->GetOriginalProfile(); 96 Profile* profile = browser->profile()->GetOriginalProfile();
99 SettingsResetPromptModel::Create(profile, std::move(config), 97
100 base::Bind(OnModelCreated)); 98 auto model = base::MakeUnique<SettingsResetPromptModel>(
99 profile, std::move(config), base::MakeUnique<ProfileResetter>(profile));
100
101 model->ReportUmaMetrics();
102
103 if (!model->ShouldPromptForReset())
104 return;
105
106 DefaultSettingsFetcher::FetchDefaultSettings(
107 base::Bind(&TryToShowSettingsResetPrompt, base::Passed(&model)));
101 } 108 }
102 109
103 } // namespace. 110 } // namespace.
104 111
105 SettingsResetPromptController::SettingsResetPromptController( 112 SettingsResetPromptController::SettingsResetPromptController(
106 std::unique_ptr<SettingsResetPromptModel> model) 113 std::unique_ptr<SettingsResetPromptModel> model,
107 : model_(std::move(model)) { 114 std::unique_ptr<BrandcodedDefaultSettings> default_settings)
115 : model_(std::move(model)), default_settings_(std::move(default_settings)) {
108 DCHECK(model_); 116 DCHECK(model_);
109 DCHECK(model_->ShouldPromptForReset()); 117 DCHECK(model_->ShouldPromptForReset());
118 DCHECK(default_settings_);
119
110 // In the current implementation of the reset dialog, we ask users to reset 120 // In the current implementation of the reset dialog, we ask users to reset
111 // one settings type. If more than one setting requires reset, the model will 121 // one settings type. If more than one setting requires reset, the model will
112 // choose which setting we should prompt the user for. 122 // choose which setting we should prompt the user for.
113 DCHECK_EQ(1, int{ResetSearchEnabled(*model_)} + 123 DCHECK_EQ(1, int{ResetSearchEnabled(*model_)} +
114 int{ResetStartupUrlsEnabled(*model_)} + 124 int{ResetStartupUrlsEnabled(*model_)} +
115 int{ResetHomepageEnabled(*model_)}); 125 int{ResetHomepageEnabled(*model_)});
116 126
117 InitMainText(); 127 InitMainText();
118 } 128 }
119 129
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 161
152 void SettingsResetPromptController::DialogShown() { 162 void SettingsResetPromptController::DialogShown() {
153 model_->DialogShown(); 163 model_->DialogShown();
154 time_dialog_shown_ = base::Time::Now(); 164 time_dialog_shown_ = base::Time::Now();
155 base::RecordAction(base::UserMetricsAction("SettingsResetPrompt_Shown")); 165 base::RecordAction(base::UserMetricsAction("SettingsResetPrompt_Shown"));
156 UMA_HISTOGRAM_BOOLEAN("SettingsResetPrompt.DialogShown", true); 166 UMA_HISTOGRAM_BOOLEAN("SettingsResetPrompt.DialogShown", true);
157 } 167 }
158 168
159 void SettingsResetPromptController::Accept() { 169 void SettingsResetPromptController::Accept() {
160 DCHECK(!time_dialog_shown_.is_null()); 170 DCHECK(!time_dialog_shown_.is_null());
171 DCHECK(default_settings_);
172
161 base::RecordAction(base::UserMetricsAction("SettingsResetPrompt_Accepted")); 173 base::RecordAction(base::UserMetricsAction("SettingsResetPrompt_Accepted"));
162 UMA_HISTOGRAM_LONG_TIMES_100("SettingsResetPrompt.TimeUntilAccepted", 174 UMA_HISTOGRAM_LONG_TIMES_100("SettingsResetPrompt.TimeUntilAccepted",
163 base::Time::Now() - time_dialog_shown_); 175 base::Time::Now() - time_dialog_shown_);
164 UMA_HISTOGRAM_BOOLEAN("SettingsResetPrompt.PromptAccepted", true); 176 UMA_HISTOGRAM_BOOLEAN("SettingsResetPrompt.PromptAccepted", true);
165 model_->PerformReset( 177 model_->PerformReset(
178 std::move(default_settings_),
166 base::Bind(&SettingsResetPromptController::OnInteractionDone, 179 base::Bind(&SettingsResetPromptController::OnInteractionDone,
167 base::Unretained(this))); 180 base::Unretained(this)));
168 } 181 }
169 182
170 void SettingsResetPromptController::Cancel() { 183 void SettingsResetPromptController::Cancel() {
171 DCHECK(!time_dialog_shown_.is_null()); 184 DCHECK(!time_dialog_shown_.is_null());
172 base::RecordAction(base::UserMetricsAction("SettingsResetPrompt_Canceled")); 185 base::RecordAction(base::UserMetricsAction("SettingsResetPrompt_Canceled"));
173 UMA_HISTOGRAM_LONG_TIMES_100("SettingsResetPrompt.TimeUntilCanceled", 186 UMA_HISTOGRAM_LONG_TIMES_100("SettingsResetPrompt.TimeUntilCanceled",
174 base::Time::Now() - time_dialog_shown_); 187 base::Time::Now() - time_dialog_shown_);
175 UMA_HISTOGRAM_BOOLEAN("SettingsResetPrompt.PromptAccepted", false); 188 UMA_HISTOGRAM_BOOLEAN("SettingsResetPrompt.PromptAccepted", false);
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 return; 263 return;
251 264
252 base::TimeDelta delay = config->delay_before_prompt(); 265 base::TimeDelta delay = config->delay_before_prompt();
253 content::BrowserThread::PostDelayedTask( 266 content::BrowserThread::PostDelayedTask(
254 content::BrowserThread::UI, FROM_HERE, 267 content::BrowserThread::UI, FROM_HERE,
255 base::BindOnce(MaybeShowSettingsResetPrompt, base::Passed(&config)), 268 base::BindOnce(MaybeShowSettingsResetPrompt, base::Passed(&config)),
256 delay); 269 delay);
257 } 270 }
258 271
259 } // namespace safe_browsing 272 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698