feat: Expose renderer spellcheck API (#25060)

This commit is contained in:
Lishid 2020-10-19 07:48:16 -04:00 committed by GitHub
parent 321395d96e
commit 05b5c197ae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 88 additions and 2 deletions

View file

@ -10,6 +10,8 @@
#include "base/command_line.h"
#include "base/memory/memory_pressure_listener.h"
#include "base/strings/utf_string_conversions.h"
#include "components/spellcheck/renderer/spellcheck.h"
#include "content/public/renderer/render_frame.h"
#include "content/public/renderer/render_frame_observer.h"
#include "content/public/renderer/render_frame_visitor.h"
@ -24,6 +26,7 @@
#include "shell/common/node_includes.h"
#include "shell/common/options_switches.h"
#include "shell/renderer/api/electron_api_spell_check_client.h"
#include "shell/renderer/electron_renderer_client.h"
#include "third_party/blink/public/common/page/page_zoom.h"
#include "third_party/blink/public/common/web_cache/web_cache_resource_type_stats.h"
#include "third_party/blink/public/platform/web_cache.h"
@ -101,6 +104,24 @@ content::RenderFrame* GetRenderFrame(v8::Local<v8::Value> value) {
return content::RenderFrame::FromWebFrame(frame);
}
bool SpellCheckWord(v8::Isolate* isolate,
v8::Local<v8::Value> window,
const std::string& word,
std::vector<base::string16>* optional_suggestions) {
size_t start;
size_t length;
ElectronRendererClient* client = ElectronRendererClient::Get();
auto* render_frame = GetRenderFrame(window);
if (!render_frame)
return true;
base::string16 w = base::UTF8ToUTF16(word);
int id = render_frame->GetRoutingID();
return client->GetSpellCheck()->SpellCheckWord(
w.c_str(), 0, word.size(), id, &start, &length, optional_suggestions);
}
class RenderFrameStatus final : public content::RenderFrameObserver {
public:
explicit RenderFrameStatus(content::RenderFrame* render_frame)
@ -671,6 +692,20 @@ blink::WebCacheResourceTypeStats GetResourceUsage(v8::Isolate* isolate) {
return stats;
}
bool IsWordMisspelled(v8::Isolate* isolate,
v8::Local<v8::Value> window,
const std::string& word) {
return !SpellCheckWord(isolate, window, word, nullptr);
}
std::vector<base::string16> GetWordSuggestions(v8::Isolate* isolate,
v8::Local<v8::Value> window,
const std::string& word) {
std::vector<base::string16> suggestions;
SpellCheckWord(isolate, window, word, &suggestions);
return suggestions;
}
void ClearCache(v8::Isolate* isolate) {
isolate->IdleNotificationDeadline(0.5);
blink::WebCache::Clear();
@ -826,6 +861,10 @@ void Initialize(v8::Local<v8::Object> exports,
&ExecuteJavaScriptInIsolatedWorld);
dict.SetMethod("setIsolatedWorldInfo", &SetIsolatedWorldInfo);
dict.SetMethod("getResourceUsage", &GetResourceUsage);
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
dict.SetMethod("isWordMisspelled", &IsWordMisspelled);
dict.SetMethod("getWordSuggestions", &GetWordSuggestions);
#endif
dict.SetMethod("clearCache", &ClearCache);
dict.SetMethod("_findFrameByRoutingId", &FindFrameByRoutingId);
dict.SetMethod("_getFrameForSelector", &GetFrameForSelector);

View file

@ -34,15 +34,27 @@ bool IsDevToolsExtension(content::RenderFrame* render_frame) {
} // namespace
// static
ElectronRendererClient* ElectronRendererClient::self_ = nullptr;
ElectronRendererClient::ElectronRendererClient()
: node_bindings_(
NodeBindings::Create(NodeBindings::BrowserEnvironment::RENDERER)),
electron_bindings_(new ElectronBindings(node_bindings_->uv_loop())) {}
electron_bindings_(new ElectronBindings(node_bindings_->uv_loop())) {
DCHECK(!self_) << "Cannot have two ElectronRendererClient";
self_ = this;
}
ElectronRendererClient::~ElectronRendererClient() {
asar::ClearArchives();
}
// static
ElectronRendererClient* ElectronRendererClient::Get() {
DCHECK(self_);
return self_;
}
void ElectronRendererClient::RenderFrameCreated(
content::RenderFrame* render_frame) {
new ElectronRenderFrameObserver(render_frame, this);

View file

@ -26,6 +26,8 @@ class ElectronRendererClient : public RendererClientBase {
ElectronRendererClient();
~ElectronRendererClient() override;
static ElectronRendererClient* Get();
// electron::RendererClientBase:
void DidCreateScriptContext(v8::Handle<v8::Context> context,
content::RenderFrame* render_frame) override;
@ -70,6 +72,8 @@ class ElectronRendererClient : public RendererClientBase {
// assertion, so we have to keep a book of injected web frames.
std::set<content::RenderFrame*> injected_frames_;
static ElectronRendererClient* self_;
DISALLOW_COPY_AND_ASSIGN(ElectronRendererClient);
};