blob: 21d434cfc4d297ef889c8492deec409b6034e003 [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
8#include <vector>
9
10#include "base/memory/scoped_ptr.h"
[email protected]23a85362014-07-07 23:26:1911#include "extensions/common/permissions/permissions_data.h"
[email protected]c11e6592014-06-27 17:07:3412#include "extensions/common/user_script.h"
13#include "third_party/WebKit/public/web/WebScriptSource.h"
14
15class GURL;
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:3423struct ScriptsRunInfo;
24
25// The pseudo-delegate class for a ScriptInjection that provides all necessary
26// information about how to inject the script, including what code to inject,
27// when (run location), and where (world), but without any injection logic.
28class ScriptInjector {
29 public:
30 // The possible reasons for not injecting the script.
31 enum InjectFailureReason {
32 EXTENSION_REMOVED, // The extension was removed before injection.
33 NOT_ALLOWED, // The script is not allowed to inject.
34 WONT_INJECT // The injection won't inject because the user rejected
35 // (or just did not accept) the injection.
36 };
37
[email protected]c11e6592014-06-27 17:07:3438 virtual ~ScriptInjector() {}
39
[email protected]23a85362014-07-07 23:26:1940 // Returns the script type of this particular injection.
41 virtual UserScript::InjectionType script_type() const = 0;
42
[email protected]c11e6592014-06-27 17:07:3443 // Returns true if the script should execute in the main world.
44 virtual bool ShouldExecuteInMainWorld() const = 0;
45
46 // Returns true if the script is running inside a user gesture.
47 virtual bool IsUserGesture() const = 0;
48
thakis1eeacc842015-03-26 07:30:5249 // Returns true if the script expects results.
[email protected]c11e6592014-06-27 17:07:3450 virtual bool ExpectsResults() const = 0;
51
52 // Returns true if the script should inject JS source at the given
53 // |run_location|.
54 virtual bool ShouldInjectJs(UserScript::RunLocation run_location) const = 0;
55
56 // Returns true if the script should inject CSS at the given |run_location|.
57 virtual bool ShouldInjectCss(UserScript::RunLocation run_location) const = 0;
58
59 // Returns true if the script should execute on the given |frame|.
[email protected]23a85362014-07-07 23:26:1960 virtual PermissionsData::AccessType CanExecuteOnFrame(
hanxia5c856cf2015-02-13 20:51:5861 const InjectionHost* injection_host,
rdevlin.cronin3e11c9862015-06-04 19:54:2562 blink::WebLocalFrame* web_frame,
rdevlin.croninf994d1e2015-06-03 22:28:1963 int tab_id) const = 0;
[email protected]c11e6592014-06-27 17:07:3464
65 // Returns the javascript sources to inject at the given |run_location|.
66 // Only called if ShouldInjectJs() is true.
67 virtual std::vector<blink::WebScriptSource> GetJsSources(
68 UserScript::RunLocation run_location) const = 0;
69
70 // Returns the css to inject at the given |run_location|.
71 // Only called if ShouldInjectCss() is true.
72 virtual std::vector<std::string> GetCssSources(
73 UserScript::RunLocation run_location) const = 0;
74
kozyatinskiyc8bc9a582015-03-06 09:33:4175 // Fill scriptrs run info based on information about injection.
76 virtual void GetRunInfo(
77 ScriptsRunInfo* scripts_run_info,
78 UserScript::RunLocation run_location) const = 0;
79
[email protected]c11e6592014-06-27 17:07:3480 // Notifies the script that injection has completed, with a possibly-populated
81 // list of results (depending on whether or not ExpectsResults() was true).
82 virtual void OnInjectionComplete(
rdevlin.cronin4bb32d72015-06-02 21:55:0183 scoped_ptr<base::Value> execution_result,
[email protected]c11e6592014-06-27 17:07:3484 UserScript::RunLocation run_location) = 0;
85
86 // Notifies the script that injection will never occur.
87 virtual void OnWillNotInject(InjectFailureReason reason) = 0;
88};
89
90} // namespace extensions
91
92#endif // EXTENSIONS_RENDERER_SCRIPT_INJECTOR_H_