chrome://policy: Remove use of deprecated cr/ui.m.js

Migrate to Web Components, instead of the outdated cr.ui.define() and
related methods.

Since this UI is also served on iOS, this CL ports the
ShouldReplaceI18nInJS/EnableReplaceI18nInJS methods from Desktop to
iOS. This is done to enable Web Components to work on iOS, since the
HTML for custom elements, including $i18n{} expressions, is inlined in
the JS file that is served at runtime.

Bug: 1322977
Change-Id: Ide884bde405709c73cde9312ebc7da1e1bad8b18
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3648387
Reviewed-by: Mike Dougherty <michaeldo@chromium.org>
Reviewed-by: Demetrios Papadopoulos <dpapad@chromium.org>
Reviewed-by: Erik Chen <erikchen@chromium.org>
Reviewed-by: Owen Min <zmin@chromium.org>
Reviewed-by: Ted Choc <tedchoc@chromium.org>
Commit-Queue: Rebekah Potter <rbpotter@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1007557}
NOKEYCHECK=True
GitOrigin-RevId: 952de22b560e4b6dac56d560c51685086ac572a0
diff --git a/chrome/browser/ui/webui/policy/policy_ui.mm b/chrome/browser/ui/webui/policy/policy_ui.mm
index 22910da..6e096bc 100644
--- a/chrome/browser/ui/webui/policy/policy_ui.mm
+++ b/chrome/browser/ui/webui/policy/policy_ui.mm
@@ -90,7 +90,14 @@
   source->AddResourcePath("policy.css", IDR_POLICY_CSS);
   source->AddResourcePath("policy_base.js", IDR_POLICY_BASE_JS);
   source->AddResourcePath("policy.js", IDR_POLICY_JS);
+  source->AddResourcePath("policy_conflict.js", IDR_POLICY_POLICY_CONFLICT_JS);
+  source->AddResourcePath("policy_row.js", IDR_POLICY_POLICY_ROW_JS);
+  source->AddResourcePath("policy_precedence_row.js",
+                          IDR_POLICY_POLICY_PRECEDENCE_ROW_JS);
+  source->AddResourcePath("policy_table.js", IDR_POLICY_POLICY_TABLE_JS);
+  source->AddResourcePath("status_box.js", IDR_POLICY_STATUS_BOX_JS);
   source->SetDefaultResource(IDR_POLICY_HTML);
+  source->EnableReplaceI18nInJS();
   return source;
 }
 
diff --git a/web/public/webui/url_data_source_ios.h b/web/public/webui/url_data_source_ios.h
index 4387df2..1924902 100644
--- a/web/public/webui/url_data_source_ios.h
+++ b/web/public/webui/url_data_source_ios.h
@@ -67,6 +67,10 @@
   // TODO: nuke this and convert all callers to not replace.
   virtual bool ShouldReplaceExistingSource() const;
 
+  // Returns true if i18n replacemenents should be performed in JS files. Needed
+  // by UIs that use Web Components.
+  virtual bool ShouldReplaceI18nInJS() const;
+
   // Returns true if responses from this URLDataSourceIOS can be cached.
   virtual bool AllowCaching() const;
 
diff --git a/web/public/webui/web_ui_ios_data_source.h b/web/public/webui/web_ui_ios_data_source.h
index b9394c4..b8cee35 100644
--- a/web/public/webui/web_ui_ios_data_source.h
+++ b/web/public/webui/web_ui_ios_data_source.h
@@ -54,6 +54,10 @@
   // URL that provides translations and dynamic data when requested.
   virtual void UseStringsJs() = 0;
 
+  // Call this to enable i18n replacements in JS files (needed by Web UIs that
+  // use Web Components).
+  virtual void EnableReplaceI18nInJS() = 0;
+
   // Adds a mapping between a path name and a resource to return.
   virtual void AddResourcePath(const std::string& path, int resource_id) = 0;
 
diff --git a/web/webui/url_data_manager_ios_backend.mm b/web/webui/url_data_manager_ios_backend.mm
index 5fe5b55..4619e08 100644
--- a/web/webui/url_data_manager_ios_backend.mm
+++ b/web/webui/url_data_manager_ios_backend.mm
@@ -332,8 +332,10 @@
                                             const std::string& mime_type) {
   set_mime_type(mime_type);
 
-  if (mime_type == "text/html")
+  if (mime_type == "text/html" || (mime_type == "application/javascript" &&
+                                   source->ShouldReplaceI18nInJS())) {
     set_source(source);
+  }
 
   NotifyHeadersComplete();
 }
