diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2018-05-15 10:20:33 +0200 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2018-05-15 10:28:57 +0000 |
commit | d17ea114e5ef69ad5d5d7413280a13e6428098aa (patch) | |
tree | 2c01a75df69f30d27b1432467cfe7c1467a498da /chromium/chrome/browser/spellchecker/spellcheck_hunspell_dictionary.cc | |
parent | 8c5c43c7b138c9b4b0bf56d946e61d3bbc111bec (diff) | |
download | qtwebengine-chromium-d17ea114e5ef69ad5d5d7413280a13e6428098aa.tar.gz |
BASELINE: Update Chromium to 67.0.3396.47
Change-Id: Idcb1341782e417561a2473eeecc82642dafda5b7
Reviewed-by: Michal Klocek <michal.klocek@qt.io>
Diffstat (limited to 'chromium/chrome/browser/spellchecker/spellcheck_hunspell_dictionary.cc')
-rw-r--r-- | chromium/chrome/browser/spellchecker/spellcheck_hunspell_dictionary.cc | 86 |
1 files changed, 55 insertions, 31 deletions
diff --git a/chromium/chrome/browser/spellchecker/spellcheck_hunspell_dictionary.cc b/chromium/chrome/browser/spellchecker/spellcheck_hunspell_dictionary.cc index cca9f376dfc..aeb5838a2a9 100644 --- a/chromium/chrome/browser/spellchecker/spellcheck_hunspell_dictionary.cc +++ b/chromium/chrome/browser/spellchecker/spellcheck_hunspell_dictionary.cc @@ -9,6 +9,7 @@ #include <utility> #include "base/files/file_util.h" +#include "base/lazy_instance.h" #include "base/location.h" #include "base/logging.h" #include "base/path_service.h" @@ -23,13 +24,15 @@ #include "components/data_use_measurement/core/data_use_user_data.h" #include "components/spellcheck/browser/spellcheck_platform.h" #include "components/spellcheck/common/spellcheck_common.h" -#include "components/spellcheck/spellcheck_build_features.h" +#include "components/spellcheck/spellcheck_buildflags.h" +#include "content/public/browser/browser_context.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/render_process_host.h" +#include "content/public/browser/storage_partition.h" #include "net/base/load_flags.h" #include "net/traffic_annotation/network_traffic_annotation.h" -#include "net/url_request/url_fetcher.h" -#include "net/url_request/url_request_context_getter.h" +#include "services/network/public/cpp/resource_request.h" +#include "services/network/public/cpp/simple_url_loader.h" #include "url/gurl.h" #if !defined(OS_ANDROID) @@ -41,6 +44,9 @@ using content::BrowserThread; namespace { +base::LazyInstance<GURL>::Leaky g_download_url_for_testing = + LAZY_INSTANCE_INITIALIZER; + // Close the file. void CloseDictionary(base::File file) { base::AssertBlockingAllowed(); @@ -99,13 +105,13 @@ SpellcheckHunspellDictionary::DictionaryFile::operator=( SpellcheckHunspellDictionary::SpellcheckHunspellDictionary( const std::string& language, - net::URLRequestContextGetter* request_context_getter, + content::BrowserContext* browser_context, SpellcheckService* spellcheck_service) : task_runner_( base::CreateSequencedTaskRunnerWithTraits({base::MayBlock()})), language_(language), use_browser_spellchecker_(false), - request_context_getter_(request_context_getter), + browser_context_(browser_context), #if !defined(OS_ANDROID) spellcheck_service_(spellcheck_service), #endif @@ -151,13 +157,13 @@ void SpellcheckHunspellDictionary::Load() { } void SpellcheckHunspellDictionary::RetryDownloadDictionary( - net::URLRequestContextGetter* request_context_getter) { + content::BrowserContext* browser_context) { DCHECK_CURRENTLY_ON(BrowserThread::UI); if (dictionary_file_.file.IsValid()) { NOTREACHED(); return; } - request_context_getter_ = request_context_getter; + browser_context_ = browser_context; DownloadDictionary(GetDictionaryURL()); } @@ -195,23 +201,27 @@ bool SpellcheckHunspellDictionary::IsDownloadFailure() { return download_status_ == DOWNLOAD_FAILED; } -void SpellcheckHunspellDictionary::OnURLFetchComplete( - const net::URLFetcher* source) { - DCHECK(source); +void SpellcheckHunspellDictionary::OnSimpleLoaderComplete( + std::unique_ptr<std::string> data) { DCHECK_CURRENTLY_ON(BrowserThread::UI); - std::unique_ptr<net::URLFetcher> fetcher_destructor(fetcher_.release()); - if ((source->GetResponseCode() / 100) != 2) { + bool is_success = simple_loader_->NetError() == net::OK; + int response_code = -1; + if (simple_loader_->ResponseInfo() && simple_loader_->ResponseInfo()->headers) + response_code = simple_loader_->ResponseInfo()->headers->response_code(); + + if (!is_success || ((response_code / 100) != 2)) { // Initialize will not try to download the file a second time. InformListenersOfDownloadFailure(); return; } + // We don't need the loader anymore. + simple_loader_.reset(); + // Basic sanity check on the dictionary. There's a small chance of 200 status // code for a body that represents some form of failure. - std::unique_ptr<std::string> data(new std::string); - source->GetResponseAsString(data.get()); - if (data->size() < 4 || data->compare(0, 4, "BDic") != 0) { + if (!data || data->size() < 4 || data->compare(0, 4, "BDic") != 0) { InformListenersOfDownloadFailure(); return; } @@ -236,20 +246,27 @@ void SpellcheckHunspellDictionary::OnURLFetchComplete( weak_ptr_factory_.GetWeakPtr())); } +void SpellcheckHunspellDictionary::SetDownloadURLForTesting(const GURL url) { + g_download_url_for_testing.Get() = url; +} + GURL SpellcheckHunspellDictionary::GetDictionaryURL() { - static const char kDownloadServerUrl[] = - "https://redirector.gvt1.com/edgedl/chrome/dict/"; - std::string bdict_file = dictionary_file_.path.BaseName().MaybeAsASCII(); + if (g_download_url_for_testing.Get() != GURL()) + return g_download_url_for_testing.Get(); + std::string bdict_file = dictionary_file_.path.BaseName().MaybeAsASCII(); DCHECK(!bdict_file.empty()); + static const char kDownloadServerUrl[] = + "https://redirector.gvt1.com/edgedl/chrome/dict/"; + return GURL(std::string(kDownloadServerUrl) + base::ToLowerASCII(bdict_file)); } void SpellcheckHunspellDictionary::DownloadDictionary(GURL url) { DCHECK_CURRENTLY_ON(BrowserThread::UI); - DCHECK(request_context_getter_); + DCHECK(browser_context_); download_status_ = DOWNLOAD_IN_PROGRESS; for (Observer& observer : observers_) @@ -278,16 +295,23 @@ void SpellcheckHunspellDictionary::DownloadDictionary(GURL url) { "Not implemented, considered not useful." })"); - fetcher_ = net::URLFetcher::Create(url, net::URLFetcher::GET, this, - traffic_annotation); - data_use_measurement::DataUseUserData::AttachToFetcher( - fetcher_.get(), data_use_measurement::DataUseUserData::SPELL_CHECKER); - fetcher_->SetRequestContext(request_context_getter_); - fetcher_->SetLoadFlags( - net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES); - fetcher_->Start(); + auto resource_request = std::make_unique<network::ResourceRequest>(); + resource_request->url = url; + resource_request->load_flags = + net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES; + simple_loader_ = network::SimpleURLLoader::Create(std::move(resource_request), + traffic_annotation); + network::mojom::URLLoaderFactory* loader_factory = + content::BrowserContext::GetDefaultStoragePartition(browser_context_) + ->GetURLLoaderFactoryForBrowserProcess() + .get(); + simple_loader_->DownloadToStringOfUnboundedSizeUntilCrashAndDie( + loader_factory, + base::BindOnce(&SpellcheckHunspellDictionary::OnSimpleLoaderComplete, + base::Unretained(this))); + // Attempt downloading the dictionary only once. - request_context_getter_ = NULL; + browser_context_ = NULL; } #if !defined(OS_ANDROID) @@ -373,11 +397,11 @@ void SpellcheckHunspellDictionary::InitializeDictionaryLocationComplete( // TODO(rouslan): Remove this test-only case. if (spellcheck_service_->SignalStatusEvent( SpellcheckService::BDICT_CORRUPTED)) { - request_context_getter_ = NULL; + browser_context_ = NULL; } - if (request_context_getter_) { - // Download from the UI thread to check that |request_context_getter_| is + if (browser_context_) { + // Download from the UI thread to check that |browser_context_| is // still valid. DownloadDictionary(GetDictionaryURL()); return; |