Skip to content
This repository has been archived by the owner on Jan 4, 2019. It is now read-only.

Commit

Permalink
Merge pull request #244 from brave/spellchecker
Browse files Browse the repository at this point in the history
Deprecate electron spellchecker by chromium's
  • Loading branch information
bridiver committed Jul 18, 2017
1 parent 75044a0 commit 393e94a
Show file tree
Hide file tree
Showing 21 changed files with 275 additions and 375 deletions.
2 changes: 0 additions & 2 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,6 @@ source_set("renderer") {
]

sources = [
"atom/renderer/api/atom_api_spell_check_client.cc",
"atom/renderer/api/atom_api_spell_check_client.h",
"atom/renderer/content_settings_manager.cc",
"atom/renderer/content_settings_manager.h",
"brave/renderer/brave_content_renderer_client.cc",
Expand Down
2 changes: 2 additions & 0 deletions atom/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ source_set("browser") {
"api/atom_api_screen.h",
"api/atom_api_session.cc",
"api/atom_api_session.h",
"api/atom_api_spellchecker.cc",
"api/atom_api_spellchecker.h",
"api/atom_api_system_preferences.cc",
"api/atom_api_system_preferences.h",
"api/atom_api_tray.cc",
Expand Down
12 changes: 11 additions & 1 deletion atom/browser/api/atom_api_session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "atom/browser/api/atom_api_cookies.h"
#include "atom/browser/api/atom_api_download_item.h"
#include "atom/browser/api/atom_api_protocol.h"
#include "atom/browser/api/atom_api_spellchecker.h"
#include "atom/browser/api/atom_api_user_prefs.h"
#include "atom/browser/api/atom_api_web_request.h"
#include "atom/browser/atom_browser_context.h"
Expand Down Expand Up @@ -578,6 +579,14 @@ v8::Local<v8::Value> Session::Autofill(v8::Isolate* isolate) {
return v8::Local<v8::Value>::New(isolate, autofill_);
}

v8::Local<v8::Value> Session::SpellChecker(v8::Isolate* isolate) {
if (spell_checker_.IsEmpty()) {
auto handle = atom::api::SpellChecker::Create(isolate, profile_);
spell_checker_.Reset(isolate, handle.ToV8());
}
return v8::Local<v8::Value>::New(isolate, spell_checker_);
}

v8::Local<v8::Value> Session::Extensions(v8::Isolate* isolate) {
#if BUILDFLAG(ENABLE_EXTENSIONS)
if (extensions_.IsEmpty()) {
Expand Down Expand Up @@ -664,7 +673,8 @@ void Session::BuildPrototype(v8::Isolate* isolate,
.SetProperty("protocol", &Session::Protocol)
.SetProperty("webRequest", &Session::WebRequest)
.SetProperty("extensions", &Session::Extensions)
.SetProperty("autofill", &Session::Autofill);
.SetProperty("autofill", &Session::Autofill)
.SetProperty("spellChecker", &Session::SpellChecker);
}

} // namespace api
Expand Down
2 changes: 2 additions & 0 deletions atom/browser/api/atom_api_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class Session: public mate::TrackableObject<Session>,
v8::Local<v8::Value> WebRequest(v8::Isolate* isolate);
v8::Local<v8::Value> UserPrefs(v8::Isolate* isolate);
v8::Local<v8::Value> Autofill(v8::Isolate* isolate);
v8::Local<v8::Value> SpellChecker(v8::Isolate* isolate);
v8::Local<v8::Value> Extensions(v8::Isolate* isolate);
bool Equal(Session* session) const;

Expand All @@ -108,6 +109,7 @@ class Session: public mate::TrackableObject<Session>,
v8::Global<v8::Value> user_prefs_;
v8::Global<v8::Value> content_settings_;
v8::Global<v8::Value> autofill_;
v8::Global<v8::Value> spell_checker_;
v8::Global<v8::Value> extensions_;

// The X-DevTools-Emulate-Network-Conditions-Client-Id.
Expand Down
88 changes: 88 additions & 0 deletions atom/browser/api/atom_api_spellchecker.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright 2016 The Brave Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "atom/browser/api/atom_api_spellchecker.h"

#include <string>

#include "atom/common/node_includes.h"
#include "chrome/browser/spellchecker/spellcheck_factory.h"
#include "chrome/browser/spellchecker/spellcheck_service.h"
#include "native_mate/dictionary.h"

namespace atom {

namespace api {

SpellChecker::SpellChecker(v8::Isolate* isolate,
content::BrowserContext* browser_context)
: browser_context_(browser_context),
weak_ptr_factory_(this) {
Init(isolate);
}

SpellChecker::~SpellChecker() {}

void SpellChecker::AddWord(mate::Arguments* args) {
if (args->Length() != 1) {
args->ThrowError("Wrong number of arguments");
return;
}

std::string word;
if (!args->GetNext(&word)) {
args->ThrowError("word is a required field");
return;
}

if (browser_context_) {
SpellcheckService* spellcheck =
SpellcheckServiceFactory::GetForContext(browser_context_);
if (spellcheck) {
spellcheck->GetCustomDictionary()->AddWord(word);
}
}
}

void SpellChecker::RemoveWord(mate::Arguments* args) {
if (args->Length() != 1) {
args->ThrowError("Wrong number of arguments");
return;
}

std::string word;
if (!args->GetNext(&word)) {
args->ThrowError("word is a required field");
return;
}

if (browser_context_) {
SpellcheckService* spellcheck =
SpellcheckServiceFactory::GetForContext(browser_context_);
if (spellcheck) {
spellcheck->GetCustomDictionary()->RemoveWord(word);
}
}
}

// static
mate::Handle<SpellChecker> SpellChecker::Create(
v8::Isolate* isolate,
content::BrowserContext* browser_context) {
return mate::CreateHandle(isolate,
new SpellChecker(isolate, browser_context));
}

// static
void SpellChecker::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(mate::StringToV8(isolate, "SpellChecker"));
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.SetMethod("addWord", &SpellChecker::AddWord)
.SetMethod("removeWord", &SpellChecker::RemoveWord);
}

} // namespace api

} // namespace atom
45 changes: 45 additions & 0 deletions atom/browser/api/atom_api_spellchecker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2016 The Brave Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ATOM_BROWSER_API_ATOM_API_SPELLCHECKER_H_
#define ATOM_BROWSER_API_ATOM_API_SPELLCHECKER_H_

#include "atom/browser/api/trackable_object.h"
#include "brave/browser/brave_browser_context.h"
#include "native_mate/handle.h"

namespace atom {

namespace api {

class SpellChecker : public mate::TrackableObject<SpellChecker> {
public:
static mate::Handle<SpellChecker> Create(v8::Isolate* isolate,
content::BrowserContext* browser_context);

// mate::TrackableObject:
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);

protected:
SpellChecker(v8::Isolate* isolate, content::BrowserContext* browser_context);
~SpellChecker() override;

void AddWord(mate::Arguments* args);

void RemoveWord(mate::Arguments* args);

private:
content::BrowserContext* browser_context_; // not owned

base::WeakPtrFactory<SpellChecker> weak_ptr_factory_;

DISALLOW_COPY_AND_ASSIGN(SpellChecker);
};

} // namespace api

} // namespace atom

#endif // ATOM_BROWSER_API_ATOM_API_SPELLCHECKER_H_
23 changes: 22 additions & 1 deletion atom/browser/api/atom_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
#include "chrome/browser/printing/print_preview_message_handler.h"
#include "chrome/browser/printing/print_view_manager_basic.h"
#include "chrome/browser/printing/print_view_manager_common.h"
#include "chrome/browser/spellchecker/spellcheck_factory.h"
#include "chrome/browser/spellchecker/spellcheck_service.h"
#include "chrome/browser/ssl/security_state_tab_helper.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
Expand Down Expand Up @@ -949,7 +951,26 @@ bool WebContents::HandleContextMenu(const content::ContextMenuParams& params) {
Emit("pepper-context-menu", std::make_pair(params, web_contents()));
web_contents()->NotifyContextMenuClosed(params.custom_context);
} else {
Emit("context-menu", std::make_pair(params, web_contents()));
// For forgetting custom dictionary option
content::ContextMenuParams new_params(params);
if (new_params.is_editable && new_params.misspelled_word.empty() &&
!new_params.selection_text.empty()) {
auto browser_context = web_contents()->GetBrowserContext();
if (browser_context) {
SpellcheckService* spellcheck =
SpellcheckServiceFactory::GetForContext(browser_context);
if (spellcheck) {
const std::string selection_text =
base::UTF16ToUTF8(new_params.selection_text);
if (spellcheck->GetCustomDictionary()->HasWord(selection_text)) {
new_params.properties.insert(
std::pair<std::string, std::string>
(std::string("customDictionaryWord"), selection_text));
}
}
}
}
Emit("context-menu", std::make_pair(new_params, web_contents()));
}

return true;
Expand Down
12 changes: 12 additions & 0 deletions atom/browser/browser_context_keyed_service_factories.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/password_manager/password_store_factory.h"
#include "chrome/browser/spellchecker/spellcheck_factory.h"
#include "components/spellcheck/spellcheck_build_features.h"
#include "extensions/features/features.h"
#include "ppapi/features/features.h"
#if BUILDFLAG(ENABLE_PLUGINS)
Expand Down Expand Up @@ -38,6 +40,10 @@
#include "extensions/browser/renderer_startup_helper.h"
#endif

#if BUILDFLAG(ENABLE_SPELLCHECK)
#include "chrome/browser/extensions/api/spellcheck/spellcheck_api.h"
#endif

namespace atom {

void EnsureBrowserContextKeyedServiceFactoriesBuilt() {
Expand Down Expand Up @@ -67,6 +73,9 @@ void EnsureBrowserContextKeyedServiceFactoriesBuilt() {
extensions::StorageFrontend::GetFactoryInstance();
extensions::WebRequestAPI::GetFactoryInstance();
extensions::AtomExtensionSystemFactory::GetInstance();
#if BUILDFLAG(ENABLE_SPELLCHECK)
extensions::SpellcheckAPI::GetFactoryInstance();
#endif
#endif
CookieSettingsFactory::GetInstance();
HostContentSettingsMapFactory::GetInstance();
Expand All @@ -76,6 +85,9 @@ void EnsureBrowserContextKeyedServiceFactoriesBuilt() {
ProtocolHandlerRegistryFactory::GetInstance();
HistoryServiceFactory::GetInstance();
PasswordStoreFactory::GetInstance();
#if BUILDFLAG(ENABLE_SPELLCHECK)
SpellcheckServiceFactory::GetInstance();
#endif
}

} // namespace atom
1 change: 1 addition & 0 deletions atom/browser/ui/atom_menu_model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "atom/browser/ui/atom_menu_model.h"

#include "base/stl_util.h"
#include "chrome/browser/renderer_context_menu/spelling_menu_observer.h"

namespace atom {

Expand Down
4 changes: 0 additions & 4 deletions atom/common/api/resources/web_frame_bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ binding.registerCustomHook(function(bindingsAPI, extensionId) {
return webFrameNatives.executeJavaScript(code)
})

apiFunctions.setHandleRequest('setSpellCheckProvider', function(lang, autoCorrectEnabled, spellCheckProvider) {
return webFrameNatives.setSpellCheckProvider(lang, autoCorrectEnabled, spellCheckProvider)
})

apiFunctions.setHandleRequest('setZoomLevel', function(level) {
return webFrameNatives.setZoomLevel(level)
})
Expand Down
1 change: 0 additions & 1 deletion atom/common/api/resources/web_view_api_bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ const asyncMethods = [
'delete',
'selectAll',
'unselect',
'replace',
'replaceMisspelling',
'findInPage',
'stopFindInPage',
Expand Down
2 changes: 2 additions & 0 deletions atom/common/native_mate_converters/content_converter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,11 @@ v8::Local<v8::Value> Converter<ContextMenuParamsWithWebContents>::ToV8(
dict.Set("selectionText", params.selection_text);
dict.Set("titleText", params.title_text);
dict.Set("misspelledWord", params.misspelled_word);
dict.Set("dictionarySuggestions", params.dictionary_suggestions);
dict.Set("frameCharset", params.frame_charset);
dict.Set("inputFieldType", params.input_field_type);
dict.Set("menuSourceType", params.source_type);
dict.Set("properties", params.properties);

if (params.custom_context.request_id || params.custom_context.is_pepper_menu)
dict.Set("menu", MenuToV8(isolate, val.second, params.custom_context,
Expand Down
Loading

0 comments on commit 393e94a

Please sign in to comment.