Skip to content

Commit

Permalink
Refactor SpellCheckHost::ToggleSpellCheck
Browse files Browse the repository at this point in the history
The mojo API is a no-op other than on Android with |enabled| being
false. This patch refactors it and renames it into
|DisconnectSessionBridge| to make the behavior more explicit.

Bug: 814225
Change-Id: I786411375edd594806fb2f7bcdc7b884be4352fe
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1500016
Commit-Queue: Xiaocheng Hu <xiaochengh@chromium.org>
Reviewed-by: Ken Buchanan <kenrb@chromium.org>
Reviewed-by: Rouslan Solomakhin <rouslan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#637847}
  • Loading branch information
xiaochengh authored and Commit Bot committed Mar 5, 2019
1 parent 22ea659 commit 551a8c1
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 25 deletions.
7 changes: 6 additions & 1 deletion chrome/browser/chrome_site_per_process_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/post_task.h"
#include "build/build_config.h"
#include "chrome/browser/chrome_content_browser_client.h"
#include "chrome/browser/external_protocol/external_protocol_handler.h"
#include "chrome/browser/profiles/profile.h"
Expand Down Expand Up @@ -947,14 +948,18 @@ class MockSpellCheckHost : spellcheck::mojom::SpellCheckHost {
TextReceived(text);
}

void ToggleSpellCheck(bool, bool) override {}
void CheckSpelling(const base::string16& word,
int,
CheckSpellingCallback) override {}
void FillSuggestionList(const base::string16& word,
FillSuggestionListCallback) override {}
#endif

#if defined(OS_ANDROID)
// spellcheck::mojom::SpellCheckHost:
void DisconnectSessionBridge() override {}
#endif

content::RenderProcessHost* process_host_;
bool text_received_ = false;
base::string16 text_;
Expand Down
15 changes: 7 additions & 8 deletions components/spellcheck/browser/spell_check_host_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,6 @@ void SpellCheckHostImpl::RequestTextCheck(const base::string16& text,
#endif
}

void SpellCheckHostImpl::ToggleSpellCheck(bool enabled, bool checked) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
#if defined(OS_ANDROID)
if (!enabled)
session_bridge_.DisconnectSession();
#endif
}

void SpellCheckHostImpl::CheckSpelling(const base::string16& word,
int route_id,
CheckSpellingCallback callback) {
Expand All @@ -89,3 +81,10 @@ void SpellCheckHostImpl::FillSuggestionList(
std::move(callback).Run(std::vector<base::string16>());
}
#endif // BUILDFLAG(USE_BROWSER_SPELLCHECKER)

#if defined(OS_ANDROID)
void SpellCheckHostImpl::DisconnectSessionBridge() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
session_bridge_.DisconnectSession();
}
#endif
6 changes: 5 additions & 1 deletion components/spellcheck/browser/spell_check_host_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,18 @@ class SpellCheckHostImpl : public spellcheck::mojom::SpellCheckHost {
void RequestTextCheck(const base::string16& text,
int route_id,
RequestTextCheckCallback callback) override;
void ToggleSpellCheck(bool enabled, bool checked) override;
void CheckSpelling(const base::string16& word,
int route_id,
CheckSpellingCallback callback) override;
void FillSuggestionList(const base::string16& word,
FillSuggestionListCallback callback) override;
#endif // BUILDFLAG(USE_BROWSER_SPELLCHECKER)

#if defined(OS_ANDROID)
// spellcheck::mojom::SpellCheckHost:
void DisconnectSessionBridge() override;
#endif

private:
#if defined(OS_ANDROID)
// Android-specific object used to query the Android spellchecker.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ void SpellCheckerSessionBridge::RequestTextCheck(
}

