Adding Peter Thatcher to the owners file.
[chromium-blink-merge.git] / extensions / renderer / script_context.h
blobb6c90f2c46f765723452a0450291768c6fc3ba6f
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>
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "extensions/common/features/feature.h"
13 #include "extensions/common/permissions/api_permission_set.h"
14 #include "extensions/renderer/module_system.h"
15 #include "extensions/renderer/request_sender.h"
16 #include "extensions/renderer/safe_builtins.h"
17 #include "gin/runner.h"
18 #include "url/gurl.h"
19 #include "v8/include/v8.h"
21 namespace blink {
22 class WebFrame;
25 namespace content {
26 class RenderFrame;
27 class RenderView;
30 namespace extensions {
31 class Extension;
33 // Extensions wrapper for a v8 context.
34 class ScriptContext : public RequestSender::Source {
35 public:
36 ScriptContext(const v8::Handle<v8::Context>& context,
37 blink::WebFrame* frame,
38 const Extension* extension,
39 Feature::Context context_type,
40 const Extension* effective_extension,
41 Feature::Context effective_context_type);
42 ~ScriptContext() override;
44 // Clears the WebFrame for this contexts and invalidates the associated
45 // ModuleSystem.
46 void Invalidate();
48 // Returns true if this context is still valid, false if it isn't.
49 // A context becomes invalid via Invalidate().
50 bool is_valid() const { return !v8_context_.IsEmpty(); }
52 v8::Handle<v8::Context> v8_context() const {
53 return v8::Local<v8::Context>::New(isolate_, v8_context_);
56 const Extension* extension() const { return extension_.get(); }
58 const Extension* effective_extension() const {
59 return effective_extension_.get();
62 blink::WebFrame* web_frame() const { return web_frame_; }
64 Feature::Context context_type() const { return context_type_; }
66 Feature::Context effective_context_type() const {
67 return effective_context_type_;
70 void set_module_system(scoped_ptr<ModuleSystem> module_system) {
71 module_system_ = module_system.Pass();
74 ModuleSystem* module_system() { return module_system_.get(); }
76 SafeBuiltins* safe_builtins() { return &safe_builtins_; }
78 const SafeBuiltins* safe_builtins() const { return &safe_builtins_; }
80 // Returns the ID of the extension associated with this context, or empty
81 // string if there is no such extension.
82 const std::string& GetExtensionID() const;
84 // Returns the RenderView associated with this context. Can return NULL if the
85 // context is in the process of being destroyed.
86 content::RenderView* GetRenderView() const;
88 // Returns the RenderFrame associated with this context. Can return NULL if
89 // the context is in the process of being destroyed.
90 content::RenderFrame* GetRenderFrame() const;
92 // Runs |function| with appropriate scopes. Doesn't catch exceptions, callers
93 // must do that if they want.
95 // USE THIS METHOD RATHER THAN v8::Function::Call WHEREVER POSSIBLE.
96 v8::Local<v8::Value> CallFunction(v8::Handle<v8::Function> function,
97 int argc,
98 v8::Handle<v8::Value> argv[]) const;
100 void DispatchEvent(const char* event_name, v8::Handle<v8::Array> args) const;
102 // Fires the onunload event on the unload_event module.
103 void DispatchOnUnloadEvent();
105 // Returns the availability of the API |api_name|.
106 Feature::Availability GetAvailability(const std::string& api_name);
108 // Returns a string description of the type of context this is.
109 std::string GetContextTypeDescription();
111 // Returns a string description of the effective type of context this is.
112 std::string GetEffectiveContextTypeDescription();
114 v8::Isolate* isolate() const { return isolate_; }
116 // Get the URL of this context's web frame.
117 GURL GetURL() const;
119 // Returns whether the API |api| or any part of the API could be
120 // available in this context without taking into account the context's
121 // extension.
122 bool IsAnyFeatureAvailableToContext(const extensions::Feature& api);
124 // Utility to get the URL we will match against for a frame. If the frame has
125 // committed, this is the commited URL. Otherwise it is the provisional URL.
126 // The returned URL may be invalid.
127 static GURL GetDataSourceURLForFrame(const blink::WebFrame* frame);
129 // Returns the first non-about:-URL in the document hierarchy above and
130 // including |frame|. The document hierarchy is only traversed if
131 // |document_url| is an about:-URL and if |match_about_blank| is true.
132 static GURL GetEffectiveDocumentURL(const blink::WebFrame* frame,
133 const GURL& document_url,
134 bool match_about_blank);
136 // RequestSender::Source implementation.
137 ScriptContext* GetContext() override;
138 void OnResponseReceived(const std::string& name,
139 int request_id,
140 bool success,
141 const base::ListValue& response,
142 const std::string& error) override;
144 // Grants a set of content capabilities to this context.
145 void SetContentCapabilities(const APIPermissionSet& permissions);
147 // Indicates if this context has an effective API permission either by being
148 // a context for an extension which has that permission, or by being a web
149 // context which has been granted the corresponding capability by an
150 // extension.
151 bool HasAPIPermission(APIPermission::ID permission) const;
153 protected:
154 // The v8 context the bindings are accessible to.
155 v8::Global<v8::Context> v8_context_;
157 private:
158 class Runner;
160 // The WebFrame associated with this context. This can be NULL because this
161 // object can outlive is destroyed asynchronously.
162 blink::WebFrame* web_frame_;
164 // The extension associated with this context, or NULL if there is none. This
165 // might be a hosted app in the case that this context is hosting a web URL.
166 scoped_refptr<const Extension> extension_;
168 // The type of context.
169 Feature::Context context_type_;
171 // The effective extension associated with this context, or NULL if there is
172 // none. This is different from the above extension if this context is in an
173 // about:blank iframe for example.
174 scoped_refptr<const Extension> effective_extension_;
176 // The type of context.
177 Feature::Context effective_context_type_;
179 // Owns and structures the JS that is injected to set up extension bindings.
180 scoped_ptr<ModuleSystem> module_system_;
182 // Contains safe copies of builtin objects like Function.prototype.
183 SafeBuiltins safe_builtins_;
185 // The set of capabilities granted to this context by extensions.
186 APIPermissionSet content_capabilities_;
188 v8::Isolate* isolate_;
190 GURL url_;
192 scoped_ptr<Runner> runner_;
194 DISALLOW_COPY_AND_ASSIGN(ScriptContext);
197 } // namespace extensions
199 #endif // EXTENSIONS_RENDERER_SCRIPT_CONTEXT_H_