diff --git a/web/webui/url_data_source_ios.mm b/web/webui/url_data_source_ios.mm
index 5b975d1..f448f82 100644
--- a/web/webui/url_data_source_ios.mm
+++ b/web/webui/url_data_source_ios.mm
@@ -39,4 +39,8 @@
   return GetWebClient()->IsAppSpecificURL(url);
 }
 
+bool URLDataSourceIOS::ShouldReplaceI18nInJS() const {
+  return false;
+}
+
 }  // namespace web
diff --git a/web/webui/url_data_source_ios_impl.cc b/web/webui/url_data_source_ios_impl.cc
index a8609f4..fef8d8f 100644
--- a/web/webui/url_data_source_ios_impl.cc
+++ b/web/webui/url_data_source_ios_impl.cc
@@ -58,4 +58,8 @@
   return nullptr;
 }
 
+bool URLDataSourceIOSImpl::ShouldReplaceI18nInJS() const {
+  return false;
+}
+
 }  // namespace web
diff --git a/web/webui/url_data_source_ios_impl.h b/web/webui/url_data_source_ios_impl.h
index 4977888..2526acd 100644
--- a/web/webui/url_data_source_ios_impl.h
+++ b/web/webui/url_data_source_ios_impl.h
@@ -68,6 +68,10 @@
   // Replacements for i18n or null if no replacements are desired.
   virtual const ui::TemplateReplacements* GetReplacements() const;
 
+  // Whether to perform i18n replacements in JS files (needed by WebUIs that are
+  // using Web Components).
+  virtual bool ShouldReplaceI18nInJS() const;
+
  protected:
   virtual ~URLDataSourceIOSImpl();
 
diff --git a/web/webui/web_ui_ios_data_source_impl.h b/web/webui/web_ui_ios_data_source_impl.h
index e51d584..675d84d 100644
--- a/web/webui/web_ui_ios_data_source_impl.h
+++ b/web/webui/web_ui_ios_data_source_impl.h
@@ -34,6 +34,8 @@
       base::span<const webui::LocalizedString> strings) override;
   void AddBoolean(const std::string& name, bool value) override;
   void UseStringsJs() override;
+  void EnableReplaceI18nInJS() override;
+  bool ShouldReplaceI18nInJS() const override;
   void AddResourcePath(const std::string& path, int resource_id) override;
   void SetDefaultResource(int resource_id) override;
   void DisableDenyXFrameOptions() override;
@@ -82,6 +84,7 @@
   bool deny_xframe_options_;
   bool load_time_data_defaults_added_;
   bool replace_existing_source_;
+  bool should_replace_i18n_in_js_;
 };
 
 }  // web
diff --git a/web/webui/web_ui_ios_data_source_impl.mm b/web/webui/web_ui_ios_data_source_impl.mm
index 4b322c7..b26fc7f 100644
--- a/web/webui/web_ui_ios_data_source_impl.mm
+++ b/web/webui/web_ui_ios_data_source_impl.mm
@@ -51,6 +51,9 @@
   bool ShouldReplaceExistingSource() const override {
     return parent_->replace_existing_source_;
   }
+  bool ShouldReplaceI18nInJS() const override {
+    return parent_->ShouldReplaceI18nInJS();
+  }
   bool AllowCaching() const override { return false; }
   bool ShouldDenyXFrameOptions() const override {
     return parent_->deny_xframe_options_;
@@ -66,7 +69,8 @@
       default_resource_(-1),
       deny_xframe_options_(true),
       load_time_data_defaults_added_(false),
-      replace_existing_source_(true) {}
+      replace_existing_source_(true),
+      should_replace_i18n_in_js_(false) {}
 
 WebUIIOSDataSourceImpl::~WebUIIOSDataSourceImpl() {}
 
@@ -112,6 +116,14 @@
   use_strings_js_ = true;
 }
 
+void WebUIIOSDataSourceImpl::EnableReplaceI18nInJS() {
+  should_replace_i18n_in_js_ = true;
+}
+
+bool WebUIIOSDataSourceImpl::ShouldReplaceI18nInJS() const {
+  return should_replace_i18n_in_js_;
+}
+
 void WebUIIOSDataSourceImpl::AddResourcePath(const std::string& path,
                                              int resource_id) {
   path_to_idr_map_[path] = resource_id;