ui: Remove some unnecessary functions from the API.
[chromium-blink-merge.git] / extensions / renderer / script_context.h
blob7b19ac14dc0864b120a3c9eed3804096f4c768e9
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/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"
20 namespace blink {
21 class WebFrame;
24 namespace content {
25 class RenderView;
28 namespace extensions {
29 class Extension;
31 // Extensions wrapper for a v8 context.
32 class ScriptContext : public RequestSender::Source, public gin::Runner {
33 public:
34 ScriptContext(const v8::Handle<v8::Context>& context,
35 blink::WebFrame* frame,
36 const Extension* extension,
37 Feature::Context context_type);
38 virtual ~ScriptContext();
40 // Clears the WebFrame for this contexts and invalidates the associated
41 // ModuleSystem.
42 void Invalidate();
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(); }
48 v8::Handle<v8::Context> v8_context() const {
49 return v8_context_.NewHandle(isolate());
52 const Extension* extension() const { return extension_.get(); }
54 blink::WebFrame* web_frame() const { return web_frame_; }
56 Feature::Context context_type() const { return context_type_; }
58 void set_module_system(scoped_ptr<ModuleSystem> module_system) {
59 module_system_ = module_system.Pass();
62 ModuleSystem* module_system() { return module_system_.get(); }
64 SafeBuiltins* safe_builtins() { return &safe_builtins_; }
66 const SafeBuiltins* safe_builtins() const { return &safe_builtins_; }
68 // Returns the ID of the extension associated with this context, or empty
69 // string if there is no such extension.
70 const std::string& GetExtensionID() const;
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;
76 // Runs |function| with appropriate scopes. Doesn't catch exceptions, callers
77 // must do that if they want.
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;
84 void DispatchEvent(const char* event_name, v8::Handle<v8::Array> args) const;
86 // Fires the onunload event on the unload_event module.
87 void DispatchOnUnloadEvent();
89 // Returns the availability of the API |api_name|.
90 Feature::Availability GetAvailability(const std::string& api_name);
92 // Returns a string description of the type of context this is.
93 std::string GetContextTypeDescription();
95 v8::Isolate* isolate() const { return isolate_; }
97 // Get the URL of this context's web frame.
98 GURL GetURL() const;
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);
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);
109 // 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);
116 // 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;
124 // 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;
133 protected:
134 // The v8 context the bindings are accessible to.
135 ScopedPersistent<v8::Context> v8_context_;
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_;
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_;
146 // The type of context.
147 Feature::Context context_type_;
149 // Owns and structures the JS that is injected to set up extension bindings.
150 scoped_ptr<ModuleSystem> module_system_;
152 // Contains safe copies of builtin objects like Function.prototype.
153 SafeBuiltins safe_builtins_;
155 v8::Isolate* isolate_;
157 DISALLOW_COPY_AND_ASSIGN(ScriptContext);
160 } // namespace extensions
162 #endif // EXTENSIONS_RENDERER_SCRIPT_CONTEXT_H_