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