[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_PROGRAMMATIC_SCRIPT_INJECTOR_H_ |
| 6 | #define EXTENSIONS_RENDERER_PROGRAMMATIC_SCRIPT_INJECTOR_H_ |
| 7 | |
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame] | 8 | #include <memory> |
| 9 | |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 10 | #include "base/macros.h" |
rdevlin.cronin | 4bb32d7 | 2015-06-02 21:55:01 | [diff] [blame] | 11 | #include "base/values.h" |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 12 | #include "extensions/renderer/script_injection.h" |
[email protected] | cde18a2d | 2014-07-01 03:13:04 | [diff] [blame] | 13 | #include "url/gurl.h" |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 14 | |
| 15 | struct ExtensionMsg_ExecuteCode_Params; |
| 16 | |
[email protected] | cde18a2d | 2014-07-01 03:13:04 | [diff] [blame] | 17 | namespace content { |
rdevlin.cronin | 3e11c986 | 2015-06-04 19:54:25 | [diff] [blame] | 18 | class RenderFrame; |
[email protected] | cde18a2d | 2014-07-01 03:13:04 | [diff] [blame] | 19 | } |
| 20 | |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 21 | namespace extensions { |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 22 | |
| 23 | // A ScriptInjector to handle tabs.executeScript(). |
| 24 | class ProgrammaticScriptInjector : public ScriptInjector { |
| 25 | public: |
jam | 69e7115 | 2016-11-02 01:15:43 | [diff] [blame] | 26 | explicit ProgrammaticScriptInjector( |
| 27 | const ExtensionMsg_ExecuteCode_Params& params); |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 28 | ~ProgrammaticScriptInjector() override; |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 29 | |
| 30 | private: |
| 31 | // ScriptInjector implementation. |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 32 | UserScript::InjectionType script_type() const override; |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 33 | bool ShouldExecuteInMainWorld() const override; |
| 34 | bool IsUserGesture() const override; |
Manish Jethani | 9494d72 | 2018-01-20 00:28:47 | [diff] [blame] | 35 | base::Optional<CSSOrigin> GetCssOrigin() const override; |
Manish Jethani | ff6ff85 | 2018-02-23 07:24:55 | [diff] [blame] | 36 | const base::Optional<std::string> GetInjectionKey() const override; |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 37 | bool ExpectsResults() const override; |
catmullings | d4faad4f | 2016-09-08 19:55:30 | [diff] [blame] | 38 | bool ShouldInjectJs( |
| 39 | UserScript::RunLocation run_location, |
| 40 | const std::set<std::string>& executing_scripts) const override; |
| 41 | bool ShouldInjectCss( |
| 42 | UserScript::RunLocation run_location, |
| 43 | const std::set<std::string>& injected_stylesheets) const override; |
Devlin Cronin | 3e532b8 | 2018-05-03 21:27:19 | [diff] [blame] | 44 | PermissionsData::PageAccess CanExecuteOnFrame( |
hanxi | a5c856cf | 2015-02-13 20:51:58 | [diff] [blame] | 45 | const InjectionHost* injection_host, |
rdevlin.cronin | 3e11c986 | 2015-06-04 19:54:25 | [diff] [blame] | 46 | blink::WebLocalFrame* web_frame, |
jam | 69e7115 | 2016-11-02 01:15:43 | [diff] [blame] | 47 | int tab_id) override; |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 48 | std::vector<blink::WebScriptSource> GetJsSources( |
catmullings | d4faad4f | 2016-09-08 19:55:30 | [diff] [blame] | 49 | UserScript::RunLocation run_location, |
| 50 | std::set<std::string>* executing_scripts, |
| 51 | size_t* num_injected_js_scripts) const override; |
lazyboy | 49cc0b3a | 2016-08-18 21:55:12 | [diff] [blame] | 52 | std::vector<blink::WebString> GetCssSources( |
catmullings | d4faad4f | 2016-09-08 19:55:30 | [diff] [blame] | 53 | UserScript::RunLocation run_location, |
| 54 | std::set<std::string>* injected_stylesheets, |
| 55 | size_t* num_injected_stylesheets) const override; |
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame] | 56 | void OnInjectionComplete(std::unique_ptr<base::Value> execution_result, |
rdevlin.cronin | d533be96 | 2015-10-02 17:01:18 | [diff] [blame] | 57 | UserScript::RunLocation run_location, |
| 58 | content::RenderFrame* render_frame) override; |
| 59 | void OnWillNotInject(InjectFailureReason reason, |
| 60 | content::RenderFrame* render_frame) override; |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 61 | |
rob | 52277c8 | 2016-02-07 17:28:57 | [diff] [blame] | 62 | // Whether it is safe to include information about the URL in error messages. |
| 63 | bool CanShowUrlInError() const; |
| 64 | |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 65 | // Notify the browser that the script was injected (or never will be), and |
| 66 | // send along any results or errors. |
rdevlin.cronin | d533be96 | 2015-10-02 17:01:18 | [diff] [blame] | 67 | void Finish(const std::string& error, content::RenderFrame* render_frame); |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 68 | |
| 69 | // The parameters for injecting the script. |
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame] | 70 | std::unique_ptr<ExtensionMsg_ExecuteCode_Params> params_; |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 71 | |
[email protected] | cde18a2d | 2014-07-01 03:13:04 | [diff] [blame] | 72 | // The url of the frame into which we are injecting. |
| 73 | GURL url_; |
| 74 | |
rob | 52277c8 | 2016-02-07 17:28:57 | [diff] [blame] | 75 | // The serialization of the frame's origin if the frame is an about:-URL. This |
| 76 | // is used to provide user-friendly messages. |
| 77 | std::string origin_for_about_error_; |
rob | a9e6e642 | 2015-03-30 21:14:02 | [diff] [blame] | 78 | |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 79 | // The results of the script execution. |
rdevlin.cronin | 4bb32d7 | 2015-06-02 21:55:01 | [diff] [blame] | 80 | base::ListValue results_; |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 81 | |
| 82 | // Whether or not this script injection has finished. |
| 83 | bool finished_; |
| 84 | |
| 85 | DISALLOW_COPY_AND_ASSIGN(ProgrammaticScriptInjector); |
| 86 | }; |
| 87 | |
| 88 | } // namespace extensions |
| 89 | |
| 90 | #endif // EXTENSIONS_RENDERER_PROGRAMMATIC_SCRIPT_INJECTOR_H_ |