| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ | 5 #ifndef EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ |
| 6 #define EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ | 6 #define EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/basictypes.h" |
| 12 #include "base/macros.h" | 13 #include "base/macros.h" |
| 13 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/memory/scoped_vector.h" |
| 14 #include "base/timer/elapsed_timer.h" | 16 #include "base/timer/elapsed_timer.h" |
| 15 #include "extensions/common/user_script.h" | 17 #include "extensions/common/user_script.h" |
| 16 | 18 |
| 17 class GURL; | 19 class GURL; |
| 18 | 20 |
| 19 namespace blink { | 21 namespace blink { |
| 20 class WebFrame; | 22 class WebFrame; |
| 21 } | 23 } |
| 22 | 24 |
| 25 namespace content { |
| 26 class RenderView; |
| 27 } |
| 28 |
| 23 namespace extensions { | 29 namespace extensions { |
| 24 class UserScriptSlave; | 30 class UserScriptSlave; |
| 25 | 31 |
| 26 // This class is a wrapper around a UserScript that knows how to inject itself | 32 // This class is a wrapper around a UserScript that knows how to inject itself |
| 27 // into a frame. | 33 // into a frame. |
| 28 class ScriptInjection { | 34 class ScriptInjection { |
| 29 public: | 35 public: |
| 30 // Map of extensions IDs to the executing script paths. | 36 // Map of extensions IDs to the executing script paths. |
| 31 typedef std::map<std::string, std::set<std::string> > ExecutingScriptsMap; | 37 typedef std::map<std::string, std::set<std::string> > ExecutingScriptsMap; |
| 32 | 38 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 49 }; | 55 }; |
| 50 | 56 |
| 51 // Return the URL to use as the document url when checking permissions for | 57 // Return the URL to use as the document url when checking permissions for |
| 52 // script injection. | 58 // script injection. |
| 53 static GURL GetDocumentUrlForFrame(blink::WebFrame* frame); | 59 static GURL GetDocumentUrlForFrame(blink::WebFrame* frame); |
| 54 | 60 |
| 55 ScriptInjection(scoped_ptr<UserScript> script, | 61 ScriptInjection(scoped_ptr<UserScript> script, |
| 56 UserScriptSlave* user_script_slave); | 62 UserScriptSlave* user_script_slave); |
| 57 ~ScriptInjection(); | 63 ~ScriptInjection(); |
| 58 | 64 |
| 65 // Inject the script into the given |frame| if the script should run on the |
| 66 // frame and has permission to do so. If the script requires user consent, |
| 67 // this will register a pending request to inject at a later time. |
| 68 // If the script is run immediately, |scripts_run_info| is updated with |
| 69 // information about the run. |
| 70 void InjectIfAllowed(blink::WebFrame* frame, |
| 71 UserScript::RunLocation location, |
| 72 const GURL& document_url, |
| 73 ScriptsRunInfo* scripts_run_info); |
| 74 |
| 75 // If a request with the given |request_id| exists, runs that request and |
| 76 // modifies |scripts_run_info| with information about the run. Otherwise, does |
| 77 // nothing. |
| 78 // If |frame_out| is non-NULL and a script was run, |frame_out| will be |
| 79 // populated with the frame in which the script was run. |
| 80 // Returns true if the request was found *and* the script was run. |
| 81 bool NotifyScriptPermitted(int64 request_id, |
| 82 content::RenderView* render_view, |
| 83 ScriptsRunInfo* scripts_run_info, |
| 84 blink::WebFrame** frame_out); |
| 85 |
| 86 // Notififies the Injection that the frame has been detached (i.e. is about |
| 87 // to be destroyed). |
| 88 void FrameDetached(blink::WebFrame* frame); |
| 89 |
| 90 void SetScript(scoped_ptr<UserScript> script); |
| 91 |
| 92 const std::string& extension_id() { return extension_id_; } |
| 93 const UserScript* script() { return script_.get(); } |
| 94 |
| 95 private: |
| 96 struct PendingInjection; |
| 97 |
| 59 // Returns true if this ScriptInjection wants to run on the given |frame| at | 98 // 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 | 99 // the given |run_location| (i.e., if this script would inject either JS or |
| 61 // CSS). | 100 // CSS). |
| 62 bool WantsToRun(blink::WebFrame* frame, | 101 bool WantsToRun(blink::WebFrame* frame, |
| 63 UserScript::RunLocation run_location, | 102 UserScript::RunLocation run_location, |
| 64 const GURL& document_url) const; | 103 const GURL& document_url) const; |
| 65 | 104 |
| 105 // Returns true if the script will inject [css|js] at the given |
| 106 // |run_location|. |
| 107 bool ShouldInjectJS(UserScript::RunLocation run_location) const; |
| 108 bool ShouldInjectCSS(UserScript::RunLocation run_location) const; |
| 109 |
| 66 // Injects the script into the given |frame|, and updates |scripts_run_info| | 110 // Injects the script into the given |frame|, and updates |scripts_run_info| |
| 67 // information about the run. | 111 // information about the run. |
| 68 void Inject(blink::WebFrame* frame, | 112 void Inject(blink::WebFrame* frame, |
| 69 UserScript::RunLocation run_location, | 113 UserScript::RunLocation run_location, |
| 70 ScriptsRunInfo* scripts_run_info) const; | 114 ScriptsRunInfo* scripts_run_info) const; |
| 71 | 115 |
| 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 | 116 // Injects the [css|js] scripts into the frame, and stores the results of |
| 81 // the run in |scripts_run_info|. | 117 // the run in |scripts_run_info|. |
| 82 void InjectJS(blink::WebFrame* frame, ScriptsRunInfo* scripts_run_info) const; | 118 void InjectJS(blink::WebFrame* frame, ScriptsRunInfo* scripts_run_info) const; |
| 83 void InjectCSS(blink::WebFrame* frame, ScriptsRunInfo* scripts_run_info) | 119 void InjectCSS(blink::WebFrame* frame, ScriptsRunInfo* scripts_run_info) |
| 84 const; | 120 const; |
| 85 | 121 |
| 86 // The UserScript this is injecting. | 122 // The UserScript this is injecting. |
| 87 scoped_ptr<UserScript> script_; | 123 scoped_ptr<UserScript> script_; |
| 88 | 124 |
| 89 // The associated extension's id. This is a safe const&, since it is owned by | 125 // The associated extension's id. This is a safe const&, since it is owned by |
| 90 // the |user_script_|. | 126 // the |user_script_|. |
| 91 const std::string& extension_id_; | 127 const std::string& extension_id_; |
| 92 | 128 |
| 93 // The associated UserScriptSlave. | 129 // The associated UserScriptSlave. |
| 94 // It's unfortunate that this is needed, but we use it to get the isolated | 130 // It's unfortunate that this is needed, but we use it to get the isolated |
| 95 // world ids and the associated extensions. | 131 // world ids and the associated extensions. |
| 96 // TODO(rdevlin.cronin): It would be nice to clean this up more. | 132 // TODO(rdevlin.cronin): It would be nice to clean this up more. |
| 97 UserScriptSlave* user_script_slave_; | 133 UserScriptSlave* user_script_slave_; |
| 98 | 134 |
| 99 // True if the script is a standalone script or emulates greasemonkey. | 135 // True if the script is a standalone script or emulates greasemonkey. |
| 100 bool is_standalone_or_emulate_greasemonkey_; | 136 bool is_standalone_or_emulate_greasemonkey_; |
| 101 | 137 |
| 138 ScopedVector<PendingInjection> pending_injections_; |
| 139 |
| 102 DISALLOW_COPY_AND_ASSIGN(ScriptInjection); | 140 DISALLOW_COPY_AND_ASSIGN(ScriptInjection); |
| 103 }; | 141 }; |
| 104 | 142 |
| 105 } // namespace extensions | 143 } // namespace extensions |
| 106 | 144 |
| 107 #endif // EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ | 145 #endif // EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ |
| OLD | NEW |