• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
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"
11 #include "extensions/common/permissions/permissions_data.h"
12 #include "extensions/common/user_script.h"
13 #include "third_party/WebKit/public/web/WebScriptSource.h"
14 
15 class GURL;
16 
17 namespace blink {
18 class WebFrame;
19 }
20 
21 namespace extensions {
22 class Extension;
23 struct 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.
28 class 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 
~ScriptInjector()38   virtual ~ScriptInjector() {}
39 
40   // Returns the script type of this particular injection.
41   virtual UserScript::InjectionType script_type() const = 0;
42 
43   // Returns true if the script should execute in child frames.
44   virtual bool ShouldExecuteInChildFrames() const = 0;
45 
46   // Returns true if the script should execute in the main world.
47   virtual bool ShouldExecuteInMainWorld() const = 0;
48 
49   // Returns true if the script is running inside a user gesture.
50   virtual bool IsUserGesture() const = 0;
51 
52   // Returns ture if the script expects results.
53   virtual bool ExpectsResults() const = 0;
54 
55   // Returns true if the script should inject JS source at the given
56   // |run_location|.
57   virtual bool ShouldInjectJs(UserScript::RunLocation run_location) const = 0;
58 
59   // Returns true if the script should inject CSS at the given |run_location|.
60   virtual bool ShouldInjectCss(UserScript::RunLocation run_location) const = 0;
61 
62   // Returns true if the script should execute on the given |frame|.
63   virtual PermissionsData::AccessType CanExecuteOnFrame(
64       const Extension* extension,
65       blink::WebFrame* web_frame,
66       int tab_id,
67       const GURL& top_url) const = 0;
68 
69   // Returns the javascript sources to inject at the given |run_location|.
70   // Only called if ShouldInjectJs() is true.
71   virtual std::vector<blink::WebScriptSource> GetJsSources(
72       UserScript::RunLocation run_location) const = 0;
73 
74   // Returns the css to inject at the given |run_location|.
75   // Only called if ShouldInjectCss() is true.
76   virtual std::vector<std::string> GetCssSources(
77       UserScript::RunLocation run_location) const = 0;
78 
79   // Notifies the script that injection has completed, with a possibly-populated
80   // list of results (depending on whether or not ExpectsResults() was true).
81   virtual void OnInjectionComplete(
82       scoped_ptr<base::ListValue> execution_results,
83       ScriptsRunInfo* scripts_run_info,
84       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_
93