script_injection_manager.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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_SCRIPT_INJECTION_MANAGER_H_
  5. #define EXTENSIONS_RENDERER_SCRIPT_INJECTION_MANAGER_H_
  6. #include <stdint.h>
  7. #include <map>
  8. #include <set>
  9. #include <string>
  10. #include <vector>
  11. #include "base/callback.h"
  12. #include "base/scoped_observation.h"
  13. #include "extensions/common/mojom/frame.mojom.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/script_injection.h"
  18. #include "extensions/renderer/user_script_set_manager.h"
  19. namespace content {
  20. class RenderFrame;
  21. }
  22. namespace extensions {
  23. // The ScriptInjectionManager manages extensions injecting scripts into frames
  24. // via both content/user scripts and tabs.executeScript(). It is responsible for
  25. // maintaining any pending injections awaiting permission or the appropriate
  26. // load point, and injecting them when ready.
  27. class ScriptInjectionManager : public UserScriptSetManager::Observer {
  28. public:
  29. explicit ScriptInjectionManager(
  30. UserScriptSetManager* user_script_set_manager);
  31. ScriptInjectionManager(const ScriptInjectionManager&) = delete;
  32. ScriptInjectionManager& operator=(const ScriptInjectionManager&) = delete;
  33. virtual ~ScriptInjectionManager();
  34. // Notifies that a new render view has been created.
  35. void OnRenderFrameCreated(content::RenderFrame* render_frame);
  36. // Removes pending injections of the unloaded extension.
  37. void OnExtensionUnloaded(const std::string& extension_id);
  38. // Handle the ExecuteCode extension message.
  39. void HandleExecuteCode(mojom::ExecuteCodeParamsPtr params,
  40. mojom::LocalFrame::ExecuteCodeCallback callback,
  41. content::RenderFrame* render_frame);
  42. void ExecuteDeclarativeScript(content::RenderFrame* render_frame,
  43. int tab_id,
  44. const ExtensionId& extension_id,
  45. const std::string& script_id,
  46. const GURL& url);
  47. void set_activity_logging_enabled(bool enabled) {
  48. activity_logging_enabled_ = enabled;
  49. }
  50. private:
  51. // A RenderFrameObserver implementation which watches the various render
  52. // frames in order to notify the ScriptInjectionManager of different
  53. // document load states and IPCs.
  54. class RFOHelper;
  55. using FrameStatusMap = std::map<content::RenderFrame*, mojom::RunLocation>;
  56. using ScriptInjectionVector = std::vector<std::unique_ptr<ScriptInjection>>;
  57. // Notifies that an injection has been finished or permission has been
  58. // handled.
  59. void OnInjectionStatusUpdated(ScriptInjection::InjectionStatus status,
  60. ScriptInjection* injection);
  61. // Notifies that an injection has been finished.
  62. void OnInjectionFinished(ScriptInjection* injection);
  63. // Handle the GrantInjectionPermission extension message.
  64. void OnPermitScriptInjectionHandled(ScriptInjection* injection);
  65. // UserScriptSetManager::Observer implementation.
  66. void OnUserScriptsUpdated(const mojom::HostID& changed_host) override;
  67. // Notifies that an RFOHelper should be removed.
  68. void RemoveObserver(RFOHelper* helper);
  69. // Invalidate any pending tasks associated with |frame|.
  70. void InvalidateForFrame(content::RenderFrame* frame);
  71. // Starts the process to inject appropriate scripts into |frame|.
  72. void StartInjectScripts(content::RenderFrame* frame,
  73. mojom::RunLocation run_location);
  74. // Actually injects the scripts into |frame|.
  75. void InjectScripts(content::RenderFrame* frame,
  76. mojom::RunLocation run_location);
  77. // Try to inject and store injection if it has not finished.
  78. void TryToInject(std::unique_ptr<ScriptInjection> injection,
  79. mojom::RunLocation run_location,
  80. ScriptsRunInfo* scripts_run_info);
  81. // The map of active web frames to their corresponding statuses. The
  82. // RunLocation of the frame corresponds to the last location that has ran.
  83. FrameStatusMap frame_statuses_;
  84. // The frames currently being injected into, so long as that frame is valid.
  85. std::set<content::RenderFrame*> active_injection_frames_;
  86. // The collection of RFOHelpers.
  87. std::vector<std::unique_ptr<RFOHelper>> rfo_helpers_;
  88. // The set of UserScripts associated with extensions. Owned by the Dispatcher.
  89. UserScriptSetManager* user_script_set_manager_;
  90. // Pending injections which are waiting for either the proper run location or
  91. // user consent.
  92. ScriptInjectionVector pending_injections_;
  93. // Running injections which are waiting for async callbacks from blink.
  94. ScriptInjectionVector running_injections_;
  95. // Whether or not dom activity should be logged for scripts injected.
  96. bool activity_logging_enabled_ = false;
  97. base::ScopedObservation<UserScriptSetManager, UserScriptSetManager::Observer>
  98. user_script_set_manager_observation_{this};
  99. };
  100. } // namespace extensions
  101. #endif // EXTENSIONS_RENDERER_SCRIPT_INJECTION_MANAGER_H_