Remove linux_chromium_gn_dbg from the chromium CQ.
[chromium-blink-merge.git] / extensions / renderer / script_context.h
blobb58b71e5f012682ae588550f9ec1fa01efd30bbc
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
3 // found in the LICENSE file.
5 #ifndef EXTENSIONS_RENDERER_SCRIPT_CONTEXT_H_
6 #define EXTENSIONS_RENDERER_SCRIPT_CONTEXT_H_
8 #include <string>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/compiler_specific.h"
14 #include "base/threading/thread_checker.h"
15 #include "extensions/common/features/feature.h"
16 #include "extensions/common/permissions/api_permission_set.h"
17 #include "extensions/renderer/module_system.h"
18 #include "extensions/renderer/request_sender.h"
19 #include "extensions/renderer/safe_builtins.h"
20 #include "gin/runner.h"
21 #include "url/gurl.h"
22 #include "v8/include/v8.h"
24 namespace blink {
25 class WebFrame;
26 class WebLocalFrame;
29 namespace content {
30 class RenderFrame;
33 namespace extensions {
34 class Extension;
36 // Extensions wrapper for a v8::Context.
38 // v8::Contexts can be constructed on any thread, and must only be accessed or
39 // destroyed that thread.
41 // Note that ScriptContexts bound to worker threads will not have the full
42 // functionality as those bound to the main RenderThread.
43 class ScriptContext : public RequestSender::Source {
44 public:
45 using RunScriptExceptionHandler = base::Callback<void(const v8::TryCatch&)>;
47 ScriptContext(const v8::Local<v8::Context>& context,
48 blink::WebLocalFrame* frame,
49 const Extension* extension,
50 Feature::Context context_type,
51 const Extension* effective_extension,
52 Feature::Context effective_context_type);
53 ~ScriptContext() override;
55 // Returns whether |url| from any Extension in |extension_set| is sandboxed,
56 // as declared in each Extension's manifest.
57 // TODO(kalman): Delete this when crbug.com/466373 is fixed.
58 // See comment in HasAccessOrThrowError.
59 static bool IsSandboxedPage(const GURL& url);
61 // Clears the WebFrame for this contexts and invalidates the associated
62 // ModuleSystem.
63 void Invalidate();
65 // Registers |observer| to be run when this context is invalidated. Closures
66 // are run immediately when Invalidate() is called, not in a message loop.
67 void AddInvalidationObserver(const base::Closure& observer);
69 // Returns true if this context is still valid, false if it isn't.
70 // A context becomes invalid via Invalidate().
71 bool is_valid() const { return is_valid_; }
73 v8::Local<v8::Context> v8_context() const {
74 return v8::Local<v8::Context>::New(isolate_, v8_context_);
77 const Extension* extension() const { return extension_.get(); }
79 const Extension* effective_extension() const {
80 return effective_extension_.get();
83 blink::WebLocalFrame* web_frame() const { return web_frame_; }
85 Feature::Context context_type() const { return context_type_; }
87 Feature::Context effective_context_type() const {
88 return effective_context_type_;
91 void set_module_system(scoped_ptr<ModuleSystem> module_system) {
92 module_system_ = module_system.Pass();
95 ModuleSystem* module_system() { return module_system_.get(); }
97 SafeBuiltins* safe_builtins() { return &safe_builtins_; }
99 const SafeBuiltins* safe_builtins() const { return &safe_builtins_; }
101 // Returns the ID of the extension associated with this context, or empty
102 // string if there is no such extension.
103 const std::string& GetExtensionID() const;
105 // Returns the RenderFrame associated with this context. Can return NULL if
106 // the context is in the process of being destroyed.
107 content::RenderFrame* GetRenderFrame() const;
109 // Runs |function| with appropriate scopes. Doesn't catch exceptions, callers
110 // must do that if they want.
112 // USE THIS METHOD RATHER THAN v8::Function::Call WHEREVER POSSIBLE.
113 v8::Local<v8::Value> CallFunction(const v8::Local<v8::Function>& function,
114 int argc,
115 v8::Local<v8::Value> argv[]) const;
116 v8::Local<v8::Value> CallFunction(
117 const v8::Local<v8::Function>& function) const;
119 void DispatchEvent(const char* event_name, v8::Local<v8::Array> args) const;
121 // Fires the onunload event on the unload_event module.
122 void DispatchOnUnloadEvent();
124 // Returns the availability of the API |api_name|.
125 Feature::Availability GetAvailability(const std::string& api_name);
127 // Returns a string description of the type of context this is.
128 std::string GetContextTypeDescription() const;
130 // Returns a string description of the effective type of context this is.
131 std::string GetEffectiveContextTypeDescription() const;
133 v8::Isolate* isolate() const { return isolate_; }
135 // Get the URL of this context's web frame.
137 // TODO(kalman): Remove this and replace with a GetOrigin() call which reads
138 // of WebDocument::securityOrigin():
139 // - The URL can change (e.g. pushState) but the origin cannot. Luckily it
140 // appears as though callers don't make security decisions based on the
141 // result of url() so it's not a problem... yet.
142 // - Origin is the correct check to be making.
143 // - It might let us remove the about:blank resolving?
144 const GURL& url() const { return url_; }
146 // Sets the URL of this ScriptContext. Usually this will automatically be set
147 // on construction, unless this isn't constructed with enough information to
148 // determine the URL (e.g. frame was null).
149 // TODO(kalman): Make this a constructor parameter (as an origin).
150 void set_url(const GURL& url) { url_ = url; }
152 // Returns whether the API |api| or any part of the API could be
153 // available in this context without taking into account the context's
154 // extension.
155 bool IsAnyFeatureAvailableToContext(const extensions::Feature& api);
157 // Utility to get the URL we will match against for a frame. If the frame has
158 // committed, this is the commited URL. Otherwise it is the provisional URL.
159 // The returned URL may be invalid.
160 static GURL GetDataSourceURLForFrame(const blink::WebFrame* frame);
162 // Returns the first non-about:-URL in the document hierarchy above and
163 // including |frame|. The document hierarchy is only traversed if
164 // |document_url| is an about:-URL and if |match_about_blank| is true.
165 static GURL GetEffectiveDocumentURL(const blink::WebFrame* frame,
166 const GURL& document_url,
167 bool match_about_blank);
169 // RequestSender::Source implementation.
170 ScriptContext* GetContext() override;
171 void OnResponseReceived(const std::string& name,
172 int request_id,
173 bool success,
174 const base::ListValue& response,
175 const std::string& error) override;
177 // Grants a set of content capabilities to this context.
178 void set_content_capabilities(const APIPermissionSet& capabilities) {
179 content_capabilities_ = capabilities;
182 // Indicates if this context has an effective API permission either by being
183 // a context for an extension which has that permission, or by being a web
184 // context which has been granted the corresponding capability by an
185 // extension.
186 bool HasAPIPermission(APIPermission::ID permission) const;
188 // Throws an Error in this context's JavaScript context, if this context does
189 // not have access to |name|. Returns true if this context has access (i.e.
190 // no exception thrown), false if it does not (i.e. an exception was thrown).
191 bool HasAccessOrThrowError(const std::string& name);
193 // Returns a string representation of this ScriptContext, for debugging.
194 std::string GetDebugString() const;
196 // Gets the current stack trace as a multi-line string to be logged.
197 std::string GetStackTraceAsString() const;
199 // Runs |code|, labelling the script that gets created as |name| (the name is
200 // used in the devtools and stack traces). |exception_handler| will be called
201 // re-entrantly if an exception is thrown during the script's execution.
202 v8::Local<v8::Value> RunScript(
203 v8::Local<v8::String> name,
204 v8::Local<v8::String> code,
205 const RunScriptExceptionHandler& exception_handler);
207 private:
208 class Runner;
210 // Whether this context is valid.
211 bool is_valid_;
213 // The v8 context the bindings are accessible to.
214 v8::Global<v8::Context> v8_context_;
216 // The WebLocalFrame associated with this context. This can be NULL because
217 // this object can outlive is destroyed asynchronously.
218 blink::WebLocalFrame* web_frame_;
220 // The extension associated with this context, or NULL if there is none. This
221 // might be a hosted app in the case that this context is hosting a web URL.
222 scoped_refptr<const Extension> extension_;
224 // The type of context.
225 Feature::Context context_type_;
227 // The effective extension associated with this context, or NULL if there is
228 // none. This is different from the above extension if this context is in an
229 // about:blank iframe for example.
230 scoped_refptr<const Extension> effective_extension_;
232 // The type of context.
233 Feature::Context effective_context_type_;
235 // Owns and structures the JS that is injected to set up extension bindings.
236 scoped_ptr<ModuleSystem> module_system_;
238 // Contains safe copies of builtin objects like Function.prototype.
239 SafeBuiltins safe_builtins_;
241 // The set of capabilities granted to this context by extensions.
242 APIPermissionSet content_capabilities_;
244 // A list of base::Closure instances as an observer interface for
245 // invalidation.
246 std::vector<base::Closure> invalidate_observers_;
248 v8::Isolate* isolate_;
250 GURL url_;
252 scoped_ptr<Runner> runner_;
254 base::ThreadChecker thread_checker_;
256 DISALLOW_COPY_AND_ASSIGN(ScriptContext);
259 } // namespace extensions
261 #endif // EXTENSIONS_RENDERER_SCRIPT_CONTEXT_H_