[go: nahoru, domu]

[waffle] Simplify search engine icons generated code

This CL reduces the amount of generated code.

To include cc files, two patterns are common in Chromium: file ending
in ".inc" or in "-inc.cc". The latter is being used here, so that
the file can be auto-formatted (`git cl format` does not process .inc
files).

Change-Id: I3a5ca81afed74244b4cdd43b265026ddfe12c51c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5193683
Code-Coverage: findit-for-me@appspot.gserviceaccount.com <findit-for-me@appspot.gserviceaccount.com>
Reviewed-by: Jack Yammine <jyammine@google.com>
Commit-Queue: David Roger <droger@chromium.org>
Reviewed-by: Orin Jaworski <orinj@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1248726}
diff --git a/chrome/browser/ui/BUILD.gn b/chrome/browser/ui/BUILD.gn
index 1cca666..f0e618b 100644
--- a/chrome/browser/ui/BUILD.gn
+++ b/chrome/browser/ui/BUILD.gn
@@ -1793,7 +1793,7 @@
       "webui/realbox/realbox_handler.h",
       "webui/sanitized_image_source.cc",
       "webui/sanitized_image_source.h",
-      "webui/search_engine_choice/generated_icon_utils.cc",
+      "webui/search_engine_choice/icon_utils.cc",
       "webui/search_engine_choice/icon_utils.h",
       "webui/search_engine_choice/search_engine_choice_handler.cc",
       "webui/search_engine_choice/search_engine_choice_handler.h",
diff --git a/chrome/browser/ui/webui/search_engine_choice/generated_icon_utils.cc b/chrome/browser/ui/webui/search_engine_choice/generated_icon_utils-inc.cc
similarity index 86%
rename from chrome/browser/ui/webui/search_engine_choice/generated_icon_utils.cc
rename to chrome/browser/ui/webui/search_engine_choice/generated_icon_utils-inc.cc
index 38e6e09..8b8fc10 100644
--- a/chrome/browser/ui/webui/search_engine_choice/generated_icon_utils.cc
+++ b/chrome/browser/ui/webui/search_engine_choice/generated_icon_utils-inc.cc
@@ -2,15 +2,10 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#include "base/containers/fixed_flat_map.h"
-#include "build/branding_buildflags.h"
-#include "chrome/browser/ui/webui/search_engine_choice/icon_utils.h"
 // This code is generated
 // using`tools/search_engine_choice/generate_search_engine_icons.py`. Don't
 // modify it manually.
 
-namespace {
-
 constexpr auto kSearchEngineIconPathMap =
     base::MakeFixedFlatMap<std::u16string_view, std::string_view>(
         {{u"bing.com", "chrome://theme/IDR_BING_COM_PNG"},
@@ -74,14 +69,3 @@
           "chrome://theme/IDR_DEFAULT_FAVICON"
 #endif
          }});
