blob: 3c1d5c1b9f0c7e074818c63e6a20a8517cf23a53 [file] [log] [blame]
[email protected]bcd9580f2014-04-17 19:17:591// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]2ee1e3a2011-10-04 15:04:042// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]bcd9580f2014-04-17 19:17:595#include "extensions/renderer/script_context_set.h"
[email protected]2ee1e3a2011-10-04 15:04:046
[email protected]68275102013-07-17 23:54:207#include "base/message_loop/message_loop.h"
kalmanf91cb892015-04-15 19:20:488#include "content/public/common/url_constants.h"
rdevlin.croninc5d9a0ea2015-06-23 21:29:109#include "content/public/renderer/render_frame.h"
[email protected]e4452d32013-11-15 23:07:4110#include "extensions/common/extension.h"
kalmanf91cb892015-04-15 19:20:4811#include "extensions/renderer/extension_groups.h"
[email protected]bcd9580f2014-04-17 19:17:5912#include "extensions/renderer/script_context.h"
kalmanf91cb892015-04-15 19:20:4813#include "extensions/renderer/script_injection.h"
14#include "third_party/WebKit/public/web/WebDocument.h"
15#include "third_party/WebKit/public/web/WebLocalFrame.h"
[email protected]885c0e92012-11-13 20:27:4216#include "v8/include/v8.h"
[email protected]2ee1e3a2011-10-04 15:04:0417
[email protected]8fe74bf2012-08-07 21:08:4218namespace extensions {
19
kalmanf91cb892015-04-15 19:20:4820ScriptContextSet::ScriptContextSet(ExtensionSet* extensions,
21 ExtensionIdSet* active_extension_ids)
22 : extensions_(extensions), active_extension_ids_(active_extension_ids) {
[email protected]2ee1e3a2011-10-04 15:04:0423}
kalmanf91cb892015-04-15 19:20:4824
[email protected]bcd9580f2014-04-17 19:17:5925ScriptContextSet::~ScriptContextSet() {
[email protected]2ee1e3a2011-10-04 15:04:0426}
27
kalmanf91cb892015-04-15 19:20:4828ScriptContext* ScriptContextSet::Register(
29 blink::WebLocalFrame* frame,
tfarinaf85316f2015-04-29 17:03:4030 const v8::Local<v8::Context>& v8_context,
kalmanf91cb892015-04-15 19:20:4831 int extension_group,
32 int world_id) {
33 const Extension* extension =
34 GetExtensionFromFrameAndWorld(frame, world_id, false);
35 const Extension* effective_extension =
36 GetExtensionFromFrameAndWorld(frame, world_id, true);
[email protected]2ee1e3a2011-10-04 15:04:0437
kalmanf91cb892015-04-15 19:20:4838 GURL frame_url = ScriptContext::GetDataSourceURLForFrame(frame);
39 Feature::Context context_type =
40 ClassifyJavaScriptContext(extension, extension_group, frame_url,
41 frame->document().securityOrigin());
42 Feature::Context effective_context_type = ClassifyJavaScriptContext(
43 effective_extension, extension_group,
44 ScriptContext::GetEffectiveDocumentURL(frame, frame_url, true),
45 frame->document().securityOrigin());
46
47 ScriptContext* context =
48 new ScriptContext(v8_context, frame, extension, context_type,
49 effective_extension, effective_context_type);
50 contexts_.insert(context); // takes ownership
51 return context;
[email protected]2ee1e3a2011-10-04 15:04:0452}
53
[email protected]bcd9580f2014-04-17 19:17:5954void ScriptContextSet::Remove(ScriptContext* context) {
[email protected]2ee1e3a2011-10-04 15:04:0455 if (contexts_.erase(context)) {
[email protected]4f1633f2013-03-09 14:26:2456 context->Invalidate();
kalman62b5911d2015-04-04 02:33:0957 base::MessageLoop::current()->DeleteSoon(FROM_HERE, context);
[email protected]2ee1e3a2011-10-04 15:04:0458 }
59}
60
[email protected]bcd9580f2014-04-17 19:17:5961ScriptContext* ScriptContextSet::GetCurrent() const {
[email protected]95c6b3012013-12-02 14:30:3162 v8::Isolate* isolate = v8::Isolate::GetCurrent();
63 return isolate->InContext() ? GetByV8Context(isolate->GetCurrentContext())
kalmanf91cb892015-04-15 19:20:4864 : nullptr;
[email protected]ad6aa8f92013-06-22 15:34:1665}
66
[email protected]bcd9580f2014-04-17 19:17:5967ScriptContext* ScriptContextSet::GetCalling() const {
[email protected]95c6b3012013-12-02 14:30:3168 v8::Isolate* isolate = v8::Isolate::GetCurrent();
69 v8::Local<v8::Context> calling = isolate->GetCallingContext();
kalmanf91cb892015-04-15 19:20:4870 return calling.IsEmpty() ? nullptr : GetByV8Context(calling);
[email protected]2ee1e3a2011-10-04 15:04:0471}
72
[email protected]bcd9580f2014-04-17 19:17:5973ScriptContext* ScriptContextSet::GetByV8Context(
tfarinaf85316f2015-04-29 17:03:4074 const v8::Local<v8::Context>& v8_context) const {
kalmanf91cb892015-04-15 19:20:4875 for (ScriptContext* script_context : contexts_) {
76 if (script_context->v8_context() == v8_context)
77 return script_context;
[email protected]2ee1e3a2011-10-04 15:04:0478 }
kalmanf91cb892015-04-15 19:20:4879 return nullptr;
[email protected]2ee1e3a2011-10-04 15:04:0480}
81
[email protected]bcd9580f2014-04-17 19:17:5982void ScriptContextSet::ForEach(
[email protected]2ee1e3a2011-10-04 15:04:0483 const std::string& extension_id,
rdevlin.croninc5d9a0ea2015-06-23 21:29:1084 content::RenderFrame* render_frame,
[email protected]bcd9580f2014-04-17 19:17:5985 const base::Callback<void(ScriptContext*)>& callback) const {
kozyatinskiy441ece52015-04-01 23:46:3286 // We copy the context list, because calling into javascript may modify it
87 // out from under us.
kalmanf91cb892015-04-15 19:20:4888 std::set<ScriptContext*> contexts_copy = contexts_;
kalman62b5911d2015-04-04 02:33:0989
kalmanf91cb892015-04-15 19:20:4890 for (ScriptContext* context : contexts_copy) {
kozyatinskiy441ece52015-04-01 23:46:3291 // For the same reason as above, contexts may become invalid while we run.
92 if (!context->is_valid())
93 continue;
94
95 if (!extension_id.empty()) {
96 const Extension* extension = context->extension();
97 if (!extension || (extension_id != extension->id()))
98 continue;
99 }
100
rdevlin.croninc5d9a0ea2015-06-23 21:29:10101 content::RenderFrame* context_render_frame = context->GetRenderFrame();
102 if (!context_render_frame)
kozyatinskiy441ece52015-04-01 23:46:32103 continue;
104
rdevlin.croninc5d9a0ea2015-06-23 21:29:10105 if (render_frame && render_frame != context_render_frame)
kozyatinskiy441ece52015-04-01 23:46:32106 continue;
107
kalman62b5911d2015-04-04 02:33:09108 callback.Run(context);
109 }
110}
kozyatinskiy441ece52015-04-01 23:46:32111
kalmanf91cb892015-04-15 19:20:48112std::set<ScriptContext*> ScriptContextSet::OnExtensionUnloaded(
kalman62b5911d2015-04-04 02:33:09113 const std::string& extension_id) {
kalmanf91cb892015-04-15 19:20:48114 std::set<ScriptContext*> removed;
115 ForEach(extension_id,
116 base::Bind(&ScriptContextSet::DispatchOnUnloadEventAndRemove,
117 base::Unretained(this), &removed));
118 return removed;
119}
kalman62b5911d2015-04-04 02:33:09120
kalmanf91cb892015-04-15 19:20:48121const Extension* ScriptContextSet::GetExtensionFromFrameAndWorld(
122 const blink::WebLocalFrame* frame,
123 int world_id,
124 bool use_effective_url) {
125 std::string extension_id;
126 if (world_id != 0) {
127 // Isolated worlds (content script).
128 extension_id = ScriptInjection::GetHostIdForIsolatedWorld(world_id);
129 } else if (!frame->document().securityOrigin().isUnique()) {
130 // TODO(kalman): Delete the above check.
131 // Extension pages (chrome-extension:// URLs).
132 GURL frame_url = ScriptContext::GetDataSourceURLForFrame(frame);
133 frame_url = ScriptContext::GetEffectiveDocumentURL(frame, frame_url,
134 use_effective_url);
135 extension_id = extensions_->GetExtensionOrAppIDByURL(frame_url);
kozyatinskiy441ece52015-04-01 23:46:32136 }
kalman62b5911d2015-04-04 02:33:09137
kalmanf91cb892015-04-15 19:20:48138 // There are conditions where despite a context being associated with an
139 // extension, no extension actually gets found. Ignore "invalid" because CSP
140 // blocks extension page loading by switching the extension ID to "invalid".
141 const Extension* extension = extensions_->GetByID(extension_id);
142 if (!extension && !extension_id.empty() && extension_id != "invalid") {
143 // TODO(kalman): Do something here?
144 }
145 return extension;
146}
147
148Feature::Context ScriptContextSet::ClassifyJavaScriptContext(
149 const Extension* extension,
150 int extension_group,
151 const GURL& url,
152 const blink::WebSecurityOrigin& origin) {
153 // WARNING: This logic must match ProcessMap::GetContextType, as much as
154 // possible.
155
156 DCHECK_GE(extension_group, 0);
157 if (extension_group == EXTENSION_GROUP_CONTENT_SCRIPTS) {
158 return extension ? // TODO(kalman): when does this happen?
159 Feature::CONTENT_SCRIPT_CONTEXT
160 : Feature::UNSPECIFIED_CONTEXT;
161 }
162
163 // We have an explicit check for sandboxed pages before checking whether the
164 // extension is active in this process because:
165 // 1. Sandboxed pages run in the same process as regular extension pages, so
166 // the extension is considered active.
167 // 2. ScriptContext creation (which triggers bindings injection) happens
168 // before the SecurityContext is updated with the sandbox flags (after
169 // reading the CSP header), so the caller can't check if the context's
170 // security origin is unique yet.
171 if (ScriptContext::IsSandboxedPage(*extensions_, url))
172 return Feature::WEB_PAGE_CONTEXT;
173
174 if (extension && active_extension_ids_->count(extension->id()) > 0) {
175 // |extension| is active in this process, but it could be either a true
176 // extension process or within the extent of a hosted app. In the latter
177 // case this would usually be considered a (blessed) web page context,
178 // unless the extension in question is a component extension, in which case
179 // we cheat and call it blessed.
180 return (extension->is_hosted_app() &&
181 extension->location() != Manifest::COMPONENT)
182 ? Feature::BLESSED_WEB_PAGE_CONTEXT
183 : Feature::BLESSED_EXTENSION_CONTEXT;
184 }
185
186 // TODO(kalman): This isUnique() check is wrong, it should be performed as
187 // part of ScriptContext::IsSandboxedPage().
188 if (!origin.isUnique() && extensions_->ExtensionBindingsAllowed(url)) {
189 if (!extension) // TODO(kalman): when does this happen?
190 return Feature::UNSPECIFIED_CONTEXT;
191 return extension->is_hosted_app() ? Feature::BLESSED_WEB_PAGE_CONTEXT
192 : Feature::UNBLESSED_EXTENSION_CONTEXT;
193 }
194
195 if (!url.is_valid())
196 return Feature::UNSPECIFIED_CONTEXT;
197
198 if (url.SchemeIs(content::kChromeUIScheme))
199 return Feature::WEBUI_CONTEXT;
200
201 return Feature::WEB_PAGE_CONTEXT;
202}
203
204void ScriptContextSet::DispatchOnUnloadEventAndRemove(
205 std::set<ScriptContext*>* out,
206 ScriptContext* context) {
207 context->DispatchOnUnloadEvent();
208 Remove(context); // deleted asynchronously
209 out->insert(context);
kozyatinskiy441ece52015-04-01 23:46:32210}
211
[email protected]8fe74bf2012-08-07 21:08:42212} // namespace extensions