blob: f79f140705720338fbea27a6d1b1efff1e21d241 [file] [log] [blame]
[email protected]c11e6592014-06-27 17:07:341// 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
dchengf6f80662016-04-20 20:26:048#include <memory>
[email protected]c11e6592014-06-27 17:07:349#include <vector>
10
Manish Jethani9494d722018-01-20 00:28:4711#include "extensions/common/constants.h"
Blink Reformata30d4232018-04-07 15:31:0612#include "extensions/common/permissions/permissions_data.h"
[email protected]c11e6592014-06-27 17:07:3413#include "extensions/common/user_script.h"
Blink Reformata30d4232018-04-07 15:31:0614#include "third_party/blink/public/web/web_script_source.h"
[email protected]c11e6592014-06-27 17:07:3415
hanxia5c856cf2015-02-13 20:51:5816class InjectionHost;
[email protected]c11e6592014-06-27 17:07:3417
18namespace blink {
rdevlin.cronin3e11c9862015-06-04 19:54:2519class WebLocalFrame;
[email protected]c11e6592014-06-27 17:07:3420}
21
22namespace extensions {
[email protected]c11e6592014-06-27 17:07:3423
24// The pseudo-delegate class for a ScriptInjection that provides all necessary
25// information about how to inject the script, including what code to inject,
26// when (run location), and where (world), but without any injection logic.
27class ScriptInjector {
28 public:
29 // The possible reasons for not injecting the script.
30 enum InjectFailureReason {
31 EXTENSION_REMOVED, // The extension was removed before injection.
32 NOT_ALLOWED, // The script is not allowed to inject.
33 WONT_INJECT // The injection won't inject because the user rejected
34 // (or just did not accept) the injection.
35 };
36
[email protected]c11e6592014-06-27 17:07:3437 virtual ~ScriptInjector() {}
38
[email protected]23a85362014-07-07 23:26:1939 // Returns the script type of this particular injection.
40 virtual UserScript::InjectionType script_type() const = 0;
41
[email protected]c11e6592014-06-27 17:07:3442 // Returns true if the script should execute in the main world.
43 virtual bool ShouldExecuteInMainWorld() const = 0;
44
45 // Returns true if the script is running inside a user gesture.
46 virtual bool IsUserGesture() const = 0;
47
Manish Jethani9494d722018-01-20 00:28:4748 // Returns the CSS origin of this injection.
49 virtual base::Optional<CSSOrigin> GetCssOrigin() const = 0;
50
Manish Jethaniff6ff852018-02-23 07:24:5551 // Returns the key for this injection, if it's a CSS injection.
52 virtual const base::Optional<std::string> GetInjectionKey() const = 0;
53
thakis1eeacc842015-03-26 07:30:5254 // Returns true if the script expects results.
[email protected]c11e6592014-06-27 17:07:3455 virtual bool ExpectsResults() const = 0;
56
57 // Returns true if the script should inject JS source at the given
58 // |run_location|.
catmullingsd4faad4f2016-09-08 19:55:3059 virtual bool ShouldInjectJs(
60 UserScript::RunLocation run_location,
61 const std::set<std::string>& executing_scripts) const = 0;
[email protected]c11e6592014-06-27 17:07:3462
63 // Returns true if the script should inject CSS at the given |run_location|.
catmullingsd4faad4f2016-09-08 19:55:3064 virtual bool ShouldInjectCss(
65 UserScript::RunLocation run_location,
66 const std::set<std::string>& injected_stylesheets) const = 0;
[email protected]c11e6592014-06-27 17:07:3467
68 // Returns true if the script should execute on the given |frame|.
Devlin Cronin3e532b82018-05-03 21:27:1969 virtual PermissionsData::PageAccess CanExecuteOnFrame(
hanxia5c856cf2015-02-13 20:51:5870 const InjectionHost* injection_host,
rdevlin.cronin3e11c9862015-06-04 19:54:2571 blink::WebLocalFrame* web_frame,
jam69e71152016-11-02 01:15:4372 int tab_id) = 0;
[email protected]c11e6592014-06-27 17:07:3473
74 // Returns the javascript sources to inject at the given |run_location|.
75 // Only called if ShouldInjectJs() is true.
76 virtual std::vector<blink::WebScriptSource> GetJsSources(
catmullingsd4faad4f2016-09-08 19:55:3077 UserScript::RunLocation run_location,
78 std::set<std::string>* executing_scripts,
79 size_t* num_injected_js_scripts) const = 0;
[email protected]c11e6592014-06-27 17:07:3480
81 // Returns the css to inject at the given |run_location|.
82 // Only called if ShouldInjectCss() is true.
lazyboy49cc0b3a2016-08-18 21:55:1283 virtual std::vector<blink::WebString> GetCssSources(
catmullingsd4faad4f2016-09-08 19:55:3084 UserScript::RunLocation run_location,
85 std::set<std::string>* injected_stylesheets,
86 size_t* num_injected_stylesheets) const = 0;
kozyatinskiyc8bc9a582015-03-06 09:33:4187
[email protected]c11e6592014-06-27 17:07:3488 // Notifies the script that injection has completed, with a possibly-populated
89 // list of results (depending on whether or not ExpectsResults() was true).
rdevlin.cronind533be962015-10-02 17:01:1890 // |render_frame| contains the render frame, or null if the frame was
91 // invalidated.
[email protected]c11e6592014-06-27 17:07:3492 virtual void OnInjectionComplete(
dchengf6f80662016-04-20 20:26:0493 std::unique_ptr<base::Value> execution_result,
rdevlin.cronind533be962015-10-02 17:01:1894 UserScript::RunLocation run_location,
95 content::RenderFrame* render_frame) = 0;
[email protected]c11e6592014-06-27 17:07:3496
97 // Notifies the script that injection will never occur.
rdevlin.cronind533be962015-10-02 17:01:1898 // |render_frame| contains the render frame, or null if the frame was
99 // invalidated.
100 virtual void OnWillNotInject(InjectFailureReason reason,
101 content::RenderFrame* render_frame) = 0;
[email protected]c11e6592014-06-27 17:07:34102};
103
104} // namespace extensions
105
106#endif // EXTENSIONS_RENDERER_SCRIPT_INJECTOR_H_