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_USER_SCRIPT_INJECTOR_H_
6 #define EXTENSIONS_RENDERER_USER_SCRIPT_INJECTOR_H_
10 #include "base/memory/scoped_ptr.h"
11 #include "base/scoped_observer.h"
12 #include "extensions/common/user_script.h"
13 #include "extensions/renderer/script_injection.h"
14 #include "extensions/renderer/user_script_set.h"
20 namespace extensions
{
23 // A ScriptInjector for UserScripts.
24 class UserScriptInjector
: public ScriptInjector
,
25 public UserScriptSet::Observer
{
27 UserScriptInjector(const UserScript
* user_script
,
28 UserScriptSet
* user_script_set
,
30 ~UserScriptInjector() override
;
33 // UserScriptSet::Observer implementation.
34 void OnUserScriptsUpdated(const std::set
<std::string
>& changed_extensions
,
35 const std::vector
<UserScript
*>& scripts
) override
;
37 // ScriptInjector implementation.
38 UserScript::InjectionType
script_type() const override
;
39 bool ShouldExecuteInChildFrames() const override
;
40 bool ShouldExecuteInMainWorld() const override
;
41 bool IsUserGesture() const override
;
42 bool ExpectsResults() const override
;
43 bool ShouldInjectJs(UserScript::RunLocation run_location
) const override
;
44 bool ShouldInjectCss(UserScript::RunLocation run_location
) const override
;
45 PermissionsData::AccessType
CanExecuteOnFrame(
46 const Extension
* extension
,
47 blink::WebFrame
* web_frame
,
49 const GURL
& top_url
) const override
;
50 std::vector
<blink::WebScriptSource
> GetJsSources(
51 UserScript::RunLocation run_location
) const override
;
52 std::vector
<std::string
> GetCssSources(
53 UserScript::RunLocation run_location
) const override
;
54 void OnInjectionComplete(scoped_ptr
<base::ListValue
> execution_results
,
55 ScriptsRunInfo
* scripts_run_info
,
56 UserScript::RunLocation run_location
) override
;
57 void OnWillNotInject(InjectFailureReason reason
) override
;
59 // The associated user script. Owned by the UserScriptInjector that created
61 const UserScript
* script_
;
63 // The id of the associated user script. We cache this because when we update
64 // the |script_| associated with this injection, the old referance may be
68 // The associated extension id, preserved for the same reason as |script_id|.
69 std::string extension_id_
;
71 // Indicates whether or not this script is declarative. This influences which
72 // script permissions are checked before injection.
75 ScopedObserver
<UserScriptSet
, UserScriptSet::Observer
>
76 user_script_set_observer_
;
78 DISALLOW_COPY_AND_ASSIGN(UserScriptInjector
);
81 } // namespace extensions
83 #endif // EXTENSIONS_RENDERER_USER_SCRIPT_INJECTOR_H_