ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / extensions / renderer / script_context.h
blob5fd87df9b807c3717829ff089f762cb21349e70d
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 "extensions/renderer/scoped_persistent.h"
18 #include "gin/runner.h"
19 #include "url/gurl.h"
20 #include "v8/include/v8.h"
22 namespace blink {
23 class WebFrame;
26 namespace content {
27 class RenderFrame;
28 class RenderView;
31 namespace extensions {
32 class Extension;
34 // Extensions wrapper for a v8 context.
35 class ScriptContext : public RequestSender::Source {
36 public:
37 ScriptContext(const v8::Handle<v8::Context>& context,
38 blink::WebFrame* frame,
39 const Extension* extension,
40 Feature::Context context_type,
41 const Extension* effective_extension,
42 Feature::Context effective_context_type);
43 ~ScriptContext() override;
45 // Clears the WebFrame for this contexts and invalidates the associated
46 // ModuleSystem.
47 void Invalidate();
49 // Returns true if this context is still valid, false if it isn't.
50 // A context becomes invalid via Invalidate().
51 bool is_valid() const { return !v8_context_.IsEmpty(); }
53 v8::Handle<v8::Context> v8_context() const {
54 return v8_context_.NewHandle(isolate());
57 const Extension* extension() const { return extension_.get(); }
59 const Extension* effective_extension() const {
60 return effective_extension_.get();
63 blink::WebFrame* web_frame() const { return web_frame_; }
65 Feature::Context context_type() const { return context_type_; }
67 Feature::Context effective_context_type() const {
68 return effective_context_type_;
71 void set_module_system(scoped_ptr<ModuleSystem> module_system) {
72 module_system_ = module_system.Pass();
75 ModuleSystem* module_system() { return module_system_.get(); }
77 SafeBuiltins* safe_builtins() { return &safe_builtins_; }
79 const SafeBuiltins* safe_builtins() const { return &safe_builtins_; }
81 // Returns the ID of the extension associated with this context, or empty
82 // string if there is no such extension.
83 const std::string& GetExtensionID() const;
85 // Returns the RenderView associated with this context. Can return NULL if the
86 // context is in the process of being destroyed.
87 content::RenderView* GetRenderView() const;
89 // Returns the RenderFrame associated with this context. Can return NULL if
90 // the context is in the process of being destroyed.
91 content::RenderFrame* GetRenderFrame() const;
93 // Runs |function| with appropriate scopes. Doesn't catch exceptions, callers
94 // must do that if they want.
96 // USE THIS METHOD RATHER THAN v8::Function::Call WHEREVER POSSIBLE.
97 v8::Local<v8::Value> CallFunction(v8::Handle<v8::Function> function,
98 int argc,
99 v8::Handle<v8::Value> argv[]) const;
101 void DispatchEvent(const char* event_name, v8::Handle<v8::Array> args) const;
103 // Fires the onunload event on the unload_event module.
104 void DispatchOnUnloadEvent();
106 // Returns the availability of the API |api_name|.
107 Feature::Availability GetAvailability(const std::string& api_name);
109 // Returns a string description of the type of context this is.
110 std::string GetContextTypeDescription();
112 // Returns a string description of the effective type of context this is.
113 std::string GetEffectiveContextTypeDescription();
115 v8::Isolate* isolate() const { return isolate_; }
117 // Get the URL of this context's web frame.
118 GURL GetURL() const;
120 // Returns whether the API |api| or any part of the API could be
121 // available in this context without taking into account the context's
122 // extension.
123 bool IsAnyFeatureAvailableToContext(const extensions::Feature& api);
125 // Utility to get the URL we will match against for a frame. If the frame has
126 // committed, this is the commited URL. Otherwise it is the provisional URL.
127 // The returned URL may be invalid.
128 static GURL GetDataSourceURLForFrame(const blink::WebFrame* frame);
130 // Returns the first non-about:-URL in the document hierarchy above and
131 // including |frame|. The document hierarchy is only traversed if
132 // |document_url| is an about:-URL and if |match_about_blank| is true.
133 static GURL GetEffectiveDocumentURL(const blink::WebFrame* frame,
134 const GURL& document_url,
135 bool match_about_blank);
137 // RequestSender::Source implementation.
138 ScriptContext* GetContext() override;
139 void OnResponseReceived(const std::string& name,
140 int request_id,
141 bool success,
142 const base::ListValue& response,
143 const std::string& error) override;
145 // Grants a set of content capabilities to this context.
146 void SetContentCapabilities(const APIPermissionSet& permissions);
148 // Indicates if this context has an effective API permission either by being
149 // a context for an extension which has that permission, or by being a web
150 // context which has been granted the corresponding capability by an
151 // extension.
152 bool HasAPIPermission(APIPermission::ID permission) const;
154 protected:
155 // The v8 context the bindings are accessible to.
156 ScopedPersistent<v8::Context> v8_context_;
158 private:
159 class Runner;
161 // The WebFrame associated with this context. This can be NULL because this
162 // object can outlive is destroyed asynchronously.
163 blink::WebFrame* web_frame_;
165 // The extension associated with this context, or NULL if there is none. This
166 // might be a hosted app in the case that this context is hosting a web URL.
167 scoped_refptr<const Extension> extension_;
169 // The type of context.
170 Feature::Context context_type_;
172 // The effective extension associated with this context, or NULL if there is
173 // none. This is different from the above extension if this context is in an
174 // about:blank iframe for example.
175 scoped_refptr<const Extension> effective_extension_;
177 // The type of context.
178 Feature::Context effective_context_type_;
180 // Owns and structures the JS that is injected to set up extension bindings.
181 scoped_ptr<ModuleSystem> module_system_;
183 // Contains safe copies of builtin objects like Function.prototype.
184 SafeBuiltins safe_builtins_;
186 // The set of capabilities granted to this context by extensions.
187 APIPermissionSet content_capabilities_;
189 v8::Isolate* isolate_;
191 GURL url_;
193 scoped_ptr<Runner> runner_;
195 DISALLOW_COPY_AND_ASSIGN(ScriptContext);
198 } // namespace extensions
200 #endif // EXTENSIONS_RENDERER_SCRIPT_CONTEXT_H_