user_script_set_manager.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2014 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_USER_SCRIPT_SET_MANAGER_H_
  5. #define EXTENSIONS_RENDERER_USER_SCRIPT_SET_MANAGER_H_
  6. #include <map>
  7. #include <set>
  8. #include <string>
  9. #include <vector>
  10. #include "base/memory/read_only_shared_memory_region.h"
  11. #include "base/observer_list.h"
  12. #include "content/public/renderer/render_thread_observer.h"
  13. #include "extensions/common/extension.h"
  14. #include "extensions/common/mojom/host_id.mojom-forward.h"
  15. #include "extensions/common/mojom/run_location.mojom-shared.h"
  16. #include "extensions/common/user_script.h"
  17. #include "extensions/renderer/user_script_set.h"
  18. namespace content {
  19. class RenderFrame;
  20. }
  21. namespace extensions {
  22. class ScriptInjection;
  23. // Manager for separate UserScriptSets, one for each shared memory region.
  24. // Regions are organized as follows:
  25. // static_scripts -- contains all extensions' scripts that are statically
  26. // declared in the extension manifest.
  27. // programmatic_scripts -- one region per host (extension or WebUI) containing
  28. // only programmatically-declared scripts, instantiated
  29. // when an extension first creates a declarative rule
  30. // that would, if triggered, request a script injection.
  31. class UserScriptSetManager {
  32. public:
  33. // Like a UserScriptSet::Observer, but automatically subscribes to all sets
  34. // associated with the manager.
  35. class Observer {
  36. public:
  37. virtual void OnUserScriptsUpdated(const mojom::HostID& changed_host) = 0;
  38. };
  39. UserScriptSetManager();
  40. UserScriptSetManager(const UserScriptSetManager&) = delete;
  41. UserScriptSetManager& operator=(const UserScriptSetManager&) = delete;
  42. ~UserScriptSetManager();
  43. void AddObserver(Observer* observer);
  44. void RemoveObserver(Observer* observer);
  45. // Looks up the script injection associated with |script_id| and
  46. // |extension_id| in the context of the given |web_frame|, |tab_id|,
  47. // and |url|.
  48. std::unique_ptr<ScriptInjection> GetInjectionForDeclarativeScript(
  49. const std::string& script_id,
  50. content::RenderFrame* render_frame,
  51. int tab_id,
  52. const GURL& url,
  53. const std::string& extension_id);
  54. // Append all injections from |static_scripts| and each of
  55. // |programmatic_scripts_| to |injections|.
  56. void GetAllInjections(
  57. std::vector<std::unique_ptr<ScriptInjection>>* injections,
  58. content::RenderFrame* render_frame,
  59. int tab_id,
  60. mojom::RunLocation run_location);
  61. // Get active extension IDs from `static_scripts_`.
  62. void GetAllActiveExtensionIds(std::set<std::string>* ids) const;
  63. // Handle the UpdateUserScripts extension message.
  64. void OnUpdateUserScripts(base::ReadOnlySharedMemoryRegion shared_memory,
  65. const mojom::HostID& host_id);
  66. // Invalidates script injections for the UserScriptSet in `scripts_`
  67. // corresponding to `extension_id` and deletes the script set.
  68. void OnExtensionUnloaded(const std::string& extension_id);
  69. void set_activity_logging_enabled(bool enabled) {
  70. activity_logging_enabled_ = enabled;
  71. }
  72. private:
  73. // Map for per-host script sets.
  74. using UserScriptSetMap =
  75. std::map<mojom::HostID, std::unique_ptr<UserScriptSet>>;
  76. UserScriptSet* GetScriptsByHostID(const mojom::HostID& host_id);
  77. // Stores all scripts, defined in extension manifests and programmatically
  78. // from extension APIs and webview tags. Each UserScriptSet is keyed by a
  79. // HostID.
  80. UserScriptSetMap scripts_;
  81. // Whether or not dom activity should be logged for injected scripts.
  82. bool activity_logging_enabled_;
  83. // The associated observers.
  84. base::ObserverList<Observer>::Unchecked observers_;
  85. };
  86. } // namespace extensions
  87. #endif // EXTENSIONS_RENDERER_USER_SCRIPT_SET_MANAGER_H_