blob: 7b19ac14dc0864b120a3c9eed3804096f4c768e9 [file] [log] [blame]
[email protected]f55c90ee62014-04-12 00:50:031// 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.
4
5#ifndef EXTENSIONS_RENDERER_SCRIPT_CONTEXT_H_
6#define EXTENSIONS_RENDERER_SCRIPT_CONTEXT_H_
7
8#include <string>
9
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"
[email protected]d9f51dad2014-07-09 05:39:3817#include "gin/runner.h"
[email protected]f55c90ee62014-04-12 00:50:0318#include "v8/include/v8.h"
19
20namespace blink {
21class WebFrame;
22}
23
24namespace content {
25class RenderView;
26}
27
28namespace extensions {
29class Extension;
30
31// Extensions wrapper for a v8 context.
[email protected]d9f51dad2014-07-09 05:39:3832class ScriptContext : public RequestSender::Source, public gin::Runner {
[email protected]f55c90ee62014-04-12 00:50:0333 public:
[email protected]e6893672014-05-01 17:29:1334 ScriptContext(const v8::Handle<v8::Context>& context,
[email protected]f55c90ee62014-04-12 00:50:0335 blink::WebFrame* frame,
36 const Extension* extension,
37 Feature::Context context_type);
38 virtual ~ScriptContext();
39
40 // Clears the WebFrame for this contexts and invalidates the associated
41 // ModuleSystem.
42 void Invalidate();
43
44 // Returns true if this context is still valid, false if it isn't.
45 // A context becomes invalid via Invalidate().
46 bool is_valid() const { return !v8_context_.IsEmpty(); }
47
48 v8::Handle<v8::Context> v8_context() const {
[email protected]800f9872014-06-12 04:12:5149 return v8_context_.NewHandle(isolate());
[email protected]f55c90ee62014-04-12 00:50:0350 }
51
52 const Extension* extension() const { return extension_.get(); }
53
54 blink::WebFrame* web_frame() const { return web_frame_; }
55
56 Feature::Context context_type() const { return context_type_; }
57
58 void set_module_system(scoped_ptr<ModuleSystem> module_system) {
59 module_system_ = module_system.Pass();
60 }
61
62 ModuleSystem* module_system() { return module_system_.get(); }
63
64 SafeBuiltins* safe_builtins() { return &safe_builtins_; }
65
66 const SafeBuiltins* safe_builtins() const { return &safe_builtins_; }
67
68 // Returns the ID of the extension associated with this context, or empty
69 // string if there is no such extension.
[email protected]800f9872014-06-12 04:12:5170 const std::string& GetExtensionID() const;
[email protected]f55c90ee62014-04-12 00:50:0371
72 // Returns the RenderView associated with this context. Can return NULL if the
73 // context is in the process of being destroyed.
74 content::RenderView* GetRenderView() const;
75
76 // Runs |function| with appropriate scopes. Doesn't catch exceptions, callers
77 // must do that if they want.
78 //
79 // USE THIS METHOD RATHER THAN v8::Function::Call WHEREVER POSSIBLE.
80 v8::Local<v8::Value> CallFunction(v8::Handle<v8::Function> function,
81 int argc,
82 v8::Handle<v8::Value> argv[]) const;
83
84 void DispatchEvent(const char* event_name, v8::Handle<v8::Array> args) const;
85
86 // Fires the onunload event on the unload_event module.
87 void DispatchOnUnloadEvent();
88
89 // Returns the availability of the API |api_name|.
90 Feature::Availability GetAvailability(const std::string& api_name);
91
92 // Returns a string description of the type of context this is.
93 std::string GetContextTypeDescription();
94
95 v8::Isolate* isolate() const { return isolate_; }
96
97 // Get the URL of this context's web frame.
98 GURL GetURL() const;
99
100 // Returns whether the API |api| or any part of the API could be
101 // available in this context without taking into account the context's
102 // extension.
103 bool IsAnyFeatureAvailableToContext(const extensions::Feature& api);
104
105 // Utility to get the URL we will match against for a frame. If the frame has
106 // committed, this is the commited URL. Otherwise it is the provisional URL.
107 static GURL GetDataSourceURLForFrame(const blink::WebFrame* frame);
108
[email protected]ae26b282014-05-15 16:40:16109 // Returns the first non-about:-URL in the document hierarchy above and
110 // including |frame|. The document hierarchy is only traversed if
111 // |document_url| is an about:-URL and if |match_about_blank| is true.
112 static GURL GetEffectiveDocumentURL(const blink::WebFrame* frame,
113 const GURL& document_url,
114 bool match_about_blank);
115
[email protected]f55c90ee62014-04-12 00:50:03116 // RequestSender::Source implementation.
117 virtual ScriptContext* GetContext() OVERRIDE;
118 virtual void OnResponseReceived(const std::string& name,
119 int request_id,
120 bool success,
121 const base::ListValue& response,
122 const std::string& error) OVERRIDE;
123
[email protected]d9f51dad2014-07-09 05:39:38124 // gin::Runner overrides.
125 virtual void Run(const std::string& source,
126 const std::string& resource_name) OVERRIDE;
127 virtual v8::Handle<v8::Value> Call(v8::Handle<v8::Function> function,
128 v8::Handle<v8::Value> receiver,
129 int argc,
130 v8::Handle<v8::Value> argv[]) OVERRIDE;
131 virtual gin::ContextHolder* GetContextHolder() OVERRIDE;
132
[email protected]f55c90ee62014-04-12 00:50:03133 protected:
134 // The v8 context the bindings are accessible to.
135 ScopedPersistent<v8::Context> v8_context_;
136
137 private:
138 // The WebFrame associated with this context. This can be NULL because this
139 // object can outlive is destroyed asynchronously.
140 blink::WebFrame* web_frame_;
141
142 // The extension associated with this context, or NULL if there is none. This
143 // might be a hosted app in the case that this context is hosting a web URL.
144 scoped_refptr<const Extension> extension_;
145
146 // The type of context.
147 Feature::Context context_type_;
148
149 // Owns and structures the JS that is injected to set up extension bindings.
150 scoped_ptr<ModuleSystem> module_system_;
151
152 // Contains safe copies of builtin objects like Function.prototype.
153 SafeBuiltins safe_builtins_;
154
155 v8::Isolate* isolate_;
156
157 DISALLOW_COPY_AND_ASSIGN(ScriptContext);
158};
159
160} // namespace extensions
161
162#endif // EXTENSIONS_RENDERER_SCRIPT_CONTEXT_H_