heavy_ad_helper.cc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2019 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. #include "components/heavy_ad_intervention/heavy_ad_helper.h"
  5. #include "base/check.h"
  6. #include "base/strings/string_piece.h"
  7. #include "components/grit/components_resources.h"
  8. #include "components/security_interstitials/core/common_string_util.h"
  9. #include "components/strings/grit/components_strings.h"
  10. #include "third_party/zlib/google/compression_utils.h"
  11. #include "ui/base/l10n/l10n_util.h"
  12. #include "ui/base/resource/resource_bundle.h"
  13. #include "ui/base/webui/jstemplate_builder.h"
  14. #include "ui/base/webui/web_ui_util.h"
  15. namespace heavy_ad_intervention {
  16. // NOTE: If adding usage of more strings/resources here, make sure that they
  17. // are allowlisted in //weblayer/grit_{resources, strings}_allowlist.txt;
  18. // otherwise empty data will be used for the new resources/strings in WebLayer.
  19. // For strings there is a partial safeguard as //weblayer's integration tests
  20. // will crash if a new-but-not-allowlisted string is fetched in a codepath that
  21. // the presentation of the heavy ad page in those tests exercises.
  22. std::string PrepareHeavyAdPage(const std::string& application_locale) {
  23. int resource_id = IDR_SECURITY_INTERSTITIAL_QUIET_HTML;
  24. std::string uncompressed;
  25. base::StringPiece template_html(
  26. ui::ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id));
  27. if (ui::ResourceBundle::GetSharedInstance().IsGzipped(resource_id)) {
  28. bool success = compression::GzipUncompress(template_html, &uncompressed);
  29. DCHECK(success);
  30. template_html = base::StringPiece(uncompressed);
  31. }
  32. DCHECK(!template_html.empty()) << "unable to load template.";
  33. // Populate load time data.
  34. base::Value::Dict load_time_data;
  35. load_time_data.Set("type", "HEAVYAD");
  36. load_time_data.Set(
  37. "heading", l10n_util::GetStringUTF16(IDS_HEAVY_AD_INTERVENTION_HEADING));
  38. load_time_data.Set(
  39. "openDetails",
  40. l10n_util::GetStringUTF16(IDS_HEAVY_AD_INTERVENTION_BUTTON_DETAILS));
  41. load_time_data.Set(
  42. "explanationParagraph",
  43. l10n_util::GetStringUTF16(IDS_HEAVY_AD_INTERVENTION_SUMMARY));
  44. // Ad frames are never the main frame, so we do not need a tab title.
  45. load_time_data.Set("tabTitle", "");
  46. load_time_data.Set("overridable", false);
  47. load_time_data.Set("is_giant", false);
  48. webui::SetLoadTimeDataDefaults(application_locale, &load_time_data);
  49. // "body" is the id of the template's root node.
  50. std::string heavy_ad_html =
  51. webui::GetTemplatesHtml(template_html, load_time_data, "body");
  52. webui::AppendWebUiCssTextDefaults(&heavy_ad_html);
  53. return heavy_ad_html;
  54. }
  55. } // namespace heavy_ad_intervention