-
-}  // namespace
-
-std::string_view GetSearchEngineGeneratedIconPath(
-    const std::u16string& engine_keyword) {
-  const base::fixed_flat_map<std::u16string_view, std::string_view,
-                             kSearchEngineIconPathMap.size()>::const_iterator
-      iterator = kSearchEngineIconPathMap.find(engine_keyword);
-  return iterator == kSearchEngineIconPathMap.cend() ? std::string_view()
-                                                     : iterator->second;
-}
diff --git a/chrome/browser/ui/webui/search_engine_choice/icon_utils.cc b/chrome/browser/ui/webui/search_engine_choice/icon_utils.cc
new file mode 100644
index 0000000..56bce72
--- /dev/null
+++ b/chrome/browser/ui/webui/search_engine_choice/icon_utils.cc
@@ -0,0 +1,26 @@
+// Copyright 2024 The Chromium Authors
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/ui/webui/search_engine_choice/icon_utils.h"
+
+#include <string_view>
+
+#include "base/containers/fixed_flat_map.h"
+#include "build/branding_buildflags.h"
+
+namespace {
+
+// Generated code defining `kSearchEngineIconPathMap`.
+#include "chrome/browser/ui/webui/search_engine_choice/generated_icon_utils-inc.cc"
+
+}  // namespace
+
+std::string_view GetSearchEngineGeneratedIconPath(
+    const std::u16string& engine_keyword) {
+  const base::fixed_flat_map<std::u16string_view, std::string_view,
+                             kSearchEngineIconPathMap.size()>::const_iterator
+      iterator = kSearchEngineIconPathMap.find(engine_keyword);
+  return iterator == kSearchEngineIconPathMap.cend() ? std::string_view()
+                                                     : iterator->second;
+}
diff --git a/components/search_engines/BUILD.gn b/components/search_engines/BUILD.gn
index 5b284b1..996b0557 100644
--- a/components/search_engines/BUILD.gn
+++ b/components/search_engines/BUILD.gn
@@ -114,10 +114,7 @@
   }
 
   if (!is_android) {
-    sources += [
-      "generated_marketing_snippets.cc",
-      "generated_search_engine_resource_ids.cc",
-    ]
+    sources += [ "generated_marketing_snippets.cc" ]
     deps += [
       "//components/resources:components_scaled_resources_grit",
       "//ui/resources",
diff --git a/components/search_engines/generated_search_engine_resource_ids.cc b/components/search_engines/generated_search_engine_resource_ids-inc.cc
similarity index 81%
rename from components/search_engines/generated_search_engine_resource_ids.cc
rename to components/search_engines/generated_search_engine_resource_ids-inc.cc
index 3446439..d03f2c91 100644
--- a/components/search_engines/generated_search_engine_resource_ids.cc
+++ b/components/search_engines/generated_search_engine_resource_ids-inc.cc
@@ -2,19 +2,10 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#include "components/search_engines/search_engine_choice_utils.h"
-
-#include "base/containers/fixed_flat_map.h"
-#include "build/branding_buildflags.h"
-#include "components/grit/components_scaled_resources.h"
-#include "ui/resources/grit/ui_resources.h"
-
 // This code is generated
 // using`tools/search_engine_choice/generate_search_engine_icons.py`. Don't
 // modify it manually.
 
-namespace {
-
 constexpr auto kSearchEngineResourceIdMap =
     base::MakeFixedFlatMap<std::u16string_view, int>(
         {{u"bing.com", IDR_BING_COM_PNG},
@@ -78,16 +69,3 @@
           IDR_DEFAULT_FAVICON
 #endif
          }});
-
-}  // namespace
-
-namespace search_engines {
-
-int GetIconResourceId(const std::u16string& engine_keyword) {
-  const base::fixed_flat_map<std::u16string_view, int,
-                             kSearchEngineResourceIdMap.size()>::const_iterator
-      iterator = kSearchEngineResourceIdMap.find(engine_keyword);
-  return iterator == kSearchEngineResourceIdMap.cend() ? -1 : iterator->second;
-}
-
-}  // namespace search_engines
diff --git a/components/search_engines/search_engine_choice_utils.cc b/components/search_engines/search_engine_choice_utils.cc
index 3d1b97e..8744d1b 100644
--- a/components/search_engines/search_engine_choice_utils.cc
+++ b/components/search_engines/search_engine_choice_utils.cc
@@ -8,11 +8,13 @@
 #include "base/check_deref.h"
 #include "base/check_op.h"
 #include "base/command_line.h"
+#include "base/containers/fixed_flat_map.h"
 #include "base/feature_list.h"
 #include "base/json/json_reader.h"
 #include "base/metrics/histogram_functions.h"
 #include "base/values.h"
 #include "base/version.h"
+#include "build/branding_buildflags.h"
 #include "components/country_codes/country_codes.h"
 #include "components/policy/core/common/policy_service.h"
 #include "components/policy/policy_constants.h"
@@ -25,8 +27,20 @@
 #include "components/strings/grit/components_strings.h"
 #include "ui/base/l10n/l10n_util.h"
 
+#if !BUILDFLAG(IS_ANDROID)
+#include "components/grit/components_scaled_resources.h"  // nogncheck
+#include "ui/resources/grit/ui_resources.h"               // nogncheck
+#endif
+
 namespace search_engines {
 
+namespace {
+#if !BUILDFLAG(IS_ANDROID)
+// Defines `kSearchEngineResourceIdMap`.
+#include "components/search_engines/generated_search_engine_resource_ids-inc.cc"
+#endif
+}  // namespace
+
 const char kSearchEngineChoiceScreenNavigationConditionsHistogram[] =
     "Search.ChoiceScreenNavigationConditions";
 
@@ -124,6 +138,16 @@
                    template_url_data.short_name())
              : l10n_util::GetStringUTF16(snippet_resource_id);
 }
