[Extensions] Remove NOTIFICATION_EXTENSION_LOAD_ERROR from test files

The NotificationService is deprecated. Added a LoadErrorReporterWaiter
in //chrome to move WaitForExtensionLoadError from //extension to
//chrome to avoid dependencies from //chrome. Thus ChromeTestExtensionLoader
and ExtensionPolicyTest will instead call the waiter. Finished removing
all NOTIFICATION_EXTENSION_LOAD_ERROR, thus removed the load error from
NotificationType enum.

Bug: 1174731

Change-Id: I6c187ed4a1686e9624897b018e21a024a3d2f631
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2691578
Reviewed-by: Nicolas Ouellet-Payeur <[email protected]>
Reviewed-by: Devlin <[email protected]>
Commit-Queue: Emilia Paz <[email protected]>
Cr-Commit-Position: refs/heads/master@{#857770}
diff --git a/chrome/browser/BUILD.gn b/chrome/browser/BUILD.gn
index d3bcf54..3179ae4 100644
--- a/chrome/browser/BUILD.gn
+++ b/chrome/browser/BUILD.gn
@@ -6905,6 +6905,8 @@
       "extensions/chrome_test_extension_loader.h",
       "extensions/extension_action_test_util.cc",
       "extensions/extension_action_test_util.h",
+      "extensions/load_error_waiter.cc",
+      "extensions/load_error_waiter.h",
       "extensions/mock_extension_special_storage_policy.cc",
       "extensions/mock_extension_special_storage_policy.h",
       "extensions/test_blocklist.cc",
diff --git a/chrome/browser/extensions/chrome_test_extension_loader.cc b/chrome/browser/extensions/chrome_test_extension_loader.cc
index 6c6882f..7a48001 100644
--- a/chrome/browser/extensions/chrome_test_extension_loader.cc
+++ b/chrome/browser/extensions/chrome_test_extension_loader.cc
@@ -12,6 +12,7 @@
 #include "chrome/browser/extensions/crx_installer.h"
 #include "chrome/browser/extensions/extension_service.h"
 #include "chrome/browser/extensions/extension_util.h"
+#include "chrome/browser/extensions/load_error_waiter.h"
 #include "chrome/browser/extensions/unpacked_installer.h"
 #include "chrome/browser/profiles/profile.h"
 #include "content/public/browser/notification_details.h"
@@ -316,13 +317,12 @@
   if (install_param_.has_value()) {
     installer->set_install_param(*install_param_);
   }
+  LoadErrorWaiter waiter;
   installer->Load(file_path);
   if (!should_fail_) {
     extension = registry_observer.WaitForExtensionLoaded();
   } else {
-    EXPECT_TRUE(ExtensionTestNotificationObserver(browser_context_)
-                    .WaitForExtensionLoadError())
-        << "No load error observed";
+    EXPECT_TRUE(waiter.Wait()) << "No load error observed";
   }
 
   return extension;
diff --git a/chrome/browser/extensions/load_error_reporter.cc b/chrome/browser/extensions/load_error_reporter.cc
index d072788..6b02b13 100644
--- a/chrome/browser/extensions/load_error_reporter.cc
+++ b/chrome/browser/extensions/load_error_reporter.cc
@@ -50,11 +50,6 @@
     const std::string& error,
     content::BrowserContext* browser_context,
     bool be_noisy) {
-  content::NotificationService::current()->Notify(
-      extensions::NOTIFICATION_EXTENSION_LOAD_ERROR,
-      content::Source<Profile>(Profile::FromBrowserContext(browser_context)),
-      content::Details<const std::string>(&error));
-
   std::string path_str = base::UTF16ToUTF8(extension_path.LossyDisplayName());
   base::string16 message = base::UTF8ToUTF16(base::StringPrintf(
       "%s %s. %s",
diff --git a/chrome/browser/extensions/load_error_waiter.cc b/chrome/browser/extensions/load_error_waiter.cc
new file mode 100644
index 0000000..41dc376c
--- /dev/null
+++ b/chrome/browser/extensions/load_error_waiter.cc
@@ -0,0 +1,29 @@
+// Copyright 2021 The Chromium 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 "chrome/browser/extensions/load_error_waiter.h"
+
+namespace extensions {
+
+LoadErrorWaiter::LoadErrorWaiter() {
+  load_error_observation_.Observe(extensions::LoadErrorReporter::GetInstance());
+}
+
+LoadErrorWaiter::~LoadErrorWaiter() = default;
+
+void LoadErrorWaiter::OnLoadFailure(content::BrowserContext* browser_context,
+                                    const base::FilePath& file_path,
+                                    const std::string& error) {
+  load_error_seen_ = true;
+  run_loop_.Quit();
+}
+
+bool LoadErrorWaiter::Wait() {
+  if (!load_error_seen_) {
+    run_loop_.Run();
+  }
+  return load_error_seen_;
+}
+
+}  // namespace extensions
diff --git a/chrome/browser/extensions/load_error_waiter.h b/chrome/browser/extensions/load_error_waiter.h
new file mode 100644
index 0000000..e24685f
--- /dev/null
+++ b/chrome/browser/extensions/load_error_waiter.h
@@ -0,0 +1,41 @@
+// Copyright 2021 The Chromium 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 CHROME_BROWSER_EXTENSIONS_LOAD_ERROR_WAITER_H_
+#define CHROME_BROWSER_EXTENSIONS_LOAD_ERROR_WAITER_H_
+
+#include "base/run_loop.h"
+#include "base/scoped_observation.h"
+#include "chrome/browser/extensions/load_error_reporter.h"
+
+namespace extensions {
+
+class LoadErrorWaiter : public extensions::LoadErrorReporter::Observer {
+ public:
+  LoadErrorWaiter();
+  ~LoadErrorWaiter() override;
+  LoadErrorWaiter(const LoadErrorWaiter& other) = delete;
+  LoadErrorWaiter& operator=(const LoadErrorWaiter& other) = delete;
+
+  // LoadErrorReporter::Observer:
+  void OnLoadFailure(content::BrowserContext* browser_context,
+                     const base::FilePath& file_path,
+                     const std::string& error) override;
+
+  // Waits until the observed LoadErrorReporter report a load error via the
+  // OnLoadFailure event.
+  bool Wait();
+
+ private:
+  base::ScopedObservation<extensions::LoadErrorReporter,
+                          extensions::LoadErrorReporter::Observer>
+      load_error_observation_{this};
+
+  base::RunLoop run_loop_;
+  bool load_error_seen_ = false;
+};
+
+}  // namespace extensions
+
+#endif  // CHROME_BROWSER_EXTENSIONS_LOAD_ERROR_WAITER_H_
diff --git a/chrome/browser/policy/extension_policy_browsertest.cc b/chrome/browser/policy/extension_policy_browsertest.cc
index bf13144..05a002c 100644
--- a/chrome/browser/policy/extension_policy_browsertest.cc
+++ b/chrome/browser/policy/extension_policy_browsertest.cc
@@ -22,6 +22,7 @@
 #include "chrome/browser/extensions/extension_service.h"
 #include "chrome/browser/extensions/forced_extensions/install_stage_tracker.h"
 #include "chrome/browser/extensions/install_verifier.h"
+#include "chrome/browser/extensions/load_error_waiter.h"
 #include "chrome/browser/extensions/shared_module_service.h"
 #include "chrome/browser/extensions/unpacked_installer.h"
 #include "chrome/browser/extensions/updater/extension_updater.h"
@@ -1207,11 +1208,9 @@
   // same ID as a force-installed extension.
   base::FilePath good_extension_path(ui_test_utils::GetTestFilePath(
       base::FilePath(kTestExtensionsDir), base::FilePath(kSimpleWithPopupExt)));
-  content::WindowedNotificationObserver extension_load_error_observer(
-      extensions::NOTIFICATION_EXTENSION_LOAD_ERROR,
-      content::NotificationService::AllSources());
+  extensions::LoadErrorWaiter waiter;
   installer->Load(good_extension_path);
-  extension_load_error_observer.Wait();
+  waiter.Wait();
 
   // Loading other unpacked extensions are not blocked.
   scoped_refptr<const extensions::Extension> extension =
diff --git a/extensions/browser/notification_types.h b/extensions/browser/notification_types.h
index ce4dc7b..cc19ca1 100644
--- a/extensions/browser/notification_types.h
+++ b/extensions/browser/notification_types.h
@@ -40,12 +40,6 @@
   // TODO(https://crbug.com/1174728): Remove.
   NOTIFICATION_CRX_INSTALLER_DONE = NOTIFICATION_EXTENSIONS_START,
 
-  // An error occurred while attempting to load an extension. The details are a
-  // string with details about why the load failed.
-  // DEPRECATED: Use extensions::LoadErrorReporter::OnLoadFailure()
-  // TODO(https://crbug.com/1174731): Remove.
-  NOTIFICATION_EXTENSION_LOAD_ERROR,
-
   // Sent when attempting to load a new extension, but they are disabled. The
   // details are an Extension, and the source is a BrowserContext*.
   // TODO(https://crbug.com/1174732): Remove.
diff --git a/extensions/test/extension_test_notification_observer.cc b/extensions/test/extension_test_notification_observer.cc
index 9482e64..8e150a3 100644
--- a/extensions/test/extension_test_notification_observer.cc
+++ b/extensions/test/extension_test_notification_observer.cc
@@ -113,7 +113,6 @@
 ExtensionTestNotificationObserver::ExtensionTestNotificationObserver(
     content::BrowserContext* context)
     : context_(context),
-      extension_load_errors_observed_(0),
       crx_installers_done_observed_(0) {
   if (context_)
     registry_observer_.Add(ExtensionRegistry::Get(context_));
@@ -134,12 +133,6 @@
       .Wait();
 }
 
-bool ExtensionTestNotificationObserver::WaitForExtensionLoadError() {
-  int before = extension_load_errors_observed_;
-  WaitForNotification(NOTIFICATION_EXTENSION_LOAD_ERROR);
-  return extension_load_errors_observed_ != before;
-}
-
 bool ExtensionTestNotificationObserver::WaitForExtensionCrash(
     const std::string& extension_id) {
   if (!GetNonTerminatedExtensions(extension_id, context_)) {
@@ -198,11 +191,6 @@
       ++crx_installers_done_observed_;
       break;
 
-    case NOTIFICATION_EXTENSION_LOAD_ERROR:
-      VLOG(1) << "Got EXTENSION_LOAD_ERROR notification.";
-      ++extension_load_errors_observed_;
-      break;
-
     default:
       NOTREACHED();
       break;
diff --git a/extensions/test/extension_test_notification_observer.h b/extensions/test/extension_test_notification_observer.h
index 500444c..03b7472 100644
--- a/extensions/test/extension_test_notification_observer.h
+++ b/extensions/test/extension_test_notification_observer.h
@@ -36,10 +36,6 @@
   explicit ExtensionTestNotificationObserver(content::BrowserContext* context);
   ~ExtensionTestNotificationObserver() override;
 
-  // Waits for an extension load error. Returns true if the error really
-  // happened.
-  bool WaitForExtensionLoadError();
-
   // Wait for the specified extension to crash. Returns true if it really
   // crashed.
   bool WaitForExtensionCrash(const std::string& extension_id);
@@ -138,7 +134,6 @@
   std::unique_ptr<content::WindowedNotificationObserver> observer_;
 
   std::string last_loaded_extension_id_;
-  int extension_load_errors_observed_;
   int crx_installers_done_observed_;
 
   // The condition for which we are waiting. This should be checked in any