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

Unified Diff: chrome/browser/safe_browsing/settings_reset_prompt/default_settings_fetcher.cc

Issue 2850103002: Settings reset prompt: Add a test for the default settings fetcher. (Closed)
Patch Set: Nits Created 3 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/safe_browsing/settings_reset_prompt/default_settings_fetcher.cc
diff --git a/chrome/browser/safe_browsing/settings_reset_prompt/default_settings_fetcher.cc b/chrome/browser/safe_browsing/settings_reset_prompt/default_settings_fetcher.cc
new file mode 100644
index 0000000000000000000000000000000000000000..ea8490e14f52eaf5a6778e6356aa9b63c0d3447c
--- /dev/null
+++ b/chrome/browser/safe_browsing/settings_reset_prompt/default_settings_fetcher.cc
@@ -0,0 +1,95 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/safe_browsing/settings_reset_prompt/default_settings_fetcher.h"
+
+#include <string>
+#include <utility>
+
+#include "base/bind_helpers.h"
+#include "base/logging.h"
+#include "base/memory/ptr_util.h"
+#include "chrome/browser/google/google_brand.h"
+#include "chrome/browser/profile_resetter/brandcode_config_fetcher.h"
+#include "chrome/browser/profile_resetter/brandcoded_default_settings.h"
+#include "content/public/browser/browser_thread.h"
+#include "url/gurl.h"
+
+namespace safe_browsing {
+
+namespace {
+
+#if defined(GOOGLE_CHROME_BUILD)
+constexpr char kOmahaUrl[] = "https://tools.google.com/service/update2";
+#endif // defined(GOOGLE_CHROME_BUILD)
+
+} // namespace
+
+// static
+void DefaultSettingsFetcher::FetchDefaultSettings(SettingsCallback callback) {
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
+
+ // |settings_fetcher| will delete itself when default settings have been
+ // fetched after the call to |Start()|.
+ DefaultSettingsFetcher* settings_fetcher =
+ new DefaultSettingsFetcher(std::move(callback));
+ settings_fetcher->Start();
+}
+
+// static
+void DefaultSettingsFetcher::FetchDefaultSettingsForTesting(
+ SettingsCallback callback,
+ std::unique_ptr<BrandcodedDefaultSettings> fetched_settings) {
+ DefaultSettingsFetcher* settings_fetcher =
+ new DefaultSettingsFetcher(std::move(callback));
+ // Inject the given settings directly by passing them to
+ // |PostCallbackAndDeleteSelf()|.
+ settings_fetcher->PostCallbackAndDeleteSelf(std::move(fetched_settings));
+}
+
+DefaultSettingsFetcher::DefaultSettingsFetcher(SettingsCallback callback)
+ : callback_(std::move(callback)) {}
+
+DefaultSettingsFetcher::~DefaultSettingsFetcher() {}
+
+void DefaultSettingsFetcher::Start() {
+ DCHECK(!config_fetcher_);
+
+#if defined(GOOGLE_CHROME_BUILD)
+ std::string brandcode;
+ if (google_brand::GetBrand(&brandcode) && !brandcode.empty()) {
+ config_fetcher_.reset(new BrandcodeConfigFetcher(
+ base::Bind(&DefaultSettingsFetcher::OnSettingsFetched,
+ base::Unretained(this)),
+ GURL(kOmahaUrl), brandcode));
+ return;
+ }
+#endif // defined(GOOGLE_CHROME_BUILD)
+
+ // For non Google Chrome builds and cases with an empty |brandcode|, we create
+ // a default-constructed |BrandcodedDefaultSettings| object and post the
+ // callback immediately.
+ PostCallbackAndDeleteSelf(base::MakeUnique<BrandcodedDefaultSettings>());
+}
+
+void DefaultSettingsFetcher::OnSettingsFetched() {
+ DCHECK(config_fetcher_);
+ DCHECK(!config_fetcher_->IsActive());
+
+ PostCallbackAndDeleteSelf(config_fetcher_->GetSettings());
+}
+
+void DefaultSettingsFetcher::PostCallbackAndDeleteSelf(
+ std::unique_ptr<BrandcodedDefaultSettings> default_settings) {
+ // Use default settings if fetching of BrandcodedDefaultSettings failed.
+ if (!default_settings)
+ default_settings.reset(new BrandcodedDefaultSettings());
+
+ content::BrowserThread::PostTask(
+ content::BrowserThread::UI, FROM_HERE,
+ base::BindOnce(std::move(callback_), base::Passed(&default_settings)));
+ delete this;
+}
+
+} // namespace safe_browsing

Powered by Google App Engine
This is Rietveld 408576698