Add UMA histograms and logging for bad IPC message handling
[chromium-blink-merge.git] / extensions / renderer / script_context.h
blob745f522c2c4328990b44506d24ba0723a4241d83
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 "gin/runner.h"
18 #include "url/gurl.h"
19 #include "v8/include/v8.h"
21 namespace blink {
22 class WebFrame;
23 class WebLocalFrame;
26 namespace content {
27 class RenderFrame;
28 class RenderView;
31 namespace extensions {
32 class Extension;
33 class ExtensionSet;
35 // Extensions wrapper for a v8 context.
36 class ScriptContext : public RequestSender::Source {
37 public:
38 ScriptContext(const v8::Handle<v8::Context>& context,
39 blink::WebLocalFrame* frame,
40 const Extension* extension,
41 Feature::Context context_type,
42 const Extension* effective_extension,
43 Feature::Context effective_context_type);
44 ~ScriptContext() override;
46 // Returns whether |url| is sandboxed (as declared in any Extension in
47 // |extension_set| as sandboxed).
49 // Declared in ScriptContext for lack of a better place, but this should
50 // become unnecessary at some point as crbug.com/466373 is worked on.
51 static bool IsSandboxedPage(const ExtensionSet& extension_set,
52 const GURL& url);
54 // Clears the WebFrame for this contexts and invalidates the associated
55 // ModuleSystem.
56 void Invalidate();
58 // Returns true if this context is still valid, false if it isn't.
59 // A context becomes invalid via Invalidate().
60 bool is_valid() const { return !v8_context_.IsEmpty(); }
62 v8::Handle<v8::Context> v8_context() const {
63 return v8::Local<v8::Context>::New(isolate_, v8_context_);
66 const Extension* extension() const { return extension_.get(); }
68 const Extension* effective_extension() const {
69 return effective_extension_.get();
72 blink::WebLocalFrame* web_frame() const { return web_frame_; }
74 Feature::Context context_type() const { return context_type_; }
76 Feature::Context effective_context_type() const {
77 return effective_context_type_;
80 void set_module_system(scoped_ptr<ModuleSystem> module_system) {
81 module_system_ = module_system.Pass();
84 ModuleSystem* module_system() { return module_system_.get(); }
86 SafeBuiltins* safe_builtins() { return &safe_builtins_; }
88 const SafeBuiltins* safe_builtins() const { return &safe_builtins_; }
90 // Returns the ID of the extension associated with this context, or empty
91 // string if there is no such extension.
92 const std::string& GetExtensionID() const;
94 // Returns the RenderView associated with this context. Can return NULL if the
95 // context is in the process of being destroyed.
96 content::RenderView* GetRenderView() const;
98 // Returns the RenderFrame associated with this context. Can return NULL if
99 // the context is in the process of being destroyed.
100 content::RenderFrame* GetRenderFrame() const;
102 // Runs |function| with appropriate scopes. Doesn't catch exceptions, callers
103 // must do that if they want.
105 // USE THIS METHOD RATHER THAN v8::Function::Call WHEREVER POSSIBLE.
106 v8::Local<v8::Value> CallFunction(v8::Handle<v8::Function> function,
107 int argc,
108 v8::Handle<v8::Value> argv[]) const;
110 void DispatchEvent(const char* event_name, v8::Handle<v8::Array> args) const;
112 // Fires the onunload event on the unload_event module.
113 void DispatchOnUnloadEvent();
115 // Returns the availability of the API |api_name|.
116 Feature::Availability GetAvailability(const std::string& api_name);
118 // Returns a string description of the type of context this is.
119 std::string GetContextTypeDescription();
121 // Returns a string description of the effective type of context this is.
122 std::string GetEffectiveContextTypeDescription();
124 v8::Isolate* isolate() const { return isolate_; }
126 // Get the URL of this context's web frame.
128 // TODO(kalman): Remove this and replace with a GetOrigin() call which reads
129 // of WebDocument::securityOrigin():
130 // - The URL can change (e.g. pushState) but the origin cannot. Luckily it
131 // appears as though callers don't make security decisions based on the
132 // result of GetURL() so it's not a problem... yet.
133 // - Origin is the correct check to be making.
134 // - It might let us remove the about:blank resolving?
135 GURL GetURL() const;
137 // Returns whether the API |api| or any part of the API could be
138 // available in this context without taking into account the context's
139 // extension.
140 bool IsAnyFeatureAvailableToContext(const extensions::Feature& api);
142 // Utility to get the URL we will match against for a frame. If the frame has
143 // committed, this is the commited URL. Otherwise it is the provisional URL.
144 // The returned URL may be invalid.
145 static GURL GetDataSourceURLForFrame(const blink::WebFrame* frame);
147 // Returns the first non-about:-URL in the document hierarchy above and
148 // including |frame|. The document hierarchy is only traversed if
149 // |document_url| is an about:-URL and if |match_about_blank| is true.
150 static GURL GetEffectiveDocumentURL(const blink::WebFrame* frame,
151 const GURL& document_url,
152 bool match_about_blank);
154 // RequestSender::Source implementation.
155 ScriptContext* GetContext() override;
156 void OnResponseReceived(const std::string& name,
157 int request_id,
158 bool success,
159 const base::ListValue& response,
160 const std::string& error) override;
162 // Grants a set of content capabilities to this context.
163 void SetContentCapabilities(const APIPermissionSet& permissions);
165 // Indicates if this context has an effective API permission either by being
166 // a context for an extension which has that permission, or by being a web
167 // context which has been granted the corresponding capability by an
168 // extension.
169 bool HasAPIPermission(APIPermission::ID permission) const;
171 protected:
172 // The v8 context the bindings are accessible to.
173 v8::Global<v8::Context> v8_context_;
175 private:
176 class Runner;
178 // The WebLocalFrame associated with this context. This can be NULL because
179 // this object can outlive is destroyed asynchronously.
180 blink::WebLocalFrame* web_frame_;
182 // The extension associated with this context, or NULL if there is none. This
183 // might be a hosted app in the case that this context is hosting a web URL.
184 scoped_refptr<const Extension> extension_;
186 // The type of context.
187 Feature::Context context_type_;
189 // The effective extension associated with this context, or NULL if there is
190 // none. This is different from the above extension if this context is in an
191 // about:blank iframe for example.
192 scoped_refptr<const Extension> effective_extension_;
194 // The type of context.
195 Feature::Context effective_context_type_;
197 // Owns and structures the JS that is injected to set up extension bindings.
198 scoped_ptr<ModuleSystem> module_system_;
200 // Contains safe copies of builtin objects like Function.prototype.
201 SafeBuiltins safe_builtins_;
203 // The set of capabilities granted to this context by extensions.
204 APIPermissionSet content_capabilities_;
206 v8::Isolate* isolate_;
208 GURL url_;
210 scoped_ptr<Runner> runner_;
212 DISALLOW_COPY_AND_ASSIGN(ScriptContext);
215 } // namespace extensions
217 #endif // EXTENSIONS_RENDERER_SCRIPT_CONTEXT_H_