feat(performspellcheck): queue spell check request asynchronously (#12112)
* feat(spellcheckrequest): implement spellcheckrequest * feat(performspellcheck): queue spell check request asynchronously
This commit is contained in:
parent
dabd61bf80
commit
4c51c03779
2 changed files with 55 additions and 5 deletions
|
@ -9,6 +9,7 @@
|
|||
|
||||
#include "atom/common/native_mate_converters/string16_converter.h"
|
||||
#include "base/logging.h"
|
||||
#include "base/threading/thread_task_runner_handle.h"
|
||||
#include "chrome/renderer/spellchecker/spellcheck_worditerator.h"
|
||||
#include "native_mate/converter.h"
|
||||
#include "native_mate/dictionary.h"
|
||||
|
@ -37,6 +38,27 @@ bool HasWordCharacters(const base::string16& text, int index) {
|
|||
|
||||
} // namespace
|
||||
|
||||
class SpellCheckClient::SpellcheckRequest {
|
||||
public:
|
||||
SpellcheckRequest(const base::string16& text,
|
||||
blink::WebTextCheckingCompletion* completion)
|
||||
: text_(text), completion_(completion) {
|
||||
DCHECK(completion);
|
||||
}
|
||||
~SpellcheckRequest() {}
|
||||
|
||||
base::string16 text() { return text_; }
|
||||
blink::WebTextCheckingCompletion* completion() { return completion_; }
|
||||
|
||||
private:
|
||||
base::string16 text_; // Text to be checked in this task.
|
||||
|
||||
// The interface to send the misspelled ranges to WebKit.
|
||||
blink::WebTextCheckingCompletion* completion_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(SpellcheckRequest);
|
||||
};
|
||||
|
||||
SpellCheckClient::SpellCheckClient(const std::string& language,
|
||||
bool auto_spell_correct_turned_on,
|
||||
v8::Isolate* isolate,
|
||||
|
@ -74,14 +96,23 @@ void SpellCheckClient::RequestCheckingOfText(
|
|||
const blink::WebString& textToCheck,
|
||||
blink::WebTextCheckingCompletion* completionCallback) {
|
||||
base::string16 text(textToCheck.Utf16());
|
||||
// Ignore invalid requests.
|
||||
if (text.empty() || !HasWordCharacters(text, 0)) {
|
||||
completionCallback->DidCancelCheckingText();
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<blink::WebTextCheckingResult> results;
|
||||
SpellCheckText(text, false, &results);
|
||||
completionCallback->DidFinishCheckingText(results);
|
||||
// Clean up the previous request before starting a new request.
|
||||
if (pending_request_param_.get()) {
|
||||
pending_request_param_->completion()->DidCancelCheckingText();
|
||||
}
|
||||
|
||||
pending_request_param_.reset(new SpellcheckRequest(text, completionCallback));
|
||||
|
||||
base::ThreadTaskRunnerHandle::Get()->PostTask(
|
||||
FROM_HERE,
|
||||
base::BindOnce(&SpellCheckClient::PerformSpellCheck, AsWeakPtr(),
|
||||
base::Owned(pending_request_param_.release())));
|
||||
}
|
||||
|
||||
void SpellCheckClient::ShowSpellingUI(bool show) {}
|
||||
|
@ -190,6 +221,14 @@ bool SpellCheckClient::IsValidContraction(const SpellCheckScope& scope,
|
|||
return true;
|
||||
}
|
||||
|
||||
void SpellCheckClient::PerformSpellCheck(SpellcheckRequest* param) {
|
||||
DCHECK(param);
|
||||
|
||||
std::vector<blink::WebTextCheckingResult> results;
|
||||
SpellCheckText(param->text(), false, &results);
|
||||
param->completion()->DidFinishCheckingText(results);
|
||||
}
|
||||
|
||||
SpellCheckClient::SpellCheckScope::SpellCheckScope(
|
||||
const SpellCheckClient& client)
|
||||
: handle_scope_(client.isolate_),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue