[WebUI] Migrate NearbyShareUI to WebUIConfig

Context:

Currently WebUI properties, e.g. CSPs, requesting schemes, host, mojo,
etc. are stored in WebUIControllers themselves or in separate lists
like IsWebUIAllowedToMakeNetworkRequests. The lifetime of a
WebUIController is bound to the frame, which makes it hard to use for
some use cases where the WebUIController hasn't been created yet. A
non-dynamically allocated class where clients could query a WebUI's
properties would be easier to use and audit.

WebUIConfig is a class that stores properties of WebUIs. For now the
properties are 1. the WebUI's origin, 2. if the WebUI is enabled,
and 3. GetURLDataSource() method for service worker initialization.
In the future this class could include information like CSPs, if we
should enable Mojo, if we should enable chrome.send(), if we should
allow network requests, URLDataSource, etc.

Bug: 1317510
Change-Id: I6611735b62ddb4e9fb36824e2f42556c47ac6259
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4015342
Reviewed-by: Giovanni Ortuno Urquidi <ortuno@chromium.org>
Commit-Queue: Clement Yan <clamclamyan@google.com>
Cr-Commit-Position: refs/heads/main@{#1069593}
diff --git a/chrome/browser/ui/webui/chrome_web_ui_controller_factory.cc b/chrome/browser/ui/webui/chrome_web_ui_controller_factory.cc
index e87c5dd..9122f4c 100644
--- a/chrome/browser/ui/webui/chrome_web_ui_controller_factory.cc
+++ b/chrome/browser/ui/webui/chrome_web_ui_controller_factory.cc
@@ -993,12 +993,6 @@
       !profile->IsOffTheRecord()) {
     return &NewWebUI<ash::multidevice::ProximityAuthUI>;
   }
-  if (NearbySharingServiceFactory::IsNearbyShareSupportedForBrowserContext(
-          profile) &&
-      url.host_piece() == chrome::kChromeUINearbyShareHost &&
-      !profile->IsOffTheRecord()) {
-    return &NewWebUI<nearby_share::NearbyShareDialogUI>;
-  }
   if (url.host_piece() == ash::kChromeUIProjectorAppHost &&
       IsProjectorAppEnabled(profile)) {
     return &NewWebUI<ash::TrustedProjectorUI>;
@@ -1008,8 +1002,6 @@
       IsProjectorAppEnabled(profile)) {
     return &NewWebUI<ash::TrustedProjectorAnnotatorUI>;
   }
-  if (url.host_piece() == chrome::kChromeUINearbyInternalsHost)
-    return &NewWebUI<NearbyInternalsUI>;
   if (arc::IsArcAllowedForProfile(profile)) {
     if (url.host_piece() == chrome::kChromeUIArcGraphicsTracingHost) {
       return &NewWebUI<
diff --git a/chrome/browser/ui/webui/chromeos/chrome_web_ui_configs_chromeos.cc b/chrome/browser/ui/webui/chromeos/chrome_web_ui_configs_chromeos.cc
index 221cad5..316a606 100644
--- a/chrome/browser/ui/webui/chromeos/chrome_web_ui_configs_chromeos.cc
+++ b/chrome/browser/ui/webui/chromeos/chrome_web_ui_configs_chromeos.cc
@@ -47,6 +47,8 @@
 #include "chrome/browser/ui/webui/ash/sys_internals/sys_internals_ui.h"
 #include "chrome/browser/ui/webui/ash/vm/vm_ui.h"
 #include "chrome/browser/ui/webui/chromeos/assistant_optin/assistant_optin_ui.h"
+#include "chrome/browser/ui/webui/nearby_internals/nearby_internals_ui.h"
+#include "chrome/browser/ui/webui/nearby_share/nearby_share_dialog_ui.h"
 #include "chrome/browser/ui/webui/settings/ash/os_settings_ui.h"
 #if !defined(OFFICIAL_BUILD)
 #include "ash/webui/sample_system_web_app_ui/sample_system_web_app_ui.h"
@@ -105,6 +107,9 @@
   map.AddWebUIConfig(std::make_unique<ash::MultideviceInternalsUIConfig>());
   map.AddWebUIConfig(std::make_unique<
                      ash::multidevice_setup::MultiDeviceSetupDialogUIConfig>());
+  map.AddWebUIConfig(std::make_unique<NearbyInternalsUIConfig>());
+  map.AddWebUIConfig(
+      std::make_unique<nearby_share::NearbyShareDialogUIConfig>());
   map.AddWebUIConfig(std::make_unique<ash::NetworkUIConfig>());
   map.AddWebUIConfig(std::make_unique<ash::NotificationTesterUIConfig>());
   map.AddWebUIConfig(
diff --git a/chrome/browser/ui/webui/nearby_internals/nearby_internals_ui.h b/chrome/browser/ui/webui/nearby_internals/nearby_internals_ui.h
index 0daaf4b..756c4d2 100644
--- a/chrome/browser/ui/webui/nearby_internals/nearby_internals_ui.h
+++ b/chrome/browser/ui/webui/nearby_internals/nearby_internals_ui.h
@@ -5,8 +5,22 @@
 #ifndef CHROME_BROWSER_UI_WEBUI_NEARBY_INTERNALS_NEARBY_INTERNALS_UI_H_
 #define CHROME_BROWSER_UI_WEBUI_NEARBY_INTERNALS_NEARBY_INTERNALS_UI_H_
 
+#include "chrome/common/webui_url_constants.h"
+#include "content/public/browser/webui_config.h"
+#include "content/public/common/url_constants.h"
 #include "ui/webui/mojo_web_ui_controller.h"
 
+class NearbyInternalsUI;
+
+// WebUIConfig for chrome://nearby-internals
+class NearbyInternalsUIConfig
+    : public content::DefaultWebUIConfig<NearbyInternalsUI> {
+ public:
+  NearbyInternalsUIConfig()
+      : DefaultWebUIConfig(content::kChromeUIScheme,
+                           chrome::kChromeUINearbyInternalsHost) {}
+};
+
 // The WebUI controller for chrome://nearby-internals.
 class NearbyInternalsUI : public ui::MojoWebUIController {
  public:
diff --git a/chrome/browser/ui/webui/nearby_share/nearby_share_dialog_ui.cc b/chrome/browser/ui/webui/nearby_share/nearby_share_dialog_ui.cc
index 7cdbef7..209662b 100644
--- a/chrome/browser/ui/webui/nearby_share/nearby_share_dialog_ui.cc
+++ b/chrome/browser/ui/webui/nearby_share/nearby_share_dialog_ui.cc
@@ -54,6 +54,14 @@
   kMax = kRejected
 };
 
+bool NearbyShareDialogUIConfig::IsWebUIEnabled(
+    content::BrowserContext* browser_context) {
+  if (browser_context->IsOffTheRecord())
+    return false;
+  return NearbySharingServiceFactory::IsNearbyShareSupportedForBrowserContext(
+      browser_context);
+}
+
 NearbyShareDialogUI::NearbyShareDialogUI(content::WebUI* web_ui)
     : ui::MojoWebUIController(web_ui, /*enable_chrome_send=*/true) {
   Profile* profile = Profile::FromWebUI(web_ui);
diff --git a/chrome/browser/ui/webui/nearby_share/nearby_share_dialog_ui.h b/chrome/browser/ui/webui/nearby_share/nearby_share_dialog_ui.h
index 4a345d64..46ddd5f 100644
--- a/chrome/browser/ui/webui/nearby_share/nearby_share_dialog_ui.h
+++ b/chrome/browser/ui/webui/nearby_share/nearby_share_dialog_ui.h
@@ -12,7 +12,10 @@
 #include "chrome/browser/sharesheet/sharesheet_controller.h"
 #include "chrome/browser/ui/webui/nearby_share/nearby_share.mojom.h"
 #include "chrome/browser/ui/webui/nearby_share/public/mojom/nearby_share_settings.mojom.h"
+#include "chrome/common/webui_url_constants.h"
 #include "content/public/browser/web_contents_delegate.h"
+#include "content/public/browser/webui_config.h"
+#include "content/public/common/url_constants.h"
 #include "mojo/public/cpp/bindings/pending_receiver.h"
 #include "ui/views/controls/webview/unhandled_keyboard_event_handler.h"
 #include "ui/webui/mojo_web_ui_controller.h"
@@ -25,6 +28,19 @@
 
 namespace nearby_share {
 
+class NearbyShareDialogUI;
+
+// WebUIConfig for chrome://nearby
+class NearbyShareDialogUIConfig
+    : public content::DefaultWebUIConfig<NearbyShareDialogUI> {
+ public:
+  NearbyShareDialogUIConfig()
+      : DefaultWebUIConfig(content::kChromeUIScheme,
+                           chrome::kChromeUINearbyShareHost) {}
+
+  bool IsWebUIEnabled(content::BrowserContext* browser_context) override;
+};
+
 // The WebUI controller for chrome://nearby.
 class NearbyShareDialogUI : public ui::MojoWebUIController,
                             content::WebContentsDelegate {