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 NotifyFrameDetached(blink::WebFrame* frame); |
| 89 |
| 90 const std::string& extension_id() { return extension_id_; } |
| 91 |
| 92 private: |
| 93 struct PendingInjection; |
| 94 |
59 // Returns true if this ScriptInjection wants to run on the given |frame| at | 95 // 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 | 96 // the given |run_location| (i.e., if this script would inject either JS or |
61 // CSS). | 97 // CSS). |
62 bool WantsToRun(blink::WebFrame* frame, | 98 bool WantsToRun(blink::WebFrame* frame, |
63 UserScript::RunLocation run_location, | 99 UserScript::RunLocation run_location, |
64 const GURL& document_url) const; | 100 const GURL& document_url) const; |
65 | 101 |
| 102 // Returns true if the script will inject [css|js] at the given |
| 103 // |run_location|. |
| 104 bool ShouldInjectJS(UserScript::RunLocation run_location) const; |
| 105 bool ShouldInjectCSS(UserScript::RunLocation run_location) const; |
| 106 |
66 // Injects the script into the given |frame|, and updates |scripts_run_info| | 107 // Injects the script into the given |frame|, and updates |scripts_run_info| |
67 // information about the run. | 108 // information about the run. |
68 void Inject(blink::WebFrame* frame, | 109 void Inject(blink::WebFrame* frame, |
69 UserScript::RunLocation run_location, | 110 UserScript::RunLocation run_location, |
70 ScriptsRunInfo* scripts_run_info) const; | 111 ScriptsRunInfo* scripts_run_info) const; |
71 | 112 |
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 | 113 // Injects the [css|js] scripts into the frame, and stores the results of |
81 // the run in |scripts_run_info|. | 114 // the run in |scripts_run_info|. |
82 void InjectJS(blink::WebFrame* frame, ScriptsRunInfo* scripts_run_info) const; | 115 void InjectJS(blink::WebFrame* frame, ScriptsRunInfo* scripts_run_info) const; |
83 void InjectCSS(blink::WebFrame* frame, ScriptsRunInfo* scripts_run_info) | 116 void InjectCSS(blink::WebFrame* frame, ScriptsRunInfo* scripts_run_info) |
84 const; | 117 const; |
85 | 118 |
86 // The UserScript this is injecting. | 119 // The UserScript this is injecting. |
87 scoped_ptr<UserScript> script_; | 120 scoped_ptr<UserScript> script_; |
88 | 121 |
89 // The associated extension's id. This is a safe const&, since it is owned by | 122 // The associated extension's id. This is a safe const&, since it is owned by |
90 // the |user_script_|. | 123 // the |user_script_|. |
91 const std::string& extension_id_; | 124 const std::string& extension_id_; |
92 | 125 |
93 // The associated UserScriptSlave. | 126 // The associated UserScriptSlave. |
94 // It's unfortunate that this is needed, but we use it to get the isolated | 127 // It's unfortunate that this is needed, but we use it to get the isolated |
95 // world ids and the associated extensions. | 128 // world ids and the associated extensions. |
96 // TODO(rdevlin.cronin): It would be nice to clean this up more. | 129 // TODO(rdevlin.cronin): It would be nice to clean this up more. |
97 UserScriptSlave* user_script_slave_; | 130 UserScriptSlave* user_script_slave_; |
98 | 131 |
99 // True if the script is a standalone script or emulates greasemonkey. | 132 // True if the script is a standalone script or emulates greasemonkey. |
100 bool is_standalone_or_emulate_greasemonkey_; | 133 bool is_standalone_or_emulate_greasemonkey_; |
101 | 134 |
| 135 ScopedVector<PendingInjection> pending_injections_; |
| 136 |
102 DISALLOW_COPY_AND_ASSIGN(ScriptInjection); | 137 DISALLOW_COPY_AND_ASSIGN(ScriptInjection); |
103 }; | 138 }; |
104 | 139 |
105 } // namespace extensions | 140 } // namespace extensions |
106 | 141 |
107 #endif // EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ | 142 #endif // EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ |
OLD | NEW |