From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Samuel Attard <sattard@slack-corp.com>
Date: Sun, 1 Mar 2020 16:33:55 -0800
Subject: feat: allow embedders to add observers on created hunspell
 dictionaries

This patch is used by Electron to implement spellchecker events.

diff --git a/chrome/browser/spellchecker/spellcheck_service.cc b/chrome/browser/spellchecker/spellcheck_service.cc
index 1018f1871490d8681f3f5769a3d7f53b8b095b30..073dbf4e3a71102ba67ad6ee34f6191207e62e7f 100644
--- a/chrome/browser/spellchecker/spellcheck_service.cc
+++ b/chrome/browser/spellchecker/spellcheck_service.cc
@@ -478,6 +478,8 @@ void SpellcheckService::LoadDictionaries() {
         std::make_unique<SpellcheckHunspellDictionary>(
             dictionary, platform_spellcheck_language, context_, this));
     hunspell_dictionaries_.back()->AddObserver(this);
+    if (hunspell_observer_)
+      hunspell_dictionaries_.back()->AddObserver(hunspell_observer_);
     hunspell_dictionaries_.back()->Load();
   }
 
@@ -532,6 +534,20 @@ bool SpellcheckService::IsSpellcheckEnabled() const {
          (!hunspell_dictionaries_.empty() || enable_if_uninitialized);
 }
 
+void SpellcheckService::SetHunspellObserver(SpellcheckHunspellDictionary::Observer* observer) {
+  if (hunspell_observer_) {
+    for (auto& dict : hunspell_dictionaries_) {
+      dict->RemoveObserver(hunspell_observer_);
+    }
+  }
+  if (observer) {
+    for (auto& dict : hunspell_dictionaries_) {
+      dict->AddObserver(observer);
+    }
+  }
+  hunspell_observer_ = observer;
+}
+
 void SpellcheckService::OnRenderProcessHostCreated(
     content::RenderProcessHost* host) {
   InitForRenderer(host);
diff --git a/chrome/browser/spellchecker/spellcheck_service.h b/chrome/browser/spellchecker/spellcheck_service.h
index 00e613bb4ca4346eb0b0e65b9b818d817ac724d9..1b40931b8654b80e9a5fd0f170217b4b008eaae9 100644
--- a/chrome/browser/spellchecker/spellcheck_service.h
+++ b/chrome/browser/spellchecker/spellcheck_service.h
@@ -135,6 +135,8 @@ class SpellcheckService : public KeyedService,
   // dictionaries available.
   bool IsSpellcheckEnabled() const;
 
+  void SetHunspellObserver(SpellcheckHunspellDictionary::Observer* observer);
+
   // content::RenderProcessHostCreationObserver implementation.
   void OnRenderProcessHostCreated(content::RenderProcessHost* host) override;
 
@@ -299,6 +301,8 @@ class SpellcheckService : public KeyedService,
   // A pointer to the BrowserContext which this service refers to.
   raw_ptr<content::BrowserContext> context_;
 
+  raw_ptr<SpellcheckHunspellDictionary::Observer> hunspell_observer_ = nullptr;
+
   std::unique_ptr<SpellCheckHostMetrics> metrics_;
 
   std::unique_ptr<SpellcheckCustomDictionary> custom_dictionary_;
