resources_private_api.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2015 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 "shell/browser/extensions/api/resources_private/resources_private_api.h"
  5. #include <memory>
  6. #include <string>
  7. #include <utility>
  8. #include "base/values.h"
  9. #include "chrome/browser/browser_process.h"
  10. #include "chrome/common/extensions/api/resources_private.h"
  11. #include "chrome/grit/generated_resources.h"
  12. #include "components/strings/grit/components_strings.h"
  13. #include "components/zoom/page_zoom_constants.h"
  14. #include "pdf/buildflags.h"
  15. #include "printing/buildflags/buildflags.h"
  16. #include "ui/base/l10n/l10n_util.h"
  17. #include "ui/base/webui/web_ui_util.h"
  18. #if BUILDFLAG(ENABLE_PDF)
  19. #include "pdf/pdf_features.h"
  20. #endif // BUILDFLAG(ENABLE_PDF)
  21. // To add a new component to this API, simply:
  22. // 1. Add your component to the Component enum in
  23. // chrome/common/extensions/api/resources_private.idl
  24. // 2. Create an AddStringsForMyComponent(base::DictionaryValue * dict) method.
  25. // 3. Tie in that method to the switch statement in Run()
  26. namespace extensions {
  27. namespace {
  28. void AddStringsForPdf(base::DictionaryValue* dict) {
  29. #if BUILDFLAG(ENABLE_PDF)
  30. static constexpr webui::LocalizedString kPdfResources[] = {
  31. {"passwordDialogTitle", IDS_PDF_PASSWORD_DIALOG_TITLE},
  32. {"passwordPrompt", IDS_PDF_NEED_PASSWORD},
  33. {"passwordSubmit", IDS_PDF_PASSWORD_SUBMIT},
  34. {"passwordInvalid", IDS_PDF_PASSWORD_INVALID},
  35. {"pageLoading", IDS_PDF_PAGE_LOADING},
  36. {"pageLoadFailed", IDS_PDF_PAGE_LOAD_FAILED},
  37. {"errorDialogTitle", IDS_PDF_ERROR_DIALOG_TITLE},
  38. {"pageReload", IDS_PDF_PAGE_RELOAD_BUTTON},
  39. {"bookmarks", IDS_PDF_BOOKMARKS},
  40. {"labelPageNumber", IDS_PDF_LABEL_PAGE_NUMBER},
  41. {"tooltipRotateCW", IDS_PDF_TOOLTIP_ROTATE_CW},
  42. {"tooltipDownload", IDS_PDF_TOOLTIP_DOWNLOAD},
  43. {"tooltipPrint", IDS_PDF_TOOLTIP_PRINT},
  44. {"tooltipFitToPage", IDS_PDF_TOOLTIP_FIT_PAGE},
  45. {"tooltipFitToWidth", IDS_PDF_TOOLTIP_FIT_WIDTH},
  46. {"tooltipTwoUpViewDisable", IDS_PDF_TOOLTIP_TWO_UP_VIEW_DISABLE},
  47. {"tooltipTwoUpViewEnable", IDS_PDF_TOOLTIP_TWO_UP_VIEW_ENABLE},
  48. {"tooltipZoomIn", IDS_PDF_TOOLTIP_ZOOM_IN},
  49. {"tooltipZoomOut", IDS_PDF_TOOLTIP_ZOOM_OUT},
  50. };
  51. for (const auto& resource : kPdfResources)
  52. dict->SetString(resource.name, l10n_util::GetStringUTF16(resource.id));
  53. dict->SetString("presetZoomFactors", zoom::GetPresetZoomFactorsAsJSON());
  54. #endif // BUILDFLAG(ENABLE_PDF)
  55. }
  56. void AddAdditionalDataForPdf(base::DictionaryValue* dict) {
  57. #if BUILDFLAG(ENABLE_PDF)
  58. dict->SetKey("pdfFormSaveEnabled",
  59. base::Value(base::FeatureList::IsEnabled(
  60. chrome_pdf::features::kSaveEditedPDFForm)));
  61. dict->SetKey("pdfAnnotationsEnabled", base::Value(false));
  62. dict->SetKey("pdfTwoUpViewEnabled",
  63. base::Value(base::FeatureList::IsEnabled(
  64. chrome_pdf::features::kPDFTwoUpView)));
  65. dict->SetKey("printingEnabled", base::Value(true));
  66. #endif // BUILDFLAG(ENABLE_PDF)
  67. }
  68. } // namespace
  69. namespace get_strings = api::resources_private::GetStrings;
  70. ResourcesPrivateGetStringsFunction::ResourcesPrivateGetStringsFunction() {}
  71. ResourcesPrivateGetStringsFunction::~ResourcesPrivateGetStringsFunction() {}
  72. ExtensionFunction::ResponseAction ResourcesPrivateGetStringsFunction::Run() {
  73. std::unique_ptr<get_strings::Params> params(
  74. get_strings::Params::Create(*args_));
  75. auto dict = std::make_unique<base::DictionaryValue>();
  76. api::resources_private::Component component = params->component;
  77. switch (component) {
  78. case api::resources_private::COMPONENT_PDF:
  79. AddStringsForPdf(dict.get());
  80. AddAdditionalDataForPdf(dict.get());
  81. break;
  82. case api::resources_private::COMPONENT_IDENTITY:
  83. NOTREACHED();
  84. break;
  85. case api::resources_private::COMPONENT_NONE:
  86. NOTREACHED();
  87. break;
  88. }
  89. const std::string& app_locale = g_browser_process->GetApplicationLocale();
  90. webui::SetLoadTimeDataDefaults(app_locale, dict.get());
  91. return RespondNow(OneArgument(std::move(dict)));
  92. }
  93. } // namespace extensions