[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 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. |
| 4 | |
| 5 | #ifndef EXTENSIONS_RENDERER_SCRIPT_CONTEXT_H_ |
| 6 | #define EXTENSIONS_RENDERER_SCRIPT_CONTEXT_H_ |
| 7 | |
| 8 | #include <string> |
kalman | b0c1c50 | 2015-04-15 00:25:06 | [diff] [blame] | 9 | #include <vector> |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 10 | |
| 11 | #include "base/basictypes.h" |
kalman | b0c1c50 | 2015-04-15 00:25:06 | [diff] [blame] | 12 | #include "base/callback.h" |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 13 | #include "base/compiler_specific.h" |
| 14 | #include "extensions/common/features/feature.h" |
rockot | e261b16 | 2014-12-12 01:59:47 | [diff] [blame] | 15 | #include "extensions/common/permissions/api_permission_set.h" |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 16 | #include "extensions/renderer/module_system.h" |
| 17 | #include "extensions/renderer/request_sender.h" |
kalman | 33076cb | 2015-08-11 19:12:07 | [diff] [blame] | 18 | #include "extensions/renderer/safe_builtins.h" |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 19 | #include "gin/runner.h" |
Sadrul Habib Chowdhury | 0d7ef9f | 2014-12-03 20:07:30 | [diff] [blame] | 20 | #include "url/gurl.h" |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 21 | #include "v8/include/v8.h" |
| 22 | |
| 23 | namespace blink { |
| 24 | class WebFrame; |
kalman | f91cb89 | 2015-04-15 19:20:48 | [diff] [blame] | 25 | class WebLocalFrame; |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | namespace content { |
[email protected] | 2101c4c | 2014-08-22 00:16:16 | [diff] [blame] | 29 | class RenderFrame; |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | namespace extensions { |
| 33 | class Extension; |
| 34 | |
| 35 | // Extensions wrapper for a v8 context. |
sammc | de54a47e | 2015-01-13 23:16:34 | [diff] [blame] | 36 | class ScriptContext : public RequestSender::Source { |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 37 | public: |
annekao | 53348222 | 2015-08-21 23:23:53 | [diff] [blame^] | 38 | using RunScriptExceptionHandler = base::Callback<void(const v8::TryCatch&)>; |
| 39 | |
tfarina | f85316f | 2015-04-29 17:03:40 | [diff] [blame] | 40 | ScriptContext(const v8::Local<v8::Context>& context, |
kalman | f91cb89 | 2015-04-15 19:20:48 | [diff] [blame] | 41 | blink::WebLocalFrame* frame, |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 42 | const Extension* extension, |
mek | 7e1d745 | 2014-09-08 23:55:57 | [diff] [blame] | 43 | Feature::Context context_type, |
| 44 | const Extension* effective_extension, |
| 45 | Feature::Context effective_context_type); |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 46 | ~ScriptContext() override; |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 47 | |
kalman | c81508d | 2015-04-23 17:14:02 | [diff] [blame] | 48 | // Returns whether |url| from any Extension in |extension_set| is sandboxed, |
| 49 | // as declared in each Extension's manifest. |
| 50 | // TODO(kalman): Delete this when crbug.com/466373 is fixed. |
| 51 | // See comment in HasAccessOrThrowError. |
annekao | 6572d5c | 2015-08-19 16:13:36 | [diff] [blame] | 52 | static bool IsSandboxedPage(const GURL& url); |
kalman | f91cb89 | 2015-04-15 19:20:48 | [diff] [blame] | 53 | |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 54 | // Clears the WebFrame for this contexts and invalidates the associated |
| 55 | // ModuleSystem. |
| 56 | void Invalidate(); |
| 57 | |
kalman | b0c1c50 | 2015-04-15 00:25:06 | [diff] [blame] | 58 | // Registers |observer| to be run when this context is invalidated. Closures |
| 59 | // are run immediately when Invalidate() is called, not in a message loop. |
| 60 | void AddInvalidationObserver(const base::Closure& observer); |
| 61 | |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 62 | // Returns true if this context is still valid, false if it isn't. |
| 63 | // A context becomes invalid via Invalidate(). |
kalman | b0c1c50 | 2015-04-15 00:25:06 | [diff] [blame] | 64 | bool is_valid() const { return is_valid_; } |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 65 | |
tfarina | f85316f | 2015-04-29 17:03:40 | [diff] [blame] | 66 | v8::Local<v8::Context> v8_context() const { |
kalman | 078a219 | 2015-03-09 18:19:39 | [diff] [blame] | 67 | return v8::Local<v8::Context>::New(isolate_, v8_context_); |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | const Extension* extension() const { return extension_.get(); } |
| 71 | |
mek | 7e1d745 | 2014-09-08 23:55:57 | [diff] [blame] | 72 | const Extension* effective_extension() const { |
| 73 | return effective_extension_.get(); |
| 74 | } |
| 75 | |
kalman | f91cb89 | 2015-04-15 19:20:48 | [diff] [blame] | 76 | blink::WebLocalFrame* web_frame() const { return web_frame_; } |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 77 | |
| 78 | Feature::Context context_type() const { return context_type_; } |
| 79 | |
mek | 7e1d745 | 2014-09-08 23:55:57 | [diff] [blame] | 80 | Feature::Context effective_context_type() const { |
| 81 | return effective_context_type_; |
| 82 | } |
| 83 | |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 84 | void set_module_system(scoped_ptr<ModuleSystem> module_system) { |
| 85 | module_system_ = module_system.Pass(); |
| 86 | } |
| 87 | |
| 88 | ModuleSystem* module_system() { return module_system_.get(); } |
| 89 | |
kalman | 33076cb | 2015-08-11 19:12:07 | [diff] [blame] | 90 | SafeBuiltins* safe_builtins() { return &safe_builtins_; } |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 91 | |
kalman | 33076cb | 2015-08-11 19:12:07 | [diff] [blame] | 92 | const SafeBuiltins* safe_builtins() const { return &safe_builtins_; } |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 93 | |
| 94 | // Returns the ID of the extension associated with this context, or empty |
| 95 | // string if there is no such extension. |
[email protected] | 800f987 | 2014-06-12 04:12:51 | [diff] [blame] | 96 | const std::string& GetExtensionID() const; |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 97 | |
[email protected] | 2101c4c | 2014-08-22 00:16:16 | [diff] [blame] | 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; |
| 101 | |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 102 | // Runs |function| with appropriate scopes. Doesn't catch exceptions, callers |
| 103 | // must do that if they want. |
| 104 | // |
| 105 | // USE THIS METHOD RATHER THAN v8::Function::Call WHEREVER POSSIBLE. |
kalman | 70c00e24 | 2015-05-15 23:42:27 | [diff] [blame] | 106 | v8::Local<v8::Value> CallFunction(const v8::Local<v8::Function>& function, |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 107 | int argc, |
tfarina | f85316f | 2015-04-29 17:03:40 | [diff] [blame] | 108 | v8::Local<v8::Value> argv[]) const; |
kalman | 70c00e24 | 2015-05-15 23:42:27 | [diff] [blame] | 109 | v8::Local<v8::Value> CallFunction( |
| 110 | const v8::Local<v8::Function>& function) const; |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 111 | |
tfarina | f85316f | 2015-04-29 17:03:40 | [diff] [blame] | 112 | void DispatchEvent(const char* event_name, v8::Local<v8::Array> args) const; |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 113 | |
| 114 | // Fires the onunload event on the unload_event module. |
| 115 | void DispatchOnUnloadEvent(); |
| 116 | |
| 117 | // Returns the availability of the API |api_name|. |
| 118 | Feature::Availability GetAvailability(const std::string& api_name); |
| 119 | |
| 120 | // Returns a string description of the type of context this is. |
kalman | 8bcbc759 | 2015-06-03 23:12:27 | [diff] [blame] | 121 | std::string GetContextTypeDescription() const; |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 122 | |
mek | 7e1d745 | 2014-09-08 23:55:57 | [diff] [blame] | 123 | // Returns a string description of the effective type of context this is. |
kalman | 8bcbc759 | 2015-06-03 23:12:27 | [diff] [blame] | 124 | std::string GetEffectiveContextTypeDescription() const; |
mek | 7e1d745 | 2014-09-08 23:55:57 | [diff] [blame] | 125 | |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 126 | v8::Isolate* isolate() const { return isolate_; } |
| 127 | |
| 128 | // Get the URL of this context's web frame. |
kalman | f91cb89 | 2015-04-15 19:20:48 | [diff] [blame] | 129 | // |
| 130 | // TODO(kalman): Remove this and replace with a GetOrigin() call which reads |
| 131 | // of WebDocument::securityOrigin(): |
| 132 | // - The URL can change (e.g. pushState) but the origin cannot. Luckily it |
| 133 | // appears as though callers don't make security decisions based on the |
| 134 | // result of GetURL() so it's not a problem... yet. |
| 135 | // - Origin is the correct check to be making. |
| 136 | // - It might let us remove the about:blank resolving? |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 137 | GURL GetURL() const; |
| 138 | |
| 139 | // Returns whether the API |api| or any part of the API could be |
| 140 | // available in this context without taking into account the context's |
| 141 | // extension. |
| 142 | bool IsAnyFeatureAvailableToContext(const extensions::Feature& api); |
| 143 | |
| 144 | // Utility to get the URL we will match against for a frame. If the frame has |
| 145 | // committed, this is the commited URL. Otherwise it is the provisional URL. |
[email protected] | c1abb323 | 2014-07-30 18:28:39 | [diff] [blame] | 146 | // The returned URL may be invalid. |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 147 | static GURL GetDataSourceURLForFrame(const blink::WebFrame* frame); |
| 148 | |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 149 | // Returns the first non-about:-URL in the document hierarchy above and |
| 150 | // including |frame|. The document hierarchy is only traversed if |
| 151 | // |document_url| is an about:-URL and if |match_about_blank| is true. |
| 152 | static GURL GetEffectiveDocumentURL(const blink::WebFrame* frame, |
| 153 | const GURL& document_url, |
| 154 | bool match_about_blank); |
| 155 | |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 156 | // RequestSender::Source implementation. |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 157 | ScriptContext* GetContext() override; |
| 158 | void OnResponseReceived(const std::string& name, |
| 159 | int request_id, |
| 160 | bool success, |
| 161 | const base::ListValue& response, |
| 162 | const std::string& error) override; |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 163 | |
rockot | e261b16 | 2014-12-12 01:59:47 | [diff] [blame] | 164 | // Grants a set of content capabilities to this context. |
| 165 | void SetContentCapabilities(const APIPermissionSet& permissions); |
| 166 | |
| 167 | // Indicates if this context has an effective API permission either by being |
| 168 | // a context for an extension which has that permission, or by being a web |
| 169 | // context which has been granted the corresponding capability by an |
| 170 | // extension. |
| 171 | bool HasAPIPermission(APIPermission::ID permission) const; |
| 172 | |
kalman | c81508d | 2015-04-23 17:14:02 | [diff] [blame] | 173 | // Throws an Error in this context's JavaScript context, if this context does |
| 174 | // not have access to |name|. Returns true if this context has access (i.e. |
| 175 | // no exception thrown), false if it does not (i.e. an exception was thrown). |
| 176 | bool HasAccessOrThrowError(const std::string& name); |
| 177 | |
kalman | 8bcbc759 | 2015-06-03 23:12:27 | [diff] [blame] | 178 | // Returns a string representation of this ScriptContext, for debugging. |
| 179 | std::string GetDebugString() const; |
| 180 | |
dmazzoni | d6848287c | 2015-07-27 23:41:31 | [diff] [blame] | 181 | // Gets the current stack trace as a multi-line string to be logged. |
| 182 | std::string GetStackTraceAsString() const; |
| 183 | |
annekao | 53348222 | 2015-08-21 23:23:53 | [diff] [blame^] | 184 | // Runs |code|, labelling the script that gets created as |name| (the name is |
| 185 | // used in the devtools and stack traces). |exception_handler| will be called |
| 186 | // re-entrantly if an exception is thrown during the script's execution. |
| 187 | v8::Local<v8::Value> RunScript( |
| 188 | v8::Local<v8::String> name, |
| 189 | v8::Local<v8::String> code, |
| 190 | const RunScriptExceptionHandler& exception_handler); |
| 191 | |
mlamouri | 60a2857d | 2015-04-14 15:22:36 | [diff] [blame] | 192 | private: |
| 193 | class Runner; |
| 194 | |
kalman | b0c1c50 | 2015-04-15 00:25:06 | [diff] [blame] | 195 | // Whether this context is valid. |
| 196 | bool is_valid_; |
| 197 | |
| 198 | // The v8 context the bindings are accessible to. |
| 199 | v8::Global<v8::Context> v8_context_; |
| 200 | |
kalman | f91cb89 | 2015-04-15 19:20:48 | [diff] [blame] | 201 | // The WebLocalFrame associated with this context. This can be NULL because |
| 202 | // this object can outlive is destroyed asynchronously. |
| 203 | blink::WebLocalFrame* web_frame_; |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 204 | |
| 205 | // The extension associated with this context, or NULL if there is none. This |
| 206 | // might be a hosted app in the case that this context is hosting a web URL. |
| 207 | scoped_refptr<const Extension> extension_; |
| 208 | |
| 209 | // The type of context. |
| 210 | Feature::Context context_type_; |
| 211 | |
mek | 7e1d745 | 2014-09-08 23:55:57 | [diff] [blame] | 212 | // The effective extension associated with this context, or NULL if there is |
| 213 | // none. This is different from the above extension if this context is in an |
| 214 | // about:blank iframe for example. |
| 215 | scoped_refptr<const Extension> effective_extension_; |
| 216 | |
| 217 | // The type of context. |
| 218 | Feature::Context effective_context_type_; |
| 219 | |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 220 | // Owns and structures the JS that is injected to set up extension bindings. |
| 221 | scoped_ptr<ModuleSystem> module_system_; |
| 222 | |
| 223 | // Contains safe copies of builtin objects like Function.prototype. |
kalman | 33076cb | 2015-08-11 19:12:07 | [diff] [blame] | 224 | SafeBuiltins safe_builtins_; |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 225 | |
rockot | e261b16 | 2014-12-12 01:59:47 | [diff] [blame] | 226 | // The set of capabilities granted to this context by extensions. |
| 227 | APIPermissionSet content_capabilities_; |
| 228 | |
kalman | b0c1c50 | 2015-04-15 00:25:06 | [diff] [blame] | 229 | // A list of base::Closure instances as an observer interface for |
| 230 | // invalidation. |
| 231 | std::vector<base::Closure> invalidate_observers_; |
| 232 | |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 233 | v8::Isolate* isolate_; |
| 234 | |
Sadrul Habib Chowdhury | 0d7ef9f | 2014-12-03 20:07:30 | [diff] [blame] | 235 | GURL url_; |
| 236 | |
sammc | de54a47e | 2015-01-13 23:16:34 | [diff] [blame] | 237 | scoped_ptr<Runner> runner_; |
| 238 | |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 239 | DISALLOW_COPY_AND_ASSIGN(ScriptContext); |
| 240 | }; |
| 241 | |
| 242 | } // namespace extensions |
| 243 | |
| 244 | #endif // EXTENSIONS_RENDERER_SCRIPT_CONTEXT_H_ |