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 #include "extensions/renderer/user_script_set.h" | 5 #include "extensions/renderer/user_script_set.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 std::set<std::string>* ids) const { | 60 std::set<std::string>* ids) const { |
61 for (const UserScript* script : scripts_) { | 61 for (const UserScript* script : scripts_) { |
62 if (script->host_id().type() != HostID::EXTENSIONS) | 62 if (script->host_id().type() != HostID::EXTENSIONS) |
63 continue; | 63 continue; |
64 DCHECK(!script->extension_id().empty()); | 64 DCHECK(!script->extension_id().empty()); |
65 ids->insert(script->extension_id()); | 65 ids->insert(script->extension_id()); |
66 } | 66 } |
67 } | 67 } |
68 | 68 |
69 void UserScriptSet::GetInjections( | 69 void UserScriptSet::GetInjections( |
70 std::vector<scoped_ptr<ScriptInjection>>* injections, | 70 std::vector<std::unique_ptr<ScriptInjection>>* injections, |
71 content::RenderFrame* render_frame, | 71 content::RenderFrame* render_frame, |
72 int tab_id, | 72 int tab_id, |
73 UserScript::RunLocation run_location) { | 73 UserScript::RunLocation run_location) { |
74 GURL document_url = GetDocumentUrlForFrame(render_frame->GetWebFrame()); | 74 GURL document_url = GetDocumentUrlForFrame(render_frame->GetWebFrame()); |
75 for (const UserScript* script : scripts_) { | 75 for (const UserScript* script : scripts_) { |
76 scoped_ptr<ScriptInjection> injection = GetInjectionForScript( | 76 std::unique_ptr<ScriptInjection> injection = |
77 script, | 77 GetInjectionForScript(script, render_frame, tab_id, run_location, |
78 render_frame, | 78 document_url, false /* is_declarative */); |
79 tab_id, | |
80 run_location, | |
81 document_url, | |
82 false /* is_declarative */); | |
83 if (injection.get()) | 79 if (injection.get()) |
84 injections->push_back(std::move(injection)); | 80 injections->push_back(std::move(injection)); |
85 } | 81 } |
86 } | 82 } |
87 | 83 |
88 bool UserScriptSet::UpdateUserScripts(base::SharedMemoryHandle shared_memory, | 84 bool UserScriptSet::UpdateUserScripts(base::SharedMemoryHandle shared_memory, |
89 const std::set<HostID>& changed_hosts, | 85 const std::set<HostID>& changed_hosts, |
90 bool whitelisted_only) { | 86 bool whitelisted_only) { |
91 bool only_inject_incognito = | 87 bool only_inject_incognito = |
92 ExtensionsRendererClient::Get()->IsIncognitoProcess(); | 88 ExtensionsRendererClient::Get()->IsIncognitoProcess(); |
(...skipping 18 matching lines...) Expand all Loading... |
111 // Unpickle scripts. | 107 // Unpickle scripts. |
112 uint32_t num_scripts = 0; | 108 uint32_t num_scripts = 0; |
113 base::Pickle pickle(reinterpret_cast<char*>(shared_memory_->memory()), | 109 base::Pickle pickle(reinterpret_cast<char*>(shared_memory_->memory()), |
114 pickle_size); | 110 pickle_size); |
115 base::PickleIterator iter(pickle); | 111 base::PickleIterator iter(pickle); |
116 CHECK(iter.ReadUInt32(&num_scripts)); | 112 CHECK(iter.ReadUInt32(&num_scripts)); |
117 | 113 |
118 scripts_.clear(); | 114 scripts_.clear(); |
119 scripts_.reserve(num_scripts); | 115 scripts_.reserve(num_scripts); |
120 for (uint32_t i = 0; i < num_scripts; ++i) { | 116 for (uint32_t i = 0; i < num_scripts; ++i) { |
121 scoped_ptr<UserScript> script(new UserScript()); | 117 std::unique_ptr<UserScript> script(new UserScript()); |
122 script->Unpickle(pickle, &iter); | 118 script->Unpickle(pickle, &iter); |
123 | 119 |
124 // Note that this is a pointer into shared memory. We don't own it. It gets | 120 // Note that this is a pointer into shared memory. We don't own it. It gets |
125 // cleared up when the last renderer or browser process drops their | 121 // cleared up when the last renderer or browser process drops their |
126 // reference to the shared memory. | 122 // reference to the shared memory. |
127 for (size_t j = 0; j < script->js_scripts().size(); ++j) { | 123 for (size_t j = 0; j < script->js_scripts().size(); ++j) { |
128 const char* body = NULL; | 124 const char* body = NULL; |
129 int body_length = 0; | 125 int body_length = 0; |
130 CHECK(iter.ReadData(&body, &body_length)); | 126 CHECK(iter.ReadData(&body, &body_length)); |
131 script->js_scripts()[j].set_external_content( | 127 script->js_scripts()[j].set_external_content( |
(...skipping 20 matching lines...) Expand all Loading... |
152 | 148 |
153 scripts_.push_back(std::move(script)); | 149 scripts_.push_back(std::move(script)); |
154 } | 150 } |
155 | 151 |
156 FOR_EACH_OBSERVER(Observer, | 152 FOR_EACH_OBSERVER(Observer, |
157 observers_, | 153 observers_, |
158 OnUserScriptsUpdated(changed_hosts, scripts_.get())); | 154 OnUserScriptsUpdated(changed_hosts, scripts_.get())); |
159 return true; | 155 return true; |
160 } | 156 } |
161 | 157 |
162 scoped_ptr<ScriptInjection> UserScriptSet::GetDeclarativeScriptInjection( | 158 std::unique_ptr<ScriptInjection> UserScriptSet::GetDeclarativeScriptInjection( |
163 int script_id, | 159 int script_id, |
164 content::RenderFrame* render_frame, | 160 content::RenderFrame* render_frame, |
165 int tab_id, | 161 int tab_id, |
166 UserScript::RunLocation run_location, | 162 UserScript::RunLocation run_location, |
167 const GURL& document_url) { | 163 const GURL& document_url) { |
168 for (const UserScript* script : scripts_) { | 164 for (const UserScript* script : scripts_) { |
169 if (script->id() == script_id) { | 165 if (script->id() == script_id) { |
170 return GetInjectionForScript(script, | 166 return GetInjectionForScript(script, |
171 render_frame, | 167 render_frame, |
172 tab_id, | 168 tab_id, |
173 run_location, | 169 run_location, |
174 document_url, | 170 document_url, |
175 true /* is_declarative */); | 171 true /* is_declarative */); |
176 } | 172 } |
177 } | 173 } |
178 return scoped_ptr<ScriptInjection>(); | 174 return std::unique_ptr<ScriptInjection>(); |
179 } | 175 } |
180 | 176 |
181 scoped_ptr<ScriptInjection> UserScriptSet::GetInjectionForScript( | 177 std::unique_ptr<ScriptInjection> UserScriptSet::GetInjectionForScript( |
182 const UserScript* script, | 178 const UserScript* script, |
183 content::RenderFrame* render_frame, | 179 content::RenderFrame* render_frame, |
184 int tab_id, | 180 int tab_id, |
185 UserScript::RunLocation run_location, | 181 UserScript::RunLocation run_location, |
186 const GURL& document_url, | 182 const GURL& document_url, |
187 bool is_declarative) { | 183 bool is_declarative) { |
188 scoped_ptr<ScriptInjection> injection; | 184 std::unique_ptr<ScriptInjection> injection; |
189 scoped_ptr<const InjectionHost> injection_host; | 185 std::unique_ptr<const InjectionHost> injection_host; |
190 blink::WebLocalFrame* web_frame = render_frame->GetWebFrame(); | 186 blink::WebLocalFrame* web_frame = render_frame->GetWebFrame(); |
191 | 187 |
192 const HostID& host_id = script->host_id(); | 188 const HostID& host_id = script->host_id(); |
193 if (host_id.type() == HostID::EXTENSIONS) { | 189 if (host_id.type() == HostID::EXTENSIONS) { |
194 injection_host = ExtensionInjectionHost::Create(host_id.id()); | 190 injection_host = ExtensionInjectionHost::Create(host_id.id()); |
195 if (!injection_host) | 191 if (!injection_host) |
196 return injection; | 192 return injection; |
197 } else { | 193 } else { |
198 DCHECK_EQ(host_id.type(), HostID::WEBUI); | 194 DCHECK_EQ(host_id.type(), HostID::WEBUI); |
199 injection_host.reset(new WebUIInjectionHost(host_id)); | 195 injection_host.reset(new WebUIInjectionHost(host_id)); |
200 } | 196 } |
201 | 197 |
202 if (web_frame->parent() && !script->match_all_frames()) | 198 if (web_frame->parent() && !script->match_all_frames()) |
203 return injection; // Only match subframes if the script declared it. | 199 return injection; // Only match subframes if the script declared it. |
204 | 200 |
205 GURL effective_document_url = ScriptContext::GetEffectiveDocumentURL( | 201 GURL effective_document_url = ScriptContext::GetEffectiveDocumentURL( |
206 web_frame, document_url, script->match_about_blank()); | 202 web_frame, document_url, script->match_about_blank()); |
207 | 203 |
208 if (!script->MatchesURL(effective_document_url)) | 204 if (!script->MatchesURL(effective_document_url)) |
209 return injection; | 205 return injection; |
210 | 206 |
211 scoped_ptr<ScriptInjector> injector(new UserScriptInjector(script, | 207 std::unique_ptr<ScriptInjector> injector( |
212 this, | 208 new UserScriptInjector(script, this, is_declarative)); |
213 is_declarative)); | |
214 | 209 |
215 if (injector->CanExecuteOnFrame( | 210 if (injector->CanExecuteOnFrame( |
216 injection_host.get(), | 211 injection_host.get(), |
217 web_frame, | 212 web_frame, |
218 tab_id) == | 213 tab_id) == |
219 PermissionsData::ACCESS_DENIED) { | 214 PermissionsData::ACCESS_DENIED) { |
220 return injection; | 215 return injection; |
221 } | 216 } |
222 | 217 |
223 bool inject_css = !script->css_scripts().empty() && | 218 bool inject_css = !script->css_scripts().empty() && |
224 run_location == UserScript::DOCUMENT_START; | 219 run_location == UserScript::DOCUMENT_START; |
225 bool inject_js = | 220 bool inject_js = |
226 !script->js_scripts().empty() && script->run_location() == run_location; | 221 !script->js_scripts().empty() && script->run_location() == run_location; |
227 if (inject_css || inject_js) { | 222 if (inject_css || inject_js) { |
228 injection.reset(new ScriptInjection(std::move(injector), render_frame, | 223 injection.reset(new ScriptInjection(std::move(injector), render_frame, |
229 std::move(injection_host), | 224 std::move(injection_host), |
230 run_location)); | 225 run_location)); |
231 } | 226 } |
232 return injection; | 227 return injection; |
233 } | 228 } |
234 | 229 |
235 } // namespace extensions | 230 } // namespace extensions |
OLD | NEW |