shared_l10n_map.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2022 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. #ifndef EXTENSIONS_RENDERER_SHARED_L10N_MAP_H_
  5. #define EXTENSIONS_RENDERER_SHARED_L10N_MAP_H_
  6. #include <map>
  7. #include <string>
  8. #include "base/synchronization/lock.h"
  9. #include "extensions/common/extension_id.h"
  10. namespace IPC {
  11. class Sender;
  12. }
  13. namespace extensions {
  14. // A helper class to retrieve l10n data for extensions. Since renderers are
  15. // always tied to a specific profile, this class is safe as a singleton (we
  16. // don't need to worry about extensions in other profiles).
  17. //
  18. // This class is thread-safe.
  19. //
  20. // NOTA BENE: ALL METHODS THAT RETRIEVE OR REPLACE MESSAGES MAY RESULT IN A
  21. // SYNC IPC TO THE BROWSER PROCESS AND/OR WAITING FOR A THREAD LOCK.
  22. //
  23. // In practice, the above is not as *quite* bad as it sounds (though still not
  24. // ideal): we cache the retrieved data for extensions, so a maximum of a single
  25. // IPC will be sent per extension per process. Additionally, extensions will
  26. // usually only be accessing this data on a single thread at a time.
  27. class SharedL10nMap {
  28. public:
  29. // A map of message name to message.
  30. using L10nMessagesMap = std::map<std::string, std::string>;
  31. SharedL10nMap();
  32. SharedL10nMap(const SharedL10nMap&) = delete;
  33. SharedL10nMap& operator=(const SharedL10nMap&) = delete;
  34. ~SharedL10nMap() = delete; // Only the global instance should be used.
  35. static SharedL10nMap& GetInstance();
  36. // Retrieves the localized message for the given `extension_id` and
  37. // `message_name`.
  38. std::string GetMessage(const ExtensionId& extension_id,
  39. const std::string& message_name,
  40. IPC::Sender* message_sender);
  41. // Replaces all messages in `text` with the messages for the given
  42. // `extension_id`. Returns false if any messages were unmatched.
  43. bool ReplaceMessages(const ExtensionId& extension_id,
  44. std::string* text,
  45. IPC::Sender* message_sender);
  46. // Erases the L10nMessagesMap for the given `id`.
  47. void EraseMessagesMap(const ExtensionId& extension_id);
  48. // Sets the L10nMessagesMap for the extension for testing purposes.
  49. void SetMessagesForTesting(const ExtensionId& id, L10nMessagesMap messages);
  50. private:
  51. const L10nMessagesMap* GetMapForExtension(const ExtensionId& extension_id,
  52. IPC::Sender* message_sender);
  53. using ExtensionToL10nMessagesMap = std::map<ExtensionId, L10nMessagesMap>;
  54. ExtensionToL10nMessagesMap map_;
  55. base::Lock lock_;
  56. };
  57. } // namespace extensions
  58. #endif // EXTENSIONS_RENDERER_SHARED_L10N_MAP_H_