Commit 940b87bb authored by Etienne Pierre-doray's avatar Etienne Pierre-doray Committed by Commit Bot

[TaskScheduler]: Use ScopedBlockingCall to mark blocking tasks.

This CL uses ScopedBlockingCall to mark blocking calls in /chrome/browser/spellchecker.

This CL was created by replacing calls to AssertBlockingAllowed()
with instantiations of ScopedBlockingCall(MAY_BLOCK).
I kindly ask the reviewer to make sure of the following:
  - ScopedBlockingCall is instantiated in a scope with minimal CPU usage.
    If this is not the case, ScopedBlockingCall should be instantiated
    closer to the blocking call. See scoped_blocking_call.h for more
    info. Please let me know when/where the blocking call happens if this needs
    to be changed.
  - Parameter |blocking_type| matches expectation (MAY_BLOCK/WILL_BLOCK). See
    BlockingType for more info. While I assumed MAY_BLOCK by default, that might
    not be the best fit if we know that this callsite is guaranteed to block.
  - The ScopedBlockingCall's scope covers the entirety of the blocking operation
    previously asserted against by the AssertBlockingAllowed().

This CL was uploaded by git cl split.

R=rouslan@chromium.org

Bug: 874080
Change-Id: I82ae2e1ea937cce2e4bc815ac66f236081b82960
Reviewed-on: https://chromium-review.googlesource.com/1191531Reviewed-by: default avatarRouslan Solomakhin <rouslan@chromium.org>
Commit-Queue: Etienne Pierre-Doray <etiennep@chromium.org>
Cr-Commit-Position: refs/heads/master@{#587290}
parent f09fef99
......@@ -18,7 +18,7 @@
#include "base/strings/string_util.h"
#include "base/task/post_task.h"
#include "base/task_runner_util.h"
#include "base/threading/thread_restrictions.h"
#include "base/threading/scoped_blocking_call.h"
#include "chrome/common/chrome_constants.h"
#include "components/spellcheck/browser/spellcheck_host_metrics.h"
#include "components/spellcheck/common/spellcheck_common.h"
......@@ -63,11 +63,14 @@ enum ChangeSanitationResult {
// invalid checksum, then returns ChecksumStatus::INVALID and clears |words|.
ChecksumStatus LoadFile(const base::FilePath& file_path,
std::set<std::string>* words) {
base::AssertBlockingAllowed();
DCHECK(words);
words->clear();
std::string contents;
base::ReadFileToString(file_path, &contents);
{
base::ScopedBlockingCall scoped_blocking_call(
base::BlockingType::MAY_BLOCK);
base::ReadFileToString(file_path, &contents);
}
size_t pos = contents.rfind(CHECKSUM_PREFIX);
if (pos != std::string::npos) {
std::string checksum = contents.substr(pos + strlen(CHECKSUM_PREFIX));
......@@ -122,7 +125,6 @@ int SanitizeWordsToAdd(const std::set<std::string>& existing,
// called on the file thread.
std::unique_ptr<SpellcheckCustomDictionary::LoadFileResult>
LoadDictionaryFileReliably(const base::FilePath& path) {
base::AssertBlockingAllowed();
// Load the contents and verify the checksum.
std::unique_ptr<SpellcheckCustomDictionary::LoadFileResult> result(
new SpellcheckCustomDictionary::LoadFileResult);
......@@ -144,22 +146,24 @@ LoadDictionaryFileReliably(const base::FilePath& path) {
// the custom spellcheck dictionary at |path|.
void SaveDictionaryFileReliably(const base::FilePath& path,
const std::set<std::string>& custom_words) {
base::AssertBlockingAllowed();
std::stringstream content;
for (const std::string& word : custom_words)
content << word << '\n';
std::string checksum = base::MD5String(content.str());
content << CHECKSUM_PREFIX << checksum;
base::CopyFile(path, path.AddExtension(BACKUP_EXTENSION));
base::ImportantFileWriter::WriteFileAtomically(path, content.str());
{
base::ScopedBlockingCall scoped_blocking_call(
base::BlockingType::MAY_BLOCK);
base::CopyFile(path, path.AddExtension(BACKUP_EXTENSION));
base::ImportantFileWriter::WriteFileAtomically(path, content.str());
}
}
void SavePassedWordsToDictionaryFileReliably(
const base::FilePath& path,
std::unique_ptr<SpellcheckCustomDictionary::LoadFileResult>
load_file_result) {
base::AssertBlockingAllowed();
DCHECK(load_file_result);
SaveDictionaryFileReliably(path, load_file_result->words);
}
......@@ -391,7 +395,6 @@ SpellcheckCustomDictionary::LoadFileResult::~LoadFileResult() {}
// static
std::unique_ptr<SpellcheckCustomDictionary::LoadFileResult>
SpellcheckCustomDictionary::LoadDictionaryFile(const base::FilePath& path) {
base::AssertBlockingAllowed();
std::unique_ptr<LoadFileResult> result = LoadDictionaryFileReliably(path);
SpellCheckHostMetrics::RecordCustomWordCountStats(result->words.size());
return result;
......@@ -401,7 +404,6 @@ SpellcheckCustomDictionary::LoadDictionaryFile(const base::FilePath& path) {
void SpellcheckCustomDictionary::UpdateDictionaryFile(
std::unique_ptr<Change> dictionary_change,
const base::FilePath& path) {
base::AssertBlockingAllowed();
DCHECK(dictionary_change);
if (dictionary_change->empty())
......
......@@ -16,7 +16,7 @@
#include "base/single_thread_task_runner.h"
#include "base/task/post_task.h"
#include "base/task_runner_util.h"
#include "base/threading/thread_restrictions.h"
#include "base/threading/scoped_blocking_call.h"
#include "base/threading/thread_task_runner_handle.h"
#include "build/build_config.h"
#include "chrome/browser/spellchecker/spellcheck_service.h"
......@@ -49,7 +49,7 @@ base::LazyInstance<GURL>::Leaky g_download_url_for_testing =
// Close the file.
void CloseDictionary(base::File file) {
base::AssertBlockingAllowed();
base::ScopedBlockingCall scoped_blocking_call(base::BlockingType::MAY_BLOCK);
file.Close();
}
......@@ -57,7 +57,7 @@ void CloseDictionary(base::File file) {
// returns false.
bool SaveDictionaryData(std::unique_ptr<std::string> data,
const base::FilePath& path) {
base::AssertBlockingAllowed();
base::ScopedBlockingCall scoped_blocking_call(base::BlockingType::MAY_BLOCK);
size_t bytes_written =
base::WriteFile(path, data->data(), data->length());
......@@ -318,7 +318,7 @@ void SpellcheckHunspellDictionary::DownloadDictionary(GURL url) {
// static
SpellcheckHunspellDictionary::DictionaryFile
SpellcheckHunspellDictionary::OpenDictionaryFile(const base::FilePath& path) {
base::AssertBlockingAllowed();
base::ScopedBlockingCall scoped_blocking_call(base::BlockingType::MAY_BLOCK);
// The default_dictionary_file can either come from the standard list of
// hunspell dictionaries (determined in InitializeDictionaryLocation), or it
......@@ -370,7 +370,7 @@ SpellcheckHunspellDictionary::OpenDictionaryFile(const base::FilePath& path) {
SpellcheckHunspellDictionary::DictionaryFile
SpellcheckHunspellDictionary::InitializeDictionaryLocation(
const std::string& language) {
base::AssertBlockingAllowed();
base::ScopedBlockingCall scoped_blocking_call(base::BlockingType::MAY_BLOCK);
// The default place where the spellcheck dictionary resides is
// chrome::DIR_APP_DICTIONARIES.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment