blob: 4a14e82476435dde89b560e0d9c72e37e6e372f0 [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"
Lei Zhang7c1d96f2021-04-30 09:04:3614#include "base/values.h"
Manish Jethani9494d722018-01-20 00:28:4715#include "extensions/common/constants.h"
Devlin Cronin5c3c9d92021-06-14 20:51:0016#include "extensions/common/mojom/code_injection.mojom.h"
Julie Jeongeun Kim0d0ac492021-03-04 01:43:2217#include "extensions/common/mojom/css_origin.mojom-shared.h"
Julie Jeongeun Kim30f64632021-03-10 01:10:0218#include "extensions/common/mojom/host_id.mojom-forward.h"
Julie Jeongeun Kim378db14d2021-03-05 01:53:0019#include "extensions/common/mojom/run_location.mojom-shared.h"
[email protected]49d9b142013-07-19 08:50:2720#include "extensions/common/user_script.h"
[email protected]3fd3cf72012-05-14 05:51:5621
[email protected]7f3b91e2012-08-07 08:05:0322class GURL;
23
[email protected]3fd3cf72012-05-14 05:51:5624namespace content {
25class WebContents;
26}
27
28namespace extensions {
Trent Apted8f733b92018-10-04 00:54:4529
30// Contains all extensions that are executing scripts, mapped to the paths for
31// those scripts. The paths may be an empty set if the script has no path
32// associated with it (e.g. in the case of tabs.executeScript), but there will
33// still be an entry for the extension.
34using ExecutingScriptsMap = std::map<std::string, std::set<std::string>>;
35
36// Callback that ScriptExecutor uses to notify when content scripts and/or
37// tabs.executeScript calls run on a page.
38using ScriptsExecutedNotification = base::RepeatingCallback<
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:4139 void(content::WebContents*, const ExecutingScriptsMap&, const GURL&)>;
[email protected]3fd3cf72012-05-14 05:51:5640
41// Interface for executing extension content scripts (e.g. executeScript) as
Julie Jeongeun Kim04fb76242021-03-11 04:16:2742// described by the mojom::ExecuteCodeParams IPC, and notifying the
Julie Jeongeun Kim1a604ad2021-03-18 10:27:2843// caller when responded with ExecuteCodeCallback.
[email protected]3fd3cf72012-05-14 05:51:5644class ScriptExecutor {
45 public:
Trent Apted8f733b92018-10-04 00:54:4546 explicit ScriptExecutor(content::WebContents* web_contents);
Peter Boström951cf77e2021-09-22 00:02:5947
48 ScriptExecutor(const ScriptExecutor&) = delete;
49 ScriptExecutor& operator=(const ScriptExecutor&) = delete;
50
[email protected]af78a802012-07-10 23:47:0251 ~ScriptExecutor();
[email protected]3fd3cf72012-05-14 05:51:5652
[email protected]3fd3cf72012-05-14 05:51:5653 // The scope of the script injection across the frames.
54 enum FrameScope {
Devlin Cronin5331a45e2020-11-18 21:04:3255 SPECIFIED_FRAMES,
rob52277c82016-02-07 17:28:5756 INCLUDE_SUB_FRAMES,
[email protected]3fd3cf72012-05-14 05:51:5657 };
58
[email protected]ae26b282014-05-15 16:40:1659 // Whether to insert the script in about: frames when its origin matches
60 // the extension's host permissions.
61 enum MatchAboutBlank {
62 DONT_MATCH_ABOUT_BLANK,
63 MATCH_ABOUT_BLANK,
64 };
65
[email protected]88c6f5c2013-08-28 04:08:4166 // The type of process the target is.
67 enum ProcessType {
68 DEFAULT_PROCESS,
69 WEB_VIEW_PROCESS,
70 };
71
Devlin Cronin7fdd38c2021-01-27 03:01:4972 struct FrameResult {
73 FrameResult();
74 FrameResult(FrameResult&&);
75 FrameResult& operator=(FrameResult&&);
76
77 // The ID of the frame of the injection.
78 int frame_id = -1;
79 // The error associated with the injection, if any. Empty if the injection
80 // succeeded.
81 std::string error;
82 // The URL of the frame from the injection. Only set if the frame exists.
83 GURL url;
84 // The result value from the injection, or null if the injection failed (or
85 // had no result).
86 base::Value value;
87 // Whether the frame responded to the attempted injection (which can fail if
88 // the frame was removed or never existed). Note this doesn't necessarily
89 // mean the injection succeeded, since it could fail due to other reasons
90 // (like permissions).
91 bool frame_responded = false;
92 };
93
94 using ScriptFinishedCallback =
95 base::OnceCallback<void(std::vector<FrameResult> frame_results)>;
[email protected]3fd3cf72012-05-14 05:51:5696
Devlin Cronin5c3c9d92021-06-14 20:51:0097 // Generates an injection key based on the host ID and either the file URL, if
98 // available, or the code string. The format of the key is
99 // "<type><host_id><digest>", where <type> is one of "F" (file) and "C"
100 // (code), <host_id> is the host ID, and <digest> is an unspecified hash
101 // digest of the file URL or the code string, respectively.
102 static std::string GenerateInjectionKey(const mojom::HostID& host_id,
103 const GURL& script_url,
104 const std::string& code);
105
Julie Jeongeun Kim04fb76242021-03-11 04:16:27106 // Executes a script. The arguments match mojom::ExecuteCodeParams in
107 // frame.mojom (request_id is populated automatically).
[email protected]3fd3cf72012-05-14 05:51:56108 //
Devlin Cronin5331a45e2020-11-18 21:04:32109 // The script will be executed in the frames identified by |frame_ids| (which
110 // are extension API frame IDs). If |frame_scope| is INCLUDE_SUB_FRAMES,
111 // then the script will also be executed in all descendants of the specified
112 // frames.
rob52277c82016-02-07 17:28:57113 //
[email protected]3fd3cf72012-05-14 05:51:56114 // |callback| will always be called even if the IPC'd renderer is destroyed
115 // before a response is received (in this case the callback will be with a
116 // failure and appropriate error message).
Julie Jeongeun Kim30f64632021-03-10 01:10:02117 void ExecuteScript(const mojom::HostID& host_id,
Devlin Cronin5c3c9d92021-06-14 20:51:00118 mojom::CodeInjectionPtr injection,
[email protected]af78a802012-07-10 23:47:02119 FrameScope frame_scope,
Devlin Croninc84d0e52021-03-23 01:16:15120 const std::set<int>& frame_ids,
[email protected]ae26b282014-05-15 16:40:16121 MatchAboutBlank match_about_blank,
Julie Jeongeun Kim378db14d2021-03-05 01:53:00122 mojom::RunLocation run_at,
[email protected]88c6f5c2013-08-28 04:08:41123 ProcessType process_type,
[email protected]6f451a42014-04-10 17:12:47124 const GURL& webview_src,
Istiaque Ahmede643f562020-04-18 09:56:39125 ScriptFinishedCallback callback);
Trent Apted8f733b92018-10-04 00:54:45126
127 // Set the observer for ScriptsExecutedNotification callbacks.
128 void set_observer(ScriptsExecutedNotification observer) {
129 observer_ = std::move(observer);
130 }
[email protected]af78a802012-07-10 23:47:02131
[email protected]af78a802012-07-10 23:47:02132 private:
[email protected]af78a802012-07-10 23:47:02133 content::WebContents* web_contents_;
134
Trent Apted8f733b92018-10-04 00:54:45135 ScriptsExecutedNotification observer_;
[email protected]3fd3cf72012-05-14 05:51:56136};
137
138} // namespace extensions
139
limasdfd70dc5ae2014-09-13 00:02:22140#endif // EXTENSIONS_BROWSER_SCRIPT_EXECUTOR_H_