From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Andy Dill <andy@discordapp.com>
Date: Wed, 23 Jan 2019 10:33:55 -0800
Subject: move text-to-speech out of chromium_src


diff --git a/chrome/browser/speech/tts_controller_impl.cc b/chrome/browser/speech/tts_controller_impl.cc
index 2ca56c3a247e674ee15b0a0ee30e6c2941aa93d3..f58e33b3c0198e4333f98cd1928c8f6d9be45738 100644
--- a/chrome/browser/speech/tts_controller_impl.cc
+++ b/chrome/browser/speech/tts_controller_impl.cc
@@ -624,12 +624,14 @@ const PrefService* TtsControllerImpl::GetPrefService(
     const Utterance* utterance) {
   const PrefService* prefs = nullptr;
   // The utterance->browser_context() is null in tests.
+#if 0
   if (utterance->browser_context()) {
     const Profile* profile =
         Profile::FromBrowserContext(utterance->browser_context());
     if (profile)
       prefs = profile->GetPrefs();
   }
+#endif
   return prefs;
 }
 
diff --git a/chrome/browser/speech/tts_mac.mm b/chrome/browser/speech/tts_mac.mm
index 74996d854df318de0e01873f27f6662bf4998cf8..d3bf665c2963560a2bf35afaca56ec5b57d0890b 100644
--- a/chrome/browser/speech/tts_mac.mm
+++ b/chrome/browser/speech/tts_mac.mm
@@ -11,7 +11,6 @@
 #include "base/values.h"
 #include "chrome/browser/speech/tts_controller.h"
 #include "chrome/browser/speech/tts_platform.h"
-#include "extensions/browser/extension_function.h"
 
 #import <Cocoa/Cocoa.h>
 
diff --git a/chrome/browser/speech/tts_message_filter.cc b/chrome/browser/speech/tts_message_filter.cc
index 013c7a9c60f95475d63701c4f6848cb00dac19d9..73a244a726e3e1a1b5b52ca0e8e3b209aaf432ff 100644
--- a/chrome/browser/speech/tts_message_filter.cc
+++ b/chrome/browser/speech/tts_message_filter.cc
@@ -9,14 +9,40 @@
 #include "base/bind.h"
 #include "base/logging.h"
 #include "chrome/browser/chrome_notification_types.h"
+#if 0
 #include "chrome/browser/profiles/profile.h"
+#endif
 #include "chrome/common/tts_messages.h"
+#include "components/keyed_service/content/browser_context_keyed_service_shutdown_notifier_factory.h"
 #include "content/public/browser/browser_context.h"
 #include "content/public/browser/notification_service.h"
 #include "content/public/browser/render_process_host.h"
 
 using content::BrowserThread;
 
+namespace {
+
+class TtsMessageFilterShutdownNotifierFactory
+    : public BrowserContextKeyedServiceShutdownNotifierFactory {
+ public:
+  static TtsMessageFilterShutdownNotifierFactory* GetInstance() {
+    return base::Singleton<TtsMessageFilterShutdownNotifierFactory>::get();
+  }
+
+ private:
+  friend struct base::DefaultSingletonTraits<
+      TtsMessageFilterShutdownNotifierFactory>;
+
+  TtsMessageFilterShutdownNotifierFactory()
+      : BrowserContextKeyedServiceShutdownNotifierFactory("TtsMessageFilter") {}
+
+  ~TtsMessageFilterShutdownNotifierFactory() override {}
+
+  DISALLOW_COPY_AND_ASSIGN(TtsMessageFilterShutdownNotifierFactory);
+};
+
+}  // namespace
+
 TtsMessageFilter::TtsMessageFilter(content::BrowserContext* browser_context)
     : BrowserMessageFilter(TtsMsgStart),
       browser_context_(browser_context),
@@ -24,28 +50,27 @@ TtsMessageFilter::TtsMessageFilter(content::BrowserContext* browser_context)
   CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
   TtsController::GetInstance()->AddVoicesChangedDelegate(this);
 
