OLD | NEW |
(Empty) | |
| 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 |
| 5 #ifndef EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ |
| 6 #define EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <set> |
| 10 #include <string> |
| 11 |
| 12 #include "base/macros.h" |
| 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/timer/elapsed_timer.h" |
| 15 #include "extensions/common/user_script.h" |
| 16 |
| 17 class GURL; |
| 18 |
| 19 namespace blink { |
| 20 class WebFrame; |
| 21 } |
| 22 |
| 23 namespace extensions { |
| 24 class UserScriptSlave; |
| 25 |
| 26 // This class is a wrapper around a UserScript that knows how to inject itself |
| 27 // into a frame. |
| 28 class ScriptInjection { |
| 29 public: |
| 30 // Map of extensions IDs to the executing script paths. |
| 31 typedef std::map<std::string, std::set<std::string> > ExecutingScriptsMap; |
| 32 |
| 33 // A struct containing information about a script run. |
| 34 struct ScriptsRunInfo { |
| 35 ScriptsRunInfo(); |
| 36 ~ScriptsRunInfo(); |
| 37 |
| 38 // The number of CSS scripts injected. |
| 39 size_t num_css; |
| 40 // The number of JS scripts injected. |
| 41 size_t num_js; |
| 42 // A map of extension ids to executing script paths. |
| 43 ExecutingScriptsMap executing_scripts; |
| 44 // The elapsed time since the ScriptsRunInfo was constructed. |
| 45 base::ElapsedTimer timer; |
| 46 |
| 47 private: |
| 48 DISALLOW_COPY_AND_ASSIGN(ScriptsRunInfo); |
| 49 }; |
| 50 |
| 51 // Return the URL to use as the document url when checking permissions for |
| 52 // script injection. |
| 53 static GURL GetDocumentUrlForFrame(blink::WebFrame* frame); |
| 54 |
| 55 ScriptInjection(scoped_ptr<UserScript> script, |
| 56 UserScriptSlave* user_script_slave); |
| 57 ~ScriptInjection(); |
| 58 |
| 59 // Returns true if this ScriptInjection wants to run on the given |frame| at |
| 60 // the given |run_location| (i.e., if this script would inject either JS or |
| 61 // CSS). |
| 62 bool WantsToRun(blink::WebFrame* frame, |
| 63 UserScript::RunLocation run_location, |
| 64 const GURL& document_url) const; |
| 65 |
| 66 // Injects the script into the given |frame|, and updates |scripts_run_info| |
| 67 // information about the run. |
| 68 void Inject(blink::WebFrame* frame, |
| 69 UserScript::RunLocation run_location, |
| 70 ScriptsRunInfo* scripts_run_info) const; |
| 71 |
| 72 const std::string& extension_id() { return extension_id_; } |
| 73 |
| 74 private: |
| 75 // Returns true if the script will inject [css|js] at the given |
| 76 // |run_location|. |
| 77 bool ShouldInjectJS(UserScript::RunLocation run_location) const; |
| 78 bool ShouldInjectCSS(UserScript::RunLocation run_location) const; |
| 79 |
| 80 // Injects the [css|js] scripts into the frame, and stores the results of |
| 81 // the run in |scripts_run_info|. |
| 82 void InjectJS(blink::WebFrame* frame, ScriptsRunInfo* scripts_run_info) const; |
| 83 void InjectCSS(blink::WebFrame* frame, ScriptsRunInfo* scripts_run_info) |
| 84 const; |
| 85 |
| 86 // The UserScript this is injecting. |
| 87 scoped_ptr<UserScript> script_; |
| 88 |
| 89 // The associated extension's id. This is a safe const&, since it is owned by |
| 90 // the |user_script_|. |
| 91 const std::string& extension_id_; |
| 92 |
| 93 // The associated UserScriptSlave. |
| 94 // It's unfortunate that this is needed, but we use it to get the isolated |
| 95 // world ids and the associated extensions. |
| 96 // TODO(rdevlin.cronin): It would be nice to clean this up more. |
| 97 UserScriptSlave* user_script_slave_; |
| 98 |
| 99 // True if the script is a standalone script or emulates greasemonkey. |
| 100 bool is_standalone_or_emulate_greasemonkey_; |
| 101 |
| 102 DISALLOW_COPY_AND_ASSIGN(ScriptInjection); |
| 103 }; |
| 104 |
| 105 } // namespace extensions |
| 106 |
| 107 #endif // EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ |
OLD | NEW |