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_
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "extensions/common/features/feature.h"
13 #include "extensions/renderer/module_system.h"
14 #include "extensions/renderer/request_sender.h"
15 #include "extensions/renderer/safe_builtins.h"
16 #include "extensions/renderer/scoped_persistent.h"
17 #include "gin/runner.h"
18 #include "v8/include/v8.h"
29 namespace extensions
{
32 // Extensions wrapper for a v8 context.
33 class ScriptContext
: public RequestSender::Source
, public gin::Runner
{
35 ScriptContext(const v8::Handle
<v8::Context
>& context
,
36 blink::WebFrame
* frame
,
37 const Extension
* extension
,
38 Feature::Context context_type
);
39 virtual ~ScriptContext();
41 // Clears the WebFrame for this contexts and invalidates the associated
45 // Returns true if this context is still valid, false if it isn't.
46 // A context becomes invalid via Invalidate().
47 bool is_valid() const { return !v8_context_
.IsEmpty(); }
49 v8::Handle
<v8::Context
> v8_context() const {
50 return v8_context_
.NewHandle(isolate());
53 const Extension
* extension() const { return extension_
.get(); }
55 blink::WebFrame
* web_frame() const { return web_frame_
; }
57 Feature::Context
context_type() const { return context_type_
; }
59 void set_module_system(scoped_ptr
<ModuleSystem
> module_system
) {
60 module_system_
= module_system
.Pass();
63 ModuleSystem
* module_system() { return module_system_
.get(); }
65 SafeBuiltins
* safe_builtins() { return &safe_builtins_
; }
67 const SafeBuiltins
* safe_builtins() const { return &safe_builtins_
; }
69 // Returns the ID of the extension associated with this context, or empty
70 // string if there is no such extension.
71 const std::string
& GetExtensionID() const;
73 // Returns the RenderView associated with this context. Can return NULL if the
74 // context is in the process of being destroyed.
75 content::RenderView
* GetRenderView() const;
77 // Returns the RenderFrame associated with this context. Can return NULL if
78 // the context is in the process of being destroyed.
79 content::RenderFrame
* GetRenderFrame() const;
81 // Runs |function| with appropriate scopes. Doesn't catch exceptions, callers
82 // must do that if they want.
84 // USE THIS METHOD RATHER THAN v8::Function::Call WHEREVER POSSIBLE.
85 v8::Local
<v8::Value
> CallFunction(v8::Handle
<v8::Function
> function
,
87 v8::Handle
<v8::Value
> argv
[]) const;
89 void DispatchEvent(const char* event_name
, v8::Handle
<v8::Array
> args
) const;
91 // Fires the onunload event on the unload_event module.
92 void DispatchOnUnloadEvent();
94 // Returns the availability of the API |api_name|.
95 Feature::Availability
GetAvailability(const std::string
& api_name
);
97 // Returns a string description of the type of context this is.
98 std::string
GetContextTypeDescription();
100 v8::Isolate
* isolate() const { return isolate_
; }
102 // Get the URL of this context's web frame.
105 // Returns whether the API |api| or any part of the API could be
106 // available in this context without taking into account the context's
108 bool IsAnyFeatureAvailableToContext(const extensions::Feature
& api
);
110 // Utility to get the URL we will match against for a frame. If the frame has
111 // committed, this is the commited URL. Otherwise it is the provisional URL.
112 // The returned URL may be invalid.
113 static GURL
GetDataSourceURLForFrame(const blink::WebFrame
* frame
);
115 // Returns the first non-about:-URL in the document hierarchy above and
116 // including |frame|. The document hierarchy is only traversed if
117 // |document_url| is an about:-URL and if |match_about_blank| is true.
118 static GURL
GetEffectiveDocumentURL(const blink::WebFrame
* frame
,
119 const GURL
& document_url
,
120 bool match_about_blank
);
122 // RequestSender::Source implementation.
123 virtual ScriptContext
* GetContext() OVERRIDE
;
124 virtual void OnResponseReceived(const std::string
& name
,
127 const base::ListValue
& response
,
128 const std::string
& error
) OVERRIDE
;
130 // gin::Runner overrides.
131 virtual void Run(const std::string
& source
,
132 const std::string
& resource_name
) OVERRIDE
;
133 virtual v8::Handle
<v8::Value
> Call(v8::Handle
<v8::Function
> function
,
134 v8::Handle
<v8::Value
> receiver
,
136 v8::Handle
<v8::Value
> argv
[]) OVERRIDE
;
137 virtual gin::ContextHolder
* GetContextHolder() OVERRIDE
;
140 // The v8 context the bindings are accessible to.
141 ScopedPersistent
<v8::Context
> v8_context_
;
144 // The WebFrame associated with this context. This can be NULL because this
145 // object can outlive is destroyed asynchronously.
146 blink::WebFrame
* web_frame_
;
148 // The extension associated with this context, or NULL if there is none. This
149 // might be a hosted app in the case that this context is hosting a web URL.
150 scoped_refptr
<const Extension
> extension_
;
152 // The type of context.
153 Feature::Context context_type_
;
155 // Owns and structures the JS that is injected to set up extension bindings.
156 scoped_ptr
<ModuleSystem
> module_system_
;
158 // Contains safe copies of builtin objects like Function.prototype.
159 SafeBuiltins safe_builtins_
;
161 v8::Isolate
* isolate_
;
163 DISALLOW_COPY_AND_ASSIGN(ScriptContext
);
166 } // namespace extensions
168 #endif // EXTENSIONS_RENDERER_SCRIPT_CONTEXT_H_