blob: 569deaa7f217b2fae1d98a9b90fb3ce8dc1f2e43 [file] [log] [blame]
[email protected]f55c90ee62014-04-12 00:50:031// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef EXTENSIONS_RENDERER_SCRIPT_CONTEXT_H_
6#define EXTENSIONS_RENDERER_SCRIPT_CONTEXT_H_
7
8#include <string>
kalmanb0c1c502015-04-15 00:25:069#include <vector>
[email protected]f55c90ee62014-04-12 00:50:0310
11#include "base/basictypes.h"
kalmanb0c1c502015-04-15 00:25:0612#include "base/callback.h"
[email protected]f55c90ee62014-04-12 00:50:0313#include "base/compiler_specific.h"
14#include "extensions/common/features/feature.h"
rockote261b162014-12-12 01:59:4715#include "extensions/common/permissions/api_permission_set.h"
[email protected]f55c90ee62014-04-12 00:50:0316#include "extensions/renderer/module_system.h"
17#include "extensions/renderer/request_sender.h"
18#include "extensions/renderer/safe_builtins.h"
[email protected]d9f51dad2014-07-09 05:39:3819#include "gin/runner.h"
Sadrul Habib Chowdhury0d7ef9f2014-12-03 20:07:3020#include "url/gurl.h"
[email protected]f55c90ee62014-04-12 00:50:0321#include "v8/include/v8.h"
22
23namespace blink {
24class WebFrame;
kalmanf91cb892015-04-15 19:20:4825class WebLocalFrame;
[email protected]f55c90ee62014-04-12 00:50:0326}
27
28namespace content {
[email protected]2101c4c2014-08-22 00:16:1629class RenderFrame;
[email protected]f55c90ee62014-04-12 00:50:0330class RenderView;
31}
32
33namespace extensions {
34class Extension;
kalmanf91cb892015-04-15 19:20:4835class ExtensionSet;
[email protected]f55c90ee62014-04-12 00:50:0336
37// Extensions wrapper for a v8 context.
sammcde54a47e2015-01-13 23:16:3438class ScriptContext : public RequestSender::Source {
[email protected]f55c90ee62014-04-12 00:50:0339 public:
[email protected]e6893672014-05-01 17:29:1340 ScriptContext(const v8::Handle<v8::Context>& context,
kalmanf91cb892015-04-15 19:20:4841 blink::WebLocalFrame* frame,
[email protected]f55c90ee62014-04-12 00:50:0342 const Extension* extension,
mek7e1d7452014-09-08 23:55:5743 Feature::Context context_type,
44 const Extension* effective_extension,
45 Feature::Context effective_context_type);
dcheng9168b2f2014-10-21 12:38:2446 ~ScriptContext() override;
[email protected]f55c90ee62014-04-12 00:50:0347
kalmanf91cb892015-04-15 19:20:4848 // Returns whether |url| is sandboxed (as declared in any Extension in
49 // |extension_set| as sandboxed).
50 //
51 // Declared in ScriptContext for lack of a better place, but this should
52 // become unnecessary at some point as crbug.com/466373 is worked on.
53 static bool IsSandboxedPage(const ExtensionSet& extension_set,
54 const GURL& url);
55
[email protected]f55c90ee62014-04-12 00:50:0356 // Clears the WebFrame for this contexts and invalidates the associated
57 // ModuleSystem.
58 void Invalidate();
59
kalmanb0c1c502015-04-15 00:25:0660 // Registers |observer| to be run when this context is invalidated. Closures
61 // are run immediately when Invalidate() is called, not in a message loop.
62 void AddInvalidationObserver(const base::Closure& observer);
63
[email protected]f55c90ee62014-04-12 00:50:0364 // Returns true if this context is still valid, false if it isn't.
65 // A context becomes invalid via Invalidate().
kalmanb0c1c502015-04-15 00:25:0666 bool is_valid() const { return is_valid_; }
[email protected]f55c90ee62014-04-12 00:50:0367
68 v8::Handle<v8::Context> v8_context() const {
kalman078a2192015-03-09 18:19:3969 return v8::Local<v8::Context>::New(isolate_, v8_context_);
[email protected]f55c90ee62014-04-12 00:50:0370 }
71
72 const Extension* extension() const { return extension_.get(); }
73
mek7e1d7452014-09-08 23:55:5774 const Extension* effective_extension() const {
75 return effective_extension_.get();
76 }
77
kalmanf91cb892015-04-15 19:20:4878 blink::WebLocalFrame* web_frame() const { return web_frame_; }
[email protected]f55c90ee62014-04-12 00:50:0379
80 Feature::Context context_type() const { return context_type_; }
81
mek7e1d7452014-09-08 23:55:5782 Feature::Context effective_context_type() const {
83 return effective_context_type_;
84 }
85
[email protected]f55c90ee62014-04-12 00:50:0386 void set_module_system(scoped_ptr<ModuleSystem> module_system) {
87 module_system_ = module_system.Pass();
88 }
89
90 ModuleSystem* module_system() { return module_system_.get(); }
91
92 SafeBuiltins* safe_builtins() { return &safe_builtins_; }
93
94 const SafeBuiltins* safe_builtins() const { return &safe_builtins_; }
95
96 // Returns the ID of the extension associated with this context, or empty
97 // string if there is no such extension.
[email protected]800f9872014-06-12 04:12:5198 const std::string& GetExtensionID() const;
[email protected]f55c90ee62014-04-12 00:50:0399
100 // Returns the RenderView associated with this context. Can return NULL if the
101 // context is in the process of being destroyed.
102 content::RenderView* GetRenderView() const;
103
[email protected]2101c4c2014-08-22 00:16:16104 // Returns the RenderFrame associated with this context. Can return NULL if
105 // the context is in the process of being destroyed.
106 content::RenderFrame* GetRenderFrame() const;
107
[email protected]f55c90ee62014-04-12 00:50:03108 // Runs |function| with appropriate scopes. Doesn't catch exceptions, callers
109 // must do that if they want.
110 //
111 // USE THIS METHOD RATHER THAN v8::Function::Call WHEREVER POSSIBLE.
112 v8::Local<v8::Value> CallFunction(v8::Handle<v8::Function> function,
113 int argc,
114 v8::Handle<v8::Value> argv[]) const;
115
116 void DispatchEvent(const char* event_name, v8::Handle<v8::Array> args) const;
117
118 // Fires the onunload event on the unload_event module.
119 void DispatchOnUnloadEvent();
120
121 // Returns the availability of the API |api_name|.
122 Feature::Availability GetAvailability(const std::string& api_name);
123
124 // Returns a string description of the type of context this is.
125 std::string GetContextTypeDescription();
126
mek7e1d7452014-09-08 23:55:57127 // Returns a string description of the effective type of context this is.
128 std::string GetEffectiveContextTypeDescription();
129
[email protected]f55c90ee62014-04-12 00:50:03130 v8::Isolate* isolate() const { return isolate_; }
131
132 // Get the URL of this context's web frame.
kalmanf91cb892015-04-15 19:20:48133 //
134 // TODO(kalman): Remove this and replace with a GetOrigin() call which reads
135 // of WebDocument::securityOrigin():
136 // - The URL can change (e.g. pushState) but the origin cannot. Luckily it
137 // appears as though callers don't make security decisions based on the
138 // result of GetURL() so it's not a problem... yet.
139 // - Origin is the correct check to be making.
140 // - It might let us remove the about:blank resolving?
[email protected]f55c90ee62014-04-12 00:50:03141 GURL GetURL() const;
142
143 // Returns whether the API |api| or any part of the API could be
144 // available in this context without taking into account the context's
145 // extension.
146 bool IsAnyFeatureAvailableToContext(const extensions::Feature& api);
147
148 // Utility to get the URL we will match against for a frame. If the frame has
149 // committed, this is the commited URL. Otherwise it is the provisional URL.
[email protected]c1abb3232014-07-30 18:28:39150 // The returned URL may be invalid.
[email protected]f55c90ee62014-04-12 00:50:03151 static GURL GetDataSourceURLForFrame(const blink::WebFrame* frame);
152
[email protected]ae26b282014-05-15 16:40:16153 // Returns the first non-about:-URL in the document hierarchy above and
154 // including |frame|. The document hierarchy is only traversed if
155 // |document_url| is an about:-URL and if |match_about_blank| is true.
156 static GURL GetEffectiveDocumentURL(const blink::WebFrame* frame,
157 const GURL& document_url,
158 bool match_about_blank);
159
[email protected]f55c90ee62014-04-12 00:50:03160 // RequestSender::Source implementation.
dcheng9168b2f2014-10-21 12:38:24161 ScriptContext* GetContext() override;
162 void OnResponseReceived(const std::string& name,
163 int request_id,
164 bool success,
165 const base::ListValue& response,
166 const std::string& error) override;
[email protected]f55c90ee62014-04-12 00:50:03167
rockote261b162014-12-12 01:59:47168 // Grants a set of content capabilities to this context.
169 void SetContentCapabilities(const APIPermissionSet& permissions);
170
171 // Indicates if this context has an effective API permission either by being
172 // a context for an extension which has that permission, or by being a web
173 // context which has been granted the corresponding capability by an
174 // extension.
175 bool HasAPIPermission(APIPermission::ID permission) const;
176
mlamouri60a2857d2015-04-14 15:22:36177 private:
178 class Runner;
179
kalmanb0c1c502015-04-15 00:25:06180 // Whether this context is valid.
181 bool is_valid_;
182
183 // The v8 context the bindings are accessible to.
184 v8::Global<v8::Context> v8_context_;
185
kalmanf91cb892015-04-15 19:20:48186 // The WebLocalFrame associated with this context. This can be NULL because
187 // this object can outlive is destroyed asynchronously.
188 blink::WebLocalFrame* web_frame_;
[email protected]f55c90ee62014-04-12 00:50:03189
190 // The extension associated with this context, or NULL if there is none. This
191 // might be a hosted app in the case that this context is hosting a web URL.
192 scoped_refptr<const Extension> extension_;
193
194 // The type of context.
195 Feature::Context context_type_;
196
mek7e1d7452014-09-08 23:55:57197 // The effective extension associated with this context, or NULL if there is
198 // none. This is different from the above extension if this context is in an
199 // about:blank iframe for example.
200 scoped_refptr<const Extension> effective_extension_;
201
202 // The type of context.
203 Feature::Context effective_context_type_;
204
[email protected]f55c90ee62014-04-12 00:50:03205 // Owns and structures the JS that is injected to set up extension bindings.
206 scoped_ptr<ModuleSystem> module_system_;
207
208 // Contains safe copies of builtin objects like Function.prototype.
209 SafeBuiltins safe_builtins_;
210
rockote261b162014-12-12 01:59:47211 // The set of capabilities granted to this context by extensions.
212 APIPermissionSet content_capabilities_;
213
kalmanb0c1c502015-04-15 00:25:06214 // A list of base::Closure instances as an observer interface for
215 // invalidation.
216 std::vector<base::Closure> invalidate_observers_;
217
[email protected]f55c90ee62014-04-12 00:50:03218 v8::Isolate* isolate_;
219
Sadrul Habib Chowdhury0d7ef9f2014-12-03 20:07:30220 GURL url_;
221
sammcde54a47e2015-01-13 23:16:34222 scoped_ptr<Runner> runner_;
223
[email protected]f55c90ee62014-04-12 00:50:03224 DISALLOW_COPY_AND_ASSIGN(ScriptContext);
225};
226
227} // namespace extensions
228
229#endif // EXTENSIONS_RENDERER_SCRIPT_CONTEXT_H_