[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" |
| 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; |
| 25 | } |
| 26 | |
| 27 | namespace content { |
[email protected] | 2101c4c | 2014-08-22 00:16:16 | [diff] [blame] | 28 | class RenderFrame; |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 29 | class RenderView; |
| 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: |
[email protected] | e689367 | 2014-05-01 17:29:13 | [diff] [blame] | 38 | ScriptContext(const v8::Handle<v8::Context>& context, |
kalman | 62b5911d | 2015-04-04 02:33:09 | [diff] [blame] | 39 | blink::WebFrame* frame, |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 40 | const Extension* extension, |
mek | 7e1d745 | 2014-09-08 23:55:57 | [diff] [blame] | 41 | Feature::Context context_type, |
| 42 | const Extension* effective_extension, |
| 43 | Feature::Context effective_context_type); |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 44 | ~ScriptContext() override; |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 45 | |
| 46 | // Clears the WebFrame for this contexts and invalidates the associated |
| 47 | // ModuleSystem. |
| 48 | void Invalidate(); |
| 49 | |
kalman | b0c1c50 | 2015-04-15 00:25:06 | [diff] [blame^] | 50 | // Registers |observer| to be run when this context is invalidated. Closures |
| 51 | // are run immediately when Invalidate() is called, not in a message loop. |
| 52 | void AddInvalidationObserver(const base::Closure& observer); |
| 53 | |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 54 | // Returns true if this context is still valid, false if it isn't. |
| 55 | // A context becomes invalid via Invalidate(). |
kalman | b0c1c50 | 2015-04-15 00:25:06 | [diff] [blame^] | 56 | bool is_valid() const { return is_valid_; } |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 57 | |
| 58 | v8::Handle<v8::Context> v8_context() const { |
kalman | 078a219 | 2015-03-09 18:19:39 | [diff] [blame] | 59 | return v8::Local<v8::Context>::New(isolate_, v8_context_); |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | const Extension* extension() const { return extension_.get(); } |
| 63 | |
mek | 7e1d745 | 2014-09-08 23:55:57 | [diff] [blame] | 64 | const Extension* effective_extension() const { |
| 65 | return effective_extension_.get(); |
| 66 | } |
| 67 | |
kalman | 62b5911d | 2015-04-04 02:33:09 | [diff] [blame] | 68 | blink::WebFrame* web_frame() const { return web_frame_; } |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 69 | |
| 70 | Feature::Context context_type() const { return context_type_; } |
| 71 | |
mek | 7e1d745 | 2014-09-08 23:55:57 | [diff] [blame] | 72 | Feature::Context effective_context_type() const { |
| 73 | return effective_context_type_; |
| 74 | } |
| 75 | |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 76 | void set_module_system(scoped_ptr<ModuleSystem> module_system) { |
| 77 | module_system_ = module_system.Pass(); |
| 78 | } |
| 79 | |
| 80 | ModuleSystem* module_system() { return module_system_.get(); } |
| 81 | |
| 82 | SafeBuiltins* safe_builtins() { return &safe_builtins_; } |
| 83 | |
| 84 | const SafeBuiltins* safe_builtins() const { return &safe_builtins_; } |
| 85 | |
| 86 | // Returns the ID of the extension associated with this context, or empty |
| 87 | // string if there is no such extension. |
[email protected] | 800f987 | 2014-06-12 04:12:51 | [diff] [blame] | 88 | const std::string& GetExtensionID() const; |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 89 | |
| 90 | // Returns the RenderView associated with this context. Can return NULL if the |
| 91 | // context is in the process of being destroyed. |
| 92 | content::RenderView* GetRenderView() const; |
| 93 | |
[email protected] | 2101c4c | 2014-08-22 00:16:16 | [diff] [blame] | 94 | // Returns the RenderFrame associated with this context. Can return NULL if |
| 95 | // the context is in the process of being destroyed. |
| 96 | content::RenderFrame* GetRenderFrame() const; |
| 97 | |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 98 | // Runs |function| with appropriate scopes. Doesn't catch exceptions, callers |
| 99 | // must do that if they want. |
| 100 | // |
| 101 | // USE THIS METHOD RATHER THAN v8::Function::Call WHEREVER POSSIBLE. |
| 102 | v8::Local<v8::Value> CallFunction(v8::Handle<v8::Function> function, |
| 103 | int argc, |
| 104 | v8::Handle<v8::Value> argv[]) const; |
| 105 | |
| 106 | void DispatchEvent(const char* event_name, v8::Handle<v8::Array> args) const; |
| 107 | |
| 108 | // Fires the onunload event on the unload_event module. |
| 109 | void DispatchOnUnloadEvent(); |
| 110 | |
| 111 | // Returns the availability of the API |api_name|. |
| 112 | Feature::Availability GetAvailability(const std::string& api_name); |
| 113 | |
| 114 | // Returns a string description of the type of context this is. |
| 115 | std::string GetContextTypeDescription(); |
| 116 | |
mek | 7e1d745 | 2014-09-08 23:55:57 | [diff] [blame] | 117 | // Returns a string description of the effective type of context this is. |
| 118 | std::string GetEffectiveContextTypeDescription(); |
| 119 | |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 120 | v8::Isolate* isolate() const { return isolate_; } |
| 121 | |
| 122 | // Get the URL of this context's web frame. |
| 123 | GURL GetURL() const; |
| 124 | |
| 125 | // Returns whether the API |api| or any part of the API could be |
| 126 | // available in this context without taking into account the context's |
| 127 | // extension. |
| 128 | bool IsAnyFeatureAvailableToContext(const extensions::Feature& api); |
| 129 | |
| 130 | // Utility to get the URL we will match against for a frame. If the frame has |
| 131 | // committed, this is the commited URL. Otherwise it is the provisional URL. |
[email protected] | c1abb323 | 2014-07-30 18:28:39 | [diff] [blame] | 132 | // The returned URL may be invalid. |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 133 | static GURL GetDataSourceURLForFrame(const blink::WebFrame* frame); |
| 134 | |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 135 | // Returns the first non-about:-URL in the document hierarchy above and |
| 136 | // including |frame|. The document hierarchy is only traversed if |
| 137 | // |document_url| is an about:-URL and if |match_about_blank| is true. |
| 138 | static GURL GetEffectiveDocumentURL(const blink::WebFrame* frame, |
| 139 | const GURL& document_url, |
| 140 | bool match_about_blank); |
| 141 | |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 142 | // RequestSender::Source implementation. |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 143 | ScriptContext* GetContext() override; |
| 144 | void OnResponseReceived(const std::string& name, |
| 145 | int request_id, |
| 146 | bool success, |
| 147 | const base::ListValue& response, |
| 148 | const std::string& error) override; |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 149 | |
rockot | e261b16 | 2014-12-12 01:59:47 | [diff] [blame] | 150 | // Grants a set of content capabilities to this context. |
| 151 | void SetContentCapabilities(const APIPermissionSet& permissions); |
| 152 | |
| 153 | // Indicates if this context has an effective API permission either by being |
| 154 | // a context for an extension which has that permission, or by being a web |
| 155 | // context which has been granted the corresponding capability by an |
| 156 | // extension. |
| 157 | bool HasAPIPermission(APIPermission::ID permission) const; |
| 158 | |
mlamouri | 60a2857d | 2015-04-14 15:22:36 | [diff] [blame] | 159 | private: |
| 160 | class Runner; |
| 161 | |
kalman | b0c1c50 | 2015-04-15 00:25:06 | [diff] [blame^] | 162 | // Whether this context is valid. |
| 163 | bool is_valid_; |
| 164 | |
| 165 | // The v8 context the bindings are accessible to. |
| 166 | v8::Global<v8::Context> v8_context_; |
| 167 | |
kalman | 62b5911d | 2015-04-04 02:33:09 | [diff] [blame] | 168 | // The WebFrame associated with this context. This can be NULL because this |
| 169 | // object can outlive is destroyed asynchronously. |
| 170 | blink::WebFrame* web_frame_; |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 171 | |
| 172 | // The extension associated with this context, or NULL if there is none. This |
| 173 | // might be a hosted app in the case that this context is hosting a web URL. |
| 174 | scoped_refptr<const Extension> extension_; |
| 175 | |
| 176 | // The type of context. |
| 177 | Feature::Context context_type_; |
| 178 | |
mek | 7e1d745 | 2014-09-08 23:55:57 | [diff] [blame] | 179 | // The effective extension associated with this context, or NULL if there is |
| 180 | // none. This is different from the above extension if this context is in an |
| 181 | // about:blank iframe for example. |
| 182 | scoped_refptr<const Extension> effective_extension_; |
| 183 | |
| 184 | // The type of context. |
| 185 | Feature::Context effective_context_type_; |
| 186 | |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 187 | // Owns and structures the JS that is injected to set up extension bindings. |
| 188 | scoped_ptr<ModuleSystem> module_system_; |
| 189 | |
| 190 | // Contains safe copies of builtin objects like Function.prototype. |
| 191 | SafeBuiltins safe_builtins_; |
| 192 | |
rockot | e261b16 | 2014-12-12 01:59:47 | [diff] [blame] | 193 | // The set of capabilities granted to this context by extensions. |
| 194 | APIPermissionSet content_capabilities_; |
| 195 | |
kalman | b0c1c50 | 2015-04-15 00:25:06 | [diff] [blame^] | 196 | // A list of base::Closure instances as an observer interface for |
| 197 | // invalidation. |
| 198 | std::vector<base::Closure> invalidate_observers_; |
| 199 | |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 200 | v8::Isolate* isolate_; |
| 201 | |
Sadrul Habib Chowdhury | 0d7ef9f | 2014-12-03 20:07:30 | [diff] [blame] | 202 | GURL url_; |
| 203 | |
sammc | de54a47e | 2015-01-13 23:16:34 | [diff] [blame] | 204 | scoped_ptr<Runner> runner_; |
| 205 | |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 206 | DISALLOW_COPY_AND_ASSIGN(ScriptContext); |
| 207 | }; |
| 208 | |
| 209 | } // namespace extensions |
| 210 | |
| 211 | #endif // EXTENSIONS_RENDERER_SCRIPT_CONTEXT_H_ |