Make a Delegate class for extension script injections.
[chromium-blink-merge.git] / extensions / renderer / script_injector.h
blob3bb0651c0ffb4380f96b13c5e66832a44f244a8f
1 // 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.
5 #ifndef EXTENSIONS_RENDERER_SCRIPT_INJECTOR_H_
6 #define EXTENSIONS_RENDERER_SCRIPT_INJECTOR_H_
8 #include <vector>
10 #include "base/memory/scoped_ptr.h"
11 #include "extensions/common/user_script.h"
12 #include "third_party/WebKit/public/web/WebScriptSource.h"
14 class GURL;
16 namespace blink {
17 class WebFrame;
20 namespace extensions {
21 class Extension;
22 struct ScriptsRunInfo;
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.
27 class 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.
37 // The possible types of access for a given frame.
38 enum AccessType {
39 DENY_ACCESS, // The script cannot access the given frame.
40 ALLOW_ACCESS, // The script can access the given frame.
41 REQUEST_ACCESS // The browser must determine if the script can access the
42 // given frame.
45 virtual ~ScriptInjector() {}
47 // Returns true if the script should execute in child frames.
48 virtual bool ShouldExecuteInChildFrames() const = 0;
50 // Returns true if the script should execute in the main world.
51 virtual bool ShouldExecuteInMainWorld() const = 0;
53 // Returns true if the script is running inside a user gesture.
54 virtual bool IsUserGesture() const = 0;
56 // Returns ture if the script expects results.
57 virtual bool ExpectsResults() const = 0;
59 // Returns true if the script should inject JS source at the given
60 // |run_location|.
61 virtual bool ShouldInjectJs(UserScript::RunLocation run_location) const = 0;
63 // Returns true if the script should inject CSS at the given |run_location|.
64 virtual bool ShouldInjectCss(UserScript::RunLocation run_location) const = 0;
66 // Returns true if the script should execute on the given |frame|.
67 virtual AccessType CanExecuteOnFrame(const Extension* extension,
68 blink::WebFrame* web_frame,
69 int tab_id,
70 const GURL& top_url) const = 0;
72 // Returns the javascript sources to inject at the given |run_location|.
73 // Only called if ShouldInjectJs() is true.
74 virtual std::vector<blink::WebScriptSource> GetJsSources(
75 UserScript::RunLocation run_location) const = 0;
77 // Returns the css to inject at the given |run_location|.
78 // Only called if ShouldInjectCss() is true.
79 virtual std::vector<std::string> GetCssSources(
80 UserScript::RunLocation run_location) const = 0;
82 // Notifies the script that injection has completed, with a possibly-populated
83 // list of results (depending on whether or not ExpectsResults() was true).
84 virtual void OnInjectionComplete(
85 scoped_ptr<base::ListValue> execution_results,
86 ScriptsRunInfo* scripts_run_info,
87 UserScript::RunLocation run_location) = 0;
89 // Notifies the script that injection will never occur.
90 virtual void OnWillNotInject(InjectFailureReason reason) = 0;
93 } // namespace extensions
95 #endif // EXTENSIONS_RENDERER_SCRIPT_INJECTOR_H_