OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/safe_browsing/settings_reset_prompt/default_settings_fe
tcher.h" |
| 6 |
| 7 #include <string> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/bind_helpers.h" |
| 11 #include "base/logging.h" |
| 12 #include "base/memory/ptr_util.h" |
| 13 #include "chrome/browser/google/google_brand.h" |
| 14 #include "chrome/browser/profile_resetter/brandcode_config_fetcher.h" |
| 15 #include "chrome/browser/profile_resetter/brandcoded_default_settings.h" |
| 16 #include "content/public/browser/browser_thread.h" |
| 17 #include "url/gurl.h" |
| 18 |
| 19 namespace safe_browsing { |
| 20 |
| 21 namespace { |
| 22 |
| 23 #if defined(GOOGLE_CHROME_BUILD) |
| 24 constexpr char kOmahaUrl[] = "https://tools.google.com/service/update2"; |
| 25 #endif // defined(GOOGLE_CHROME_BUILD) |
| 26 |
| 27 } // namespace |
| 28 |
| 29 // static |
| 30 void DefaultSettingsFetcher::FetchDefaultSettings(SettingsCallback callback) { |
| 31 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 32 |
| 33 // |settings_fetcher| will delete itself when default settings have been |
| 34 // fetched after the call to |Start()|. |
| 35 DefaultSettingsFetcher* settings_fetcher = |
| 36 new DefaultSettingsFetcher(std::move(callback)); |
| 37 settings_fetcher->Start(); |
| 38 } |
| 39 |
| 40 // static |
| 41 void DefaultSettingsFetcher::FetchDefaultSettingsForTesting( |
| 42 SettingsCallback callback, |
| 43 std::unique_ptr<BrandcodedDefaultSettings> fetched_settings) { |
| 44 DefaultSettingsFetcher* settings_fetcher = |
| 45 new DefaultSettingsFetcher(std::move(callback)); |
| 46 // Inject the given settings directly by passing them to |
| 47 // |PostCallbackAndDeleteSelf()|. |
| 48 settings_fetcher->PostCallbackAndDeleteSelf(std::move(fetched_settings)); |
| 49 } |
| 50 |
| 51 DefaultSettingsFetcher::DefaultSettingsFetcher(SettingsCallback callback) |
| 52 : callback_(std::move(callback)) {} |
| 53 |
| 54 DefaultSettingsFetcher::~DefaultSettingsFetcher() {} |
| 55 |
| 56 void DefaultSettingsFetcher::Start() { |
| 57 DCHECK(!config_fetcher_); |
| 58 |
| 59 #if defined(GOOGLE_CHROME_BUILD) |
| 60 std::string brandcode; |
| 61 if (google_brand::GetBrand(&brandcode) && !brandcode.empty()) { |
| 62 config_fetcher_.reset(new BrandcodeConfigFetcher( |
| 63 base::Bind(&DefaultSettingsFetcher::OnSettingsFetched, |
| 64 base::Unretained(this)), |
| 65 GURL(kOmahaUrl), brandcode)); |
| 66 return; |
| 67 } |
| 68 #endif // defined(GOOGLE_CHROME_BUILD) |
| 69 |
| 70 // For non Google Chrome builds and cases with an empty |brandcode|, we create |
| 71 // a default-constructed |BrandcodedDefaultSettings| object and post the |
| 72 // callback immediately. |
| 73 PostCallbackAndDeleteSelf(base::MakeUnique<BrandcodedDefaultSettings>()); |
| 74 } |
| 75 |
| 76 void DefaultSettingsFetcher::OnSettingsFetched() { |
| 77 DCHECK(config_fetcher_); |
| 78 DCHECK(!config_fetcher_->IsActive()); |
| 79 |
| 80 PostCallbackAndDeleteSelf(config_fetcher_->GetSettings()); |
| 81 } |
| 82 |
| 83 void DefaultSettingsFetcher::PostCallbackAndDeleteSelf( |
| 84 std::unique_ptr<BrandcodedDefaultSettings> default_settings) { |
| 85 // Use default settings if fetching of BrandcodedDefaultSettings failed. |
| 86 if (!default_settings) |
| 87 default_settings.reset(new BrandcodedDefaultSettings()); |
| 88 |
| 89 content::BrowserThread::PostTask( |
| 90 content::BrowserThread::UI, FROM_HERE, |
| 91 base::BindOnce(std::move(callback_), base::Passed(&default_settings))); |
| 92 delete this; |
| 93 } |
| 94 |
| 95 } // namespace safe_browsing |
OLD | NEW |