blob: 096ea70ac881cbaaa13089ea55fb84ec20b38192 [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
rdevlin.croninb2cec912015-06-24 20:36:0120namespace {
21// There is only ever one instance of the ScriptContextSet.
22ScriptContextSet* g_context_set = nullptr;
23}
24
kalmanf91cb892015-04-15 19:20:4825ScriptContextSet::ScriptContextSet(ExtensionSet* extensions,
26 ExtensionIdSet* active_extension_ids)
27 : extensions_(extensions), active_extension_ids_(active_extension_ids) {
rdevlin.croninb2cec912015-06-24 20:36:0128 DCHECK(!g_context_set);
29 g_context_set = this;
[email protected]2ee1e3a2011-10-04 15:04:0430}
kalmanf91cb892015-04-15 19:20:4831
[email protected]bcd9580f2014-04-17 19:17:5932ScriptContextSet::~ScriptContextSet() {
rdevlin.croninb2cec912015-06-24 20:36:0133 g_context_set = nullptr;
[email protected]2ee1e3a2011-10-04 15:04:0434}
35
kalmanf91cb892015-04-15 19:20:4836ScriptContext* ScriptContextSet::Register(
37 blink::WebLocalFrame* frame,
tfarinaf85316f2015-04-29 17:03:4038 const v8::Local<v8::Context>& v8_context,
kalmanf91cb892015-04-15 19:20:4839 int extension_group,
40 int world_id) {
41 const Extension* extension =
42 GetExtensionFromFrameAndWorld(frame, world_id, false);
43 const Extension* effective_extension =
44 GetExtensionFromFrameAndWorld(frame, world_id, true);
[email protected]2ee1e3a2011-10-04 15:04:0445
kalmanf91cb892015-04-15 19:20:4846 GURL frame_url = ScriptContext::GetDataSourceURLForFrame(frame);
47 Feature::Context context_type =
48 ClassifyJavaScriptContext(extension, extension_group, frame_url,
49 frame->document().securityOrigin());
50 Feature::Context effective_context_type = ClassifyJavaScriptContext(
51 effective_extension, extension_group,
52 ScriptContext::GetEffectiveDocumentURL(frame, frame_url, true),
53 frame->document().securityOrigin());
54
55 ScriptContext* context =
56 new ScriptContext(v8_context, frame, extension, context_type,
57 effective_extension, effective_context_type);
58 contexts_.insert(context); // takes ownership
59 return context;
[email protected]2ee1e3a2011-10-04 15:04:0460}
61
[email protected]bcd9580f2014-04-17 19:17:5962void ScriptContextSet::Remove(ScriptContext* context) {
[email protected]2ee1e3a2011-10-04 15:04:0463 if (contexts_.erase(context)) {
[email protected]4f1633f2013-03-09 14:26:2464 context->Invalidate();
kalman62b5911d2015-04-04 02:33:0965 base::MessageLoop::current()->DeleteSoon(FROM_HERE, context);
[email protected]2ee1e3a2011-10-04 15:04:0466 }
67}
68
[email protected]bcd9580f2014-04-17 19:17:5969ScriptContext* ScriptContextSet::GetCurrent() const {
[email protected]95c6b3012013-12-02 14:30:3170 v8::Isolate* isolate = v8::Isolate::GetCurrent();
71 return isolate->InContext() ? GetByV8Context(isolate->GetCurrentContext())
kalmanf91cb892015-04-15 19:20:4872 : nullptr;
[email protected]ad6aa8f92013-06-22 15:34:1673}
74
[email protected]bcd9580f2014-04-17 19:17:5975ScriptContext* ScriptContextSet::GetCalling() const {
[email protected]95c6b3012013-12-02 14:30:3176 v8::Isolate* isolate = v8::Isolate::GetCurrent();
77 v8::Local<v8::Context> calling = isolate->GetCallingContext();
kalmanf91cb892015-04-15 19:20:4878 return calling.IsEmpty() ? nullptr : GetByV8Context(calling);
[email protected]2ee1e3a2011-10-04 15:04:0479}
80
[email protected]bcd9580f2014-04-17 19:17:5981ScriptContext* ScriptContextSet::GetByV8Context(
tfarinaf85316f2015-04-29 17:03:4082 const v8::Local<v8::Context>& v8_context) const {
kalmanf91cb892015-04-15 19:20:4883 for (ScriptContext* script_context : contexts_) {
84 if (script_context->v8_context() == v8_context)
85 return script_context;
[email protected]2ee1e3a2011-10-04 15:04:0486 }
kalmanf91cb892015-04-15 19:20:4887 return nullptr;
[email protected]2ee1e3a2011-10-04 15:04:0488}
89
rdevlin.croninb2cec912015-06-24 20:36:0190ScriptContext* ScriptContextSet::GetContextByV8Context(
91 const v8::Local<v8::Context>& v8_context) {
92 // g_context_set can be null in unittests.
93 return g_context_set ? g_context_set->GetByV8Context(v8_context) : nullptr;
94}
95
[email protected]bcd9580f2014-04-17 19:17:5996void ScriptContextSet::ForEach(
[email protected]2ee1e3a2011-10-04 15:04:0497 const std::string& extension_id,
rdevlin.croninc5d9a0ea2015-06-23 21:29:1098 content::RenderFrame* render_frame,
[email protected]bcd9580f2014-04-17 19:17:5999 const base::Callback<void(ScriptContext*)>& callback) const {
kozyatinskiy441ece52015-04-01 23:46:32100 // We copy the context list, because calling into javascript may modify it
101 // out from under us.
kalmanf91cb892015-04-15 19:20:48102 std::set<ScriptContext*> contexts_copy = contexts_;
kalman62b5911d2015-04-04 02:33:09103
kalmanf91cb892015-04-15 19:20:48104 for (ScriptContext* context : contexts_copy) {
kozyatinskiy441ece52015-04-01 23:46:32105 // For the same reason as above, contexts may become invalid while we run.
106 if (!context->is_valid())
107 continue;
108
109 if (!extension_id.empty()) {
110 const Extension* extension = context->extension();
111 if (!extension || (extension_id != extension->id()))
112 continue;
113 }
114
rdevlin.croninc5d9a0ea2015-06-23 21:29:10115 content::RenderFrame* context_render_frame = context->GetRenderFrame();
116 if (!context_render_frame)
kozyatinskiy441ece52015-04-01 23:46:32117 continue;
118
rdevlin.croninc5d9a0ea2015-06-23 21:29:10119 if (render_frame && render_frame != context_render_frame)
kozyatinskiy441ece52015-04-01 23:46:32120 continue;
121
kalman62b5911d2015-04-04 02:33:09122 callback.Run(context);
123 }
124}
kozyatinskiy441ece52015-04-01 23:46:32125
kalmanf91cb892015-04-15 19:20:48126std::set<ScriptContext*> ScriptContextSet::OnExtensionUnloaded(
kalman62b5911d2015-04-04 02:33:09127 const std::string& extension_id) {
kalmanf91cb892015-04-15 19:20:48128 std::set<ScriptContext*> removed;
129 ForEach(extension_id,
130 base::Bind(&ScriptContextSet::DispatchOnUnloadEventAndRemove,
131 base::Unretained(this), &removed));
132 return removed;
133}
kalman62b5911d2015-04-04 02:33:09134
kalmanf91cb892015-04-15 19:20:48135const Extension* ScriptContextSet::GetExtensionFromFrameAndWorld(
136 const blink::WebLocalFrame* frame,
137 int world_id,
138 bool use_effective_url) {
139 std::string extension_id;
140 if (world_id != 0) {
141 // Isolated worlds (content script).
142 extension_id = ScriptInjection::GetHostIdForIsolatedWorld(world_id);
143 } else if (!frame->document().securityOrigin().isUnique()) {
144 // TODO(kalman): Delete the above check.
145 // Extension pages (chrome-extension:// URLs).
146 GURL frame_url = ScriptContext::GetDataSourceURLForFrame(frame);
147 frame_url = ScriptContext::GetEffectiveDocumentURL(frame, frame_url,
148 use_effective_url);
149 extension_id = extensions_->GetExtensionOrAppIDByURL(frame_url);
kozyatinskiy441ece52015-04-01 23:46:32150 }
kalman62b5911d2015-04-04 02:33:09151
kalmanf91cb892015-04-15 19:20:48152 // There are conditions where despite a context being associated with an
153 // extension, no extension actually gets found. Ignore "invalid" because CSP
154 // blocks extension page loading by switching the extension ID to "invalid".
155 const Extension* extension = extensions_->GetByID(extension_id);
156 if (!extension && !extension_id.empty() && extension_id != "invalid") {
157 // TODO(kalman): Do something here?
158 }
159 return extension;
160}
161
162Feature::Context ScriptContextSet::ClassifyJavaScriptContext(
163 const Extension* extension,
164 int extension_group,
165 const GURL& url,
166 const blink::WebSecurityOrigin& origin) {
167 // WARNING: This logic must match ProcessMap::GetContextType, as much as
168 // possible.
169
170 DCHECK_GE(extension_group, 0);
171 if (extension_group == EXTENSION_GROUP_CONTENT_SCRIPTS) {
172 return extension ? // TODO(kalman): when does this happen?
173 Feature::CONTENT_SCRIPT_CONTEXT
174 : Feature::UNSPECIFIED_CONTEXT;
175 }
176
177 // We have an explicit check for sandboxed pages before checking whether the
178 // extension is active in this process because:
179 // 1. Sandboxed pages run in the same process as regular extension pages, so
180 // the extension is considered active.
181 // 2. ScriptContext creation (which triggers bindings injection) happens
182 // before the SecurityContext is updated with the sandbox flags (after
183 // reading the CSP header), so the caller can't check if the context's
184 // security origin is unique yet.
185 if (ScriptContext::IsSandboxedPage(*extensions_, url))
186 return Feature::WEB_PAGE_CONTEXT;
187
188 if (extension && active_extension_ids_->count(extension->id()) > 0) {
189 // |extension| is active in this process, but it could be either a true
190 // extension process or within the extent of a hosted app. In the latter
191 // case this would usually be considered a (blessed) web page context,
192 // unless the extension in question is a component extension, in which case
193 // we cheat and call it blessed.
194 return (extension->is_hosted_app() &&
195 extension->location() != Manifest::COMPONENT)
196 ? Feature::BLESSED_WEB_PAGE_CONTEXT
197 : Feature::BLESSED_EXTENSION_CONTEXT;
198 }
199
200 // TODO(kalman): This isUnique() check is wrong, it should be performed as
201 // part of ScriptContext::IsSandboxedPage().
202 if (!origin.isUnique() && extensions_->ExtensionBindingsAllowed(url)) {
203 if (!extension) // TODO(kalman): when does this happen?
204 return Feature::UNSPECIFIED_CONTEXT;
205 return extension->is_hosted_app() ? Feature::BLESSED_WEB_PAGE_CONTEXT
206 : Feature::UNBLESSED_EXTENSION_CONTEXT;
207 }
208
209 if (!url.is_valid())
210 return Feature::UNSPECIFIED_CONTEXT;
211
212 if (url.SchemeIs(content::kChromeUIScheme))
213 return Feature::WEBUI_CONTEXT;
214
215 return Feature::WEB_PAGE_CONTEXT;
216}
217
218void ScriptContextSet::DispatchOnUnloadEventAndRemove(
219 std::set<ScriptContext*>* out,
220 ScriptContext* context) {
221 context->DispatchOnUnloadEvent();
222 Remove(context); // deleted asynchronously
223 out->insert(context);
kozyatinskiy441ece52015-04-01 23:46:32224}
225
[email protected]8fe74bf2012-08-07 21:08:42226} // namespace extensions