-  // TODO(dmazzoni): make it so that we can listen for a BrowserContext
-  // being destroyed rather than a Profile.  http://crbug.com/444668
-  Profile* profile = Profile::FromBrowserContext(browser_context);
-  notification_registrar_.Add(this,
-                              chrome::NOTIFICATION_PROFILE_DESTROYED,
-                              content::Source<Profile>(profile));
+  browser_context_shutdown_notifier_ =
+      TtsMessageFilterShutdownNotifierFactory::GetInstance()
+          ->Get(browser_context)
+          ->Subscribe(base::Bind(&TtsMessageFilter::BrowserContextDestroyed,
+                                 base::RetainedRef(this)));
 
   // Balanced in OnChannelClosingInUIThread() to keep the ref-count be non-zero
   // until all pointers to this class are invalidated.
   AddRef();
 }
 
-void TtsMessageFilter::OverrideThreadForMessage(
-    const IPC::Message& message, BrowserThread::ID* thread) {
+void TtsMessageFilter::OverrideThreadForMessage(const IPC::Message& message,
+                                                BrowserThread::ID* thread) {
   switch (message.type()) {
-  case TtsHostMsg_InitializeVoiceList::ID:
-  case TtsHostMsg_Speak::ID:
-  case TtsHostMsg_Pause::ID:
-  case TtsHostMsg_Resume::ID:
-  case TtsHostMsg_Cancel::ID:
-    *thread = BrowserThread::UI;
-    break;
+    case TtsHostMsg_InitializeVoiceList::ID:
+    case TtsHostMsg_Speak::ID:
+    case TtsHostMsg_Pause::ID:
+    case TtsHostMsg_Resume::ID:
+    case TtsHostMsg_Cancel::ID:
+      *thread = BrowserThread::UI;
+      break;
   }
 }
 
@@ -207,10 +232,8 @@ void TtsMessageFilter::Cleanup() {
   TtsController::GetInstance()->RemoveUtteranceEventDelegate(this);
 }
 
-void TtsMessageFilter::Observe(
-    int type,
-    const content::NotificationSource& source,
-    const content::NotificationDetails& details) {
+void TtsMessageFilter::BrowserContextDestroyed() {
+  CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
   browser_context_ = nullptr;
-  notification_registrar_.RemoveAll();
+  browser_context_shutdown_notifier_.reset();
 }
diff --git a/chrome/browser/speech/tts_message_filter.h b/chrome/browser/speech/tts_message_filter.h
index cc9e2806b5c3942472785bf3a3a32e23d859971d..d21fb42f1aca2906b8d8968bd1a46721fbc55edb 100644
--- a/chrome/browser/speech/tts_message_filter.h
+++ b/chrome/browser/speech/tts_message_filter.h
@@ -9,10 +9,9 @@
 #include "base/memory/weak_ptr.h"
 #include "base/synchronization/lock.h"
 #include "chrome/browser/speech/tts_controller.h"
+#include "components/keyed_service/core/keyed_service_shutdown_notifier.h"
 #include "content/public/browser/browser_message_filter.h"
 #include "content/public/browser/browser_thread.h"
-#include "content/public/browser/notification_observer.h"
-#include "content/public/browser/notification_registrar.h"
 
 namespace content {
 class BrowserContext;
@@ -22,7 +21,6 @@ struct TtsUtteranceRequest;
 
 class TtsMessageFilter
     : public content::BrowserMessageFilter,
-      public content::NotificationObserver,
       public UtteranceEventDelegate,
       public VoicesChangedDelegate {
  public:
@@ -64,15 +62,13 @@ class TtsMessageFilter
   // about to be deleted.
   bool Valid();
 
-  // content::NotificationObserver implementation.
-  void Observe(int type,
-               const content::NotificationSource& source,
-               const content::NotificationDetails& details) override;
+  void BrowserContextDestroyed();
 
+  std::unique_ptr<KeyedServiceShutdownNotifier::Subscription>
+      browser_context_shutdown_notifier_;
   content::BrowserContext* browser_context_;
   mutable base::Lock mutex_;
   mutable bool valid_;
-  content::NotificationRegistrar notification_registrar_;
 
   DISALLOW_COPY_AND_ASSIGN(TtsMessageFilter);
 };
