blob: f254a9587144a8cacd269a269baa2cb2ae6bb160 [file] [log] [blame]
[email protected]16a4206f2014-08-15 09:44:431// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]3fd3cf72012-05-14 05:51:562// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
limasdfd70dc5ae2014-09-13 00:02:225#ifndef EXTENSIONS_BROWSER_SCRIPT_EXECUTOR_H_
6#define EXTENSIONS_BROWSER_SCRIPT_EXECUTOR_H_
[email protected]3fd3cf72012-05-14 05:51:567
Trent Apted8f733b92018-10-04 00:54:458#include <map>
9#include <set>
10#include <string>
Devlin Cronin5331a45e2020-11-18 21:04:3211#include <vector>
Trent Apted8f733b92018-10-04 00:54:4512
13#include "base/callback.h"
Manish Jethani9494d722018-01-20 00:28:4714#include "base/optional.h"
Lei Zhang7c1d96f2021-04-30 09:04:3615#include "base/values.h"
Manish Jethani9494d722018-01-20 00:28:4716#include "extensions/common/constants.h"
Julie Jeongeun Kim58785e92021-03-03 07:30:4517#include "extensions/common/mojom/action_type.mojom-shared.h"
Julie Jeongeun Kim0d0ac492021-03-04 01:43:2218#include "extensions/common/mojom/css_origin.mojom-shared.h"
Julie Jeongeun Kim30f64632021-03-10 01:10:0219#include "extensions/common/mojom/host_id.mojom-forward.h"
Julie Jeongeun Kim378db14d2021-03-05 01:53:0020#include "extensions/common/mojom/run_location.mojom-shared.h"
[email protected]49d9b142013-07-19 08:50:2721#include "extensions/common/user_script.h"
[email protected]3fd3cf72012-05-14 05:51:5622
[email protected]7f3b91e2012-08-07 08:05:0323class GURL;
[email protected]7f3b91e2012-08-07 08:05:0324
[email protected]3fd3cf72012-05-14 05:51:5625namespace content {
26class WebContents;
27}
28
29namespace extensions {
Trent Apted8f733b92018-10-04 00:54:4530
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.
35using 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.
39using ScriptsExecutedNotification = base::RepeatingCallback<
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:4140 void(content::WebContents*, const ExecutingScriptsMap&, const GURL&)>;
[email protected]3fd3cf72012-05-14 05:51:5641
42// Interface for executing extension content scripts (e.g. executeScript) as
Julie Jeongeun Kim04fb76242021-03-11 04:16:2743// described by the mojom::ExecuteCodeParams IPC, and notifying the
Julie Jeongeun Kim1a604ad2021-03-18 10:27:2844// caller when responded with ExecuteCodeCallback.
[email protected]3fd3cf72012-05-14 05:51:5645class ScriptExecutor {
46 public:
Trent Apted8f733b92018-10-04 00:54:4547 explicit ScriptExecutor(content::WebContents* web_contents);
[email protected]af78a802012-07-10 23:47:0248 ~ScriptExecutor();
[email protected]3fd3cf72012-05-14 05:51:5649
[email protected]3fd3cf72012-05-14 05:51:5650 // The scope of the script injection across the frames.
51 enum FrameScope {
Devlin Cronin5331a45e2020-11-18 21:04:3252 SPECIFIED_FRAMES,
rob52277c82016-02-07 17:28:5753 INCLUDE_SUB_FRAMES,
[email protected]3fd3cf72012-05-14 05:51:5654 };
55
[email protected]ae26b282014-05-15 16:40:1656 // 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]88c6f5c2013-08-28 04:08:4163 // 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
Devlin Cronin7fdd38c2021-01-27 03:01:4975 struct FrameResult {
76 FrameResult();
77 FrameResult(FrameResult&&);
78 FrameResult& operator=(FrameResult&&);
79
80 // The ID of the frame of the injection.
81 int frame_id = -1;
82 // The error associated with the injection, if any. Empty if the injection
83 // succeeded.
84 std::string error;
85 // The URL of the frame from the injection. Only set if the frame exists.
86 GURL url;
87 // The result value from the injection, or null if the injection failed (or
88 // had no result).
89 base::Value value;
90 // Whether the frame responded to the attempted injection (which can fail if
91 // the frame was removed or never existed). Note this doesn't necessarily
92 // mean the injection succeeded, since it could fail due to other reasons
93 // (like permissions).
94 bool frame_responded = false;
95 };
96
97 using ScriptFinishedCallback =
98 base::OnceCallback<void(std::vector<FrameResult> frame_results)>;
[email protected]3fd3cf72012-05-14 05:51:5699
Julie Jeongeun Kim04fb76242021-03-11 04:16:27100 // Executes a script. The arguments match mojom::ExecuteCodeParams in
101 // frame.mojom (request_id is populated automatically).
[email protected]3fd3cf72012-05-14 05:51:56102 //
Devlin Cronin5331a45e2020-11-18 21:04:32103 // The script will be executed in the frames identified by |frame_ids| (which
104 // are extension API frame IDs). If |frame_scope| is INCLUDE_SUB_FRAMES,
105 // then the script will also be executed in all descendants of the specified
106 // frames.
rob52277c82016-02-07 17:28:57107 //
[email protected]3fd3cf72012-05-14 05:51:56108 // |callback| will always be called even if the IPC'd renderer is destroyed
109 // before a response is received (in this case the callback will be with a
110 // failure and appropriate error message).
Julie Jeongeun Kim30f64632021-03-10 01:10:02111 void ExecuteScript(const mojom::HostID& host_id,
Julie Jeongeun Kim58785e92021-03-03 07:30:45112 mojom::ActionType action_type,
[email protected]af78a802012-07-10 23:47:02113 const std::string& code,
114 FrameScope frame_scope,
Devlin Croninc84d0e52021-03-23 01:16:15115 const std::set<int>& frame_ids,
[email protected]ae26b282014-05-15 16:40:16116 MatchAboutBlank match_about_blank,
Julie Jeongeun Kim378db14d2021-03-05 01:53:00117 mojom::RunLocation run_at,
[email protected]88c6f5c2013-08-28 04:08:41118 ProcessType process_type,
[email protected]6f451a42014-04-10 17:12:47119 const GURL& webview_src,
Devlin Cronind52ea54ea2019-12-20 17:59:02120 const GURL& script_url,
[email protected]0df49432014-03-04 01:02:50121 bool user_gesture,
Julie Jeongeun Kim0d0ac492021-03-04 01:43:22122 mojom::CSSOrigin css_origin,
[email protected]88c6f5c2013-08-28 04:08:41123 ResultType result_type,
Istiaque Ahmede643f562020-04-18 09:56:39124 ScriptFinishedCallback callback);
Trent Apted8f733b92018-10-04 00:54:45125
126 // Set the observer for ScriptsExecutedNotification callbacks.
127 void set_observer(ScriptsExecutedNotification observer) {
128 observer_ = std::move(observer);
129 }
[email protected]af78a802012-07-10 23:47:02130
[email protected]af78a802012-07-10 23:47:02131 private:
[email protected]af78a802012-07-10 23:47:02132 content::WebContents* web_contents_;
133
Trent Apted8f733b92018-10-04 00:54:45134 ScriptsExecutedNotification observer_;
135
136 DISALLOW_COPY_AND_ASSIGN(ScriptExecutor);
[email protected]3fd3cf72012-05-14 05:51:56137};
138
139} // namespace extensions
140
limasdfd70dc5ae2014-09-13 00:02:22141#endif // EXTENSIONS_BROWSER_SCRIPT_EXECUTOR_H_