[email protected] | c11e659 | 2014-06-27 17:07:34 | [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_INJECTOR_H_ |
| 6 | #define EXTENSIONS_RENDERER_SCRIPT_INJECTOR_H_ |
| 7 | |
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame] | 8 | #include <memory> |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 11 | #include "extensions/common/permissions/permissions_data.h" |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 12 | #include "extensions/common/user_script.h" |
| 13 | #include "third_party/WebKit/public/web/WebScriptSource.h" |
| 14 | |
| 15 | class GURL; |
hanxi | a5c856cf | 2015-02-13 20:51:58 | [diff] [blame] | 16 | class InjectionHost; |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 17 | |
| 18 | namespace blink { |
rdevlin.cronin | 3e11c986 | 2015-06-04 19:54:25 | [diff] [blame] | 19 | class WebLocalFrame; |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | namespace extensions { |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 23 | struct ScriptsRunInfo; |
| 24 | |
| 25 | // The pseudo-delegate class for a ScriptInjection that provides all necessary |
| 26 | // information about how to inject the script, including what code to inject, |
| 27 | // when (run location), and where (world), but without any injection logic. |
| 28 | class ScriptInjector { |
| 29 | public: |
| 30 | // The possible reasons for not injecting the script. |
| 31 | enum InjectFailureReason { |
| 32 | EXTENSION_REMOVED, // The extension was removed before injection. |
| 33 | NOT_ALLOWED, // The script is not allowed to inject. |
| 34 | WONT_INJECT // The injection won't inject because the user rejected |
| 35 | // (or just did not accept) the injection. |
| 36 | }; |
| 37 | |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 38 | virtual ~ScriptInjector() {} |
| 39 | |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 40 | // Returns the script type of this particular injection. |
| 41 | virtual UserScript::InjectionType script_type() const = 0; |
| 42 | |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 43 | // Returns true if the script should execute in the main world. |
| 44 | virtual bool ShouldExecuteInMainWorld() const = 0; |
| 45 | |
| 46 | // Returns true if the script is running inside a user gesture. |
| 47 | virtual bool IsUserGesture() const = 0; |
| 48 | |
thakis | 1eeacc84 | 2015-03-26 07:30:52 | [diff] [blame] | 49 | // Returns true if the script expects results. |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 50 | virtual bool ExpectsResults() const = 0; |
| 51 | |
| 52 | // Returns true if the script should inject JS source at the given |
| 53 | // |run_location|. |
catmullings | d4faad4f | 2016-09-08 19:55:30 | [diff] [blame^] | 54 | virtual bool ShouldInjectJs( |
| 55 | UserScript::RunLocation run_location, |
| 56 | const std::set<std::string>& executing_scripts) const = 0; |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 57 | |
| 58 | // Returns true if the script should inject CSS at the given |run_location|. |
catmullings | d4faad4f | 2016-09-08 19:55:30 | [diff] [blame^] | 59 | virtual bool ShouldInjectCss( |
| 60 | UserScript::RunLocation run_location, |
| 61 | const std::set<std::string>& injected_stylesheets) const = 0; |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 62 | |
| 63 | // Returns true if the script should execute on the given |frame|. |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 64 | virtual PermissionsData::AccessType CanExecuteOnFrame( |
hanxi | a5c856cf | 2015-02-13 20:51:58 | [diff] [blame] | 65 | const InjectionHost* injection_host, |
rdevlin.cronin | 3e11c986 | 2015-06-04 19:54:25 | [diff] [blame] | 66 | blink::WebLocalFrame* web_frame, |
rdevlin.cronin | f994d1e | 2015-06-03 22:28:19 | [diff] [blame] | 67 | int tab_id) const = 0; |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 68 | |
| 69 | // Returns the javascript sources to inject at the given |run_location|. |
| 70 | // Only called if ShouldInjectJs() is true. |
| 71 | virtual std::vector<blink::WebScriptSource> GetJsSources( |
catmullings | d4faad4f | 2016-09-08 19:55:30 | [diff] [blame^] | 72 | UserScript::RunLocation run_location, |
| 73 | std::set<std::string>* executing_scripts, |
| 74 | size_t* num_injected_js_scripts) const = 0; |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 75 | |
| 76 | // Returns the css to inject at the given |run_location|. |
| 77 | // Only called if ShouldInjectCss() is true. |
lazyboy | 49cc0b3a | 2016-08-18 21:55:12 | [diff] [blame] | 78 | virtual std::vector<blink::WebString> GetCssSources( |
catmullings | d4faad4f | 2016-09-08 19:55:30 | [diff] [blame^] | 79 | UserScript::RunLocation run_location, |
| 80 | std::set<std::string>* injected_stylesheets, |
| 81 | size_t* num_injected_stylesheets) const = 0; |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 82 | |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 83 | // Notifies the script that injection has completed, with a possibly-populated |
| 84 | // list of results (depending on whether or not ExpectsResults() was true). |
rdevlin.cronin | d533be96 | 2015-10-02 17:01:18 | [diff] [blame] | 85 | // |render_frame| contains the render frame, or null if the frame was |
| 86 | // invalidated. |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 87 | virtual void OnInjectionComplete( |
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame] | 88 | std::unique_ptr<base::Value> execution_result, |
rdevlin.cronin | d533be96 | 2015-10-02 17:01:18 | [diff] [blame] | 89 | UserScript::RunLocation run_location, |
| 90 | content::RenderFrame* render_frame) = 0; |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 91 | |
| 92 | // Notifies the script that injection will never occur. |
rdevlin.cronin | d533be96 | 2015-10-02 17:01:18 | [diff] [blame] | 93 | // |render_frame| contains the render frame, or null if the frame was |
| 94 | // invalidated. |
| 95 | virtual void OnWillNotInject(InjectFailureReason reason, |
| 96 | content::RenderFrame* render_frame) = 0; |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | } // namespace extensions |
| 100 | |
| 101 | #endif // EXTENSIONS_RENDERER_SCRIPT_INJECTOR_H_ |