// RequestTextCheck API call arrives at the SpellCheckHost before
// ToggleSpellCheck when the user focuses an input field that already
// DisconnectSessionBridge when the user focuses an input field that already
// contains completed text. We need to initialize the spellchecker here
// rather than in response to ToggleSpellCheck so that the existing text
// will be spellchecked immediately.
// rather than in response to DisconnectSessionBridge so that the existing
// text will be spellchecked immediately.
if (java_object_.is_null()) {
java_object_.Reset(Java_SpellCheckerSessionBridge_create(
base::android::AttachCurrentThread(),
Expand Down
6 changes: 3 additions & 3 deletions components/spellcheck/common/spellcheck.mojom
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ interface SpellCheckHost {
RequestTextCheck(mojo_base.mojom.String16 text, int32 route_id) =>
(array<SpellCheckResult> results);

// Toggles the enabled state of the platform-specific spell checker.
[EnableIf=USE_BROWSER_SPELLCHECKER]
ToggleSpellCheck(bool enabled, bool checked);
// Disconnects the Android spell checker session bridge.
[EnableIf=is_android]
DisconnectSessionBridge();

// Checks the correctness of a word with a platform-specific spell checker.
// TODO(groby): This needs to originate from SpellcheckProvider.
Expand Down
12 changes: 8 additions & 4 deletions components/spellcheck/renderer/spellcheck_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "base/bind.h"
#include "base/metrics/histogram_macros.h"
#include "build/build_config.h"
#include "components/spellcheck/common/spellcheck.mojom.h"
#include "components/spellcheck/common/spellcheck_result.h"
#include "components/spellcheck/renderer/spellcheck.h"
Expand Down Expand Up @@ -144,15 +145,18 @@ void SpellCheckProvider::RequestTextChecking(
}

void SpellCheckProvider::FocusedNodeChanged(const blink::WebNode& unused) {
#if BUILDFLAG(USE_BROWSER_SPELLCHECKER)
#if defined(OS_ANDROID)
WebLocalFrame* frame = render_frame()->GetWebFrame();
WebElement element = frame->GetDocument().IsNull()
? WebElement()
: frame->GetDocument().FocusedElement();
bool enabled = !element.IsNull() && element.IsEditable();
bool checked = enabled && IsSpellCheckingEnabled();
GetSpellCheckHost().ToggleSpellCheck(enabled, checked);
#endif // USE_BROWSER_SPELLCHECKER

// TODO(xiaochengh): Don't call GetSpellCheckHost() if SpellCheckHost is not
// bound yet. crbug.com/814225
if (!enabled)
GetSpellCheckHost().DisconnectSessionBridge();
#endif // defined(OS_ANDROID)
}

bool SpellCheckProvider::IsSpellCheckingEnabled() const {
Expand Down
10 changes: 6 additions & 4 deletions components/spellcheck/renderer/spellcheck_provider_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,6 @@ void TestingSpellCheckProvider::RequestTextCheck(
text_check_requests_.push_back(std::make_pair(text, std::move(callback)));
}

void TestingSpellCheckProvider::ToggleSpellCheck(bool, bool) {
NOTREACHED();
}

void TestingSpellCheckProvider::CheckSpelling(const base::string16&,
int,
CheckSpellingCallback) {
Expand All @@ -125,6 +121,12 @@ void TestingSpellCheckProvider::FillSuggestionList(const base::string16&,
}
#endif // BUILDFLAG(USE_BROWSER_SPELLCHECKER)

#if defined(OS_ANDROID)
void TestingSpellCheckProvider::DisconnectSessionBridge() {
NOTREACHED();
}
#endif

void TestingSpellCheckProvider::SetLastResults(
const base::string16 last_request,
blink::WebVector<blink::WebTextCheckingResult>& last_results) {
Expand Down
6 changes: 5 additions & 1 deletion components/spellcheck/renderer/spellcheck_provider_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <vector>

#include "base/strings/string16.h"
#include "build/build_config.h"
#include "components/spellcheck/renderer/empty_local_interface_provider.h"
#include "components/spellcheck/renderer/spellcheck_provider.h"
#include "mojo/public/cpp/bindings/binding.h"
Expand Down Expand Up @@ -88,7 +89,6 @@ class TestingSpellCheckProvider : public SpellCheckProvider,
void RequestTextCheck(const base::string16&,
int,
RequestTextCheckCallback) override;
void ToggleSpellCheck(bool, bool) override;
using SpellCheckProvider::CheckSpelling;
void CheckSpelling(const base::string16&,
int,
Expand All @@ -97,6 +97,10 @@ class TestingSpellCheckProvider : public SpellCheckProvider,
FillSuggestionListCallback) override;
#endif

#if defined(OS_ANDROID)
void DisconnectSessionBridge() override;
#endif

// Message loop (if needed) to deliver the SpellCheckHost request flow.
std::unique_ptr<base::MessageLoop> loop_;

Expand Down

0 comments on commit 551a8c1

Please sign in to comment.