1 // Copyright (c) 2012 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 CHROME_RENDERER_EXTENSIONS_CHROME_V8_CONTEXT_H_
6 #define CHROME_RENDERER_EXTENSIONS_CHROME_V8_CONTEXT_H_
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "chrome/renderer/extensions/module_system.h"
13 #include "chrome/renderer/extensions/request_sender.h"
14 #include "chrome/renderer/extensions/safe_builtins.h"
15 #include "chrome/renderer/extensions/scoped_persistent.h"
16 #include "extensions/common/features/feature.h"
17 #include "v8/include/v8.h"
27 namespace extensions {
30 // Chrome's wrapper for a v8 context.
31 class ChromeV8Context : public RequestSender::Source {
33 ChromeV8Context(v8::Handle<v8::Context> context,
34 WebKit::WebFrame* frame,
35 const Extension* extension,
36 Feature::Context context_type);
37 virtual ~ChromeV8Context();
39 // Clears the WebFrame for this contexts and invalidates the associated
43 // Returns true if this context is still valid, false if it isn't.
44 // A context becomes invalid via Invalidate().
45 bool is_valid() const {
46 return !v8_context_.IsEmpty();
49 v8::Handle<v8::Context> v8_context() const {
50 return v8_context_.NewHandle(v8::Isolate::GetCurrent());
53 const Extension* extension() const {
54 return extension_.get();
57 WebKit::WebFrame* web_frame() const {
61 Feature::Context context_type() const {
65 void set_module_system(scoped_ptr<ModuleSystem> module_system) {
66 module_system_ = module_system.Pass();
69 ModuleSystem* module_system() { return module_system_.get(); }
71 SafeBuiltins* safe_builtins() {
72 return &safe_builtins_;
74 const SafeBuiltins* safe_builtins() const {
75 return &safe_builtins_;
78 // Returns the ID of the extension associated with this context, or empty
79 // string if there is no such extension.
80 std::string GetExtensionID() const;
82 // Returns the RenderView associated with this context. Can return NULL if the
83 // context is in the process of being destroyed.
84 content::RenderView* GetRenderView() const;
86 // Get the URL of this context's web frame.
89 // Runs |function| with appropriate scopes. Doesn't catch exceptions, callers
90 // must do that if they want.
92 // USE THIS METHOD RATHER THAN v8::Function::Call WHEREVER POSSIBLE.
93 v8::Local<v8::Value> CallFunction(v8::Handle<v8::Function> function,
95 v8::Handle<v8::Value> argv[]) const;
97 // Fires the onunload event on the unload_event module.
98 void DispatchOnUnloadEvent();
100 // Returns the availability of the API |api_name|.
101 Feature::Availability GetAvailability(const std::string& api_name);
103 // Returns whether the API |api_name| or any part of the API could be
104 // available in this context without taking into account the context's
106 bool IsAnyFeatureAvailableToContext(const std::string& api_name);
108 // Returns a string description of the type of context this is.
109 std::string GetContextTypeDescription();
111 // RequestSender::Source implementation.
112 virtual ChromeV8Context* GetContext() OVERRIDE;
113 virtual void OnResponseReceived(const std::string& name,
116 const base::ListValue& response,
117 const std::string& error) OVERRIDE;
119 v8::Isolate* isolate() const {
124 // The v8 context the bindings are accessible to.
125 ScopedPersistent<v8::Context> v8_context_;
127 // The WebFrame associated with this context. This can be NULL because this
128 // object can outlive is destroyed asynchronously.
129 WebKit::WebFrame* web_frame_;
131 // The extension associated with this context, or NULL if there is none. This
132 // might be a hosted app in the case that this context is hosting a web URL.
133 scoped_refptr<const Extension> extension_;
135 // The type of context.
136 Feature::Context context_type_;
138 // Owns and structures the JS that is injected to set up extension bindings.
139 scoped_ptr<ModuleSystem> module_system_;
141 // Contains safe copies of builtin objects like Function.prototype.
142 SafeBuiltins safe_builtins_;
144 v8::Isolate* isolate_;
146 DISALLOW_COPY_AND_ASSIGN(ChromeV8Context);
149 } // namespace extensions
151 #endif // CHROME_RENDERER_EXTENSIONS_CHROME_V8_CONTEXT_H_