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_INJECTION_H_
6 #define EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_
8 #include "base/basictypes.h"
9 #include "base/macros.h"
10 #include "extensions/common/user_script.h"
11 #include "extensions/renderer/script_injector.h"
20 namespace extensions
{
21 struct ScriptsRunInfo
;
23 // A script wrapper which is aware of whether or not it is allowed to execute,
24 // and contains the implementation to do so.
25 class ScriptInjection
{
27 // Return the id of the injection host associated with the given world.
28 static std::string
GetHostIdForIsolatedWorld(int world_id
);
30 // Remove the isolated world associated with the given injection host.
31 static void RemoveIsolatedWorld(const std::string
& host_id
);
33 ScriptInjection(scoped_ptr
<ScriptInjector
> injector
,
34 blink::WebLocalFrame
* web_frame
,
35 const HostID
& host_id
,
36 UserScript::RunLocation run_location
,
40 // Try to inject the script at the |current_location|. This returns true if
41 // the script has either injected or will never inject (i.e., if the object
42 // is done), and false if injection is delayed (either for permission purposes
43 // or because |current_location| is not the designated |run_location_|).
44 // NOTE: |injection_host| may be NULL, if the injection_host is removed!
45 bool TryToInject(UserScript::RunLocation current_location
,
46 const InjectionHost
* injection_host
,
47 ScriptsRunInfo
* scripts_run_info
);
49 // Called when permission for the given injection has been granted.
50 // Returns true if the injection ran.
51 bool OnPermissionGranted(const InjectionHost
* injection_host
,
52 ScriptsRunInfo
* scripts_run_info
);
55 blink::WebLocalFrame
* web_frame() const { return web_frame_
; }
56 const HostID
& host_id() const { return host_id_
; }
57 int64
request_id() const { return request_id_
; }
60 // Sends a message to the browser, either that the script injection would
61 // like to inject, or to notify the browser that it is currently injecting.
62 void SendInjectionMessage(bool request_permission
);
64 // Injects the script, optionally populating |scripts_run_info|.
65 void Inject(const InjectionHost
* injection_host
,
66 ScriptsRunInfo
* scripts_run_info
);
68 // Inject any JS scripts into the |frame|, optionally populating
69 // |execution_results|.
70 void InjectJs(const InjectionHost
* injection_host
,
71 blink::WebLocalFrame
* frame
,
72 base::ListValue
* execution_results
);
74 // Inject any CSS source into the |frame|.
75 void InjectCss(blink::WebLocalFrame
* frame
);
77 // Notify that we will not inject, and mark it as acknowledged.
78 void NotifyWillNotInject(ScriptInjector::InjectFailureReason reason
);
80 // The injector for this injection.
81 scoped_ptr
<ScriptInjector
> injector_
;
83 // The (main) WebFrame into which this should inject the script.
84 blink::WebLocalFrame
* web_frame_
;
86 // The id of the associated injection_host.
89 // The location in the document load at which we inject the script.
90 UserScript::RunLocation run_location_
;
92 // The tab id associated with the frame.
95 // This injection's request id. This will be -1 unless the injection is
96 // currently waiting on permission.
99 // Whether or not the injection is complete, either via injecting the script
100 // or because it will never complete.
103 DISALLOW_COPY_AND_ASSIGN(ScriptInjection
);
106 } // namespace extensions
108 #endif // EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_