+
+int GetIconResourceId(const std::u16string& engine_keyword) {
+  // `kSearchEngineResourceIdMap` is defined in
+  // `components/search_engines/generated_search_engine_resource_ids-inc.cc`
+  const base::fixed_flat_map<std::u16string_view, int,
+                             kSearchEngineResourceIdMap.size()>::const_iterator
+      iterator = kSearchEngineResourceIdMap.find(engine_keyword);
+  return iterator == kSearchEngineResourceIdMap.cend() ? -1 : iterator->second;
+}
+
 #endif
 
 }  // namespace search_engines
diff --git a/tools/search_engine_choice/generate_search_engine_icons.py b/tools/search_engine_choice/generate_search_engine_icons.py
index dab2125e..0f533e6 100644
--- a/tools/search_engine_choice/generate_search_engine_icons.py
+++ b/tools/search_engine_choice/generate_search_engine_icons.py
@@ -234,15 +234,15 @@
     grdp_file.write('</grit-part>\n')
 
 
-def create_get_search_engine_generated_icon_path_function():
-  """Generates the `GetSearchEngineGeneratedIconPath` function.
+def generate_icon_path_map():
+  """Generates the `kSearchEngineIconPathMap` map.
 
-  The code is generated in `search_engine_choice/generated_icon_utils.cc`.
+  The code is generated in `search_engine_choice/generated_icon_utils-inc.cc`.
   """
-  print('Creating `GetSearchEngineGeneratedIconPath` function...')
+  print('Creating `kSearchEngineIconPathMap`...')
 
   with open(
-      '../../chrome/browser/ui/webui/search_engine_choice/generated_icon_utils.cc',
+      '../../chrome/browser/ui/webui/search_engine_choice/generated_icon_utils-inc.cc',
       'w',
       encoding='utf-8',
       newline='') as utils_file:
@@ -253,22 +253,11 @@
                      ' license that can be\n')
     utils_file.write('// found in the LICENSE file.\n\n')
 
-    # Include the required header files.
-    utils_file.write(
-        '#include "chrome/browser/ui/webui/search_engine_choice/icon_utils.h"\n\n'
-    )
-
-    utils_file.write('#include "base/containers/fixed_flat_map.h"\n')
-    utils_file.write('#include "build/branding_buildflags.h"\n')
-
     utils_file.write(
         ("// This code is generated using"
          "`tools/search_engine_choice/generate_search_engine_icons.py`."
          " Don't modify it manually.\n\n"))
 
-    # Create the base::fixed_flat_map
-    utils_file.write('namespace {\n\n')
-
     utils_file.write('constexpr auto kSearchEngineIconPathMap =\n')
     utils_file.write(
         '\tbase::MakeFixedFlatMap<std::u16string_view, std::string_view>({\n')
@@ -286,33 +275,19 @@
     utils_file.write('#else\n')
     utils_file.write('\t\t "chrome://theme/IDR_DEFAULT_FAVICON"\n')
     utils_file.write('#endif\n')
+    utils_file.write('\t}});\n')
 
-    utils_file.write('\t}});\n\n')
 
-    utils_file.write('}  // namespace\n\n')
-
-    # Create the function `GetSearchEngineGeneratedIconPath()`.
-    utils_file.write('std::string_view GetSearchEngineGeneratedIconPath(\n')
-    utils_file.write('\t\tconst std::u16string& engine_keyword) {\n')
-    utils_file.write(
-        '\tconst base::fixed_flat_map<std::u16string_view, std::string_view,\n')
-    utils_file.write(
-        '\t\tkSearchEngineIconPathMap.size()>::const_iterator iterator =\n')
-    utils_file.write('\t\t\tkSearchEngineIconPathMap.find(engine_keyword);\n')
-    utils_file.write('\treturn iterator == kSearchEngineIconPathMap.cend() ?\n')
-    utils_file.write('\t\tstd::string_view() : iterator->second;\n')
-    utils_file.write('}\n')
-
-def generate_get_icon_resource_id_function():
-  """Generates the `GetIconResourceId` function.
+def generate_icon_resource_id_map():
+  """Generates the `kSearchEngineResourceIdMap` map.
 
   The code is generated in
-  `components/search_engines/generated_search_engine_resource_ids.cc`.
+  `components/search_engines/generated_search_engine_resource_ids-inc.cc`.
   """
