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