[email protected] | 16a4206f | 2014-08-15 09:44:43 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 3fd3cf7 | 2012-05-14 05:51:56 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
limasdf | d70dc5ae | 2014-09-13 00:02:22 | [diff] [blame] | 5 | #ifndef EXTENSIONS_BROWSER_SCRIPT_EXECUTOR_H_ |
| 6 | #define EXTENSIONS_BROWSER_SCRIPT_EXECUTOR_H_ |
[email protected] | 3fd3cf7 | 2012-05-14 05:51:56 | [diff] [blame] | 7 | |
Trent Apted | 8f733b9 | 2018-10-04 00:54:45 | [diff] [blame] | 8 | #include <map> |
| 9 | #include <set> |
| 10 | #include <string> |
Devlin Cronin | 5331a45e | 2020-11-18 21:04:32 | [diff] [blame^] | 11 | #include <vector> |
Trent Apted | 8f733b9 | 2018-10-04 00:54:45 | [diff] [blame] | 12 | |
| 13 | #include "base/callback.h" |
Manish Jethani | 9494d72 | 2018-01-20 00:28:47 | [diff] [blame] | 14 | #include "base/optional.h" |
| 15 | #include "extensions/common/constants.h" |
[email protected] | 49d9b14 | 2013-07-19 08:50:27 | [diff] [blame] | 16 | #include "extensions/common/user_script.h" |
[email protected] | 3fd3cf7 | 2012-05-14 05:51:56 | [diff] [blame] | 17 | |
[email protected] | 7f3b91e | 2012-08-07 08:05:03 | [diff] [blame] | 18 | class GURL; |
[email protected] | ac02ac5 | 2014-05-20 01:11:26 | [diff] [blame] | 19 | struct ExtensionMsg_ExecuteCode_Params; |
[email protected] | 7f3b91e | 2012-08-07 08:05:03 | [diff] [blame] | 20 | |
[email protected] | cab8cd98 | 2012-07-20 20:57:03 | [diff] [blame] | 21 | namespace base { |
| 22 | class ListValue; |
| 23 | } // namespace base |
| 24 | |
[email protected] | 3fd3cf7 | 2012-05-14 05:51:56 | [diff] [blame] | 25 | namespace content { |
| 26 | class WebContents; |
| 27 | } |
| 28 | |
| 29 | namespace extensions { |
Trent Apted | 8f733b9 | 2018-10-04 00:54:45 | [diff] [blame] | 30 | |
| 31 | // Contains all extensions that are executing scripts, mapped to the paths for |
| 32 | // those scripts. The paths may be an empty set if the script has no path |
| 33 | // associated with it (e.g. in the case of tabs.executeScript), but there will |
| 34 | // still be an entry for the extension. |
| 35 | using ExecutingScriptsMap = std::map<std::string, std::set<std::string>>; |
| 36 | |
| 37 | // Callback that ScriptExecutor uses to notify when content scripts and/or |
| 38 | // tabs.executeScript calls run on a page. |
| 39 | using ScriptsExecutedNotification = base::RepeatingCallback< |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 40 | void(content::WebContents*, const ExecutingScriptsMap&, const GURL&)>; |
[email protected] | 3fd3cf7 | 2012-05-14 05:51:56 | [diff] [blame] | 41 | |
| 42 | // Interface for executing extension content scripts (e.g. executeScript) as |
| 43 | // described by the ExtensionMsg_ExecuteCode_Params IPC, and notifying the |
| 44 | // caller when responded with ExtensionHostMsg_ExecuteCodeFinished. |
| 45 | class ScriptExecutor { |
| 46 | public: |
Trent Apted | 8f733b9 | 2018-10-04 00:54:45 | [diff] [blame] | 47 | explicit ScriptExecutor(content::WebContents* web_contents); |
[email protected] | af78a80 | 2012-07-10 23:47:02 | [diff] [blame] | 48 | ~ScriptExecutor(); |
[email protected] | 3fd3cf7 | 2012-05-14 05:51:56 | [diff] [blame] | 49 | |
[email protected] | 3fd3cf7 | 2012-05-14 05:51:56 | [diff] [blame] | 50 | // The scope of the script injection across the frames. |
| 51 | enum FrameScope { |
Devlin Cronin | 5331a45e | 2020-11-18 21:04:32 | [diff] [blame^] | 52 | SPECIFIED_FRAMES, |
rob | 52277c8 | 2016-02-07 17:28:57 | [diff] [blame] | 53 | INCLUDE_SUB_FRAMES, |
[email protected] | 3fd3cf7 | 2012-05-14 05:51:56 | [diff] [blame] | 54 | }; |
| 55 | |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 56 | // Whether to insert the script in about: frames when its origin matches |
| 57 | // the extension's host permissions. |
| 58 | enum MatchAboutBlank { |
| 59 | DONT_MATCH_ABOUT_BLANK, |
| 60 | MATCH_ABOUT_BLANK, |
| 61 | }; |
| 62 | |
[email protected] | 88c6f5c | 2013-08-28 04:08:41 | [diff] [blame] | 63 | // The type of process the target is. |
| 64 | enum ProcessType { |
| 65 | DEFAULT_PROCESS, |
| 66 | WEB_VIEW_PROCESS, |
| 67 | }; |
| 68 | |
| 69 | // The type of result the caller is interested in. |
| 70 | enum ResultType { |
| 71 | NO_RESULT, |
| 72 | JSON_SERIALIZED_RESULT, |
| 73 | }; |
| 74 | |
[email protected] | 0f8bf4f | 2014-07-02 19:26:53 | [diff] [blame] | 75 | // Callback from ExecuteScript. The arguments are (error, on_url, result). |
| 76 | // Success is implied by an empty error. |
Istiaque Ahmed | e643f56 | 2020-04-18 09:56:39 | [diff] [blame] | 77 | using ScriptFinishedCallback = base::OnceCallback< |
| 78 | void(const std::string&, const GURL&, const base::ListValue&)>; |
[email protected] | 3fd3cf7 | 2012-05-14 05:51:56 | [diff] [blame] | 79 | |
| 80 | // Executes a script. The arguments match ExtensionMsg_ExecuteCode_Params in |
| 81 | // extension_messages.h (request_id is populated automatically). |
| 82 | // |
Devlin Cronin | 5331a45e | 2020-11-18 21:04:32 | [diff] [blame^] | 83 | // The script will be executed in the frames identified by |frame_ids| (which |
| 84 | // are extension API frame IDs). If |frame_scope| is INCLUDE_SUB_FRAMES, |
| 85 | // then the script will also be executed in all descendants of the specified |
| 86 | // frames. |
rob | 52277c8 | 2016-02-07 17:28:57 | [diff] [blame] | 87 | // |
[email protected] | 3fd3cf7 | 2012-05-14 05:51:56 | [diff] [blame] | 88 | // |callback| will always be called even if the IPC'd renderer is destroyed |
| 89 | // before a response is received (in this case the callback will be with a |
| 90 | // failure and appropriate error message). |
hanxi | 79f7a57 | 2015-03-09 20:46:59 | [diff] [blame] | 91 | void ExecuteScript(const HostID& host_id, |
Antonio Gomes | a4e391a | 2020-10-01 13:42:34 | [diff] [blame] | 92 | UserScript::ActionType action_type, |
[email protected] | af78a80 | 2012-07-10 23:47:02 | [diff] [blame] | 93 | const std::string& code, |
| 94 | FrameScope frame_scope, |
Devlin Cronin | 5331a45e | 2020-11-18 21:04:32 | [diff] [blame^] | 95 | const std::vector<int>& frame_ids, |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 96 | MatchAboutBlank match_about_blank, |
[email protected] | af78a80 | 2012-07-10 23:47:02 | [diff] [blame] | 97 | UserScript::RunLocation run_at, |
[email protected] | 88c6f5c | 2013-08-28 04:08:41 | [diff] [blame] | 98 | ProcessType process_type, |
[email protected] | 6f451a4 | 2014-04-10 17:12:47 | [diff] [blame] | 99 | const GURL& webview_src, |
Devlin Cronin | d52ea54ea | 2019-12-20 17:59:02 | [diff] [blame] | 100 | const GURL& script_url, |
[email protected] | 0df4943 | 2014-03-04 01:02:50 | [diff] [blame] | 101 | bool user_gesture, |
Manish Jethani | 9494d72 | 2018-01-20 00:28:47 | [diff] [blame] | 102 | base::Optional<CSSOrigin> css_origin, |
[email protected] | 88c6f5c | 2013-08-28 04:08:41 | [diff] [blame] | 103 | ResultType result_type, |
Istiaque Ahmed | e643f56 | 2020-04-18 09:56:39 | [diff] [blame] | 104 | ScriptFinishedCallback callback); |
Trent Apted | 8f733b9 | 2018-10-04 00:54:45 | [diff] [blame] | 105 | |
| 106 | // Set the observer for ScriptsExecutedNotification callbacks. |
| 107 | void set_observer(ScriptsExecutedNotification observer) { |
| 108 | observer_ = std::move(observer); |
| 109 | } |
[email protected] | af78a80 | 2012-07-10 23:47:02 | [diff] [blame] | 110 | |
[email protected] | af78a80 | 2012-07-10 23:47:02 | [diff] [blame] | 111 | private: |
| 112 | // The next value to use for request_id in ExtensionMsg_ExecuteCode_Params. |
Trent Apted | 8f733b9 | 2018-10-04 00:54:45 | [diff] [blame] | 113 | int next_request_id_ = 0; |
[email protected] | af78a80 | 2012-07-10 23:47:02 | [diff] [blame] | 114 | |
[email protected] | af78a80 | 2012-07-10 23:47:02 | [diff] [blame] | 115 | content::WebContents* web_contents_; |
| 116 | |
Trent Apted | 8f733b9 | 2018-10-04 00:54:45 | [diff] [blame] | 117 | ScriptsExecutedNotification observer_; |
| 118 | |
| 119 | DISALLOW_COPY_AND_ASSIGN(ScriptExecutor); |
[email protected] | 3fd3cf7 | 2012-05-14 05:51:56 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | } // namespace extensions |
| 123 | |
limasdf | d70dc5ae | 2014-09-13 00:02:22 | [diff] [blame] | 124 | #endif // EXTENSIONS_BROWSER_SCRIPT_EXECUTOR_H_ |