-  print('Creating `GetIconResourceId` function...')
+  print('Creating `kSearchEngineResourceIdMap`...')
 
   with open(
-      '../../components/search_engines/generated_search_engine_resource_ids.cc',
+      '../../components/search_engines/generated_search_engine_resource_ids-inc.cc',
       'w',
       encoding='utf-8',
       newline='') as utils_file:
@@ -322,26 +297,11 @@
     utils_file.write('// Use of this source code is governed by a BSD-style'
                      ' license that can be\n')
     utils_file.write('// found in the LICENSE file.\n\n')
-
-    # Include the required header files.
-    utils_file.write(
-        '#include "components/search_engines/search_engine_choice_utils.h"\n')
-    utils_file.write('\n')
-    utils_file.write('#include "base/containers/fixed_flat_map.h"\n')
-    utils_file.write('#include "build/branding_buildflags.h"\n')
-    utils_file.write(
-        '#include "components/grit/components_scaled_resources.h"\n')
-    utils_file.write('#include "ui/resources/grit/ui_resources.h"\n')
-    utils_file.write('\n\n')
-
     utils_file.write(
         ("// This code is generated using"
          "`tools/search_engine_choice/generate_search_engine_icons.py`."
          " Don't modify it manually.\n\n"))
 
-    # Create the base::fixed_flat_map
-    utils_file.write('namespace {\n\n')
-
     utils_file.write('constexpr auto kSearchEngineResourceIdMap =\n')
     utils_file.write('\tbase::MakeFixedFlatMap<std::u16string_view, int>({\n')
 
@@ -357,27 +317,8 @@
     utils_file.write('#else\n')
     utils_file.write('\t\t IDR_DEFAULT_FAVICON\n')
     utils_file.write('#endif\n')
-
     utils_file.write('\t}});\n\n')
 
-    utils_file.write('}  // namespace\n\n')
-
-    utils_file.write('namespace search_engines {\n\n')
-
-    # Create the function `GetIconResourceId()`.
-    utils_file.write(
-        'int GetIconResourceId(const std::u16string& engine_keyword) {\n')
-    utils_file.write('\tconst base::fixed_flat_map<std::u16string_view, int,\n')
-    utils_file.write(
-        '\t\tkSearchEngineResourceIdMap.size()>::const_iterator iterator =\n')
-    utils_file.write('\t\t\tkSearchEngineResourceIdMap.find(engine_keyword);\n')
-    utils_file.write(
-        '\treturn iterator == kSearchEngineResourceIdMap.cend() ?\n')
-    utils_file.write('\t\t-1 : iterator->second;\n')
-    utils_file.write('}\n\n')
-
-    utils_file.write('}  // namespace search_engines\n')
-
 
 if sys.platform != 'linux':
   print(
@@ -407,8 +348,8 @@
 populate_used_engines()
 download_icons_from_android_search()
 generate_icon_resource_code()
-generate_get_icon_resource_id_function()
-create_get_search_engine_generated_icon_path_function()
+generate_icon_path_map()
+generate_icon_resource_id_map()
 # Format the generated code
 os.system('git cl format')
 print('Icon and code generation completed.')