user_script_set.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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_H_
  5. #define EXTENSIONS_RENDERER_USER_SCRIPT_SET_H_
  6. #include <map>
  7. #include <memory>
  8. #include <set>
  9. #include <string>
  10. #include <vector>
  11. #include "base/memory/read_only_shared_memory_region.h"
  12. #include "base/observer_list.h"
  13. #include "extensions/common/mojom/host_id.mojom-forward.h"
  14. #include "extensions/common/mojom/run_location.mojom-shared.h"
  15. #include "extensions/common/user_script.h"
  16. #include "third_party/blink/public/platform/web_string.h"
  17. class GURL;
  18. namespace content {
  19. class RenderFrame;
  20. }
  21. namespace extensions {
  22. class ScriptInjection;
  23. // The UserScriptSet is a collection of UserScripts which knows how to update
  24. // itself from SharedMemory and create ScriptInjections for UserScripts to
  25. // inject on a page.
  26. class UserScriptSet {
  27. public:
  28. class Observer {
  29. public:
  30. // Called when the set of user scripts is updated, which invalidates all
  31. // previous script objects from this UserScriptSet.
  32. virtual void OnUserScriptsUpdated() = 0;
  33. // Called when this UserScriptSet is destroyed.
  34. virtual void OnUserScriptSetDestroyed() = 0;
  35. };
  36. explicit UserScriptSet(mojom::HostID host_id);
  37. UserScriptSet(const UserScriptSet&) = delete;
  38. UserScriptSet& operator=(const UserScriptSet&) = delete;
  39. ~UserScriptSet();
  40. // Adds or removes observers.
  41. void AddObserver(Observer* observer);
  42. void RemoveObserver(Observer* observer);
  43. // Append any ScriptInjections that should run on the given |render_frame| and
  44. // |tab_id|, at the given |run_location|, to |injections|.
  45. // |extensions| is passed in to verify the corresponding extension is still
  46. // valid.
  47. void GetInjections(std::vector<std::unique_ptr<ScriptInjection>>* injections,
  48. content::RenderFrame* render_frame,
  49. int tab_id,
  50. mojom::RunLocation run_location,
  51. bool log_activity);
  52. std::unique_ptr<ScriptInjection> GetDeclarativeScriptInjection(
  53. const std::string& script_id,
  54. content::RenderFrame* render_frame,
  55. int tab_id,
  56. mojom::RunLocation run_location,
  57. const GURL& document_url,
  58. bool log_activity);
  59. // Updates scripts given the shared memory region containing user scripts.
  60. // Returns true if the scripts were successfully updated.
  61. bool UpdateUserScripts(base::ReadOnlySharedMemoryRegion shared_memory);
  62. bool HasScripts() const { return !scripts_.empty(); }
  63. // Clears all user scripts managed by this set and notifies observers.
  64. void ClearUserScripts();
  65. // Returns the contents of a script file.
  66. // Note that copying is cheap as this uses WebString.
  67. blink::WebString GetJsSource(const UserScript::File& file,
  68. bool emulate_greasemonkey);
  69. blink::WebString GetCssSource(const UserScript::File& file);
  70. private:
  71. // Returns a new ScriptInjection for the given |script| to execute in the
  72. // |render_frame|, or NULL if the script should not execute.
  73. std::unique_ptr<ScriptInjection> GetInjectionForScript(
  74. const UserScript* script,
  75. content::RenderFrame* render_frame,
  76. int tab_id,
  77. mojom::RunLocation run_location,
  78. const GURL& document_url,
  79. bool is_declarative,
  80. bool log_activity);
  81. // Shared memory mapping containing raw script data.
  82. base::ReadOnlySharedMemoryMapping shared_memory_mapping_;
  83. // The UserScripts this injector manages.
  84. UserScriptList scripts_;
  85. // Map of user script file url -> source.
  86. std::map<GURL, blink::WebString> script_sources_;
  87. // The HostID which |scripts_| is associated with.
  88. mojom::HostID host_id_;
  89. // The associated observers.
  90. base::ObserverList<Observer>::Unchecked observers_;
  91. };
  92. } // namespace extensions
  93. #endif // EXTENSIONS_RENDERER_USER_SCRIPT_SET_H_