tld_cleanup.cc 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. // This command-line program converts an effective-TLD data file in UTF-8 from
  5. // the format provided by Mozilla to the format expected by Chrome. This
  6. // program generates an intermediate file which is then used by gperf to
  7. // generate a perfect hash map. The benefit of this approach is that no time is
  8. // spent on program initialization to generate the map of this data.
  9. //
  10. // Running this program finds "effective_tld_names.dat" in the expected location
  11. // in the source checkout and generates "effective_tld_names.gperf" next to it.
  12. //
  13. // Any errors or warnings from this program are recorded in tld_cleanup.log.
  14. //
  15. // In particular, it
  16. // * Strips blank lines and comments, as well as notes for individual rules.
  17. // * Strips a single leading and/or trailing dot from each rule, if present.
  18. // * Logs a warning if a rule contains '!' or '*.' other than at the beginning
  19. // of the rule. (This also catches multiple ! or *. at the start of a rule.)
  20. // * Logs a warning if GURL reports a rule as invalid, but keeps the rule.
  21. // * Canonicalizes each rule's domain by converting it to a GURL and back.
  22. // * Adds explicit rules for true TLDs found in any rule.
  23. // * Marks entries in the file between "// ===BEGIN PRIVATE DOMAINS==="
  24. // and "// ===END PRIVATE DOMAINS===" as private.
  25. #include "base/at_exit.h"
  26. #include "base/command_line.h"
  27. #include "base/files/file_path.h"
  28. #include "base/files/file_util.h"
  29. #include "base/i18n/icu_util.h"
  30. #include "base/logging.h"
  31. #include "base/path_service.h"
  32. #include "base/process/memory.h"
  33. #include "net/tools/tld_cleanup/tld_cleanup_util.h"
  34. int main(int argc, const char* argv[]) {
  35. base::EnableTerminationOnHeapCorruption();
  36. if (argc != 1) {
  37. fprintf(stderr, "Normalizes and verifies UTF-8 TLD data files\n");
  38. fprintf(stderr, "Usage: %s\n", argv[0]);
  39. return 1;
  40. }
  41. // Manages the destruction of singletons.
  42. base::AtExitManager exit_manager;
  43. // Only use OutputDebugString in debug mode.
  44. #ifdef NDEBUG
  45. logging::LoggingDestination destination = logging::LOG_TO_FILE;
  46. #else
  47. logging::LoggingDestination destination =
  48. logging::LOG_TO_ALL;
  49. #endif
  50. base::CommandLine::Init(argc, argv);
  51. base::FilePath log_filename;
  52. base::PathService::Get(base::DIR_EXE, &log_filename);
  53. log_filename = log_filename.AppendASCII("tld_cleanup.log");
  54. logging::LoggingSettings settings;
  55. settings.logging_dest = destination;
  56. settings.log_file_path = log_filename.value().c_str();
  57. settings.delete_old = logging::DELETE_OLD_LOG_FILE;
  58. logging::InitLogging(settings);
  59. base::i18n::InitializeICU();
  60. base::FilePath input_file;
  61. base::PathService::Get(base::DIR_SOURCE_ROOT, &input_file);
  62. input_file = input_file.Append(FILE_PATH_LITERAL("net"))
  63. .Append(FILE_PATH_LITERAL("base"))
  64. .Append(FILE_PATH_LITERAL(
  65. "registry_controlled_domains"))
  66. .Append(FILE_PATH_LITERAL("effective_tld_names.dat"));
  67. base::FilePath output_file;
  68. base::PathService::Get(base::DIR_SOURCE_ROOT, &output_file);
  69. output_file = output_file.Append(FILE_PATH_LITERAL("net"))
  70. .Append(FILE_PATH_LITERAL("base"))
  71. .Append(FILE_PATH_LITERAL(
  72. "registry_controlled_domains"))
  73. .Append(FILE_PATH_LITERAL(
  74. "effective_tld_names.gperf"));
  75. net::tld_cleanup::NormalizeResult result =
  76. net::tld_cleanup::NormalizeFile(input_file, output_file);
  77. if (result != net::tld_cleanup::kSuccess) {
  78. fprintf(stderr,
  79. "Errors or warnings processing file. See log in tld_cleanup.log.");
  80. }
  81. if (result == net::tld_cleanup::kError)
  82. return 1;
  83. return 0;
  84. }