[email protected] | bcd9580f | 2014-04-17 19:17:59 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | bcd9580f | 2014-04-17 19:17:59 | [diff] [blame] | 5 | #include "extensions/renderer/script_context_set.h" |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 6 | |
[email protected] | 6827510 | 2013-07-17 23:54:20 | [diff] [blame] | 7 | #include "base/message_loop/message_loop.h" |
kalman | 745b662 | 2015-03-17 21:07:20 | [diff] [blame] | 8 | #include "content/public/common/url_constants.h" |
kozyatinskiy | 441ece5 | 2015-04-01 23:46:32 | [diff] [blame] | 9 | #include "content/public/renderer/render_frame.h" |
| 10 | #include "content/public/renderer/render_frame_observer.h" |
[email protected] | a2ef54c | 2011-10-10 16:20:31 | [diff] [blame] | 11 | #include "content/public/renderer/render_view.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 12 | #include "extensions/common/extension.h" |
kalman | 745b662 | 2015-03-17 21:07:20 | [diff] [blame] | 13 | #include "extensions/renderer/extension_groups.h" |
[email protected] | bcd9580f | 2014-04-17 19:17:59 | [diff] [blame] | 14 | #include "extensions/renderer/script_context.h" |
kalman | 745b662 | 2015-03-17 21:07:20 | [diff] [blame] | 15 | #include "extensions/renderer/script_injection.h" |
kozyatinskiy | 441ece5 | 2015-04-01 23:46:32 | [diff] [blame] | 16 | #include "third_party/WebKit/public/platform/WebSuspendableTask.h" |
kalman | 745b662 | 2015-03-17 21:07:20 | [diff] [blame] | 17 | #include "third_party/WebKit/public/web/WebDocument.h" |
| 18 | #include "third_party/WebKit/public/web/WebLocalFrame.h" |
[email protected] | 885c0e9 | 2012-11-13 20:27:42 | [diff] [blame] | 19 | #include "v8/include/v8.h" |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 20 | |
[email protected] | 8fe74bf | 2012-08-07 21:08:42 | [diff] [blame] | 21 | namespace extensions { |
| 22 | |
kozyatinskiy | 441ece5 | 2015-04-01 23:46:32 | [diff] [blame] | 23 | namespace { |
| 24 | |
| 25 | // This class inherits content::RenderFrameObserver for tracking when render |
| 26 | // frame goes away. |
| 27 | class BlinkTaskRunner : public blink::WebSuspendableTask, |
| 28 | public content::RenderFrameObserver { |
| 29 | public: |
| 30 | BlinkTaskRunner(content::RenderFrame* render_frame, |
| 31 | ScriptContext* context, |
| 32 | const base::Callback<void(ScriptContext*)>& callback) |
| 33 | : RenderFrameObserver(render_frame), |
| 34 | context_(context), |
| 35 | callback_(callback) {} |
| 36 | |
| 37 | void run() override { |
| 38 | if (render_frame() && context_->is_valid()) |
| 39 | callback_.Run(context_); |
| 40 | } |
| 41 | |
| 42 | private: |
| 43 | ScriptContext* context_; |
| 44 | base::Callback<void(ScriptContext*)> callback_; |
| 45 | |
| 46 | // Overriden to avoid being destroyed when RenderFrame goes away. |
| 47 | // BlinkTaskRunner objects are owned by WebLocalFrame. |
| 48 | void OnDestruct() override {} |
| 49 | |
| 50 | DISALLOW_COPY_AND_ASSIGN(BlinkTaskRunner); |
| 51 | }; |
| 52 | |
kozyatinskiy | a08a310 | 2015-04-03 17:50:03 | [diff] [blame] | 53 | // This class deletes ScriptContext on run or on contextDestoyed. |
| 54 | // It allows us to pass ScriptContext as raw pointer to BlinkTaskRunner. |
| 55 | // In ScriptContextSet::Remove method we mark the context as invalid, but |
| 56 | // don't delete the object until all scheduled tasks are finished. |
| 57 | class ScriptContextDeleter : public blink::WebSuspendableTask { |
| 58 | public: |
| 59 | explicit ScriptContextDeleter(ScriptContext* context) : context_(context) {} |
| 60 | |
| 61 | void run() override { delete context_; } |
| 62 | void contextDestroyed() override { delete context_; } |
| 63 | |
| 64 | private: |
| 65 | ScriptContext* context_; |
| 66 | |
| 67 | DISALLOW_COPY_AND_ASSIGN(ScriptContextDeleter); |
| 68 | }; |
| 69 | |
kozyatinskiy | 441ece5 | 2015-04-01 23:46:32 | [diff] [blame] | 70 | } // namespace |
| 71 | |
kalman | 745b662 | 2015-03-17 21:07:20 | [diff] [blame] | 72 | ScriptContextSet::ScriptContextSet(ExtensionSet* extensions, |
| 73 | ExtensionIdSet* active_extension_ids) |
| 74 | : extensions_(extensions), active_extension_ids_(active_extension_ids) { |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 75 | } |
kalman | 745b662 | 2015-03-17 21:07:20 | [diff] [blame] | 76 | |
[email protected] | bcd9580f | 2014-04-17 19:17:59 | [diff] [blame] | 77 | ScriptContextSet::~ScriptContextSet() { |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 78 | } |
| 79 | |
kalman | 745b662 | 2015-03-17 21:07:20 | [diff] [blame] | 80 | ScriptContext* ScriptContextSet::Register( |
| 81 | blink::WebLocalFrame* frame, |
| 82 | const v8::Handle<v8::Context>& v8_context, |
| 83 | int extension_group, |
| 84 | int world_id) { |
| 85 | const Extension* extension = |
| 86 | GetExtensionFromFrameAndWorld(frame, world_id, false); |
| 87 | const Extension* effective_extension = |
| 88 | GetExtensionFromFrameAndWorld(frame, world_id, true); |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 89 | |
kalman | 745b662 | 2015-03-17 21:07:20 | [diff] [blame] | 90 | GURL frame_url = ScriptContext::GetDataSourceURLForFrame(frame); |
| 91 | Feature::Context context_type = |
| 92 | ClassifyJavaScriptContext(extension, extension_group, frame_url, |
| 93 | frame->document().securityOrigin()); |
| 94 | Feature::Context effective_context_type = ClassifyJavaScriptContext( |
| 95 | effective_extension, extension_group, |
| 96 | ScriptContext::GetEffectiveDocumentURL(frame, frame_url, true), |
| 97 | frame->document().securityOrigin()); |
| 98 | |
| 99 | ScriptContext* context = |
| 100 | new ScriptContext(v8_context, frame, extension, context_type, |
| 101 | effective_extension, effective_context_type); |
| 102 | contexts_.insert(context); // takes ownership |
| 103 | return context; |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 104 | } |
| 105 | |
[email protected] | bcd9580f | 2014-04-17 19:17:59 | [diff] [blame] | 106 | void ScriptContextSet::Remove(ScriptContext* context) { |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 107 | if (contexts_.erase(context)) { |
kozyatinskiy | a08a310 | 2015-04-03 17:50:03 | [diff] [blame] | 108 | content::RenderFrame* context_render_frame = context->GetRenderFrame(); |
| 109 | blink::WebLocalFrame* web_local_frame = |
| 110 | context_render_frame ? context_render_frame->GetWebFrame() : nullptr; |
| 111 | |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 112 | context->Invalidate(); |
kozyatinskiy | a08a310 | 2015-04-03 17:50:03 | [diff] [blame] | 113 | |
| 114 | if (!web_local_frame) |
| 115 | base::MessageLoop::current()->DeleteSoon(FROM_HERE, context); |
| 116 | else |
| 117 | web_local_frame->requestRunTask(new ScriptContextDeleter(context)); |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
[email protected] | bcd9580f | 2014-04-17 19:17:59 | [diff] [blame] | 121 | ScriptContext* ScriptContextSet::GetCurrent() const { |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 122 | v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 123 | return isolate->InContext() ? GetByV8Context(isolate->GetCurrentContext()) |
kalman | 745b662 | 2015-03-17 21:07:20 | [diff] [blame] | 124 | : nullptr; |
[email protected] | ad6aa8f9 | 2013-06-22 15:34:16 | [diff] [blame] | 125 | } |
| 126 | |
[email protected] | bcd9580f | 2014-04-17 19:17:59 | [diff] [blame] | 127 | ScriptContext* ScriptContextSet::GetCalling() const { |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 128 | v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 129 | v8::Local<v8::Context> calling = isolate->GetCallingContext(); |
kalman | 745b662 | 2015-03-17 21:07:20 | [diff] [blame] | 130 | return calling.IsEmpty() ? nullptr : GetByV8Context(calling); |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 131 | } |
| 132 | |
[email protected] | bcd9580f | 2014-04-17 19:17:59 | [diff] [blame] | 133 | ScriptContext* ScriptContextSet::GetByV8Context( |
kalman | 745b662 | 2015-03-17 21:07:20 | [diff] [blame] | 134 | const v8::Handle<v8::Context>& v8_context) const { |
| 135 | for (ScriptContext* script_context : contexts_) { |
| 136 | if (script_context->v8_context() == v8_context) |
| 137 | return script_context; |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 138 | } |
kalman | 745b662 | 2015-03-17 21:07:20 | [diff] [blame] | 139 | return nullptr; |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 140 | } |
| 141 | |
[email protected] | bcd9580f | 2014-04-17 19:17:59 | [diff] [blame] | 142 | void ScriptContextSet::ForEach( |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 143 | const std::string& extension_id, |
kozyatinskiy | 441ece5 | 2015-04-01 23:46:32 | [diff] [blame] | 144 | content::RenderView* restrict_to_render_view, |
[email protected] | bcd9580f | 2014-04-17 19:17:59 | [diff] [blame] | 145 | const base::Callback<void(ScriptContext*)>& callback) const { |
kozyatinskiy | 441ece5 | 2015-04-01 23:46:32 | [diff] [blame] | 146 | ForEachImpl(extension_id, restrict_to_render_view, callback, false); |
| 147 | } |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 148 | |
kozyatinskiy | 441ece5 | 2015-04-01 23:46:32 | [diff] [blame] | 149 | void ScriptContextSet::RequestRunForEach( |
| 150 | const std::string& extension_id, |
| 151 | content::RenderFrame* restrict_to_render_frame, |
| 152 | const base::Callback<void(ScriptContext*)>& callback) const { |
| 153 | content::RenderView* restrict_to_render_view = |
| 154 | restrict_to_render_frame ? restrict_to_render_frame->GetRenderView() |
| 155 | : NULL; |
| 156 | ForEachImpl(extension_id, restrict_to_render_view, callback, true); |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 157 | } |
[email protected] | 8fe74bf | 2012-08-07 21:08:42 | [diff] [blame] | 158 | |
kalman | 745b662 | 2015-03-17 21:07:20 | [diff] [blame] | 159 | std::set<ScriptContext*> ScriptContextSet::OnExtensionUnloaded( |
[email protected] | 280055f | 2013-04-23 00:50:47 | [diff] [blame] | 160 | const std::string& extension_id) { |
kalman | 745b662 | 2015-03-17 21:07:20 | [diff] [blame] | 161 | std::set<ScriptContext*> removed; |
| 162 | ForEach(extension_id, |
| 163 | base::Bind(&ScriptContextSet::DispatchOnUnloadEventAndRemove, |
| 164 | base::Unretained(this), &removed)); |
| 165 | return removed; |
| 166 | } |
kalman | 77b0411 | 2015-03-16 21:20:44 | [diff] [blame] | 167 | |
kalman | 745b662 | 2015-03-17 21:07:20 | [diff] [blame] | 168 | const Extension* ScriptContextSet::GetExtensionFromFrameAndWorld( |
| 169 | const blink::WebLocalFrame* frame, |
| 170 | int world_id, |
| 171 | bool use_effective_url) { |
| 172 | std::string extension_id; |
| 173 | if (world_id != 0) { |
| 174 | // Isolated worlds (content script). |
| 175 | extension_id = ScriptInjection::GetHostIdForIsolatedWorld(world_id); |
| 176 | } else if (!frame->document().securityOrigin().isUnique()) { |
| 177 | // TODO(kalman): Delete the above check. |
| 178 | // Extension pages (chrome-extension:// URLs). |
| 179 | GURL frame_url = ScriptContext::GetDataSourceURLForFrame(frame); |
| 180 | frame_url = ScriptContext::GetEffectiveDocumentURL(frame, frame_url, |
| 181 | use_effective_url); |
| 182 | extension_id = extensions_->GetExtensionOrAppIDByURL(frame_url); |
kalman | 77b0411 | 2015-03-16 21:20:44 | [diff] [blame] | 183 | } |
| 184 | |
kalman | 745b662 | 2015-03-17 21:07:20 | [diff] [blame] | 185 | // There are conditions where despite a context being associated with an |
| 186 | // extension, no extension actually gets found. Ignore "invalid" because CSP |
| 187 | // blocks extension page loading by switching the extension ID to "invalid". |
| 188 | const Extension* extension = extensions_->GetByID(extension_id); |
| 189 | if (!extension && !extension_id.empty() && extension_id != "invalid") { |
| 190 | // TODO(kalman): Do something here? |
| 191 | } |
| 192 | return extension; |
| 193 | } |
| 194 | |
| 195 | Feature::Context ScriptContextSet::ClassifyJavaScriptContext( |
| 196 | const Extension* extension, |
| 197 | int extension_group, |
| 198 | const GURL& url, |
| 199 | const blink::WebSecurityOrigin& origin) { |
| 200 | // WARNING: This logic must match ProcessMap::GetContextType, as much as |
| 201 | // possible. |
| 202 | |
| 203 | DCHECK_GE(extension_group, 0); |
| 204 | if (extension_group == EXTENSION_GROUP_CONTENT_SCRIPTS) { |
| 205 | return extension ? // TODO(kalman): when does this happen? |
| 206 | Feature::CONTENT_SCRIPT_CONTEXT |
| 207 | : Feature::UNSPECIFIED_CONTEXT; |
| 208 | } |
| 209 | |
| 210 | // We have an explicit check for sandboxed pages before checking whether the |
| 211 | // extension is active in this process because: |
| 212 | // 1. Sandboxed pages run in the same process as regular extension pages, so |
| 213 | // the extension is considered active. |
| 214 | // 2. ScriptContext creation (which triggers bindings injection) happens |
| 215 | // before the SecurityContext is updated with the sandbox flags (after |
| 216 | // reading the CSP header), so the caller can't check if the context's |
| 217 | // security origin is unique yet. |
| 218 | if (ScriptContext::IsSandboxedPage(*extensions_, url)) |
| 219 | return Feature::WEB_PAGE_CONTEXT; |
| 220 | |
| 221 | if (extension && active_extension_ids_->count(extension->id()) > 0) { |
| 222 | // |extension| is active in this process, but it could be either a true |
| 223 | // extension process or within the extent of a hosted app. In the latter |
| 224 | // case this would usually be considered a (blessed) web page context, |
| 225 | // unless the extension in question is a component extension, in which case |
| 226 | // we cheat and call it blessed. |
| 227 | return (extension->is_hosted_app() && |
| 228 | extension->location() != Manifest::COMPONENT) |
| 229 | ? Feature::BLESSED_WEB_PAGE_CONTEXT |
| 230 | : Feature::BLESSED_EXTENSION_CONTEXT; |
| 231 | } |
| 232 | |
| 233 | // TODO(kalman): This isUnique() check is wrong, it should be performed as |
| 234 | // part of ScriptContext::IsSandboxedPage(). |
| 235 | if (!origin.isUnique() && extensions_->ExtensionBindingsAllowed(url)) { |
| 236 | if (!extension) // TODO(kalman): when does this happen? |
| 237 | return Feature::UNSPECIFIED_CONTEXT; |
| 238 | return extension->is_hosted_app() ? Feature::BLESSED_WEB_PAGE_CONTEXT |
| 239 | : Feature::UNBLESSED_EXTENSION_CONTEXT; |
| 240 | } |
| 241 | |
| 242 | if (!url.is_valid()) |
| 243 | return Feature::UNSPECIFIED_CONTEXT; |
| 244 | |
| 245 | if (url.SchemeIs(content::kChromeUIScheme)) |
| 246 | return Feature::WEBUI_CONTEXT; |
| 247 | |
| 248 | return Feature::WEB_PAGE_CONTEXT; |
| 249 | } |
| 250 | |
| 251 | void ScriptContextSet::DispatchOnUnloadEventAndRemove( |
| 252 | std::set<ScriptContext*>* out, |
| 253 | ScriptContext* context) { |
kalman | 0eda971 | 2015-03-30 23:25:55 | [diff] [blame] | 254 | context->DispatchOnUnloadEvent(); |
kalman | 745b662 | 2015-03-17 21:07:20 | [diff] [blame] | 255 | Remove(context); // deleted asynchronously |
| 256 | out->insert(context); |
kalman | 52f4caf | 2015-03-16 19:16:26 | [diff] [blame] | 257 | } |
kalman | 8ee8ee9 | 2015-03-14 00:52:26 | [diff] [blame] | 258 | |
kozyatinskiy | 441ece5 | 2015-04-01 23:46:32 | [diff] [blame] | 259 | void ScriptContextSet::ForEachImpl( |
| 260 | const std::string& extension_id, |
| 261 | content::RenderView* restrict_to_render_view, |
| 262 | const base::Callback<void(ScriptContext*)>& callback, |
| 263 | bool run_asynchronously) const { |
| 264 | // We copy the context list, because calling into javascript may modify it |
| 265 | // out from under us. |
| 266 | std::set<ScriptContext*> contexts_copy = contexts_; |
| 267 | for (ScriptContext* context : contexts_copy) { |
| 268 | // For the same reason as above, contexts may become invalid while we run. |
| 269 | if (!context->is_valid()) |
| 270 | continue; |
| 271 | |
| 272 | if (!extension_id.empty()) { |
| 273 | const Extension* extension = context->extension(); |
| 274 | if (!extension || (extension_id != extension->id())) |
| 275 | continue; |
| 276 | } |
| 277 | |
| 278 | content::RenderView* context_render_view = context->GetRenderView(); |
| 279 | if (!context_render_view) |
| 280 | continue; |
| 281 | |
| 282 | if (restrict_to_render_view && |
| 283 | restrict_to_render_view != context_render_view) |
| 284 | continue; |
| 285 | |
| 286 | content::RenderFrame* context_render_frame = context->GetRenderFrame(); |
| 287 | if (!context_render_frame) |
| 288 | continue; |
| 289 | |
| 290 | if (run_asynchronously) { |
| 291 | blink::WebLocalFrame* web_local_frame = |
| 292 | context_render_frame->GetWebFrame(); |
| 293 | if (!web_local_frame) |
| 294 | continue; |
| 295 | // WebLocalFrame takes BlinkTaskRunner ownership. |
| 296 | web_local_frame->requestRunTask( |
| 297 | new BlinkTaskRunner(context_render_frame, context, callback)); |
| 298 | } else { |
| 299 | callback.Run(context); |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
[email protected] | 8fe74bf | 2012-08-07 21:08:42 | [diff] [blame] | 304 | } // namespace extensions |